Streamline setup of deCONZ group platform (#70712)

This commit is contained in:
Robert Svensson 2022-04-26 11:44:47 +02:00 committed by GitHub
parent bdba3e193e
commit fe3fb230be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View File

@ -62,13 +62,12 @@ class DeconzGateway:
self.ignore_state_updates = False self.ignore_state_updates = False
self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}" self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}"
self.signal_reload_groups = f"deconz_reload_group_{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_light = f"deconz_new_light_{config_entry.entry_id}"
self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}" self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
self.deconz_resource_type_to_signal_new_device = { self.deconz_resource_type_to_signal_new_device = {
ResourceGroup.GROUP.value: self.signal_new_group,
ResourceGroup.LIGHT.value: self.signal_new_light, ResourceGroup.LIGHT.value: self.signal_new_light,
ResourceGroup.SENSOR.value: self.signal_new_sensor, ResourceGroup.SENSOR.value: self.signal_new_sensor,
} }
@ -77,6 +76,10 @@ class DeconzGateway:
self.entities: dict[str, set[str]] = {} self.entities: dict[str, set[str]] = {}
self.events: list[DeconzAlarmEvent | DeconzEvent] = [] self.events: list[DeconzAlarmEvent | DeconzEvent] = []
self._option_allow_deconz_groups = self.config_entry.options.get(
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
)
@property @property
def bridgeid(self) -> str: def bridgeid(self) -> str:
"""Return the unique identifier of the gateway.""" """Return the unique identifier of the gateway."""
@ -218,11 +221,13 @@ class DeconzGateway:
] ]
if self.option_allow_deconz_groups: if self.option_allow_deconz_groups:
self.async_add_device_callback(ResourceGroup.GROUP.value) if not self._option_allow_deconz_groups:
async_dispatcher_send(self.hass, self.signal_reload_groups)
else: else:
deconz_ids += [group.deconz_id for group in self.api.groups.values()] deconz_ids += [group.deconz_id for group in self.api.groups.values()]
self._option_allow_deconz_groups = self.option_allow_deconz_groups
entity_registry = er.async_get(self.hass) entity_registry = er.async_get(self.hass)
for entity_id, deconz_id in self.deconz_ids.items(): for entity_id, deconz_id in self.deconz_ids.items():

View File

@ -5,6 +5,7 @@ from typing import Any, Generic, TypedDict, TypeVar
from pydeconz.interfaces.lights import LightResources from pydeconz.interfaces.lights import LightResources
from pydeconz.models import ResourceType from pydeconz.models import ResourceType
from pydeconz.models.event import EventType
from pydeconz.models.group import Group from pydeconz.models.group import Group
from pydeconz.models.light import ( from pydeconz.models.light import (
ALERT_LONG, ALERT_LONG,
@ -110,39 +111,42 @@ async def async_setup_entry(
) )
) )
async_add_light()
@callback @callback
def async_add_group(groups: list[Group] | None = None) -> None: def async_add_group(_: EventType, group_id: str) -> None:
"""Add group from deCONZ.""" """Add group from deCONZ."""
if not gateway.option_allow_deconz_groups: if (
not gateway.option_allow_deconz_groups
or (group := gateway.api.groups[group_id])
and not group.lights
):
return return
entities = [] async_add_entities([DeconzGroup(group, gateway)])
if groups is None: config_entry.async_on_unload(
groups = list(gateway.api.groups.values()) gateway.api.groups.subscribe(
async_add_group,
EventType.ADDED,
)
)
for group in groups: @callback
if not group.lights: def async_load_groups() -> None:
continue """Load deCONZ groups."""
for group_id in gateway.api.groups:
known_groups = set(gateway.entities[DOMAIN]) async_add_group(EventType.ADDED, group_id)
new_group = DeconzGroup(group, gateway)
if new_group.unique_id not in known_groups:
entities.append(new_group)
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
gateway.signal_new_group, gateway.signal_reload_groups,
async_add_group, async_load_groups,
) )
) )
async_add_light() async_load_groups()
async_add_group()
class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity): class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):