Delay ZHA group updates to ensure all members are updated first (#46861)

* Delay ZHA group updates to ensure all members are updated first

After turning off a group, when the first device reports "off", the
other devices may still be "on". If HA processes the group state update
quickly enough, the group will see that some devices are on, so the
state of the group will revert back to "on", and then "off" when the
remaining devices all report "off". That would cause the UI toggle to go
back and forward quickly, and automations that trigger with "state: on"
to fire when the user turns the group off.

This PR fixes that by delaying the group state update, giving time for
all the devices to report their states first.

* Fix zha group tests

* Reorder sleeping.

* Update tests/components/zha/common.py

Co-authored-by: Alexei Chetroi <lexoid@gmail.com>
This commit is contained in:
Abílio Costa 2021-03-16 21:38:16 +00:00 committed by GitHub
parent f1c274b3dd
commit d49a436573
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 23 deletions

View file

@ -32,6 +32,7 @@ from .core.typing import CALLABLE_T, ChannelType, ZhaDeviceType
_LOGGER = logging.getLogger(__name__)
ENTITY_SUFFIX = "entity_suffix"
UPDATE_GROUP_FROM_CHILD_DELAY = 0.2
class BaseZhaEntity(LogMixin, entity.Entity):
@ -267,7 +268,11 @@ class ZhaGroupEntity(BaseZhaEntity):
@callback
def async_state_changed_listener(self, event: Event):
"""Handle child updates."""
self.async_schedule_update_ha_state(True)
# Delay to ensure that we get updates from all members before updating the group
self.hass.loop.call_later(
UPDATE_GROUP_FROM_CHILD_DELAY,
lambda: self.async_schedule_update_ha_state(True),
)
async def async_will_remove_from_hass(self) -> None:
"""Handle removal from Home Assistant."""