Improve deCONZ signal strings (#57140)

This commit is contained in:
Robert Svensson 2021-10-07 12:48:27 +02:00 committed by GitHub
parent 2ba8e1030c
commit 750dd9186e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 72 additions and 71 deletions

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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"

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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()]

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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,
)
)

View File

@ -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],
)

View File

@ -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):

View File

@ -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,
)
)

View File

@ -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,
)
)