Remove eager_start argument from internal _async_add_hass_job function (#116310)

This commit is contained in:
J. Nick Koston 2024-04-28 16:30:19 -05:00 committed by GitHub
parent cdfd0aa7d4
commit e215270c05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 47 deletions

View File

@ -577,9 +577,7 @@ class HomeAssistant:
if TYPE_CHECKING: if TYPE_CHECKING:
target = cast(Callable[[*_Ts], Any], target) target = cast(Callable[[*_Ts], Any], target)
self.loop.call_soon_threadsafe( self.loop.call_soon_threadsafe(
functools.partial( functools.partial(self._async_add_hass_job, HassJob(target), *args)
self._async_add_hass_job, HassJob(target), *args, eager_start=True
)
) )
@overload @overload
@ -650,7 +648,7 @@ class HomeAssistant:
# https://github.com/home-assistant/core/pull/71960 # https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING: if TYPE_CHECKING:
target = cast(Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], target) 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 @overload
@callback @callback
@ -700,9 +698,7 @@ class HomeAssistant:
error_if_core=False, error_if_core=False,
) )
return self._async_add_hass_job( return self._async_add_hass_job(hassjob, *args, background=background)
hassjob, *args, eager_start=eager_start, background=background
)
@overload @overload
@callback @callback
@ -710,7 +706,6 @@ class HomeAssistant:
self, self,
hassjob: HassJob[..., Coroutine[Any, Any, _R]], hassjob: HassJob[..., Coroutine[Any, Any, _R]],
*args: Any, *args: Any,
eager_start: bool = False,
background: bool = False, background: bool = False,
) -> asyncio.Future[_R] | None: ... ) -> asyncio.Future[_R] | None: ...
@ -720,7 +715,6 @@ class HomeAssistant:
self, self,
hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R], hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R],
*args: Any, *args: Any,
eager_start: bool = False,
background: bool = False, background: bool = False,
) -> asyncio.Future[_R] | None: ... ) -> asyncio.Future[_R] | None: ...
@ -729,7 +723,6 @@ class HomeAssistant:
self, self,
hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R], hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R],
*args: Any, *args: Any,
eager_start: bool = False,
background: bool = False, background: bool = False,
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
"""Add a HassJob from within the event loop. """Add a HassJob from within the event loop.
@ -751,16 +744,11 @@ class HomeAssistant:
hassjob.target = cast( hassjob.target = cast(
Callable[..., Coroutine[Any, Any, _R]], hassjob.target Callable[..., Coroutine[Any, Any, _R]], hassjob.target
) )
# Use loop.create_task task = create_eager_task(
# to avoid the extra function call in asyncio.create_task. hassjob.target(*args), name=hassjob.name, loop=self.loop
if eager_start: )
task = create_eager_task( if task.done():
hassjob.target(*args), name=hassjob.name, loop=self.loop return task
)
if task.done():
return task
else:
task = self.loop.create_task(hassjob.target(*args), name=hassjob.name)
elif hassjob.job_type is HassJobType.Callback: elif hassjob.job_type is HassJobType.Callback:
if TYPE_CHECKING: if TYPE_CHECKING:
hassjob.target = cast(Callable[..., _R], hassjob.target) hassjob.target = cast(Callable[..., _R], hassjob.target)
@ -914,9 +902,7 @@ class HomeAssistant:
hassjob.target(*args) hassjob.target(*args)
return None return None
return self._async_add_hass_job( return self._async_add_hass_job(hassjob, *args, background=background)
hassjob, *args, eager_start=True, background=background
)
@overload @overload
@callback @callback

View File

@ -109,9 +109,7 @@ async def test_async_add_hass_job_eager_start_coro_suspends(
async def job_that_suspends(): async def job_that_suspends():
await asyncio.sleep(0) await asyncio.sleep(0)
task = hass._async_add_hass_job( task = hass._async_add_hass_job(ha.HassJob(ha.callback(job_that_suspends)))
ha.HassJob(ha.callback(job_that_suspends)), eager_start=True
)
assert not task.done() assert not task.done()
assert task in hass._tasks assert task in hass._tasks
await task await task
@ -247,7 +245,7 @@ async def test_async_add_hass_job_eager_start(hass: HomeAssistant) -> None:
job = ha.HassJob(mycoro, "named coro") job = ha.HassJob(mycoro, "named coro")
assert "named coro" in str(job) assert "named coro" in str(job)
assert job.name == "named coro" 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) 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 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: async def test_async_add_hass_job_schedule_corofunction_eager_start() -> None:
"""Test that we schedule coroutines and add jobs to the job pool.""" """Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop())) hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))
@ -287,15 +272,15 @@ async def test_async_add_hass_job_schedule_corofunction_eager_start() -> None:
"homeassistant.core.create_eager_task", wraps=create_eager_task "homeassistant.core.create_eager_task", wraps=create_eager_task
) as mock_create_eager_task: ) as mock_create_eager_task:
hass_job = ha.HassJob(job) 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.loop.call_soon.mock_calls) == 0
assert len(hass.add_job.mock_calls) == 0 assert len(hass.add_job.mock_calls) == 0
assert mock_create_eager_task.mock_calls assert mock_create_eager_task.mock_calls
await task await task
async def test_async_add_hass_job_schedule_partial_coroutinefunction() -> None: async def test_async_add_hass_job_schedule_partial_corofunction_eager_start() -> None:
"""Test that we schedule partial coros and add jobs to the job pool.""" """Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop())) hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))
async def job(): async def job():
@ -303,10 +288,15 @@ async def test_async_add_hass_job_schedule_partial_coroutinefunction() -> None:
partial = functools.partial(job) partial = functools.partial(job)
ha.HomeAssistant._async_add_hass_job(hass, ha.HassJob(partial)) with patch(
assert len(hass.loop.call_soon.mock_calls) == 0 "homeassistant.core.create_eager_task", wraps=create_eager_task
assert len(hass.loop.create_task.mock_calls) == 1 ) as mock_create_eager_task:
assert len(hass.add_job.mock_calls) == 0 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: async def test_async_add_job_add_hass_threaded_job_to_pool() -> None: