diff --git a/homeassistant/components/notify/legacy.py b/homeassistant/components/notify/legacy.py index 3d6d1582848..c3bb02896e0 100644 --- a/homeassistant/components/notify/legacy.py +++ b/homeassistant/components/notify/legacy.py @@ -2,7 +2,7 @@ from __future__ import annotations import asyncio -from collections.abc import Coroutine +from collections.abc import Callable, Coroutine from functools import partial from typing import Any, Optional, Protocol, cast @@ -162,9 +162,11 @@ async def async_reload(hass: HomeAssistant, integration_name: str) -> None: if not _async_integration_has_notify_services(hass, integration_name): return + notify_services: list[BaseNotificationService] = hass.data[NOTIFY_SERVICES][ + integration_name + ] tasks = [ - notify_service.async_register_services() - for notify_service in hass.data[NOTIFY_SERVICES][integration_name] + notify_service.async_register_services() for notify_service in notify_services ] await asyncio.gather(*tasks) @@ -173,15 +175,20 @@ async def async_reload(hass: HomeAssistant, integration_name: str) -> None: @bind_hass async def async_reset_platform(hass: HomeAssistant, integration_name: str) -> None: """Unregister notify services for an integration.""" - if NOTIFY_DISCOVERY_DISPATCHER in hass.data: - hass.data[NOTIFY_DISCOVERY_DISPATCHER]() + notify_discovery_dispatcher: Callable[[], None] | None = hass.data.get( + NOTIFY_DISCOVERY_DISPATCHER + ) + if notify_discovery_dispatcher: + notify_discovery_dispatcher() hass.data[NOTIFY_DISCOVERY_DISPATCHER] = None if not _async_integration_has_notify_services(hass, integration_name): return + notify_services: list[BaseNotificationService] = hass.data[NOTIFY_SERVICES][ + integration_name + ] tasks = [ - notify_service.async_unregister_services() - for notify_service in hass.data[NOTIFY_SERVICES][integration_name] + notify_service.async_unregister_services() for notify_service in notify_services ] await asyncio.gather(*tasks)