From 92c06f0818d813aac4a9fa2b606237b66eeb0960 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 28 Aug 2020 14:08:09 -0500 Subject: [PATCH] Ensure mobile_app notifications get re-registered after adding,removing,adding (#39362) --- homeassistant/components/notify/__init__.py | 15 +++++++++++---- tests/components/mobile_app/test_notify.py | 7 +++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index a68d6ded5c8..aa6de463e8c 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -66,7 +66,12 @@ async def async_reload(hass, integration_name): ): return - data = hass.data[NOTIFY_SERVICES][integration_name] + for data in hass.data[NOTIFY_SERVICES][integration_name]: + await _async_setup_notify_services(hass, data) + + +async def _async_setup_notify_services(hass, data): + """Create or remove the notify services.""" notify_service = data[SERVICE] friendly_name = data[FRIENDLY_NAME] targets = data[TARGETS] @@ -94,6 +99,7 @@ async def async_reload(hass, integration_name): ) for stale_target_name in stale_targets: + del targets[stale_target_name] hass.services.async_remove( DOMAIN, stale_target_name, @@ -187,12 +193,11 @@ async def async_setup(hass, config): target_friendly_name = ( p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or integration_name ) - friendly_name = ( p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or SERVICE_NOTIFY ) - hass.data[NOTIFY_SERVICES][integration_name] = { + data = { FRIENDLY_NAME: friendly_name, # The targets use a slightly different friendly name # selection pattern than the base service @@ -200,8 +205,10 @@ async def async_setup(hass, config): SERVICE: notify_service, TARGETS: {}, } + hass.data[NOTIFY_SERVICES].setdefault(integration_name, []) + hass.data[NOTIFY_SERVICES][integration_name].append(data) - await async_reload(hass, integration_name) + await _async_setup_notify_services(hass, data) hass.config.components.add(f"{DOMAIN}.{integration_name}") diff --git a/tests/components/mobile_app/test_notify.py b/tests/components/mobile_app/test_notify.py index e1320a1f4f7..5041b2453d9 100644 --- a/tests/components/mobile_app/test_notify.py +++ b/tests/components/mobile_app/test_notify.py @@ -96,6 +96,13 @@ async def setup_push_receiver(hass, aioclient_mock): assert hass.services.has_service("notify", "mobile_app_test") assert not hass.services.has_service("notify", "mobile_app_loaded_late") + loaded_late_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(loaded_late_entry.entry_id) + await hass.async_block_till_done() + + assert hass.services.has_service("notify", "mobile_app_test") + assert hass.services.has_service("notify", "mobile_app_loaded_late") + async def test_notify_works(hass, aioclient_mock, setup_push_receiver): """Test notify works."""