diff --git a/homeassistant/components/deconz/alarm_control_panel.py b/homeassistant/components/deconz/alarm_control_panel.py index 73e85f13713..c16a074bc06 100644 --- a/homeassistant/components/deconz/alarm_control_panel.py +++ b/homeassistant/components/deconz/alarm_control_panel.py @@ -35,7 +35,6 @@ from homeassistant.const import ( from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import NEW_SENSOR from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -89,7 +88,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities) -> None: config_entry.async_on_unload( async_dispatcher_connect( hass, - gateway.async_signal_new_device(NEW_SENSOR), + gateway.signal_new_sensor, async_add_alarm_control_panel, ) ) diff --git a/homeassistant/components/deconz/binary_sensor.py b/homeassistant/components/deconz/binary_sensor.py index e0479d9a0c6..d79e8708d4b 100644 --- a/homeassistant/components/deconz/binary_sensor.py +++ b/homeassistant/components/deconz/binary_sensor.py @@ -26,7 +26,7 @@ from homeassistant.const import ATTR_TEMPERATURE from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR +from .const import ATTR_DARK, ATTR_ON from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -105,7 +105,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor + hass, + gateway.signal_new_sensor, + async_add_sensor, ) ) diff --git a/homeassistant/components/deconz/climate.py b/homeassistant/components/deconz/climate.py index d80ecc6cc77..b9401e6d5a3 100644 --- a/homeassistant/components/deconz/climate.py +++ b/homeassistant/components/deconz/climate.py @@ -46,7 +46,7 @@ from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import ATTR_LOCKED, ATTR_OFFSET, ATTR_VALVE, NEW_SENSOR +from .const import ATTR_LOCKED, ATTR_OFFSET, ATTR_VALVE from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -118,7 +118,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_climate + hass, + gateway.signal_new_sensor, + async_add_climate, ) ) diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py index 67753aa0355..f0372273253 100644 --- a/homeassistant/components/deconz/const.py +++ b/homeassistant/components/deconz/const.py @@ -46,11 +46,6 @@ PLATFORMS = [ SWITCH_DOMAIN, ] -NEW_GROUP = "groups" -NEW_LIGHT = "lights" -NEW_SCENE = "scenes" -NEW_SENSOR = "sensors" - ATTR_DARK = "dark" ATTR_LOCKED = "locked" ATTR_OFFSET = "offset" diff --git a/homeassistant/components/deconz/cover.py b/homeassistant/components/deconz/cover.py index abf1fe4eea4..5cf90c4dca1 100644 --- a/homeassistant/components/deconz/cover.py +++ b/homeassistant/components/deconz/cover.py @@ -21,7 +21,6 @@ from homeassistant.components.cover import ( from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import NEW_LIGHT from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -54,7 +53,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_cover + hass, + gateway.signal_new_light, + async_add_cover, ) ) diff --git a/homeassistant/components/deconz/deconz_event.py b/homeassistant/components/deconz/deconz_event.py index 2d9799c4d02..b04e393103f 100644 --- a/homeassistant/components/deconz/deconz_event.py +++ b/homeassistant/components/deconz/deconz_event.py @@ -20,7 +20,7 @@ from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.util import slugify -from .const import CONF_ANGLE, CONF_GESTURE, LOGGER, NEW_SENSOR +from .const import CONF_ANGLE, CONF_GESTURE, LOGGER from .deconz_device import DeconzBase CONF_DECONZ_EVENT = "deconz_event" @@ -63,7 +63,9 @@ async def async_setup_events(gateway) -> None: gateway.config_entry.async_on_unload( async_dispatcher_connect( - gateway.hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor + gateway.hass, + gateway.signal_new_sensor, + async_add_sensor, ) ) diff --git a/homeassistant/components/deconz/fan.py b/homeassistant/components/deconz/fan.py index 3b1f3bd256d..af73135cd2a 100644 --- a/homeassistant/components/deconz/fan.py +++ b/homeassistant/components/deconz/fan.py @@ -26,7 +26,6 @@ from homeassistant.util.percentage import ( percentage_to_ordered_list_item, ) -from .const import NEW_LIGHT from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -74,7 +73,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities) -> None: config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_fan + hass, + gateway.signal_new_light, + async_add_fan, ) ) diff --git a/homeassistant/components/deconz/gateway.py b/homeassistant/components/deconz/gateway.py index ecbc36ebadc..17c927b300e 100644 --- a/homeassistant/components/deconz/gateway.py +++ b/homeassistant/components/deconz/gateway.py @@ -2,7 +2,7 @@ import asyncio import async_timeout -from pydeconz import DeconzSession, errors +from pydeconz import DeconzSession, errors, group, light, sensor from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT from homeassistant.core import callback @@ -21,10 +21,6 @@ from .const import ( DEFAULT_ALLOW_NEW_DEVICES, DOMAIN as DECONZ_DOMAIN, LOGGER, - NEW_GROUP, - NEW_LIGHT, - NEW_SCENE, - NEW_SENSOR, PLATFORMS, ) from .deconz_event import async_setup_events, async_unload_events @@ -50,6 +46,20 @@ class DeconzGateway: self.available = True self.ignore_state_updates = False + self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}" + + self.signal_new_group = f"deconz_new_group_{config_entry.entry_id}" + self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}" + self.signal_new_scene = f"deconz_new_scene_{config_entry.entry_id}" + self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}" + + self.deconz_resource_type_to_signal_new_device = { + group.RESOURCE_TYPE: self.signal_new_group, + light.RESOURCE_TYPE: self.signal_new_light, + group.RESOURCE_TYPE_SCENE: self.signal_new_scene, + sensor.RESOURCE_TYPE: self.signal_new_sensor, + } + self.deconz_ids = {} self.entities = {} self.events = [] @@ -92,24 +102,6 @@ class DeconzGateway: CONF_ALLOW_NEW_DEVICES, DEFAULT_ALLOW_NEW_DEVICES ) - # Signals - - @property - def signal_reachable(self) -> str: - """Gateway specific event to signal a change in connection status.""" - return f"deconz-reachable-{self.bridgeid}" - - @callback - def async_signal_new_device(self, device_type) -> str: - """Gateway specific event to signal new device.""" - new_device = { - NEW_GROUP: f"deconz_new_group_{self.bridgeid}", - NEW_LIGHT: f"deconz_new_light_{self.bridgeid}", - NEW_SCENE: f"deconz_new_scene_{self.bridgeid}", - NEW_SENSOR: f"deconz_new_sensor_{self.bridgeid}", - } - return new_device[device_type] - # Callbacks @callback @@ -121,10 +113,14 @@ class DeconzGateway: @callback def async_add_device_callback( - self, device_type, device=None, force: bool = False + self, resource_type, device=None, force: bool = False ) -> None: """Handle event of new device creation in deCONZ.""" - if not force and not self.option_allow_new_devices: + if ( + not force + and not self.option_allow_new_devices + or resource_type not in self.deconz_resource_type_to_signal_new_device + ): return args = [] @@ -134,7 +130,7 @@ class DeconzGateway: async_dispatcher_send( self.hass, - self.async_signal_new_device(device_type), + self.deconz_resource_type_to_signal_new_device[resource_type], *args, # Don't send device if None, it would override default value in listeners ) @@ -207,7 +203,7 @@ class DeconzGateway: deconz_ids = [] if self.option_allow_clip_sensor: - self.async_add_device_callback(NEW_SENSOR) + self.async_add_device_callback(sensor.RESOURCE_TYPE) else: deconz_ids += [ @@ -217,7 +213,7 @@ class DeconzGateway: ] if self.option_allow_deconz_groups: - self.async_add_device_callback(NEW_GROUP) + self.async_add_device_callback(group.RESOURCE_TYPE) else: deconz_ids += [group.deconz_id for group in self.api.groups.values()] diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index 1a3cca6df05..0069e0fb7d9 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -37,7 +37,7 @@ from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.util.color import color_hs_to_xy -from .const import DOMAIN as DECONZ_DOMAIN, NEW_GROUP, NEW_LIGHT, POWER_PLUGS +from .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -69,7 +69,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_light + hass, + gateway.signal_new_light, + async_add_light, ) ) @@ -95,7 +97,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_GROUP), async_add_group + hass, + gateway.signal_new_group, + async_add_group, ) ) diff --git a/homeassistant/components/deconz/lock.py b/homeassistant/components/deconz/lock.py index bb23ec4be7a..fb344e54176 100644 --- a/homeassistant/components/deconz/lock.py +++ b/homeassistant/components/deconz/lock.py @@ -7,7 +7,6 @@ from homeassistant.components.lock import DOMAIN, LockEntity from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import NEW_LIGHT, NEW_SENSOR from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -35,7 +34,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_lock_from_light + hass, + gateway.signal_new_light, + async_add_lock_from_light, ) ) @@ -58,7 +59,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( hass, - gateway.async_signal_new_device(NEW_SENSOR), + gateway.signal_new_sensor, async_add_lock_from_sensor, ) ) diff --git a/homeassistant/components/deconz/scene.py b/homeassistant/components/deconz/scene.py index 45e891add28..69f3d48c82c 100644 --- a/homeassistant/components/deconz/scene.py +++ b/homeassistant/components/deconz/scene.py @@ -5,7 +5,6 @@ from homeassistant.components.scene import Scene from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import NEW_SCENE from .gateway import get_gateway_from_config_entry @@ -23,7 +22,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_SCENE), async_add_scene + hass, + gateway.signal_new_scene, + async_add_scene, ) ) diff --git a/homeassistant/components/deconz/sensor.py b/homeassistant/components/deconz/sensor.py index ba8e5c8adec..aed4d2df7ee 100644 --- a/homeassistant/components/deconz/sensor.py +++ b/homeassistant/components/deconz/sensor.py @@ -45,7 +45,7 @@ from homeassistant.helpers.dispatcher import ( async_dispatcher_send, ) -from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR +from .const import ATTR_DARK, ATTR_ON from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -167,7 +167,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor + hass, + gateway.signal_new_sensor, + async_add_sensor, ) ) @@ -340,7 +342,7 @@ class DeconzSensorStateTracker: if "battery" in self.sensor.changed_keys: async_dispatcher_send( self.gateway.hass, - self.gateway.async_signal_new_device(NEW_SENSOR), + self.gateway.signal_new_sensor, [self.sensor], ) diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index 5ae2875305d..88006a851f2 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -14,15 +14,7 @@ from homeassistant.helpers.entity_registry import ( ) from .config_flow import get_master_gateway -from .const import ( - CONF_BRIDGE_ID, - DOMAIN, - LOGGER, - NEW_GROUP, - NEW_LIGHT, - NEW_SCENE, - NEW_SENSOR, -) +from .const import CONF_BRIDGE_ID, DOMAIN, LOGGER DECONZ_SERVICES = "deconz_services" @@ -145,8 +137,8 @@ async def async_refresh_devices_service(gateway): await gateway.api.refresh_state() gateway.ignore_state_updates = False - for new_device_type in (NEW_GROUP, NEW_LIGHT, NEW_SCENE, NEW_SENSOR): - gateway.async_add_device_callback(new_device_type, force=True) + for resource_type in gateway.deconz_resource_type_to_signal_new_device: + gateway.async_add_device_callback(resource_type, force=True) async def async_remove_orphaned_entries_service(gateway): diff --git a/homeassistant/components/deconz/siren.py b/homeassistant/components/deconz/siren.py index 9138bb3ac14..c3679b6ad89 100644 --- a/homeassistant/components/deconz/siren.py +++ b/homeassistant/components/deconz/siren.py @@ -13,7 +13,6 @@ from homeassistant.components.siren import ( from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import NEW_LIGHT from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -41,7 +40,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_siren + hass, + gateway.signal_new_light, + async_add_siren, ) ) diff --git a/homeassistant/components/deconz/switch.py b/homeassistant/components/deconz/switch.py index f7559e37838..a00def33b72 100644 --- a/homeassistant/components/deconz/switch.py +++ b/homeassistant/components/deconz/switch.py @@ -6,7 +6,7 @@ from homeassistant.components.switch import DOMAIN, SwitchEntity from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect -from .const import DOMAIN as DECONZ_DOMAIN, NEW_LIGHT, POWER_PLUGS +from .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry @@ -48,7 +48,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.async_on_unload( async_dispatcher_connect( - hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_switch + hass, + gateway.signal_new_light, + async_add_switch, ) )