From bbb80caed33e4c3aae110ca7b23dc73e20579e0c Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 22 Mar 2024 18:10:07 +0100 Subject: [PATCH] =?UTF-8?q?Tweak=20marking=20private=20methods=20in=20UniF?= =?UTF-8?q?i,=20no=20need=20to=20mark=20inner=20functio=E2=80=A6=20(#11396?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/unifi/hub/entity_loader.py | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/unifi/hub/entity_loader.py b/homeassistant/components/unifi/hub/entity_loader.py index 5ae3e8f789c..2ed2c09d049 100644 --- a/homeassistant/components/unifi/hub/entity_loader.py +++ b/homeassistant/components/unifi/hub/entity_loader.py @@ -61,11 +61,11 @@ class UnifiEntityLoader: async def initialize(self) -> None: """Initialize API data and extra client support.""" - await self.refresh_api_data() - self.restore_inactive_clients() + await self._refresh_api_data() + self._restore_inactive_clients() 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.""" results = await asyncio.gather( *[update() for update in self.api_updaters], @@ -76,7 +76,7 @@ class UnifiEntityLoader: LOGGER.warning("Exception on update %s", result) @callback - def restore_inactive_clients(self) -> None: + def _restore_inactive_clients(self) -> None: """Restore inactive clients. Provide inactive clients to device tracker and switch platform. @@ -110,7 +110,7 @@ class UnifiEntityLoader: @callback def load_entities(self) -> None: - """Populate UniFi platforms with entities.""" + """Load entities into the registered UniFi platforms.""" for ( async_add_entities, entity_class, @@ -125,7 +125,7 @@ class UnifiEntityLoader: def _should_add_entity( self, description: UnifiEntityDescription, obj_id: str ) -> bool: - """Check if entity should be added.""" + """Validate if entity is allowed and supported before creating it.""" return bool( (description.key, obj_id) not in self.known_objects and description.allowed_fn(self.hub, obj_id) @@ -139,11 +139,11 @@ class UnifiEntityLoader: descriptions: tuple[UnifiEntityDescription, ...], async_add_entities: AddEntitiesCallback, ) -> None: - """Subscribe to UniFi API handlers and create entities.""" + """Load entities and subscribe for future entities.""" @callback - def _add_unifi_entities() -> None: - """Add UniFi entity.""" + def add_unifi_entities() -> None: + """Add currently known UniFi entities.""" async_add_entities( unifi_platform_entity(obj_id, self.hub, description) for description in descriptions @@ -151,10 +151,20 @@ class UnifiEntityLoader: 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 - def _create_unifi_entity( + def create_unifi_entity( description: UnifiEntityDescription, event: ItemEvent, obj_id: str ) -> None: """Create new UniFi entity on event.""" @@ -165,13 +175,5 @@ class UnifiEntityLoader: for description in descriptions: 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, - ) - )