Unregister mobile_app notification services when a device is removed (#39359)

This commit is contained in:
J. Nick Koston 2020-08-28 12:37:19 -05:00 committed by GitHub
parent 33a05541a4
commit 85869be2d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View file

@ -119,7 +119,11 @@ async def async_unload_entry(hass, entry):
if not unload_ok: if not unload_ok:
return False 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 return True

View file

@ -60,7 +60,10 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema(
@bind_hass @bind_hass
async def async_reload(hass, integration_name): async def async_reload(hass, integration_name):
"""Register notify services for an integration.""" """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 return
data = hass.data[NOTIFY_SERVICES][integration_name] data = hass.data[NOTIFY_SERVICES][integration_name]
@ -74,8 +77,12 @@ async def async_reload(hass, integration_name):
if hasattr(notify_service, "targets"): if hasattr(notify_service, "targets"):
target_friendly_name = data[TARGET_FRIENDLY_NAME] target_friendly_name = data[TARGET_FRIENDLY_NAME]
stale_targets = set(targets)
for name, target in notify_service.targets.items(): for name, target in notify_service.targets.items():
target_name = slugify(f"{target_friendly_name}_{name}") target_name = slugify(f"{target_friendly_name}_{name}")
if target_name in stale_targets:
stale_targets.remove(target_name)
if target_name in targets: if target_name in targets:
continue continue
targets[target_name] = target targets[target_name] = target
@ -86,6 +93,12 @@ async def async_reload(hass, integration_name):
schema=NOTIFY_SERVICE_SCHEMA, 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) friendly_name_slug = slugify(friendly_name)
if hass.services.has_service(DOMAIN, friendly_name_slug): if hass.services.has_service(DOMAIN, friendly_name_slug):
return return

View file

@ -90,6 +90,12 @@ async def setup_push_receiver(hass, aioclient_mock):
assert hass.services.has_service("notify", "mobile_app_loaded_late") 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): async def test_notify_works(hass, aioclient_mock, setup_push_receiver):
"""Test notify works.""" """Test notify works."""