Skip to content

Commit

Permalink
Remove eager_start argument from internal _async_add_hass_job function (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Apr 28, 2024
1 parent cdfd0aa commit e215270
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
32 changes: 9 additions & 23 deletions homeassistant/core.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -700,17 +698,14 @@ 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
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: ...

Expand All @@ -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: ...

Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
38 changes: 14 additions & 24 deletions tests/test_core.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand All @@ -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()))
Expand All @@ -287,26 +272,31 @@ 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():
pass

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:
Expand Down

0 comments on commit e215270

Please sign in to comment.