mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Move add/remove logic of deCONZ groups to gateway class (#73952)
This commit is contained in:
parent
29a546f4e8
commit
fd89108483
@ -10,6 +10,7 @@ from typing import TYPE_CHECKING, Any, cast
|
|||||||
import async_timeout
|
import async_timeout
|
||||||
from pydeconz import DeconzSession, errors
|
from pydeconz import DeconzSession, errors
|
||||||
from pydeconz.interfaces.api import APIItems, GroupedAPIItems
|
from pydeconz.interfaces.api import APIItems, GroupedAPIItems
|
||||||
|
from pydeconz.interfaces.groups import Groups
|
||||||
from pydeconz.models.event import EventType
|
from pydeconz.models.event import EventType
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry
|
from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry
|
||||||
@ -59,18 +60,18 @@ 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_reload_clip_sensors = f"deconz_reload_clip_{config_entry.entry_id}"
|
self.signal_reload_clip_sensors = f"deconz_reload_clip_{config_entry.entry_id}"
|
||||||
|
|
||||||
self.deconz_ids: dict[str, str] = {}
|
self.deconz_ids: dict[str, str] = {}
|
||||||
self.entities: dict[str, set[str]] = {}
|
self.entities: dict[str, set[str]] = {}
|
||||||
self.events: list[DeconzAlarmEvent | DeconzEvent] = []
|
self.events: list[DeconzAlarmEvent | DeconzEvent] = []
|
||||||
self.ignored_devices: set[tuple[Callable[[EventType, str], None], str]] = set()
|
self.ignored_devices: set[tuple[Callable[[EventType, str], None], str]] = set()
|
||||||
|
self.deconz_groups: set[tuple[Callable[[EventType, str], None], str]] = set()
|
||||||
|
|
||||||
self._option_allow_deconz_groups = self.config_entry.options.get(
|
self.option_allow_deconz_groups = config_entry.options.get(
|
||||||
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
||||||
)
|
)
|
||||||
self.option_allow_new_devices = self.config_entry.options.get(
|
self.option_allow_new_devices = config_entry.options.get(
|
||||||
CONF_ALLOW_NEW_DEVICES, DEFAULT_ALLOW_NEW_DEVICES
|
CONF_ALLOW_NEW_DEVICES, DEFAULT_ALLOW_NEW_DEVICES
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -98,13 +99,6 @@ class DeconzGateway:
|
|||||||
CONF_ALLOW_CLIP_SENSOR, DEFAULT_ALLOW_CLIP_SENSOR
|
CONF_ALLOW_CLIP_SENSOR, DEFAULT_ALLOW_CLIP_SENSOR
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def option_allow_deconz_groups(self) -> bool:
|
|
||||||
"""Allow loading deCONZ groups from gateway."""
|
|
||||||
return self.config_entry.options.get(
|
|
||||||
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def register_platform_add_device_callback(
|
def register_platform_add_device_callback(
|
||||||
self,
|
self,
|
||||||
@ -113,16 +107,28 @@ class DeconzGateway:
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Wrap add_device_callback to check allow_new_devices option."""
|
"""Wrap add_device_callback to check allow_new_devices option."""
|
||||||
|
|
||||||
def async_add_device(event: EventType, device_id: str) -> None:
|
initializing = True
|
||||||
|
|
||||||
|
def async_add_device(_: EventType, device_id: str) -> None:
|
||||||
"""Add device or add it to ignored_devices set.
|
"""Add device or add it to ignored_devices set.
|
||||||
|
|
||||||
If ignore_state_updates is True means device_refresh service is used.
|
If ignore_state_updates is True means device_refresh service is used.
|
||||||
Device_refresh is expected to load new devices.
|
Device_refresh is expected to load new devices.
|
||||||
"""
|
"""
|
||||||
if not self.option_allow_new_devices and not self.ignore_state_updates:
|
if (
|
||||||
|
not initializing
|
||||||
|
and not self.option_allow_new_devices
|
||||||
|
and not self.ignore_state_updates
|
||||||
|
):
|
||||||
self.ignored_devices.add((async_add_device, device_id))
|
self.ignored_devices.add((async_add_device, device_id))
|
||||||
return
|
return
|
||||||
add_device_callback(event, device_id)
|
|
||||||
|
if isinstance(deconz_device_interface, Groups):
|
||||||
|
self.deconz_groups.add((async_add_device, device_id))
|
||||||
|
if not self.option_allow_deconz_groups:
|
||||||
|
return
|
||||||
|
|
||||||
|
add_device_callback(EventType.ADDED, device_id)
|
||||||
|
|
||||||
self.config_entry.async_on_unload(
|
self.config_entry.async_on_unload(
|
||||||
deconz_device_interface.subscribe(
|
deconz_device_interface.subscribe(
|
||||||
@ -132,7 +138,9 @@ class DeconzGateway:
|
|||||||
)
|
)
|
||||||
|
|
||||||
for device_id in deconz_device_interface:
|
for device_id in deconz_device_interface:
|
||||||
add_device_callback(EventType.ADDED, device_id)
|
async_add_device(EventType.ADDED, device_id)
|
||||||
|
|
||||||
|
initializing = False
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def load_ignored_devices(self) -> None:
|
def load_ignored_devices(self) -> None:
|
||||||
@ -216,13 +224,16 @@ class DeconzGateway:
|
|||||||
|
|
||||||
# Allow Groups
|
# Allow Groups
|
||||||
|
|
||||||
if self.option_allow_deconz_groups:
|
option_allow_deconz_groups = self.config_entry.options.get(
|
||||||
if not self._option_allow_deconz_groups:
|
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
||||||
async_dispatcher_send(self.hass, self.signal_reload_groups)
|
)
|
||||||
else:
|
if option_allow_deconz_groups != self.option_allow_deconz_groups:
|
||||||
deconz_ids += [group.deconz_id for group in self.api.groups.values()]
|
self.option_allow_deconz_groups = option_allow_deconz_groups
|
||||||
|
if option_allow_deconz_groups:
|
||||||
self._option_allow_deconz_groups = self.option_allow_deconz_groups
|
for add_device, device_id in self.deconz_groups:
|
||||||
|
add_device(EventType.ADDED, device_id)
|
||||||
|
else:
|
||||||
|
deconz_ids += [group.deconz_id for group in self.api.groups.values()]
|
||||||
|
|
||||||
# Allow adding new devices
|
# Allow adding new devices
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@ from homeassistant.components.light import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.color import color_hs_to_xy
|
from homeassistant.util.color import color_hs_to_xy
|
||||||
@ -109,11 +108,7 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
Update group states based on its sum of related lights.
|
Update group states based on its sum of related lights.
|
||||||
"""
|
"""
|
||||||
if (
|
if (group := gateway.api.groups[group_id]) and not group.lights:
|
||||||
not gateway.option_allow_deconz_groups
|
|
||||||
or (group := gateway.api.groups[group_id])
|
|
||||||
and not group.lights
|
|
||||||
):
|
|
||||||
return
|
return
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
@ -128,29 +123,11 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
async_add_entities([DeconzGroup(group, gateway)])
|
async_add_entities([DeconzGroup(group, gateway)])
|
||||||
|
|
||||||
config_entry.async_on_unload(
|
gateway.register_platform_add_device_callback(
|
||||||
gateway.api.groups.subscribe(
|
async_add_group,
|
||||||
async_add_group,
|
gateway.api.groups,
|
||||||
EventType.ADDED,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
|
||||||
def async_load_groups() -> None:
|
|
||||||
"""Load deCONZ groups."""
|
|
||||||
for group_id in gateway.api.groups:
|
|
||||||
async_add_group(EventType.ADDED, group_id)
|
|
||||||
|
|
||||||
config_entry.async_on_unload(
|
|
||||||
async_dispatcher_connect(
|
|
||||||
hass,
|
|
||||||
gateway.signal_reload_groups,
|
|
||||||
async_load_groups,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
async_load_groups()
|
|
||||||
|
|
||||||
|
|
||||||
class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
|
class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
|
||||||
"""Representation of a deCONZ light."""
|
"""Representation of a deCONZ light."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user