deCONZ improve options updating entities (#42320)

This commit is contained in:
Robert Svensson 2020-12-02 16:21:27 +01:00 committed by GitHub
parent ff4897a09e
commit ac2c01d20c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 49 additions and 53 deletions

View file

@ -39,7 +39,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway.entities[DOMAIN] = set()
@callback
def async_add_sensor(sensors):
def async_add_sensor(sensors=gateway.api.sensors.values()):
"""Add binary sensor from deCONZ."""
entities = []

View file

@ -34,7 +34,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway.entities[DOMAIN] = set()
@callback
def async_add_climate(sensors):
def async_add_climate(sensors=gateway.api.sensors.values()):
"""Add climate devices from deCONZ."""
entities = []
@ -59,7 +59,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_climate(gateway.api.sensors.values())
async_add_climate()
class DeconzThermostat(DeconzDevice, ClimateEntity):

View file

@ -28,7 +28,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway.entities[DOMAIN] = set()
@callback
def async_add_cover(lights):
def async_add_cover(lights=gateway.api.lights.values()):
"""Add cover from deCONZ."""
entities = []
@ -48,7 +48,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_cover(gateway.api.lights.values())
async_add_cover()
class DeconzCover(DeconzDevice, CoverEntity):

View file

@ -16,7 +16,7 @@ async def async_setup_events(gateway) -> None:
"""Set up the deCONZ events."""
@callback
def async_add_sensor(sensors):
def async_add_sensor(sensors=gateway.api.sensors.values()):
"""Create DeconzEvent."""
for sensor in sensors:
@ -38,9 +38,7 @@ async def async_setup_events(gateway) -> None:
)
)
async_add_sensor(
[gateway.api.sensors[key] for key in sorted(gateway.api.sensors, key=int)]
)
async_add_sensor()
@callback

View file

@ -37,7 +37,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities) -> None:
gateway.entities[DOMAIN] = set()
@callback
def async_add_fan(lights) -> None:
def async_add_fan(lights=gateway.api.lights.values()) -> None:
"""Add fan from deCONZ."""
entities = []
@ -55,7 +55,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities) -> None:
)
)
async_add_fan(gateway.api.lights.values())
async_add_fan()
class DeconzFan(DeconzDevice, FanEntity):

View file

@ -55,9 +55,6 @@ class DeconzGateway:
self.events = []
self.listeners = []
self._current_option_allow_clip_sensor = self.option_allow_clip_sensor
self._current_option_allow_deconz_groups = self.option_allow_deconz_groups
@property
def bridgeid(self) -> str:
"""Return the unique identifier of the gateway."""
@ -124,16 +121,20 @@ class DeconzGateway:
async_dispatcher_send(self.hass, self.signal_reachable, True)
@callback
def async_add_device_callback(self, device_type, device) -> None:
def async_add_device_callback(self, device_type, device=None) -> None:
"""Handle event of new device creation in deCONZ."""
if not self.option_allow_new_devices:
return
if not isinstance(device, list):
device = [device]
args = []
if device is not None and not isinstance(device, list):
args.append([device])
async_dispatcher_send(
self.hass, self.async_signal_new_device(device_type), device
self.hass,
self.async_signal_new_device(device_type),
*args, # Don't send device if None, it would override default value in listeners
)
async def async_update_device_registry(self) -> None:
@ -181,7 +182,7 @@ class DeconzGateway:
)
)
self.hass.async_create_task(async_setup_events(self))
await async_setup_events(self)
self.api.start()
@ -210,29 +211,21 @@ class DeconzGateway:
"""Manage entities affected by config entry options."""
deconz_ids = []
if self._current_option_allow_clip_sensor != self.option_allow_clip_sensor:
self._current_option_allow_clip_sensor = self.option_allow_clip_sensor
if self.option_allow_clip_sensor:
self.async_add_device_callback(NEW_SENSOR)
sensors = [
sensor
else:
deconz_ids += [
sensor.deconz_id
for sensor in self.api.sensors.values()
if sensor.type.startswith("CLIP")
]
if self.option_allow_clip_sensor:
self.async_add_device_callback(NEW_SENSOR, sensors)
else:
deconz_ids += [sensor.deconz_id for sensor in sensors]
if self.option_allow_deconz_groups:
self.async_add_device_callback(NEW_GROUP)
if self._current_option_allow_deconz_groups != self.option_allow_deconz_groups:
self._current_option_allow_deconz_groups = self.option_allow_deconz_groups
groups = list(self.api.groups.values())
if self.option_allow_deconz_groups:
self.async_add_device_callback(NEW_GROUP, groups)
else:
deconz_ids += [group.deconz_id for group in groups]
else:
deconz_ids += [group.deconz_id for group in self.api.groups.values()]
entity_registry = await self.hass.helpers.entity_registry.async_get_registry()

View file

@ -45,7 +45,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
other_light_resource_types = CONTROLLER + COVER_TYPES + LOCK_TYPES + SWITCH_TYPES
@callback
def async_add_light(lights):
def async_add_light(lights=gateway.api.lights.values()):
"""Add light from deCONZ."""
entities = []
@ -66,7 +66,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
@callback
def async_add_group(groups):
def async_add_group(groups=gateway.api.groups.values()):
"""Add group from deCONZ."""
if not gateway.option_allow_deconz_groups:
return
@ -91,8 +91,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_light(gateway.api.lights.values())
async_add_group(gateway.api.groups.values())
async_add_light()
async_add_group()
class DeconzBaseLight(DeconzDevice, LightEntity):

View file

@ -14,7 +14,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway.entities[DOMAIN] = set()
@callback
def async_add_lock(lights):
def async_add_lock(lights=gateway.api.lights.values()):
"""Add lock from deCONZ."""
entities = []
@ -32,7 +32,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_lock(gateway.api.lights.values())
async_add_lock()
class DeconzLock(DeconzDevice, LockEntity):

View file

@ -14,7 +14,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway = get_gateway_from_config_entry(hass, config_entry)
@callback
def async_add_scene(scenes):
def async_add_scene(scenes=gateway.api.scenes.values()):
"""Add scene from deCONZ."""
entities = [DeconzScene(scene, gateway) for scene in scenes]
@ -27,7 +27,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_scene(gateway.api.scenes.values())
async_add_scene()
class DeconzScene(Scene):

View file

@ -76,7 +76,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
battery_handler = DeconzBatteryHandler(gateway)
@callback
def async_add_sensor(sensors):
def async_add_sensor(sensors=gateway.api.sensors.values()):
"""Add sensors from deCONZ.
Create DeconzBattery if sensor has a battery attribute.

View file

@ -1,4 +1,7 @@
"""deCONZ services."""
import asyncio
from pydeconz.utils import normalize_bridge_id
import voluptuous as vol
@ -143,10 +146,10 @@ async def async_refresh_devices_service(hass, data):
await gateway.api.refresh_state()
gateway.ignore_state_updates = False
gateway.async_add_device_callback(NEW_GROUP, list(gateway.api.groups.values()))
gateway.async_add_device_callback(NEW_LIGHT, list(gateway.api.lights.values()))
gateway.async_add_device_callback(NEW_SCENE, list(gateway.api.scenes.values()))
gateway.async_add_device_callback(NEW_SENSOR, list(gateway.api.sensors.values()))
gateway.async_add_device_callback(NEW_GROUP)
gateway.async_add_device_callback(NEW_LIGHT)
gateway.async_add_device_callback(NEW_SCENE)
gateway.async_add_device_callback(NEW_SENSOR)
async def async_remove_orphaned_entries_service(hass, data):
@ -155,8 +158,10 @@ async def async_remove_orphaned_entries_service(hass, data):
if CONF_BRIDGE_ID in data:
gateway = hass.data[DOMAIN][normalize_bridge_id(data[CONF_BRIDGE_ID])]
entity_registry = await hass.helpers.entity_registry.async_get_registry()
device_registry = await hass.helpers.device_registry.async_get_registry()
device_registry, entity_registry = await asyncio.gather(
hass.helpers.device_registry.async_get_registry(),
hass.helpers.entity_registry.async_get_registry(),
)
entity_entries = async_entries_for_config_entry(
entity_registry, gateway.config_entry.entry_id

View file

@ -17,7 +17,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
gateway.entities[DOMAIN] = set()
@callback
def async_add_switch(lights):
def async_add_switch(lights=gateway.api.lights.values()):
"""Add switch from deCONZ."""
entities = []
@ -43,7 +43,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
async_add_switch(gateway.api.lights.values())
async_add_switch()
class DeconzPowerPlug(DeconzDevice, SwitchEntity):