Group adding unifi entities together to reduce number of tasks (#110965)

This commit is contained in:
J. Nick Koston 2024-02-19 13:58:44 -06:00 committed by GitHub
parent aa9f0f5734
commit 1f1c66b3c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,9 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Iterable
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import partial
import ssl import ssl
from types import MappingProxyType from types import MappingProxyType
from typing import Any, Literal from typing import Any, Literal
@ -185,6 +187,17 @@ class UniFiController:
entity_class, descriptions, async_add_entities entity_class, descriptions, async_add_entities
) )
@callback
def _async_should_add_entity(
self, description: UnifiEntityDescription, obj_id: str
) -> bool:
"""Check if entity should be added."""
return bool(
(description.key, obj_id) not in self.known_objects
and description.allowed_fn(self, obj_id)
and description.supported_fn(self, obj_id)
)
@callback @callback
def register_platform_add_entities( def register_platform_add_entities(
self, self,
@ -195,45 +208,47 @@ class UniFiController:
"""Subscribe to UniFi API handlers and create entities.""" """Subscribe to UniFi API handlers and create entities."""
@callback @callback
def async_load_entities(description: UnifiEntityDescription) -> None: def async_load_entities(descriptions: Iterable[UnifiEntityDescription]) -> None:
"""Load and subscribe to UniFi endpoints.""" """Load and subscribe to UniFi endpoints."""
api_handler = description.api_handler_fn(self.api)
@callback @callback
def async_add_unifi_entity(obj_ids: list[str]) -> None: def async_add_unifi_entities() -> None:
"""Add UniFi entity.""" """Add UniFi entity."""
async_add_entities( async_add_entities(
[ [
unifi_platform_entity(obj_id, self, description) unifi_platform_entity(obj_id, self, description)
for obj_id in obj_ids for description in descriptions
if (description.key, obj_id) not in self.known_objects for obj_id in description.api_handler_fn(self.api)
if description.allowed_fn(self, obj_id) if self._async_should_add_entity(description, obj_id)
if description.supported_fn(self, obj_id)
] ]
) )
async_add_unifi_entity(list(api_handler)) async_add_unifi_entities()
@callback @callback
def async_create_entity(event: ItemEvent, obj_id: str) -> None: def async_create_entity(
description: UnifiEntityDescription, event: ItemEvent, obj_id: str
) -> None:
"""Create new UniFi entity on event.""" """Create new UniFi entity on event."""
async_add_unifi_entity([obj_id]) if self._async_should_add_entity(description, obj_id):
async_add_entities(
[unifi_platform_entity(obj_id, self, description)]
)
api_handler.subscribe(async_create_entity, ItemEvent.ADDED) for description in descriptions:
description.api_handler_fn(self.api).subscribe(
@callback partial(async_create_entity, description), ItemEvent.ADDED
def async_options_updated() -> None: )
"""Load new entities based on changed options."""
async_add_unifi_entity(list(api_handler))
self.config_entry.async_on_unload( self.config_entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(
self.hass, self.signal_options_update, async_options_updated self.hass,
self.signal_options_update,
async_add_unifi_entities,
) )
) )
for description in descriptions: async_load_entities(descriptions)
async_load_entities(description)
@property @property
def signal_reachable(self) -> str: def signal_reachable(self) -> str: