1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add the ability to create Debouncer tasks as background tasks (#113128)

* Add the ability to Debouncer tasks in the background

This is a more general solution as a followup to
https://github.com/home-assistant/core/pull/112652#discussion_r1517159607

* Add the ability to Debouncer tasks in the background

This is a more general solution as a followup to
https://github.com/home-assistant/core/pull/112652#discussion_r1517159607

* fix
This commit is contained in:
J. Nick Koston
2024-03-12 02:41:12 -10:00
committed by GitHub
parent b3dedb3efb
commit 120525e94f
2 changed files with 50 additions and 7 deletions

View File

@@ -497,3 +497,35 @@ async def test_shutdown(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -
assert len(calls) == 1
assert debouncer._timer_task is None
async def test_background(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test background tasks are created when background is True."""
calls = []
async def _func() -> None:
await asyncio.sleep(0.1)
calls.append(None)
debouncer = debounce.Debouncer(
hass, _LOGGER, cooldown=0.05, immediate=True, function=_func, background=True
)
await debouncer.async_call()
assert len(calls) == 1
debouncer.async_schedule_call()
assert len(calls) == 1
async_fire_time_changed(hass, utcnow() + timedelta(seconds=1))
await hass.async_block_till_done(wait_background_tasks=False)
assert len(calls) == 1
await hass.async_block_till_done(wait_background_tasks=True)
assert len(calls) == 2
async_fire_time_changed(hass, utcnow() + timedelta(seconds=1))
await hass.async_block_till_done(wait_background_tasks=False)
assert len(calls) == 2