Move unifiprotect services register to async_setup (#119786)

This commit is contained in:
J. Nick Koston 2024-06-16 14:50:03 -05:00 committed by GitHub
parent 151b3b3b0a
commit affbc30d0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 21 deletions

View file

@ -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(

View file

@ -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,
)

View file

@ -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)