From a754647568e726358b22a27c8e1b447fffd318bd Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:23:02 +0100 Subject: [PATCH 1/2] Show an unknown cover position as unknown, not -155 CurrentPositionLiftPercentage and CurrentPositionTiltPercentage are 0-100, with 0xFF as their default (ZCL R8 Table 7-40): a cover that doesn't know its position reports 255 (Zigbee2MQTT notes this for the ubisys J1). ZHA converted that to 100 - 255 = -155 and showed the cover as closed at -155 %. Treat a percentage outside 0-100 as an unknown position (None), and let set position / set tilt position send their command when the current position is unknown instead of failing an assert. --- tests/test_cover.py | 55 +++++++++++++++++++++ zha/application/platforms/cover/__init__.py | 42 +++++++++------- 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/tests/test_cover.py b/tests/test_cover.py index 5598dbc4b..05ac8aed6 100644 --- a/tests/test_cover.py +++ b/tests/test_cover.py @@ -716,6 +716,61 @@ 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_failures(zha_gateway: Gateway) -> None: """Test ZHA cover platform failure cases.""" diff --git a/zha/application/platforms/cover/__init__.py b/zha/application/platforms/cover/__init__.py index 00266e93f..896fdbe6d 100644 --- a/zha/application/platforms/cover/__init__.py +++ b/zha/application/platforms/cover/__init__.py @@ -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: @@ -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: @@ -686,7 +679,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) @@ -697,17 +689,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) @@ -718,10 +710,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() @@ -745,6 +741,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. From ff8a5af208d726e2d74545993f6258f68fc9fc59 Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:38:53 +0100 Subject: [PATCH 2/2] Keep a cover's transition target while its position is unknown A set-position command sent while the lift (or tilt) position is unknown leaves that axis's state at None. Any later recompute, such as a tilt report on a lift-and-tilt cover, treated that None as "not moving" and cleared the transition target and timer, so the first known position report showed a static state instead of the direction towards the target. Keep a running transition on an axis whose state is unknown until its timer runs out. A known non-moving state still clears it, and so does any recompute when no timer runs (a command that raised leaves a target without one). --- tests/test_cover.py | 71 +++++++++++++++++++++ zha/application/platforms/cover/__init__.py | 18 ++++-- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/tests/test_cover.py b/tests/test_cover.py index 05ac8aed6..dc72cbc29 100644 --- a/tests/test_cover.py +++ b/tests/test_cover.py @@ -771,6 +771,77 @@ async def test_cover_unknown_position(zha_gateway: Gateway) -> None: 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.""" diff --git a/zha/application/platforms/cover/__init__.py b/zha/application/platforms/cover/__init__.py index 896fdbe6d..5ea6af361 100644 --- a/zha/application/platforms/cover/__init__.py +++ b/zha/application/platforms/cover/__init__.py @@ -452,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 (