Simplify adding unifi entities (#88571)

pull/88766/head
Robert Svensson 2023-02-25 17:18:49 +01:00 committed by GitHub
parent 1519a78567
commit b4a3a663cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 16 deletions

View File

@ -198,26 +198,26 @@ class UniFiController:
@callback
def async_load_entities(description: UnifiEntityDescription) -> None:
"""Load and subscribe to UniFi endpoints."""
entities: list[UnifiEntity] = []
api_handler = description.api_handler_fn(self.api)
@callback
def async_add_unifi_entity(obj_ids: list[str]) -> None:
"""Add UniFi entity."""
async_add_entities(
[
unifi_platform_entity(obj_id, self, description)
for obj_id in obj_ids
if description.allowed_fn(self, obj_id)
if description.supported_fn(self, obj_id)
]
)
async_add_unifi_entity(list(api_handler))
@callback
def async_create_entity(event: ItemEvent, obj_id: str) -> None:
"""Create UniFi entity."""
if not description.allowed_fn(
self, obj_id
) or not description.supported_fn(self, obj_id):
return
entity = unifi_platform_entity(obj_id, self, description)
if event == ItemEvent.ADDED:
async_add_entities([entity])
return
entities.append(entity)
for obj_id in api_handler:
async_create_entity(ItemEvent.CHANGED, obj_id)
async_add_entities(entities)
"""Create new UniFi entity on event."""
async_add_unifi_entity([obj_id])
api_handler.subscribe(async_create_entity, ItemEvent.ADDED)