From 77cdecf0f1eb3c79f961261b5a2f458ccf017d76 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 11 Mar 2024 11:30:46 -1000 Subject: [PATCH] Migrate async_run_job to use eager_start for tasks (#113011) --- homeassistant/core.py | 2 +- tests/test_core.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 056774a2adc..ae5c9119738 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -855,7 +855,7 @@ class HomeAssistant: # https://github.com/home-assistant/core/pull/71960 if TYPE_CHECKING: target = cast(Callable[..., Coroutine[Any, Any, _R] | _R], target) - return self.async_run_hass_job(HassJob(target), *args) + return self.async_run_hass_job(HassJob(target), *args, eager_start=True) def block_till_done(self) -> None: """Block until all pending work is done.""" diff --git a/tests/test_core.py b/tests/test_core.py index 1d6f2830925..99490fe4365 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -2116,6 +2116,20 @@ async def test_async_functions_with_callback(hass: HomeAssistant) -> None: assert len(runs) == 3 +async def test_async_run_job_starts_tasks_eagerly(hass: HomeAssistant) -> None: + """Test async_run_job starts tasks eagerly.""" + runs = [] + + async def _test(): + runs.append(True) + + task = hass.async_run_job(_test) + # No call to hass.async_block_till_done to ensure the task is run eagerly + assert len(runs) == 1 + assert task.done() + await task + + def test_valid_entity_id() -> None: """Test valid entity ID.""" for invalid in [