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:
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 @@ class HomeAssistant:
# 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 @@ class HomeAssistant:
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 @@ class HomeAssistant:
self,
hassjob: HassJob[..., Coroutine[Any, Any, _R]],
*args: Any,
eager_start: bool = False,
background: bool = False,
) -> asyncio.Future[_R] | None: ...
@ -720,7 +715,6 @@ class HomeAssistant:
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 @@ class HomeAssistant:
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 @@ class HomeAssistant:
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 @@ class HomeAssistant:
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

View file

@ -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 test_async_add_hass_job_eager_start(hass: HomeAssistant) -> None:
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 test_async_add_hass_job_schedule_corofunction_eager_start() -> None:
"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 test_async_add_hass_job_schedule_partial_coroutinefunction() -> None:
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: