diff --git a/homeassistant/core.py b/homeassistant/core.py index 7a5d2b228620f7..9cab560cd2f404 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -577,9 +577,7 @@ def add_job( if TYPE_CHECKING: target = cast(Callable[[*_Ts], Any], target) self.loop.call_soon_threadsafe( - functools.partial( - self._async_add_hass_job, HassJob(target), *args, eager_start=True - ) + functools.partial(self._async_add_hass_job, HassJob(target), *args) ) @overload @@ -650,7 +648,7 @@ def async_add_job( # https://github.com/home-assistant/core/pull/71960 if TYPE_CHECKING: target = cast(Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], target) - return self._async_add_hass_job(HassJob(target), *args, eager_start=eager_start) + return self._async_add_hass_job(HassJob(target), *args) @overload @callback @@ -700,9 +698,7 @@ def async_add_hass_job( error_if_core=False, ) - return self._async_add_hass_job( - hassjob, *args, eager_start=eager_start, background=background - ) + return self._async_add_hass_job(hassjob, *args, background=background) @overload @callback @@ -710,7 +706,6 @@ def _async_add_hass_job( self, hassjob: HassJob[..., Coroutine[Any, Any, _R]], *args: Any, - eager_start: bool = False, background: bool = False, ) -> asyncio.Future[_R] | None: ... @@ -720,7 +715,6 @@ def _async_add_hass_job( self, hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R], *args: Any, - eager_start: bool = False, background: bool = False, ) -> asyncio.Future[_R] | None: ... @@ -729,7 +723,6 @@ def _async_add_hass_job( self, hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R], *args: Any, - eager_start: bool = False, background: bool = False, ) -> asyncio.Future[_R] | None: """Add a HassJob from within the event loop. @@ -751,16 +744,11 @@ def _async_add_hass_job( hassjob.target = cast( Callable[..., Coroutine[Any, Any, _R]], hassjob.target ) - # Use loop.create_task - # to avoid the extra function call in asyncio.create_task. - if eager_start: - task = create_eager_task( - hassjob.target(*args), name=hassjob.name, loop=self.loop - ) - if task.done(): - return task - else: - task = self.loop.create_task(hassjob.target(*args), name=hassjob.name) + task = create_eager_task( + hassjob.target(*args), name=hassjob.name, loop=self.loop + ) + if task.done(): + return task elif hassjob.job_type is HassJobType.Callback: if TYPE_CHECKING: hassjob.target = cast(Callable[..., _R], hassjob.target) @@ -914,9 +902,7 @@ def async_run_hass_job( hassjob.target(*args) return None - return self._async_add_hass_job( - hassjob, *args, eager_start=True, background=background - ) + return self._async_add_hass_job(hassjob, *args, background=background) @overload @callback diff --git a/tests/test_core.py b/tests/test_core.py index a553d5bbbedfef..123054540b1c50 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -109,9 +109,7 @@ async def test_async_add_hass_job_eager_start_coro_suspends( async def job_that_suspends(): await asyncio.sleep(0) - task = hass._async_add_hass_job( - ha.HassJob(ha.callback(job_that_suspends)), eager_start=True - ) + task = hass._async_add_hass_job(ha.HassJob(ha.callback(job_that_suspends))) assert not task.done() assert task in hass._tasks await task @@ -247,7 +245,7 @@ async def mycoro(): job = ha.HassJob(mycoro, "named coro") assert "named coro" in str(job) assert job.name == "named coro" - task = ha.HomeAssistant._async_add_hass_job(hass, job, eager_start=True) + task = ha.HomeAssistant._async_add_hass_job(hass, job) assert "named coro" in str(task) @@ -263,19 +261,6 @@ async def test_async_add_hass_job_schedule_partial_callback() -> None: assert len(hass.add_job.mock_calls) == 0 -async def test_async_add_hass_job_schedule_coroutinefunction() -> None: - """Test that we schedule coroutines and add jobs to the job pool.""" - hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop())) - - async def job(): - pass - - ha.HomeAssistant._async_add_hass_job(hass, ha.HassJob(job)) - assert len(hass.loop.call_soon.mock_calls) == 0 - assert len(hass.loop.create_task.mock_calls) == 1 - assert len(hass.add_job.mock_calls) == 0 - - async def test_async_add_hass_job_schedule_corofunction_eager_start() -> None: """Test that we schedule coroutines and add jobs to the job pool.""" hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop())) @@ -287,15 +272,15 @@ async def job(): "homeassistant.core.create_eager_task", wraps=create_eager_task ) as mock_create_eager_task: hass_job = ha.HassJob(job) - task = ha.HomeAssistant._async_add_hass_job(hass, hass_job, eager_start=True) + task = ha.HomeAssistant._async_add_hass_job(hass, hass_job) assert len(hass.loop.call_soon.mock_calls) == 0 assert len(hass.add_job.mock_calls) == 0 assert mock_create_eager_task.mock_calls await task -async def test_async_add_hass_job_schedule_partial_coroutinefunction() -> None: - """Test that we schedule partial coros and add jobs to the job pool.""" +async def test_async_add_hass_job_schedule_partial_corofunction_eager_start() -> None: + """Test that we schedule coroutines and add jobs to the job pool.""" hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop())) async def job(): @@ -303,10 +288,15 @@ async def job(): partial = functools.partial(job) - ha.HomeAssistant._async_add_hass_job(hass, ha.HassJob(partial)) - assert len(hass.loop.call_soon.mock_calls) == 0 - assert len(hass.loop.create_task.mock_calls) == 1 - assert len(hass.add_job.mock_calls) == 0 + with patch( + "homeassistant.core.create_eager_task", wraps=create_eager_task + ) as mock_create_eager_task: + hass_job = ha.HassJob(partial) + task = ha.HomeAssistant._async_add_hass_job(hass, hass_job) + assert len(hass.loop.call_soon.mock_calls) == 0 + assert len(hass.add_job.mock_calls) == 0 + assert mock_create_eager_task.mock_calls + await task async def test_async_add_job_add_hass_threaded_job_to_pool() -> None: