diff --git a/homeassistant/components/matter/fan.py b/homeassistant/components/matter/fan.py index 02d8531b831d..4863cc24f1eb 100644 --- a/homeassistant/components/matter/fan.py +++ b/homeassistant/components/matter/fan.py @@ -242,7 +242,7 @@ class MatterFan(MatterEntity, FanEntity): # keep track of the last known mode for turn_on commands without preset if self._attr_preset_mode is not None: self._last_known_preset_mode = self._attr_preset_mode - if current_percent: + if current_percent and current_percent != 255: self._last_known_percentage = current_percent @callback diff --git a/tests/components/matter/test_fan.py b/tests/components/matter/test_fan.py index b9bba9d218d5..573a8d2698c2 100644 --- a/tests/components/matter/test_fan.py +++ b/tests/components/matter/test_fan.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock, call +from chip.clusters import Objects as clusters from matter_server.client.models.node import MatterNode import pytest from syrupy.assertion import SnapshotAssertion @@ -133,8 +134,10 @@ async def test_fan_turn_on_with_percentage( attribute_path="1/514/2", value=50, ) - # test again where preset_mode is omitted in the service call - # which should select the last active percentage + # test again where percentage is omitted in the service call + # PercentCurrent is 255 (auto) on this fixture, which is not a valid + # value to write back, so it should fall back to the last known preset + # mode instead of blindly replaying the sentinel value. matter_client.write_attribute.reset_mock() await hass.services.async_call( FAN_DOMAIN, @@ -145,8 +148,8 @@ async def test_fan_turn_on_with_percentage( assert matter_client.write_attribute.call_count == 1 assert matter_client.write_attribute.call_args == call( node_id=matter_node.node_id, - attribute_path="1/514/2", - value=255, + attribute_path="1/514/0", + value=clusters.FanControl.Enums.FanModeEnum.kAuto, )