diff --git a/homeassistant/components/unifiprotect/__init__.py b/homeassistant/components/unifiprotect/__init__.py index fa20c892850..068c5665e6b 100644 --- a/homeassistant/components/unifiprotect/__init__.py +++ b/homeassistant/components/unifiprotect/__init__.py @@ -39,7 +39,7 @@ from .const import ( from .data import ProtectData, UFPConfigEntry from .discovery import async_start_discovery from .migrate import async_migrate_data -from .services import async_cleanup_services, async_setup_services +from .services import async_setup_services from .utils import ( _async_unifi_mac_from_hass, async_create_api_client, @@ -61,6 +61,7 @@ EARLY_ACCESS_URL = ( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the UniFi Protect.""" # Only start discovery once regardless of how many entries they have + async_setup_services(hass) async_start_discovery(hass) return True @@ -174,7 +175,6 @@ async def _async_setup_entry( raise ConfigEntryNotReady await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - async_setup_services(hass) hass.http.register_view(ThumbnailProxyView(hass)) hass.http.register_view(VideoProxyView(hass)) @@ -186,13 +186,9 @@ async def _async_options_updated(hass: HomeAssistant, entry: UFPConfigEntry) -> async def async_unload_entry(hass: HomeAssistant, entry: UFPConfigEntry) -> bool: """Unload UniFi Protect config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - data = entry.runtime_data - await data.async_stop() - async_cleanup_services(hass) - - return bool(unload_ok) + await entry.runtime_data.async_stop() + return unload_ok async def async_remove_config_entry_device( diff --git a/homeassistant/components/unifiprotect/data.py b/homeassistant/components/unifiprotect/data.py index 59a5242273a..7dcb9768f9a 100644 --- a/homeassistant/components/unifiprotect/data.py +++ b/homeassistant/components/unifiprotect/data.py @@ -345,6 +345,7 @@ def async_ufp_instance_for_config_entry_ids( entry.runtime_data.api for entry_id in config_entry_ids if (entry := hass.config_entries.async_get_entry(entry_id)) + and hasattr(entry, "runtime_data") ), None, ) diff --git a/homeassistant/components/unifiprotect/services.py b/homeassistant/components/unifiprotect/services.py index 60345ac6403..119fe52756c 100644 --- a/homeassistant/components/unifiprotect/services.py +++ b/homeassistant/components/unifiprotect/services.py @@ -13,7 +13,6 @@ from uiprotect.exceptions import ClientError import voluptuous as vol from homeassistant.components.binary_sensor import BinarySensorDeviceClass -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_DEVICE_ID, ATTR_NAME, Platform from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError @@ -238,15 +237,3 @@ def async_setup_services(hass: HomeAssistant) -> None: if hass.services.has_service(DOMAIN, name): continue hass.services.async_register(DOMAIN, name, method, schema=schema) - - -def async_cleanup_services(hass: HomeAssistant) -> None: - """Cleanup global UniFi Protect services (if all config entries unloaded).""" - loaded_entries = [ - entry - for entry in hass.config_entries.async_entries(DOMAIN) - if entry.state == ConfigEntryState.LOADED - ] - if len(loaded_entries) == 1: - for name in ALL_GLOBAL_SERIVCES: - hass.services.async_remove(DOMAIN, name)