Move legacy notify setup to use tracked tasks (#113716)

* Move legacy notify setup to a tracked task

* fix test

* fix test

* comment
This commit is contained in:
J. Nick Koston 2024-03-18 02:09:21 -10:00 committed by GitHub
parent 264e023ab4
commit 10f2d8b4b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 15 deletions

View file

@ -2,8 +2,6 @@
from __future__ import annotations
import asyncio
import voluptuous as vol
import homeassistant.components.persistent_notification as pn
@ -43,19 +41,14 @@ PLATFORM_SCHEMA = vol.Schema(
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the notify services."""
platform_setups = async_setup_legacy(hass, config)
# We need to add the component here break the deadlock
# when setting up integrations from config entries as
# they would otherwise wait for notify to be
# setup and thus the config entries would not be able to
# setup their platforms, but we need to do it after
# the dispatcher is connected so we don't miss integrations
# that are registered before the dispatcher is connected
hass.config.components.add(DOMAIN)
if platform_setups:
await asyncio.wait([asyncio.create_task(setup) for setup in platform_setups])
for setup in async_setup_legacy(hass, config):
# Tasks are created as tracked tasks to ensure startup
# waits for them to finish, but we explicitly do not
# want to wait for them to finish here because we want
# any config entries that use notify as a base platform
# to be able to start with out having to wait for the
# legacy platforms to finish setting up.
hass.async_create_task(setup, eager_start=True)
async def persistent_notification(service: ServiceCall) -> None:
"""Send notification via the built-in persistent_notify integration."""

View file

@ -20,6 +20,7 @@ async def test_bad_config(hass: HomeAssistant) -> None:
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
with assert_setup_component(0) as handle_config:
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
assert not handle_config[notify.DOMAIN]
@ -49,6 +50,7 @@ async def test_notify_file(
}
},
)
await hass.async_block_till_done()
assert handle_config[notify.DOMAIN]
freezer.move_to(dt_util.utcnow())

View file

@ -49,6 +49,7 @@ async def test_setup_legacy_platform(hass: HomeAssistant) -> None:
}
with assert_setup_component(1, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
assert hass.services.has_service(notify.DOMAIN, "tts_test")
@ -65,6 +66,7 @@ async def test_setup_platform(hass: HomeAssistant) -> None:
}
with assert_setup_component(1, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
assert hass.services.has_service(notify.DOMAIN, "tts_test")
@ -80,6 +82,7 @@ async def test_setup_platform_missing_key(hass: HomeAssistant) -> None:
}
with assert_setup_component(0, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
assert not hass.services.has_service(notify.DOMAIN, "tts_test")
@ -107,6 +110,8 @@ async def test_setup_legacy_service(hass: HomeAssistant) -> None:
with assert_setup_component(1, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
await hass.services.async_call(
notify.DOMAIN,
"tts_test",
@ -142,6 +147,8 @@ async def test_setup_service(
with assert_setup_component(1, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)
await hass.async_block_till_done()
await hass.services.async_call(
notify.DOMAIN,
"tts_test",