mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Simplify adding unifi entities (#88571)
This commit is contained in:
parent
1519a78567
commit
b4a3a663cf
@ -198,26 +198,26 @@ class UniFiController:
|
|||||||
@callback
|
@callback
|
||||||
def async_load_entities(description: UnifiEntityDescription) -> None:
|
def async_load_entities(description: UnifiEntityDescription) -> None:
|
||||||
"""Load and subscribe to UniFi endpoints."""
|
"""Load and subscribe to UniFi endpoints."""
|
||||||
entities: list[UnifiEntity] = []
|
|
||||||
api_handler = description.api_handler_fn(self.api)
|
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
|
@callback
|
||||||
def async_create_entity(event: ItemEvent, obj_id: str) -> None:
|
def async_create_entity(event: ItemEvent, obj_id: str) -> None:
|
||||||
"""Create UniFi entity."""
|
"""Create new UniFi entity on event."""
|
||||||
if not description.allowed_fn(
|
async_add_unifi_entity([obj_id])
|
||||||
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)
|
|
||||||
|
|
||||||
api_handler.subscribe(async_create_entity, ItemEvent.ADDED)
|
api_handler.subscribe(async_create_entity, ItemEvent.ADDED)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user