Move add Device tracker entities to UniFi controller (#84883)
parent
45fbbbaea1
commit
a75bad3a83
|
@ -206,7 +206,7 @@ class UniFiController:
|
|||
"""Create UniFi entity."""
|
||||
if not description.allowed_fn(
|
||||
self, obj_id
|
||||
) or not description.supported_fn(self.api, obj_id):
|
||||
) or not description.supported_fn(self, obj_id):
|
||||
return
|
||||
|
||||
entity = unifi_platform_entity(obj_id, self, description)
|
||||
|
|
|
@ -136,6 +136,10 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up device tracker for UniFi Network integration."""
|
||||
controller: UniFiController = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
controller.register_platform_add_entities(
|
||||
UnifiScannerEntity, ENTITY_DESCRIPTIONS, async_add_entities
|
||||
)
|
||||
|
||||
controller.entities[DOMAIN] = {CLIENT_TRACKER: set(), DEVICE_TRACKER: set()}
|
||||
|
||||
@callback
|
||||
|
@ -153,35 +157,6 @@ async def async_setup_entry(
|
|||
|
||||
items_added()
|
||||
|
||||
@callback
|
||||
def async_load_entities(description: UnifiEntityDescription) -> None:
|
||||
"""Load and subscribe to UniFi devices."""
|
||||
entities: list[ScannerEntity] = []
|
||||
api_handler = description.api_handler_fn(controller.api)
|
||||
|
||||
@callback
|
||||
def async_create_entity(event: ItemEvent, obj_id: str) -> None:
|
||||
"""Create UniFi entity."""
|
||||
if not description.allowed_fn(
|
||||
controller, obj_id
|
||||
) or not description.supported_fn(controller.api, obj_id):
|
||||
return
|
||||
|
||||
entity = UnifiScannerEntity(obj_id, controller, 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)
|
||||
|
||||
for description in ENTITY_DESCRIPTIONS:
|
||||
async_load_entities(description)
|
||||
|
||||
|
||||
@callback
|
||||
def add_client_entities(controller, async_add_entities, clients):
|
||||
|
|
Loading…
Reference in New Issue