UniFi - Change handling of updated options (#31762)

* Change handling of updated options

* Add tests
This commit is contained in:
Robert Svensson authored and GitHub committed 2020-02-12 16:15:08 -08:00
1 parent e0a035ce35
commit 4cac0443e2
5 files changed
+320 -98

No files matched your search

+24 -21
View File
@@ -3,9 +3,7 @@ import logging
from homeassistant.components.unifi.config_flow import get_controller_from_config_entry
from homeassistant.core import callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_registry import DISABLED_CONFIG_ENTRY
from .unifi_client import UniFiClient
@@ -24,11 +22,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
controller = get_controller_from_config_entry(hass, config_entry)
sensors = {}
registry = await entity_registry.async_get_registry(hass)
option_allow_bandwidth_sensors = controller.option_allow_bandwidth_sensors
entity_registry = await hass.helpers.entity_registry.async_get_registry()
@callback
def update_controller():
"""Update the values of the controller."""
nonlocal option_allow_bandwidth_sensors
if not option_allow_bandwidth_sensors:
return
add_entities(controller, async_add_entities, sensors)
controller.listeners.append(
@@ -36,24 +41,29 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
@callback
def update_disable_on_entities():
def options_updated():
"""Update the values of the controller."""
for entity in sensors.values():
nonlocal option_allow_bandwidth_sensors
if entity.entity_registry_enabled_default == entity.enabled:
continue
if option_allow_bandwidth_sensors != controller.option_allow_bandwidth_sensors:
option_allow_bandwidth_sensors = controller.option_allow_bandwidth_sensors
disabled_by = None
if not entity.entity_registry_enabled_default and entity.enabled:
disabled_by = DISABLED_CONFIG_ENTRY
if option_allow_bandwidth_sensors:
update_controller()
registry.async_update_entity(
entity.registry_entry.entity_id, disabled_by=disabled_by
)
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()
controller.listeners.append(
async_dispatcher_connect(
hass, controller.signal_options_update, update_disable_on_entities
hass, controller.signal_options_update, options_updated
)
)
@@ -87,13 +97,6 @@ def add_entities(controller, async_add_entities, sensors):
class UniFiRxBandwidthSensor(UniFiClient):
"""Receiving bandwidth sensor."""
@property
def entity_registry_enabled_default(self):
"""Return if the entity should be enabled when first added to the entity registry."""
if self.controller.option_allow_bandwidth_sensors:
return True
return False
@property
def state(self):
"""Return the state of the sensor."""