From 10f2d8b4b12d1b64f56e28b327212056a55875b2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 18 Mar 2024 02:09:21 -1000 Subject: [PATCH] Move legacy notify setup to use tracked tasks (#113716) * Move legacy notify setup to a tracked task * fix test * fix test * comment --- homeassistant/components/notify/__init__.py | 23 +++++++-------------- tests/components/file/test_notify.py | 2 ++ tests/components/tts/test_notify.py | 7 +++++++ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 2aa110649f2..e7390a49676 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -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.""" diff --git a/tests/components/file/test_notify.py b/tests/components/file/test_notify.py index 071c68caea3..093be77c08f 100644 --- a/tests/components/file/test_notify.py +++ b/tests/components/file/test_notify.py @@ -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()) diff --git a/tests/components/tts/test_notify.py b/tests/components/tts/test_notify.py index acefca21f6c..07ba2f2f3f5 100644 --- a/tests/components/tts/test_notify.py +++ b/tests/components/tts/test_notify.py @@ -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",