Tweak marking private methods in UniFi, no need to mark inner functio… (#113964)

pull/114018/head
Robert Svensson 2024-03-22 18:10:07 +01:00 committed by GitHub
parent 6800034d5a
commit bbb80caed3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 20 deletions

View File

@ -61,11 +61,11 @@ class UnifiEntityLoader:
async def initialize(self) -> None: async def initialize(self) -> None:
"""Initialize API data and extra client support.""" """Initialize API data and extra client support."""
await self.refresh_api_data() await self._refresh_api_data()
self.restore_inactive_clients() self._restore_inactive_clients()
self.wireless_clients.update_clients(set(self.hub.api.clients.values())) self.wireless_clients.update_clients(set(self.hub.api.clients.values()))
async def refresh_api_data(self) -> None: async def _refresh_api_data(self) -> None:
"""Refresh API data from network application.""" """Refresh API data from network application."""
results = await asyncio.gather( results = await asyncio.gather(
*[update() for update in self.api_updaters], *[update() for update in self.api_updaters],
@ -76,7 +76,7 @@ class UnifiEntityLoader:
LOGGER.warning("Exception on update %s", result) LOGGER.warning("Exception on update %s", result)
@callback @callback
def restore_inactive_clients(self) -> None: def _restore_inactive_clients(self) -> None:
"""Restore inactive clients. """Restore inactive clients.
Provide inactive clients to device tracker and switch platform. Provide inactive clients to device tracker and switch platform.
@ -110,7 +110,7 @@ class UnifiEntityLoader:
@callback @callback
def load_entities(self) -> None: def load_entities(self) -> None:
"""Populate UniFi platforms with entities.""" """Load entities into the registered UniFi platforms."""
for ( for (
async_add_entities, async_add_entities,
entity_class, entity_class,
@ -125,7 +125,7 @@ class UnifiEntityLoader:
def _should_add_entity( def _should_add_entity(
self, description: UnifiEntityDescription, obj_id: str self, description: UnifiEntityDescription, obj_id: str
) -> bool: ) -> bool:
"""Check if entity should be added.""" """Validate if entity is allowed and supported before creating it."""
return bool( return bool(
(description.key, obj_id) not in self.known_objects (description.key, obj_id) not in self.known_objects
and description.allowed_fn(self.hub, obj_id) and description.allowed_fn(self.hub, obj_id)
@ -139,11 +139,11 @@ class UnifiEntityLoader:
descriptions: tuple[UnifiEntityDescription, ...], descriptions: tuple[UnifiEntityDescription, ...],
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Subscribe to UniFi API handlers and create entities.""" """Load entities and subscribe for future entities."""
@callback @callback
def _add_unifi_entities() -> None: def add_unifi_entities() -> None:
"""Add UniFi entity.""" """Add currently known UniFi entities."""
async_add_entities( async_add_entities(
unifi_platform_entity(obj_id, self.hub, description) unifi_platform_entity(obj_id, self.hub, description)
for description in descriptions for description in descriptions
@ -151,10 +151,20 @@ class UnifiEntityLoader:
if self._should_add_entity(description, obj_id) if self._should_add_entity(description, obj_id)
) )
_add_unifi_entities() add_unifi_entities()
self.hub.config.entry.async_on_unload(
async_dispatcher_connect(
self.hub.hass,
self.hub.signal_options_update,
add_unifi_entities,
)
)
# Subscribe for future entities
@callback @callback
def _create_unifi_entity( def create_unifi_entity(
description: UnifiEntityDescription, event: ItemEvent, obj_id: str description: UnifiEntityDescription, event: ItemEvent, obj_id: str
) -> None: ) -> None:
"""Create new UniFi entity on event.""" """Create new UniFi entity on event."""
@ -165,13 +175,5 @@ class UnifiEntityLoader:
for description in descriptions: for description in descriptions:
description.api_handler_fn(self.hub.api).subscribe( description.api_handler_fn(self.hub.api).subscribe(
partial(_create_unifi_entity, description), ItemEvent.ADDED partial(create_unifi_entity, description), ItemEvent.ADDED
)
self.hub.config.entry.async_on_unload(
async_dispatcher_connect(
self.hub.hass,
self.hub.signal_options_update,
_add_unifi_entities,
)
) )