From b2c8e97d40a6834685178b2cac2bebb4caf46aa2 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 19 Oct 2026 21:38:48 +0200 Subject: [PATCH] Ignore stale moving operational status for position aware Matter covers (#177571) Co-authored-by: Claude Fable 5 Co-authored-by: Ingo Fischer Co-authored-by: TheJulianJES --- homeassistant/components/matter/cover.py | 80 +++++ tests/components/matter/test_cover.py | 435 ++++++++++++++++++++++- 2 files changed, 514 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/matter/cover.py b/homeassistant/components/matter/cover.py index afc55ba71018..8e0772a9043c 100644 --- a/homeassistant/components/matter/cover.py +++ b/homeassistant/components/matter/cover.py @@ -126,6 +126,55 @@ class MatterCover(MatterEntity, CoverEntity): clusters.WindowCovering.Commands.GoToTiltPercentage((100 - position) * 100) ) + @callback + def _positions_at_target(self) -> bool | None: + """Return whether all supported positions match their target position. + + Returns None if the positions cannot be compared, e.g. the device is + not position aware or a position is (still) unknown. The axes to + compare are taken from the FeatureMap: lift and tilt are independently + position aware (PA_LF conformance `[LF]`, PA_TL conformance `[TL]`), + so a supported axis which is not position aware, or lacks its position + attributes, makes the covering not comparable as a whole, while + position attributes of an unsupported axis (reported as null by e.g. + the Shelly 2PM Gen4) are ignored. + """ + feature_map = self.get_matter_attribute_value( + clusters.WindowCovering.Attributes.FeatureMap + ) + features = clusters.WindowCovering.Bitmaps.Feature + at_target: bool | None = None + for feature, position_aware_feature, current_attribute, target_attribute in ( + ( + features.kLift, + features.kPositionAwareLift, + clusters.WindowCovering.Attributes.CurrentPositionLiftPercent100ths, + clusters.WindowCovering.Attributes.TargetPositionLiftPercent100ths, + ), + ( + features.kTilt, + features.kPositionAwareTilt, + clusters.WindowCovering.Attributes.CurrentPositionTiltPercent100ths, + clusters.WindowCovering.Attributes.TargetPositionTiltPercent100ths, + ), + ): + if not feature_map & feature: + continue + if not feature_map & position_aware_feature: + return None + if not self._entity_info.endpoint.has_attribute( + None, current_attribute + ) or not self._entity_info.endpoint.has_attribute(None, target_attribute): + return None + current_position = self.get_matter_attribute_value(current_attribute) + target_position = self.get_matter_attribute_value(target_attribute) + if current_position is None or target_position is None: + return None + if current_position != target_position: + return False + at_target = True + return at_target + @callback @override def _update_from_device(self) -> None: @@ -154,6 +203,29 @@ class MatterCover(MatterEntity, CoverEntity): self._attr_is_opening = False self._attr_is_closing = False + # Some bridges (e.g. Rademacher SmartHome Box) report current = target + # with a moving status that never clears. Being at the target only ever + # clears the moving state; at movement start the debounce hides a status + # report arriving before the target update. + if (at_target := self._positions_at_target()) is None: + detection = "positions not comparable" + else: + detection = f"positions at target: {at_target}" + if at_target and (self._attr_is_opening or self._attr_is_closing): + LOGGER.debug( + "Ignoring moving operational status for %s, position(s) match target position(s)", + self.entity_id, + ) + self._attr_is_opening = False + self._attr_is_closing = False + LOGGER.debug( + "Movement state for %s: opening=%s closing=%s (%s)", + self.entity_id, + self._attr_is_opening, + self._attr_is_closing, + detection, + ) + if self._entity_info.endpoint.has_attribute( None, clusters.WindowCovering.Attributes.CurrentPositionLiftPercent100ths ): @@ -251,6 +323,9 @@ DISCOVERY_SCHEMAS = [ clusters.WindowCovering.Attributes.Type, clusters.WindowCovering.Attributes.CurrentPositionLiftPercent100ths, ), + optional_attributes=( + clusters.WindowCovering.Attributes.TargetPositionLiftPercent100ths, + ), absent_attributes=( clusters.WindowCovering.Attributes.CurrentPositionTiltPercent100ths, ), @@ -266,6 +341,9 @@ DISCOVERY_SCHEMAS = [ clusters.WindowCovering.Attributes.Type, clusters.WindowCovering.Attributes.CurrentPositionTiltPercent100ths, ), + optional_attributes=( + clusters.WindowCovering.Attributes.TargetPositionTiltPercent100ths, + ), absent_attributes=( clusters.WindowCovering.Attributes.CurrentPositionLiftPercent100ths, ), @@ -286,6 +364,8 @@ DISCOVERY_SCHEMAS = [ # disabled, which would otherwise fail schema matching optional_attributes=( clusters.WindowCovering.Attributes.CurrentPositionTiltPercent100ths, + clusters.WindowCovering.Attributes.TargetPositionLiftPercent100ths, + clusters.WindowCovering.Attributes.TargetPositionTiltPercent100ths, ), ), ] diff --git a/tests/components/matter/test_cover.py b/tests/components/matter/test_cover.py index 4f727dafb7bd..dcb8a254cfa1 100644 --- a/tests/components/matter/test_cover.py +++ b/tests/components/matter/test_cover.py @@ -12,12 +12,13 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.cover import CoverEntityFeature, CoverState from homeassistant.components.matter.cover import STATE_WRITE_DEBOUNCE_COOLDOWN -from homeassistant.const import Platform +from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from .common import ( set_node_attribute, + set_node_attribute_and_notify, snapshot_matter_entities, trigger_subscription_callback, ) @@ -151,6 +152,8 @@ async def test_cover_lift( ) matter_client.send_device_command.reset_mock() + set_node_attribute(matter_node, 1, 258, 14, 5000) + set_node_attribute(matter_node, 1, 258, 11, 10000) set_node_attribute(matter_node, 1, 258, 10, 0b001010) await trigger_subscription_callback_debounced(hass, freezer, matter_client) @@ -158,6 +161,7 @@ async def test_cover_lift( assert state assert state.state == CoverState.CLOSING + set_node_attribute(matter_node, 1, 258, 11, 0) set_node_attribute(matter_node, 1, 258, 10, 0b000101) await trigger_subscription_callback_debounced(hass, freezer, matter_client) @@ -315,6 +319,334 @@ async def test_cover_split_attribute_updates( assert state.state == CoverState.CLOSED +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_full", "cover.mock_full_window_covering"), + ], +) +async def test_cover_stale_moving_operational_status( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test moving operational status is ignored when the cover is at its target. + + Replays the Matter Server debug log of a Rademacher SmartHome Box bridged + shutter (home-assistant/core#186168): resting at 42% lift with the tilt + closed, it answers GoToLiftPercentage(10000) within ~100 ms with three + attribute updates, OperationalStatus 0b001010, then target and current + lift both 10000, and never reports the operational status clearing. + """ + + set_node_attribute(matter_node, 1, 258, 11, 4200) + set_node_attribute(matter_node, 1, 258, 14, 4200) + set_node_attribute(matter_node, 1, 258, 12, 10000) + set_node_attribute(matter_node, 1, 258, 15, 10000) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.OPEN + assert state.attributes["current_position"] == 58 + + for attribute_id, value in ( + (10, 0b001010), + (11, 10000), + (14, 10000), + ): + await set_node_attribute_and_notify( + hass, + matter_client, + matter_node, + endpoint=1, + cluster_id=258, + attribute_id=attribute_id, + value=value, + ) + + # no intermittent state is written while the report is still coalescing + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.OPEN + + freezer.tick(STATE_WRITE_DEBOUNCE_COOLDOWN) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSED + assert state.attributes["current_position"] == 0 + + +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_full", "cover.mock_full_window_covering"), + ], +) +async def test_cover_stale_moving_operational_status_since_interview( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test a stale moving operational status is ignored across position reports. + + Replays the Matter Server debug log of a Rademacher SmartHome Box bridged + venetian blind (home-assistant/core#186168): OperationalStatus 0b010101 as + read at the interview is never reported again, neither on + GoToLiftPercentage(10000) nor on GoToLiftPercentage(0). The bridge reports + target and current lift as the same value on command receipt and during + travel, except for one correction to the physical position 1.3 s after + the open command. + """ + + set_node_attribute(matter_node, 1, 258, 10, 0b010101) + set_node_attribute(matter_node, 1, 258, 11, 10000) + set_node_attribute(matter_node, 1, 258, 14, 10000) + set_node_attribute(matter_node, 1, 258, 12, 0) + set_node_attribute(matter_node, 1, 258, 15, 0) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSED + assert state.attributes["current_position"] == 0 + + for updates, expected_state, expected_position in ( + # open command: target and current both reported as 0 + (((11, 0), (14, 0)), CoverState.OPEN, 100), + # correction to the physical position: the status is trusted again + (((14, 10000),), CoverState.OPENING, 0), + # mid-travel report with target = current + (((11, 7400), (14, 7400), (12, 1500), (15, 1500)), CoverState.OPEN, 26), + # end of travel + (((11, 0), (14, 0)), CoverState.OPEN, 100), + ): + for attribute_id, value in updates: + await set_node_attribute_and_notify( + hass, + matter_client, + matter_node, + endpoint=1, + cluster_id=258, + attribute_id=attribute_id, + value=value, + ) + freezer.tick(STATE_WRITE_DEBOUNCE_COOLDOWN) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == expected_state + assert state.attributes["current_position"] == expected_position + + +@pytest.mark.parametrize( + ("target_position", "current_position"), + [ + pytest.param(None, 10000, id="unknown_target"), + pytest.param(None, None, id="unknown_target_and_position"), + ], +) +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_pa_lift", "cover.longan_link_wncv_da01"), + ], +) +async def test_cover_moving_status_with_unknown_target( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, + target_position: int | None, + current_position: int | None, +) -> None: + """Test operational status is trusted when the positions cannot be compared.""" + + set_node_attribute(matter_node, 1, 258, 11, target_position) + set_node_attribute(matter_node, 1, 258, 14, current_position) + set_node_attribute(matter_node, 1, 258, 10, 0b001010) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSING + + +@pytest.mark.parametrize( + ( + "node_fixture", + "entity_id", + "current_attribute_id", + "target_attribute_id", + "resting_state", + ), + [ + pytest.param( + "mock_window_covering_pa_lift", + "cover.longan_link_wncv_da01", + 14, + 11, + CoverState.OPEN, + id="lift", + ), + # a tilt only cover has no lift position to derive open/closed from + pytest.param( + "mock_window_covering_pa_tilt", + "cover.mock_pa_tilt_window_covering", + 15, + 12, + STATE_UNKNOWN, + id="tilt", + ), + pytest.param( + "mock_window_covering_full", + "cover.mock_full_window_covering", + 14, + 11, + CoverState.OPEN, + id="lift_and_tilt_lift", + ), + pytest.param( + "mock_window_covering_full", + "cover.mock_full_window_covering", + 15, + 12, + CoverState.OPEN, + id="lift_and_tilt_tilt", + ), + ], +) +async def test_cover_target_position_subscription( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + current_attribute_id: int, + target_attribute_id: int, + resting_state: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test a target position only report reaches the entity.""" + + set_node_attribute(matter_node, 1, 258, current_attribute_id, 4900) + set_node_attribute(matter_node, 1, 258, target_attribute_id, 0) + set_node_attribute(matter_node, 1, 258, 10, 0b001010) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSING + + # StopMotion sets the target to the current position, so a target only + # report is enough to clear a stale moving operational status + await set_node_attribute_and_notify( + hass, + matter_client, + matter_node, + endpoint=1, + cluster_id=258, + attribute_id=target_attribute_id, + value=4900, + ) + freezer.tick(STATE_WRITE_DEBOUNCE_COOLDOWN) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == resting_state + + +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_pa_lift", "cover.longan_link_wncv_da01"), + ], +) +async def test_cover_moving_status_masked_until_target_updates( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test a moving operational status is ignored while the target is stale.""" + + # the cover rests at its target and starts moving, but the report carries + # only the operational status, so the stale target masks the movement + set_node_attribute(matter_node, 1, 258, 11, 10000) + set_node_attribute(matter_node, 1, 258, 14, 10000) + set_node_attribute(matter_node, 1, 258, 10, 0b000101) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSED + + # the movement is reported again once the target catches up + await set_node_attribute_and_notify( + hass, + matter_client, + matter_node, + endpoint=1, + cluster_id=258, + attribute_id=11, + value=0, + ) + freezer.tick(STATE_WRITE_DEBOUNCE_COOLDOWN) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.OPENING + + +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_pa_lift", "cover.longan_link_wncv_da01"), + ], +) +async def test_cover_position_updates_without_operational_status( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test a device which only reports positions, never an operational status.""" + + # the device reports the new target and position updates while the + # operational status remains "not moving" the whole time + set_node_attribute(matter_node, 1, 258, 11, 10000) + set_node_attribute(matter_node, 1, 258, 14, 5000) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.attributes["current_position"] == 50 + assert state.state == CoverState.OPEN + + set_node_attribute(matter_node, 1, 258, 14, 10000) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.attributes["current_position"] == 0 + assert state.state == CoverState.CLOSED + + @pytest.mark.parametrize( ("node_fixture", "entity_id"), [ @@ -352,12 +684,15 @@ async def test_cover_tilt( await trigger_subscription_callback_debounced(hass, freezer, matter_client) + set_node_attribute(matter_node, 1, 258, 15, 5000) + set_node_attribute(matter_node, 1, 258, 12, 10000) set_node_attribute(matter_node, 1, 258, 10, 0b100010) await trigger_subscription_callback_debounced(hass, freezer, matter_client) state = hass.states.get(entity_id) assert state assert state.state == CoverState.CLOSING + set_node_attribute(matter_node, 1, 258, 12, 0) set_node_attribute(matter_node, 1, 258, 10, 0b010001) await trigger_subscription_callback_debounced(hass, freezer, matter_client) @@ -535,3 +870,101 @@ async def test_cover_full_features( state = hass.states.get(entity_id) assert state assert state.state == "unknown" + + +@pytest.mark.parametrize( + ("node_fixture", "entity_id", "feature_map", "expected_state"), + [ + # a covering that tilts without being position aware on tilt: the lift + # sitting at its target says nothing about whether the tilt is moving + pytest.param( + "mock_window_covering_pa_lift", + "cover.longan_link_wncv_da01", + 0b00111, + CoverState.CLOSING, + id="tilt_not_position_aware", + ), + # the same, with tilt positions exposed regardless + pytest.param( + "mock_window_covering_full", + "cover.mock_full_window_covering", + 0b00111, + CoverState.CLOSING, + id="tilt_not_position_aware_positions_exposed", + ), + # a covering which is not position aware on lift but exposes lift + # positions regardless + pytest.param( + "mock_window_covering_pa_lift", + "cover.longan_link_wncv_da01", + 0b00001, + CoverState.CLOSING, + id="lift_not_position_aware_positions_exposed", + ), + # a covering claiming to be position aware on tilt without exposing + # the tilt position attributes + pytest.param( + "mock_window_covering_pa_lift", + "cover.longan_link_wncv_da01", + 0b10111, + CoverState.CLOSING, + id="tilt_positions_missing", + ), + # the Shelly 2PM Gen4 exposes null tilt positions without supporting + # tilt, which must not keep the lift from being checked + pytest.param( + "mock_window_covering_pa_lift_null_tilt", + "cover.shelly_2pm_gen4", + 0b00101, + CoverState.CLOSED, + id="tilt_unsupported_null_positions", + ), + ], +) +async def test_cover_moving_status_axis_selection( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + feature_map: int, + expected_state: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the axes compared against their target follow the FeatureMap.""" + + set_node_attribute(matter_node, 1, 258, 65532, feature_map) + set_node_attribute(matter_node, 1, 258, 11, 10000) + set_node_attribute(matter_node, 1, 258, 14, 10000) + set_node_attribute(matter_node, 1, 258, 10, 0b001010) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == expected_state + + +@pytest.mark.parametrize( + ("node_fixture", "entity_id"), + [ + ("mock_window_covering_full", "cover.mock_full_window_covering"), + ], +) +async def test_cover_moving_status_with_unknown_tilt_position( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, + entity_id: str, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the lift at its target does not hide movement with an unknown tilt.""" + + set_node_attribute(matter_node, 1, 258, 11, 10000) + set_node_attribute(matter_node, 1, 258, 14, 10000) + set_node_attribute(matter_node, 1, 258, 12, 0) + set_node_attribute(matter_node, 1, 258, 15, None) + set_node_attribute(matter_node, 1, 258, 10, 0b001010) + await trigger_subscription_callback_debounced(hass, freezer, matter_client) + + state = hass.states.get(entity_id) + assert state + assert state.state == CoverState.CLOSING