Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
126 changes: 126 additions & 0 deletions tests/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,132 @@ async def test_cover_recompute_capabilities_on_type_update(
assert len(subscriber.mock_calls) == 1


async def test_cover_unknown_position(zha_gateway: Gateway) -> None:
"""Test that a position percentage of 0xFF (unknown) is not shown as a position."""

# 0xFF is the attribute's default (ZCL R8 Table 7-40): the cover does not know
# its position (Zigbee2MQTT notes that a ubisys J1 reports 255 then)
zha_device, zigpy_cover_device = await device_cover_mock(
zha_gateway,
current_position_lift_percentage=0xFF,
current_position_tilt_percentage=0xFF,
window_covering_type=WCT.Tilt_blind_tilt_and_lift,
)
cluster = zigpy_cover_device.endpoints[1].window_covering
entity = get_entity(zha_device, platform=Platform.COVER)

assert entity.state.current_position is None
assert entity.state.current_tilt_position is None
assert _cover_state(entity) is None

# a known position is shown
await send_attributes_report(
zha_gateway,
cluster,
{
WCAttrs.current_position_lift_percentage.id: 40,
WCAttrs.current_position_tilt_percentage.id: 30,
},
)
assert entity.state.current_position == 60
assert entity.state.current_tilt_position == 70

# and becomes unknown again when the cover reports 0xFF
await send_attributes_report(
zha_gateway,
cluster,
{
WCAttrs.current_position_lift_percentage.id: 0xFF,
WCAttrs.current_position_tilt_percentage.id: 0xFF,
},
)
assert entity.state.current_position is None
assert entity.state.current_tilt_position is None

# moving a cover with an unknown position still sends the command
with patch("zigpy.zcl.Cluster.request", return_value=[0x05, zcl_f.Status.SUCCESS]):
await entity.async_set_cover_position(position=50)
await zha_gateway.async_block_till_done()
assert cluster.request.call_count == 1
assert cluster.request.call_args[0][3] == 50

await entity.async_set_cover_tilt_position(tilt_position=20)
await zha_gateway.async_block_till_done()
assert cluster.request.call_count == 2
assert cluster.request.call_args[0][3] == 80


async def test_cover_unknown_position_keeps_transition(zha_gateway: Gateway) -> None:
"""Test that moving a cover with an unknown position keeps its target."""

zha_device, zigpy_cover_device = await device_cover_mock(
zha_gateway,
current_position_lift_percentage=0xFF,
current_position_tilt_percentage=0,
window_covering_type=WCT.Tilt_blind_tilt_and_lift,
)
cluster = zigpy_cover_device.endpoints[1].window_covering
entity = get_entity(zha_device, platform=Platform.COVER)
assert entity.state.current_position is None
assert _cover_state(entity) == CoverState.OPEN # from the known tilt

with patch("zigpy.zcl.Cluster.request", return_value=[0x05, zcl_f.Status.SUCCESS]):
await entity.async_set_cover_position(position=50)
await zha_gateway.async_block_till_done()

# a tilt report does not drop the lift target while the lift position is unknown
await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_tilt_percentage.id: 0}
)
assert entity._target_lift_position == 50
assert entity._lift_transition_timer is not None

# the first known lift position shows the direction towards the target
await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_lift_percentage.id: 60}
)
assert entity.state.current_position == 40
assert _cover_state(entity) == CoverState.OPENING

await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_lift_percentage.id: 50}
)
assert entity.state.current_position == 50
assert _cover_state(entity) == CoverState.OPEN
assert entity._lift_transition_timer is None

# without a position report the transition ends with its timer
await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_lift_percentage.id: 0xFF}
)
with patch("zigpy.zcl.Cluster.request", return_value=[0x05, zcl_f.Status.SUCCESS]):
await entity.async_set_cover_position(position=20)
await zha_gateway.async_block_till_done()
assert entity._lift_transition_timer is not None
await asyncio.sleep(DEFAULT_MOVEMENT_TIMEOUT)
assert entity._target_lift_position is None
assert entity._lift_transition_timer is None
assert _cover_state(entity) == CoverState.OPEN # from the known tilt

# a command that raises starts no timer, and its target is dropped on the next
# state update instead of being kept while the position is unknown
with (
patch("zigpy.zcl.Cluster.request", side_effect=TimeoutError),
pytest.raises(TimeoutError),
):
await entity.async_set_cover_position(position=20)
assert entity._target_lift_position == 20
assert entity._lift_transition_timer is None
await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_tilt_percentage.id: 0}
)
assert entity._target_lift_position is None
await send_attributes_report(
zha_gateway, cluster, {WCAttrs.current_position_lift_percentage.id: 60}
)
assert _cover_state(entity) == CoverState.OPEN


async def test_cover_failures(zha_gateway: Gateway) -> None:
"""Test ZHA cover platform failure cases."""

Expand Down
60 changes: 36 additions & 24 deletions zha/application/platforms/cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,9 @@ def current_cover_position(self) -> int | None:
"""
if not self.supported_features & CoverEntityFeature.OPEN:
return None
lift_percentage = self._cluster.get(
WCAttrs.current_position_lift_percentage.name
return self._zcl_percentage_to_ha_position(
self._cluster.get(WCAttrs.current_position_lift_percentage.name)
)
if lift_percentage is None:
return None
# ZCL 0=open / 100=closed → HA 0=closed / 100=open
return 100 - lift_percentage

@property
def current_cover_tilt_position(self) -> int | None:
Expand All @@ -356,12 +352,9 @@ def current_cover_tilt_position(self) -> int | None:
"""
if not self.supported_features & CoverEntityFeature.OPEN_TILT:
return None
tilt_percentage = self._cluster.get(
WCAttrs.current_position_tilt_percentage.name
return self._zcl_percentage_to_ha_position(
self._cluster.get(WCAttrs.current_position_tilt_percentage.name)
)
if tilt_percentage is None:
return None
return 100 - tilt_percentage

@property
def _previous_cover_position(self) -> int | None:
Expand Down Expand Up @@ -459,16 +452,20 @@ def _determine_cover_state(
self._tilt_position_history,
)

# Clear transition if the cover axis is not moving, else update the timer
if self._lift_state not in (CoverState.OPENING, CoverState.CLOSING):
# Clear transition if the cover axis is not moving, else update the timer.
# An axis with an unknown position (state None) keeps a running transition
# until its timer runs out: the direction shows once a position is reported.
if self._lift_state in (CoverState.OPENING, CoverState.CLOSING):
if is_lift_update:
self._start_lift_transition(is_position_update=True)
elif self._lift_state is not None or self._lift_transition_timer is None:
self._clear_lift_transition()
elif is_lift_update:
self._start_lift_transition(is_position_update=True)

if self._tilt_state not in (CoverState.OPENING, CoverState.CLOSING):
if self._tilt_state in (CoverState.OPENING, CoverState.CLOSING):
if is_tilt_update:
self._start_tilt_transition(is_position_update=True)
elif self._tilt_state is not None or self._tilt_transition_timer is None:
self._clear_tilt_transition()
elif is_tilt_update:
self._start_tilt_transition(is_position_update=True)

# Keep the last direction if either axis is still moving
if (
Expand Down Expand Up @@ -686,7 +683,6 @@ async def async_close_cover_tilt(self) -> None:

async def async_set_cover_position(self, position: int) -> None:
"""Move the cover to a specific position."""
assert self.current_cover_position is not None
target_position = position

self._set_lift_transition_target(target_position)
Expand All @@ -697,17 +693,17 @@ async def async_set_cover_position(self, position: int) -> None:
self._clear_lift_transition()
raise ZHAException(f"Failed to set cover position: {res[1]}")

if target_position != self.current_cover_position:
current_position = self.current_cover_position
if current_position is not None and target_position != current_position:
self.async_update_state(
CoverState.CLOSING
if target_position < self.current_cover_position
if target_position < current_position
else CoverState.OPENING
)
self._start_lift_transition()

async def async_set_cover_tilt_position(self, tilt_position: int) -> None:
"""Move the cover tilt to a specific position."""
assert self.current_cover_tilt_position is not None
target_position = tilt_position

self._set_tilt_transition_target(target_position)
Expand All @@ -718,10 +714,14 @@ async def async_set_cover_tilt_position(self, tilt_position: int) -> None:
self._clear_tilt_transition()
raise ZHAException(f"Failed to set cover tilt position: {res[1]}")

if target_position != self.current_cover_tilt_position:
current_tilt_position = self.current_cover_tilt_position
if (
current_tilt_position is not None
and target_position != current_tilt_position
):
self.async_update_state(
CoverState.CLOSING
if target_position < self.current_cover_tilt_position
if target_position < current_tilt_position
else CoverState.OPENING
)
self._start_tilt_transition()
Expand All @@ -745,6 +745,18 @@ async def async_stop_cover_tilt(self) -> None:
"""
await self.async_stop_cover()

@staticmethod
def _zcl_percentage_to_ha_position(percentage: int | None) -> int | None:
"""Convert a ZCL position percentage to the HA position range.

In HA None is unknown, 0 is closed, 100 is fully open.
In ZCL 0 is fully open, 100 is fully closed; a value above 100 means the
position is unknown (0xFF is the attribute's default).
"""
if percentage is None or not 0 <= percentage <= 100:
return None
return 100 - percentage

@staticmethod
def _ha_position_to_zcl(position: int) -> int:
"""Convert the HA position to the ZCL position range.
Expand Down
Loading