UniFi - Support automatic removal of clients (#34307)

This commit is contained in:
Robert Svensson authored and GitHub committed 2020-04-16 15:08:53 -07:00
1 parent abae48c287
commit 374fe47809
11 files changed
+227 -46

No files matched your search

+28 -8
View File
@@ -25,7 +25,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entity_registry = await hass.helpers.entity_registry.async_get_registry()
@callback
def update_controller():
def items_added():
"""Update the values of the controller."""
nonlocal option_allow_bandwidth_sensors
@@ -35,7 +35,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
add_entities(controller, async_add_entities, sensors)
controller.listeners.append(
async_dispatcher_connect(hass, controller.signal_update, update_controller)
async_dispatcher_connect(hass, controller.signal_update, items_added)
)
@callback
def items_removed(mac_addresses: set) -> None:
"""Items have been removed from the controller."""
remove_entities(controller, mac_addresses, sensors, entity_registry)
controller.listeners.append(
async_dispatcher_connect(hass, controller.signal_remove, items_removed)
)
@callback
@@ -47,14 +56,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
option_allow_bandwidth_sensors = controller.option_allow_bandwidth_sensors
if option_allow_bandwidth_sensors:
update_controller()
items_added()
else:
for sensor in sensors.values():
if entity_registry.async_is_registered(sensor.entity_id):
entity_registry.async_remove(sensor.entity_id)
hass.async_create_task(sensor.async_remove())
sensors.clear()
@@ -65,7 +70,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
update_controller()
items_added()
@callback
@@ -92,6 +97,21 @@ def add_entities(controller, async_add_entities, sensors):
async_add_entities(new_sensors)
@callback
def remove_entities(controller, mac_addresses, sensors, entity_registry):
"""Remove select sensor entities."""
for mac in mac_addresses:
for direction in ("rx", "tx"):
item_id = f"{direction}-{mac}"
if item_id not in sensors:
continue
entity = sensors.pop(item_id)
controller.hass.async_create_task(entity.async_remove())
class UniFiRxBandwidthSensor(UniFiClient):
"""Receiving bandwidth sensor."""