diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index 3f5b42259ee..264017796aa 100644 --- a/homeassistant/components/mobile_app/__init__.py +++ b/homeassistant/components/mobile_app/__init__.py @@ -119,7 +119,11 @@ async def async_unload_entry(hass, entry): if not unload_ok: return False - webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID]) + webhook_id = entry.data[CONF_WEBHOOK_ID] + + webhook_unregister(hass, webhook_id) + del hass.data[DOMAIN][DATA_CONFIG_ENTRIES][webhook_id] + await hass_notify.async_reload(hass, DOMAIN) return True diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 8d7a86d17ee..a68d6ded5c8 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -60,7 +60,10 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema( @bind_hass async def async_reload(hass, integration_name): """Register notify services for an integration.""" - if NOTIFY_SERVICES not in hass.data: + if ( + NOTIFY_SERVICES not in hass.data + or integration_name not in hass.data[NOTIFY_SERVICES] + ): return data = hass.data[NOTIFY_SERVICES][integration_name] @@ -74,8 +77,12 @@ async def async_reload(hass, integration_name): if hasattr(notify_service, "targets"): target_friendly_name = data[TARGET_FRIENDLY_NAME] + stale_targets = set(targets) + for name, target in notify_service.targets.items(): target_name = slugify(f"{target_friendly_name}_{name}") + if target_name in stale_targets: + stale_targets.remove(target_name) if target_name in targets: continue targets[target_name] = target @@ -86,6 +93,12 @@ async def async_reload(hass, integration_name): schema=NOTIFY_SERVICE_SCHEMA, ) + for stale_target_name in stale_targets: + hass.services.async_remove( + DOMAIN, + stale_target_name, + ) + friendly_name_slug = slugify(friendly_name) if hass.services.has_service(DOMAIN, friendly_name_slug): return diff --git a/tests/components/mobile_app/test_notify.py b/tests/components/mobile_app/test_notify.py index a52477c6642..e1320a1f4f7 100644 --- a/tests/components/mobile_app/test_notify.py +++ b/tests/components/mobile_app/test_notify.py @@ -90,6 +90,12 @@ async def setup_push_receiver(hass, aioclient_mock): assert hass.services.has_service("notify", "mobile_app_loaded_late") + assert await hass.config_entries.async_remove(loaded_late_entry.entry_id) + await hass.async_block_till_done() + + assert hass.services.has_service("notify", "mobile_app_test") + assert not hass.services.has_service("notify", "mobile_app_loaded_late") + async def test_notify_works(hass, aioclient_mock, setup_push_receiver): """Test notify works."""