Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group entity availability handling #164

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,36 @@ async def test_zha_group_fan_entity(
# test that group fan is now off
assert entity.state["is_on"] is False

assert entity.state["available"] is True

device_fan_1.on_network = False
device_fan_2.on_network = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_fan_1.on_network = True
device_fan_2.on_network = True
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True

device_fan_1.available = False
device_fan_2.available = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_fan_1.available = True
device_fan_2.available = True
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True


@patch(
"zigpy.zcl.clusters.hvac.Fan.write_attributes",
Expand Down
30 changes: 30 additions & 0 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,36 @@ async def test_zha_group_light_entity(
await zha_gateway.async_block_till_done()
assert bool(entity.state["on"]) is True

assert entity.state["available"] is True

device_light_1.on_network = False
device_light_2.on_network = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_light_1.on_network = True
device_light_2.on_network = True
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True

device_light_1.available = False
device_light_2.available = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_light_1.available = True
device_light_2.available = True
dmulcahey marked this conversation as resolved.
Show resolved Hide resolved
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True

# turn it off to test a new member add being tracked
await send_attributes_report(zha_gateway, dev1_cluster_on_off, {0: 0})
await zha_gateway.async_block_till_done()
Expand Down
30 changes: 30 additions & 0 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,36 @@ async def test_zha_group_switch_entity(
# test that group light is now back on
assert bool(entity.state["state"]) is True

assert entity.state["available"] is True

device_switch_1.on_network = False
device_switch_2.on_network = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_switch_1.on_network = True
device_switch_2.on_network = True
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True

device_switch_1.available = False
device_switch_2.available = False
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is False

device_switch_1.available = True
device_switch_2.available = True
await asyncio.sleep(0.1)
await zha_gateway.async_block_till_done()

assert entity.state["available"] is True


class WindowDetectionFunctionQuirk(CustomDevice):
"""Quirk with window detection function attribute."""
Expand Down
17 changes: 16 additions & 1 deletion zha/application/platforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def __init__(
_LOGGER,
cooldown=update_group_from_member_delay,
immediate=False,
function=self.async_update,
function=self.update,
)
self._group.register_group_entity(self)

Expand All @@ -442,6 +442,21 @@ def info_object(self) -> BaseEntityInfo:
group_id=self.group_id,
)

@property
def state(self) -> dict[str, Any]:
"""Return the arguments to use in the command."""
state = super().state
state["available"] = self.available
return state

@property
def available(self) -> bool:
"""Return true if all member entities are available."""
return any(
platform_entity.available
for platform_entity in self._group.get_platform_entities(self.PLATFORM)
)

@property
def group_id(self) -> int:
"""Return the group id."""
Expand Down
2 changes: 0 additions & 2 deletions zha/application/platforms/fan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def __init__(self, group: Group):
"""Initialize a fan group."""
self._fan_cluster_handler: ClusterHandler = group.endpoint[hvac.Fan.cluster_id]
super().__init__(group)
self._available: bool = False
self._percentage = None
self._preset_mode = None
if hasattr(self, "info_object"):
Expand Down Expand Up @@ -355,7 +354,6 @@ def update(self, _: Any = None) -> None:
"All platform entity states for group entity members: %s", all_states
)

self._available = any(entity.available for entity in platform_entities)
percentage_states: list[dict] = [
state for state in all_states if state.get(ATTR_PERCENTAGE)
]
Expand Down
11 changes: 0 additions & 11 deletions zha/application/platforms/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def __init__(self, *args, **kwargs):
"""Initialize the light."""
self._device: Device = None
super().__init__(*args, **kwargs)
self._available: bool = False
self._min_mireds: int | None = 153
self._max_mireds: int | None = 500
self._hs_color: tuple[float, float] | None = None
Expand Down Expand Up @@ -1207,12 +1206,6 @@ def info_object(self) -> LightEntityInfo:
max_mireds=self.max_mireds,
)

# remove this when all ZHA platforms and base entities are updated
@property
def available(self) -> bool:
"""Return entity availability."""
return self._available

async def on_remove(self) -> None:
"""Cancel tasks this entity owns."""
await super().on_remove()
Expand Down Expand Up @@ -1261,10 +1254,6 @@ def update(self, _: Any = None) -> None:
self._off_with_transition = False
self._off_brightness = None

self._available = any(
platform_entity.device.available for platform_entity in platform_entities
)

self._brightness = reduce_attribute(on_states, ATTR_BRIGHTNESS)

self._xy_color = reduce_attribute(on_states, ATTR_XY_COLOR, reduce=mean_tuple)
Expand Down
1 change: 0 additions & 1 deletion zha/application/platforms/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def update(self, _: Any | None = None) -> None:
on_states = [state for state in all_states if state["state"]]

self._state = len(on_states) > 0
self._available = any(entity.available for entity in platform_entities)

self.maybe_emit_state_changed_event()

Expand Down
Loading