* Initial "group members assume state" implementation for ZHA * Remove left-over debug flag (where polling was disabled) * Implement _send_member_assume_state_event() method and also use after turn_off * Only assume updated arguments from service call to group * Make code more readable and change checks slightly * Move "send member assume state" events to LightGroup on/off calls * Include new config option in tests * Check that member is available before updating to assumed state * Lower "update group from child delay" for debouncer to basically 0 when using assumed member state * Allow "child to group" updates regardless of config option This is not needed, as group members will not update their state, as long as they're transitioning. (If a group transitions, it also sets its members to transitioning mode) This fixes multiple issues. Previously, the state of a group was completely wrong when: - turn on group with 10 second transition - turn on members individually - turn off members individually - group state would not update correctly * Move "default update group from child delay" constant * Change to new constant name in test * Also update fan test to new constant name * Decrease "update group from child delay" to 10ms In my testing, 0.0 also works without any issues and correctly de-bounces child updates when using the "assume state option". This is just for avoiding multiple state changes when changing the group -> children issue individual updates. With 2 children in a group and delay 0, both child updates only cause one group re-calculation and state change. 0.01 (10ms) should be plenty for very slow systems to de-bounce the update (and in the worst case, it'll cause just another state change but nothing breaks) * Also implement "assuming state" for effect Not sure if anybody even uses this, but this one is a bit special because the effect is always deactivated if it's not provided in the light.turn_on call. * Move shortened delay for "assuming members" to a constant * Add basic test to verify that group members assume on/off state * Move _assume_group_state function declaration out of async_added_to_hass * Fix rare edge-case when rapidly toggling lights and light groups at the same time This prevents an issue where either the group transition would unset the transition flag or the single light would unset the group transition status midst-transition. Note: When a new individual transition is started, we want to unset the group flag, as we actually cancel that transition. * Check that effect list exists, add return type * Re-trigger CI due to timeout * Increase ASSUME_UPDATE_GROUP_FROM_CHILD_DELAY slightly The debouncer is used when updating group member states either by assuming them (in which case we want to barely have any delay), or between the time we get the results back from polling (where we want a slightly longer time). As it's not easily possible to distinguish if a group member was updated via assuming the state of the group or by the polling that follows, 50 ms seems to be a good middle point. * Add debug print for when updating group state * Fix issues with "off brightness" when switching between group/members This fixes a bunch of issues with "off brightness" and passes it down to the members correctly. For example, if a light group is turned off with a transition (so bulbs get their level set to 1), this will also set the "off brightness" of all individual bulbs to the last level that they were at. (It really fixes a lot of issues when using the "member assume group state" option. It's not really possible to fix them without that.) Furthermore, issues where polling was previously needed to get the correct state after "playing with transitions", should now get be resolved and get correct state when using the "members assume group state" option. Note: The only case which still can't be fixed is the following: If individual lights have off_with_transition set, but not the group, and the group is then turned on without a level, individual lights might fall back to brightness level 1 (<- at least now shows correctly in UI even before polling). Since all lights might need different brightness levels to be turned on, we can't use one group call. But making individual calls when turning on a ZHA group would cause a lot of traffic and thereby be counter-productive. In this case, light.turn_on should just be called with a level (or individual calls to the lights should be made). Another thing that was changed is to reset off_with_transition/off_brightness for a LightGroup when a member is turned on (even if the LightGroup wasn't turned on using its turn_on method). off_with_transition/off_brightness for individual bulbs is now also turned off when a light is detected to be on during polling. Lastly, the waiting for polled attributes could previously cause "invalid state" to be set (so mid-transition levels). This could happen when group and members are repeatedly toggled at similar times. These "invalid states" could cause wrong "off brightness" levels if transitions are also used. To fix this, we check after waiting for the polled attributes in async_get_state to see if a transition has started in the meanwhile. If so, the values can be discarded. A new poll will happen later and if using the "members assume group state" config option, the values should already be correct before the polling. * Enable "group members assume state" config option by default The config tests are also updated to expect the config option be enabled by default. For all tests, the config option is generally disabled though: There are only two group related tests. The one that tests this new feature overrides the config option to be enabled anyway. The other tests works in a similar way but also "sends" attribute reports, so we want to disable the feature for that test. (It would also run with it enabled (if the correct CHILD_UPDATE value is patched), but then it would test the same stuff as the other test, hence we're disabling the config option for that test.)
350 lines
11 KiB
Python
350 lines
11 KiB
Python
"""Entity for Zigbee Home Automation."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from collections.abc import Callable
|
|
import functools
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any, TypeVar
|
|
|
|
from homeassistant.const import ATTR_NAME
|
|
from homeassistant.core import CALLBACK_TYPE, Event, callback
|
|
from homeassistant.helpers import entity
|
|
from homeassistant.helpers.debounce import Debouncer
|
|
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
|
from homeassistant.helpers.dispatcher import (
|
|
async_dispatcher_connect,
|
|
async_dispatcher_send,
|
|
)
|
|
from homeassistant.helpers.event import async_track_state_change_event
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
|
|
from .core.const import (
|
|
ATTR_MANUFACTURER,
|
|
ATTR_MODEL,
|
|
DATA_ZHA,
|
|
DATA_ZHA_BRIDGE_ID,
|
|
DOMAIN,
|
|
SIGNAL_GROUP_ENTITY_REMOVED,
|
|
SIGNAL_GROUP_MEMBERSHIP_CHANGE,
|
|
SIGNAL_REMOVE,
|
|
)
|
|
from .core.helpers import LogMixin
|
|
|
|
if TYPE_CHECKING:
|
|
from .core.channels.base import ZigbeeChannel
|
|
from .core.device import ZHADevice
|
|
|
|
_ZhaEntitySelfT = TypeVar("_ZhaEntitySelfT", bound="ZhaEntity")
|
|
_ZhaGroupEntitySelfT = TypeVar("_ZhaGroupEntitySelfT", bound="ZhaGroupEntity")
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
ENTITY_SUFFIX = "entity_suffix"
|
|
DEFAULT_UPDATE_GROUP_FROM_CHILD_DELAY = 0.5
|
|
|
|
|
|
class BaseZhaEntity(LogMixin, entity.Entity):
|
|
"""A base class for ZHA entities."""
|
|
|
|
unique_id_suffix: str | None = None
|
|
_attr_has_entity_name = True
|
|
_attr_should_poll = False
|
|
|
|
def __init__(self, unique_id: str, zha_device: ZHADevice, **kwargs: Any) -> None:
|
|
"""Init ZHA entity."""
|
|
self._name: str = ""
|
|
self._unique_id: str = unique_id
|
|
if self.unique_id_suffix:
|
|
self._unique_id += f"-{self.unique_id_suffix}"
|
|
self._state: Any = None
|
|
self._extra_state_attributes: dict[str, Any] = {}
|
|
self._zha_device = zha_device
|
|
self._unsubs: list[Callable[[], None]] = []
|
|
self.remove_future: asyncio.Future[Any] = asyncio.Future()
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
"""Return Entity's default name."""
|
|
if hasattr(self, "_attr_name") and self._attr_name is not None:
|
|
return self._attr_name
|
|
return self._name
|
|
|
|
@property
|
|
def unique_id(self) -> str:
|
|
"""Return a unique ID."""
|
|
return self._unique_id
|
|
|
|
@property
|
|
def zha_device(self) -> ZHADevice:
|
|
"""Return the ZHA device this entity is attached to."""
|
|
return self._zha_device
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
"""Return device specific state attributes."""
|
|
return self._extra_state_attributes
|
|
|
|
@property
|
|
def device_info(self) -> entity.DeviceInfo:
|
|
"""Return a device description for device registry."""
|
|
zha_device_info = self._zha_device.device_info
|
|
ieee = zha_device_info["ieee"]
|
|
return entity.DeviceInfo(
|
|
connections={(CONNECTION_ZIGBEE, ieee)},
|
|
identifiers={(DOMAIN, ieee)},
|
|
manufacturer=zha_device_info[ATTR_MANUFACTURER],
|
|
model=zha_device_info[ATTR_MODEL],
|
|
name=zha_device_info[ATTR_NAME],
|
|
via_device=(DOMAIN, self.hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID]),
|
|
)
|
|
|
|
@callback
|
|
def async_state_changed(self) -> None:
|
|
"""Entity state changed."""
|
|
self.async_write_ha_state()
|
|
|
|
@callback
|
|
def async_update_state_attribute(self, key: str, value: Any) -> None:
|
|
"""Update a single device state attribute."""
|
|
self._extra_state_attributes.update({key: value})
|
|
self.async_write_ha_state()
|
|
|
|
@callback
|
|
def async_set_state(self, attr_id: int, attr_name: str, value: Any) -> None:
|
|
"""Set the entity state."""
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
"""Disconnect entity object when removed."""
|
|
for unsub in self._unsubs[:]:
|
|
unsub()
|
|
self._unsubs.remove(unsub)
|
|
|
|
@callback
|
|
def async_accept_signal(
|
|
self,
|
|
channel: ZigbeeChannel | None,
|
|
signal: str,
|
|
func: Callable[..., Any],
|
|
signal_override=False,
|
|
):
|
|
"""Accept a signal from a channel."""
|
|
unsub = None
|
|
if signal_override:
|
|
unsub = async_dispatcher_connect(self.hass, signal, func)
|
|
else:
|
|
assert channel
|
|
unsub = async_dispatcher_connect(
|
|
self.hass, f"{channel.unique_id}_{signal}", func
|
|
)
|
|
self._unsubs.append(unsub)
|
|
|
|
def log(self, level: int, msg: str, *args, **kwargs):
|
|
"""Log a message."""
|
|
msg = f"%s: {msg}"
|
|
args = (self.entity_id,) + args
|
|
_LOGGER.log(level, msg, *args, **kwargs)
|
|
|
|
|
|
class ZhaEntity(BaseZhaEntity, RestoreEntity):
|
|
"""A base class for non group ZHA entities."""
|
|
|
|
def __init_subclass__(cls, id_suffix: str | None = None, **kwargs: Any) -> None:
|
|
"""Initialize subclass.
|
|
|
|
:param id_suffix: suffix to add to the unique_id of the entity. Used for multi
|
|
entities using the same channel/cluster id for the entity.
|
|
"""
|
|
super().__init_subclass__(**kwargs)
|
|
if id_suffix:
|
|
cls.unique_id_suffix = id_suffix
|
|
|
|
def __init__(
|
|
self,
|
|
unique_id: str,
|
|
zha_device: ZHADevice,
|
|
channels: list[ZigbeeChannel],
|
|
**kwargs: Any,
|
|
) -> None:
|
|
"""Init ZHA entity."""
|
|
super().__init__(unique_id, zha_device, **kwargs)
|
|
self._name: str = (
|
|
self.__class__.__name__.lower()
|
|
.replace("zha", "")
|
|
.replace("entity", "")
|
|
.replace("sensor", "")
|
|
.capitalize()
|
|
)
|
|
self.cluster_channels: dict[str, ZigbeeChannel] = {}
|
|
for channel in channels:
|
|
self.cluster_channels[channel.name] = channel
|
|
|
|
@classmethod
|
|
def create_entity(
|
|
cls: type[_ZhaEntitySelfT],
|
|
unique_id: str,
|
|
zha_device: ZHADevice,
|
|
channels: list[ZigbeeChannel],
|
|
**kwargs: Any,
|
|
) -> _ZhaEntitySelfT | None:
|
|
"""Entity Factory.
|
|
|
|
Return entity if it is a supported configuration, otherwise return None
|
|
"""
|
|
return cls(unique_id, zha_device, channels, **kwargs)
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return entity availability."""
|
|
return self._zha_device.available
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Run when about to be added to hass."""
|
|
self.remove_future = asyncio.Future()
|
|
self.async_accept_signal(
|
|
None,
|
|
f"{SIGNAL_REMOVE}_{self.zha_device.ieee}",
|
|
functools.partial(self.async_remove, force_remove=True),
|
|
signal_override=True,
|
|
)
|
|
|
|
if last_state := await self.async_get_last_state():
|
|
self.async_restore_last_state(last_state)
|
|
|
|
self.async_accept_signal(
|
|
None,
|
|
f"{self.zha_device.available_signal}_entity",
|
|
self.async_state_changed,
|
|
signal_override=True,
|
|
)
|
|
self._zha_device.gateway.register_entity_reference(
|
|
self._zha_device.ieee,
|
|
self.entity_id,
|
|
self._zha_device,
|
|
self.cluster_channels,
|
|
self.device_info,
|
|
self.remove_future,
|
|
)
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
"""Disconnect entity object when removed."""
|
|
await super().async_will_remove_from_hass()
|
|
self.zha_device.gateway.remove_entity_reference(self)
|
|
self.remove_future.set_result(True)
|
|
|
|
@callback
|
|
def async_restore_last_state(self, last_state) -> None:
|
|
"""Restore previous state."""
|
|
|
|
async def async_update(self) -> None:
|
|
"""Retrieve latest state."""
|
|
tasks = [
|
|
channel.async_update()
|
|
for channel in self.cluster_channels.values()
|
|
if hasattr(channel, "async_update")
|
|
]
|
|
if tasks:
|
|
await asyncio.gather(*tasks)
|
|
|
|
|
|
class ZhaGroupEntity(BaseZhaEntity):
|
|
"""A base class for ZHA group entities."""
|
|
|
|
def __init__(
|
|
self,
|
|
entity_ids: list[str],
|
|
unique_id: str,
|
|
group_id: int,
|
|
zha_device: ZHADevice,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
"""Initialize a ZHA group."""
|
|
super().__init__(unique_id, zha_device, **kwargs)
|
|
self._available = False
|
|
self._group = zha_device.gateway.groups.get(group_id)
|
|
self._name = (
|
|
f"{self._group.name}_zha_group_0x{group_id:04x}".lower().capitalize()
|
|
)
|
|
self._group_id: int = group_id
|
|
self._entity_ids: list[str] = entity_ids
|
|
self._async_unsub_state_changed: CALLBACK_TYPE | None = None
|
|
self._handled_group_membership = False
|
|
self._change_listener_debouncer: Debouncer | None = None
|
|
self._update_group_from_child_delay = DEFAULT_UPDATE_GROUP_FROM_CHILD_DELAY
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return entity availability."""
|
|
return self._available
|
|
|
|
@classmethod
|
|
def create_entity(
|
|
cls: type[_ZhaGroupEntitySelfT],
|
|
entity_ids: list[str],
|
|
unique_id: str,
|
|
group_id: int,
|
|
zha_device: ZHADevice,
|
|
**kwargs: Any,
|
|
) -> _ZhaGroupEntitySelfT | None:
|
|
"""Group Entity Factory.
|
|
|
|
Return entity if it is a supported configuration, otherwise return None
|
|
"""
|
|
return cls(entity_ids, unique_id, group_id, zha_device, **kwargs)
|
|
|
|
async def _handle_group_membership_changed(self):
|
|
"""Handle group membership changed."""
|
|
# Make sure we don't call remove twice as members are removed
|
|
if self._handled_group_membership:
|
|
return
|
|
|
|
self._handled_group_membership = True
|
|
await self.async_remove(force_remove=True)
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Register callbacks."""
|
|
await super().async_added_to_hass()
|
|
await self.async_update()
|
|
|
|
self.async_accept_signal(
|
|
None,
|
|
f"{SIGNAL_GROUP_MEMBERSHIP_CHANGE}_0x{self._group_id:04x}",
|
|
self._handle_group_membership_changed,
|
|
signal_override=True,
|
|
)
|
|
|
|
if self._change_listener_debouncer is None:
|
|
self._change_listener_debouncer = Debouncer(
|
|
self.hass,
|
|
_LOGGER,
|
|
cooldown=self._update_group_from_child_delay,
|
|
immediate=False,
|
|
function=functools.partial(self.async_update_ha_state, True),
|
|
)
|
|
self._async_unsub_state_changed = async_track_state_change_event(
|
|
self.hass, self._entity_ids, self.async_state_changed_listener
|
|
)
|
|
|
|
def send_removed_signal():
|
|
async_dispatcher_send(
|
|
self.hass, SIGNAL_GROUP_ENTITY_REMOVED, self._group_id
|
|
)
|
|
|
|
self.async_on_remove(send_removed_signal)
|
|
|
|
@callback
|
|
def async_state_changed_listener(self, event: Event):
|
|
"""Handle child updates."""
|
|
# Delay to ensure that we get updates from all members before updating the group
|
|
assert self._change_listener_debouncer
|
|
self.hass.create_task(self._change_listener_debouncer.async_call())
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
"""Handle removal from Home Assistant."""
|
|
await super().async_will_remove_from_hass()
|
|
if self._async_unsub_state_changed is not None:
|
|
self._async_unsub_state_changed()
|
|
self._async_unsub_state_changed = None
|
|
|
|
async def async_update(self) -> None:
|
|
"""Update the state of the group entity."""
|