UniFi - Move some preloading of unavailable clients earlier in setup phase (#34599)
Improve readability of setting up switch platform
This commit is contained in:
parent
a7e8446454
commit
b578a76efa
4 changed files with 48 additions and 54 deletions
|
@ -265,6 +265,30 @@ class UniFiController:
|
||||||
LOGGER.error("Unknown error connecting with UniFi controller: %s", err)
|
LOGGER.error("Unknown error connecting with UniFi controller: %s", err)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Restore clients that is not a part of active clients list.
|
||||||
|
entity_registry = await self.hass.helpers.entity_registry.async_get_registry()
|
||||||
|
for entity in entity_registry.entities.values():
|
||||||
|
if (
|
||||||
|
entity.config_entry_id != self.config_entry.entry_id
|
||||||
|
or "-" not in entity.unique_id
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
|
mac = ""
|
||||||
|
if entity.domain == TRACKER_DOMAIN:
|
||||||
|
mac, _ = entity.unique_id.split("-", 1)
|
||||||
|
elif entity.domain == SWITCH_DOMAIN:
|
||||||
|
_, mac = entity.unique_id.split("-", 1)
|
||||||
|
|
||||||
|
if mac in self.api.clients or mac not in self.api.clients_all:
|
||||||
|
continue
|
||||||
|
|
||||||
|
client = self.api.clients_all[mac]
|
||||||
|
self.api.clients.process_raw([client.raw])
|
||||||
|
LOGGER.debug(
|
||||||
|
"Restore disconnected client %s (%s)", entity.entity_id, client.mac,
|
||||||
|
)
|
||||||
|
|
||||||
wireless_clients = self.hass.data[UNIFI_WIRELESS_CLIENTS]
|
wireless_clients = self.hass.data[UNIFI_WIRELESS_CLIENTS]
|
||||||
self.wireless_clients = wireless_clients.get_data(self.config_entry)
|
self.wireless_clients = wireless_clients.get_data(self.config_entry)
|
||||||
self.update_wireless_clients()
|
self.update_wireless_clients()
|
||||||
|
|
|
@ -47,26 +47,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||||
controller.entities[DOMAIN] = {CLIENT_TRACKER: set(), DEVICE_TRACKER: set()}
|
controller.entities[DOMAIN] = {CLIENT_TRACKER: set(), DEVICE_TRACKER: set()}
|
||||||
|
|
||||||
# Restore clients that is not a part of active clients list.
|
|
||||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
|
||||||
for entity in entity_registry.entities.values():
|
|
||||||
|
|
||||||
if (
|
|
||||||
entity.config_entry_id == config_entry.entry_id
|
|
||||||
and entity.domain == DOMAIN
|
|
||||||
and "-" in entity.unique_id
|
|
||||||
):
|
|
||||||
|
|
||||||
mac, _ = entity.unique_id.split("-", 1)
|
|
||||||
if mac in controller.api.clients or mac not in controller.api.clients_all:
|
|
||||||
continue
|
|
||||||
|
|
||||||
client = controller.api.clients_all[mac]
|
|
||||||
controller.api.clients.process_raw([client.raw])
|
|
||||||
LOGGER.debug(
|
|
||||||
"Restore disconnected client %s (%s)", entity.entity_id, client.mac,
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def items_added():
|
def items_added():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
|
|
|
@ -30,63 +30,53 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
if controller.site_role != "admin":
|
if controller.site_role != "admin":
|
||||||
return
|
return
|
||||||
|
|
||||||
switches_off = []
|
# Store previously known POE control entities in case their POE are turned off.
|
||||||
|
previously_known_poe_clients = []
|
||||||
# Restore clients that is not a part of active clients list.
|
|
||||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
for entity in entity_registry.entities.values():
|
for entity in entity_registry.entities.values():
|
||||||
|
|
||||||
if (
|
if (
|
||||||
entity.config_entry_id == config_entry.entry_id
|
entity.config_entry_id != config_entry.entry_id
|
||||||
and entity.unique_id.startswith(f"{POE_SWITCH}-")
|
or not entity.unique_id.startswith(POE_SWITCH)
|
||||||
):
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
_, mac = entity.unique_id.split("-", 1)
|
mac = entity.unique_id.replace(f"{POE_SWITCH}-", "")
|
||||||
|
if mac in controller.api.clients or mac in controller.api.clients_all:
|
||||||
|
previously_known_poe_clients.append(entity.unique_id)
|
||||||
|
|
||||||
if mac in controller.api.clients:
|
for mac in controller.option_block_clients:
|
||||||
switches_off.append(entity.unique_id)
|
if mac not in controller.api.clients and mac in controller.api.clients_all:
|
||||||
continue
|
client = controller.api.clients_all[mac]
|
||||||
|
controller.api.clients.process_raw([client.raw])
|
||||||
if mac in controller.api.clients_all:
|
|
||||||
client = controller.api.clients_all[mac]
|
|
||||||
controller.api.clients.process_raw([client.raw])
|
|
||||||
switches_off.append(entity.unique_id)
|
|
||||||
continue
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def items_added():
|
def items_added():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
if controller.option_block_clients or controller.option_poe_clients:
|
if controller.option_block_clients or controller.option_poe_clients:
|
||||||
add_entities(controller, async_add_entities, switches_off)
|
add_entities(controller, async_add_entities, previously_known_poe_clients)
|
||||||
|
|
||||||
for signal in (controller.signal_update, controller.signal_options_update):
|
for signal in (controller.signal_update, controller.signal_options_update):
|
||||||
controller.listeners.append(async_dispatcher_connect(hass, signal, items_added))
|
controller.listeners.append(async_dispatcher_connect(hass, signal, items_added))
|
||||||
|
|
||||||
items_added()
|
items_added()
|
||||||
switches_off.clear()
|
previously_known_poe_clients.clear()
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def add_entities(controller, async_add_entities, switches_off):
|
def add_entities(controller, async_add_entities, previously_known_poe_clients):
|
||||||
"""Add new switch entities from the controller."""
|
"""Add new switch entities from the controller."""
|
||||||
switches = []
|
switches = []
|
||||||
|
|
||||||
for mac in controller.option_block_clients:
|
for mac in controller.option_block_clients:
|
||||||
|
|
||||||
if mac in controller.entities[DOMAIN][BLOCK_SWITCH]:
|
if (
|
||||||
continue
|
mac in controller.entities[DOMAIN][BLOCK_SWITCH]
|
||||||
|
or mac not in controller.api.clients
|
||||||
client = None
|
):
|
||||||
|
|
||||||
if mac in controller.api.clients:
|
|
||||||
client = controller.api.clients[mac]
|
|
||||||
|
|
||||||
elif mac in controller.api.clients_all:
|
|
||||||
client = controller.api.clients_all[mac]
|
|
||||||
|
|
||||||
if not client:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
client = controller.api.clients[mac]
|
||||||
switches.append(UniFiBlockClientSwitch(client, controller))
|
switches.append(UniFiBlockClientSwitch(client, controller))
|
||||||
|
|
||||||
if controller.option_poe_clients:
|
if controller.option_poe_clients:
|
||||||
|
@ -101,7 +91,7 @@ def add_entities(controller, async_add_entities, switches_off):
|
||||||
|
|
||||||
client = controller.api.clients[mac]
|
client = controller.api.clients[mac]
|
||||||
|
|
||||||
if poe_client_id not in switches_off and (
|
if poe_client_id not in previously_known_poe_clients and (
|
||||||
mac in controller.wireless_clients
|
mac in controller.wireless_clients
|
||||||
or client.sw_mac not in devices
|
or client.sw_mac not in devices
|
||||||
or not devices[client.sw_mac].ports[client.sw_port].port_poe
|
or not devices[client.sw_mac].ports[client.sw_port].port_poe
|
||||||
|
@ -114,7 +104,7 @@ def add_entities(controller, async_add_entities, switches_off):
|
||||||
multi_clients_on_port = False
|
multi_clients_on_port = False
|
||||||
for client2 in controller.api.clients.values():
|
for client2 in controller.api.clients.values():
|
||||||
|
|
||||||
if poe_client_id in switches_off:
|
if poe_client_id in previously_known_poe_clients:
|
||||||
break
|
break
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -383,7 +383,7 @@ async def test_option_block_clients(hass):
|
||||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]},
|
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||||
|
|
||||||
# Remove the second switch again
|
# Remove the second switch again
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
|
@ -397,7 +397,7 @@ async def test_option_block_clients(hass):
|
||||||
controller.config_entry, options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
controller.config_entry, options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||||
|
|
||||||
# Remove one
|
# Remove one
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue