Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister mobile_app notification services when a device is removed #39359

Merged
merged 1 commit into from Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/mobile_app/__init__.py
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion homeassistant/components/notify/__init__.py
Expand Up @@ -60,7 +60,10 @@
@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]
Expand All @@ -74,8 +77,12 @@ async def _async_notify_message(service):

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
Expand All @@ -86,6 +93,12 @@ async def _async_notify_message(service):
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
Expand Down
6 changes: 6 additions & 0 deletions tests/components/mobile_app/test_notify.py
Expand Up @@ -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."""
Expand Down