Avoid looking up the callable type for HassJob when we already know it (#102962)

* Avoid looking up the callable type for HassJob when we already know it

When we connect the frontend we call async_listen with run_immediately MANY
times when we already know the job type (it will always be a callback). This
reduces the latency to get the frontend going

* missing coverage
This commit is contained in:
J. Nick Koston 2023-10-30 06:45:22 -05:00 committed by GitHub
parent 7dbe0c3a48
commit b3743937de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 8 deletions

View file

@ -816,6 +816,16 @@ async def test_eventbus_run_immediately(hass: HomeAssistant) -> None:
unsub()
async def test_eventbus_run_immediately_not_callback(hass: HomeAssistant) -> None:
"""Test we raise when passing a non-callback with run_immediately."""
def listener(event):
"""Mock listener."""
with pytest.raises(HomeAssistantError):
hass.bus.async_listen("test", listener, run_immediately=True)
async def test_eventbus_unsubscribe_listener(hass: HomeAssistant) -> None:
"""Test unsubscribe listener from returned function."""
calls = []
@ -2534,3 +2544,25 @@ def test_is_callback_check_partial():
assert HassJob(ha.callback(functools.partial(not_callback_func))).job_type == (
ha.HassJobType.Executor
)
def test_hassjob_passing_job_type():
"""Test passing the job type to HassJob when we already know it."""
@ha.callback
def callback_func():
pass
def not_callback_func():
pass
assert (
HassJob(callback_func, job_type=ha.HassJobType.Callback).job_type
== ha.HassJobType.Callback
)
# We should trust the job_type passed in
assert (
HassJob(not_callback_func, job_type=ha.HassJobType.Callback).job_type
== ha.HassJobType.Callback
)