Give the UniFi integration better control over what data to load (#112804)

pull/112805/head
Robert Svensson 2024-03-09 10:35:25 +01:00 committed by GitHub
parent 2b0b3c238a
commit bf5537eb5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -5,6 +5,7 @@ Make sure expected clients are available for platforms.
"""
from __future__ import annotations
import asyncio
from collections.abc import Iterable
from datetime import timedelta
from functools import partial
@ -16,6 +17,7 @@ from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from ..const import LOGGER
from ..entity import UnifiEntity, UnifiEntityDescription
if TYPE_CHECKING:
@ -30,6 +32,17 @@ class UnifiEntityLoader:
def __init__(self, hub: UnifiHub) -> None:
"""Initialize the UniFi entity loader."""
self.hub = hub
self.api_updaters = (
hub.api.clients.update,
hub.api.clients_all.update,
hub.api.devices.update,
hub.api.dpi_apps.update,
hub.api.dpi_groups.update,
hub.api.port_forwarding.update,
hub.api.sites.update,
hub.api.system_information.update,
hub.api.wlans.update,
)
self.platforms: list[
tuple[
@ -43,6 +56,16 @@ class UnifiEntityLoader:
self.known_objects: set[tuple[str, str]] = set()
"""Tuples of entity description key and object ID of loaded entities."""
async def refresh_api_data(self) -> None:
"""Refresh API data from network application."""
results = await asyncio.gather(
*[update() for update in self.api_updaters],
return_exceptions=True,
)
for result in results:
if result is not None:
LOGGER.warning("Exception on update %s", result)
@callback
def register_platform(
self,

View File

@ -88,7 +88,7 @@ class UnifiHub:
async def initialize(self) -> None:
"""Set up a UniFi Network instance."""
await self.api.initialize()
await self.entity_loader.refresh_api_data()
assert self.config.entry.unique_id is not None
self.is_admin = self.api.sites[self.config.entry.unique_id].role == "admin"