Add support for background tasks in HA (#88265)

* Add support for background tasks

* make name mandatory for background tasks

* Update docstring

* async_create_background_task

* Grammar
This commit is contained in:
Paulus Schoutsen
2023-02-16 20:39:29 -05:00
committed by GitHub
parent 2ce631733a
commit 6cab27f378
3 changed files with 49 additions and 2 deletions

View File

@@ -1933,3 +1933,21 @@ async def test_state_changed_events_to_not_leak_contexts(hass: HomeAssistant) ->
gc.collect()
assert len(_get_by_type("homeassistant.core.Context")) == init_count
async def test_background_task(hass):
"""Test background tasks being quit."""
result = asyncio.Future()
async def test_task():
try:
await asyncio.sleep(1)
except asyncio.CancelledError:
result.set_result(hass.state)
raise
task = hass.async_create_background_task(test_task(), "happy task")
assert "happy task" in str(task)
await asyncio.sleep(0)
await hass.async_stop()
assert result.result() == ha.CoreState.stopping