From fce96975917e4189ceb1dc06dbfcfa025831b8d6 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 5 Feb 2020 01:37:01 +0100 Subject: [PATCH] deCONZ - Revert from using disabled_by when setting options (#31446) * Revert from using disabled_by when setting options * Remove signalling for changed options * Evaluate allow group option earlier when adding a group --- .../components/deconz/binary_sensor.py | 17 +++--- homeassistant/components/deconz/climate.py | 9 ++- .../components/deconz/deconz_device.py | 11 ---- homeassistant/components/deconz/gateway.py | 58 +------------------ homeassistant/components/deconz/light.py | 11 ++-- homeassistant/components/deconz/sensor.py | 12 ++-- 6 files changed, 30 insertions(+), 88 deletions(-) diff --git a/homeassistant/components/deconz/binary_sensor.py b/homeassistant/components/deconz/binary_sensor.py index 667eb6db075..6a528a66ba6 100644 --- a/homeassistant/components/deconz/binary_sensor.py +++ b/homeassistant/components/deconz/binary_sensor.py @@ -8,7 +8,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR from .deconz_device import DeconzDevice -from .gateway import DeconzEntityHandler, get_gateway_from_config_entry +from .gateway import get_gateway_from_config_entry ATTR_ORIENTATION = "orientation" ATTR_TILTANGLE = "tiltangle" @@ -23,8 +23,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the deCONZ binary sensor.""" gateway = get_gateway_from_config_entry(hass, config_entry) - entity_handler = DeconzEntityHandler(gateway) - @callback def async_add_sensor(sensors, new=True): """Add binary sensor from deCONZ.""" @@ -32,10 +30,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): for sensor in sensors: - if new and sensor.BINARY: - new_sensor = DeconzBinarySensor(sensor, gateway) - entity_handler.add_entity(new_sensor) - entities.append(new_sensor) + if ( + new + and sensor.BINARY + and ( + gateway.option_allow_clip_sensor + or not sensor.type.startswith("CLIP") + ) + ): + entities.append(DeconzBinarySensor(sensor, gateway)) async_add_entities(entities, True) diff --git a/homeassistant/components/deconz/climate.py b/homeassistant/components/deconz/climate.py index ba1f1ce846a..7b0f44807ec 100644 --- a/homeassistant/components/deconz/climate.py +++ b/homeassistant/components/deconz/climate.py @@ -37,7 +37,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities): for sensor in sensors: - if new and sensor.type in Thermostat.ZHATYPE: + if ( + new + and sensor.type in Thermostat.ZHATYPE + and ( + gateway.option_allow_clip_sensor + or not sensor.type.startswith("CLIP") + ) + ): entities.append(DeconzThermostat(sensor, gateway)) async_add_entities(entities, True) diff --git a/homeassistant/components/deconz/deconz_device.py b/homeassistant/components/deconz/deconz_device.py index 85fd560da1c..615fd3db473 100644 --- a/homeassistant/components/deconz/deconz_device.py +++ b/homeassistant/components/deconz/deconz_device.py @@ -63,17 +63,6 @@ class DeconzDevice(DeconzBase, Entity): Daylight is a virtual sensor from deCONZ that should never be enabled by default. """ - if not self.gateway.option_allow_clip_sensor and self._device.type.startswith( - "CLIP" - ): - return False - - if ( - not self.gateway.option_allow_deconz_groups - and self._device.type == "LightGroup" - ): - return False - if self._device.type == "Daylight": return False diff --git a/homeassistant/components/deconz/gateway.py b/homeassistant/components/deconz/gateway.py index f33e753e600..48fecc1ec4f 100644 --- a/homeassistant/components/deconz/gateway.py +++ b/homeassistant/components/deconz/gateway.py @@ -9,14 +9,7 @@ from homeassistant.core import callback from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import aiohttp_client from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity_registry import ( - DISABLED_CONFIG_ENTRY, - async_get_registry, -) +from homeassistant.helpers.dispatcher import async_dispatcher_send from .const import ( CONF_ALLOW_CLIP_SENSOR, @@ -116,7 +109,6 @@ class DeconzGateway: self.api.start() self.config_entry.add_update_listener(self.async_new_address) - self.config_entry.add_update_listener(self.async_options_updated) return True @@ -144,19 +136,6 @@ class DeconzGateway: self.available = available async_dispatcher_send(self.hass, self.signal_reachable, True) - @property - def signal_options_update(self) -> str: - """Event specific per deCONZ entry to signal new options.""" - return f"deconz-options-{self.bridgeid}" - - @staticmethod - async def async_options_updated(hass, entry) -> None: - """Triggered by config entry options updates.""" - gateway = get_gateway_from_config_entry(hass, entry) - - registry = await async_get_registry(hass) - async_dispatcher_send(hass, gateway.signal_options_update, registry) - @callback def async_signal_new_device(self, device_type) -> str: """Gateway specific event to signal new device.""" @@ -227,38 +206,3 @@ async def get_gateway( except (asyncio.TimeoutError, errors.RequestError): LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST]) raise CannotConnect - - -class DeconzEntityHandler: - """Platform entity handler to help with updating disabled by.""" - - def __init__(self, gateway) -> None: - """Create an entity handler.""" - self.gateway = gateway - self._entities = [] - - gateway.listeners.append( - async_dispatcher_connect( - gateway.hass, gateway.signal_options_update, self.update_entity_registry - ) - ) - - @callback - def add_entity(self, entity) -> None: - """Add a new entity to handler.""" - self._entities.append(entity) - - @callback - def update_entity_registry(self, entity_registry) -> None: - """Update entity registry disabled by status.""" - for entity in self._entities: - - if entity.entity_registry_enabled_default != entity.enabled: - disabled_by = None - - if entity.enabled: - disabled_by = DISABLED_CONFIG_ENTRY - - entity_registry.async_update_entity( - entity.registry_entry.entity_id, disabled_by=disabled_by - ) diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index d65f9fb3ee7..e836f1e4490 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -30,7 +30,7 @@ from .const import ( SWITCH_TYPES, ) from .deconz_device import DeconzDevice -from .gateway import DeconzEntityHandler, get_gateway_from_config_entry +from .gateway import get_gateway_from_config_entry async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): @@ -41,8 +41,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the deCONZ lights and groups from a config entry.""" gateway = get_gateway_from_config_entry(hass, config_entry) - entity_handler = DeconzEntityHandler(gateway) - @callback def async_add_light(lights): """Add light from deCONZ.""" @@ -63,13 +61,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities): @callback def async_add_group(groups): """Add group from deCONZ.""" + if not gateway.option_allow_deconz_groups: + return + entities = [] for group in groups: if group.lights: - new_group = DeconzGroup(group, gateway) - entity_handler.add_entity(new_group) - entities.append(new_group) + entities.append(DeconzGroup(group, gateway)) async_add_entities(entities, True) diff --git a/homeassistant/components/deconz/sensor.py b/homeassistant/components/deconz/sensor.py index 0792e436321..c32b26f299d 100644 --- a/homeassistant/components/deconz/sensor.py +++ b/homeassistant/components/deconz/sensor.py @@ -19,7 +19,7 @@ from homeassistant.helpers.dispatcher import ( from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR from .deconz_device import DeconzDevice from .deconz_event import DeconzEvent -from .gateway import DeconzEntityHandler, get_gateway_from_config_entry +from .gateway import get_gateway_from_config_entry ATTR_CURRENT = "current" ATTR_POWER = "power" @@ -37,7 +37,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): batteries = set() battery_handler = DeconzBatteryHandler(gateway) - entity_handler = DeconzEntityHandler(gateway) @callback def async_add_sensor(sensors, new=True): @@ -65,11 +64,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities): new and sensor.BINARY is False and sensor.type not in Battery.ZHATYPE + Thermostat.ZHATYPE + and ( + gateway.option_allow_clip_sensor + or not sensor.type.startswith("CLIP") + ) ): - - new_sensor = DeconzSensor(sensor, gateway) - entity_handler.add_entity(new_sensor) - entities.append(new_sensor) + entities.append(DeconzSensor(sensor, gateway)) if sensor.battery is not None: new_battery = DeconzBattery(sensor, gateway)