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

Add background tasks to config entries (#88335)

* Use a set for config entries task tracking

* Allow adding background tasks to config entries

* Add tests for config entry add tasks

* Update docstrings on core create task

* Migrate roon and august

* Use in more places

* Guard for None
This commit is contained in:
Paulus Schoutsen
2023-02-17 13:50:05 -05:00
committed by GitHub
parent 2b8abf84bd
commit 3a32d2bdcb
10 changed files with 82 additions and 21 deletions

View File

@@ -3573,3 +3573,26 @@ async def test_initializing_flows_canceled_on_shutdown(hass: HomeAssistant, mana
with pytest.raises(asyncio.exceptions.CancelledError):
await task
async def test_task_tracking(hass):
"""Test task tracking for a config entry."""
entry = MockConfigEntry(title="test_title", domain="test")
event = asyncio.Event()
results = []
async def test_task():
try:
await event.wait()
results.append("normal")
except asyncio.CancelledError:
results.append("background")
raise
entry.async_create_task(hass, test_task())
entry.async_create_background_task(hass, test_task(), "background-task-name")
await asyncio.sleep(0)
hass.loop.call_soon(event.set)
await entry._async_process_on_unload()
assert results == ["background", "normal"]