From 1c983cd5e345dbdcd7680d6f18b3c761a978e20b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 29 Aug 2026 01:18:22 +0200 Subject: [PATCH] Leave a SwitchBot air purifier without a preset it does not report (#180560) --- .../components/switchbot_cloud/fan.py | 11 ++++++--- tests/components/switchbot_cloud/test_fan.py | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/switchbot_cloud/fan.py b/homeassistant/components/switchbot_cloud/fan.py index a26b86f68303..5301ac6dc615 100644 --- a/homeassistant/components/switchbot_cloud/fan.py +++ b/homeassistant/components/switchbot_cloud/fan.py @@ -137,6 +137,11 @@ class SwitchBotCloudFan(SwitchBotCloudEntity, FanEntity): await self.coordinator.async_request_refresh() +_AIR_PURIFIER_PRESET_MODES = { + mode.value: mode.name.lower() for mode in AirPurifierModeV2 +} + + class SwitchBotAirPurifierEntity(SwitchBotCloudEntity, FanEntity): """Representation of a Switchbot air purifier.""" @@ -164,9 +169,9 @@ class SwitchBotAirPurifierEntity(SwitchBotCloudEntity, FanEntity): return self._attr_is_on = self.coordinator.data.get("power") == STATE_ON.upper() - mode = self.coordinator.data.get("mode") - self._attr_preset_mode = ( - AirPurifierModeV2(mode).name.lower() if mode is not None else None + # An unplugged purifier reports a mode of its own that is none of these + self._attr_preset_mode = _AIR_PURIFIER_PRESET_MODES.get( + self.coordinator.data.get("mode") ) @override diff --git a/tests/components/switchbot_cloud/test_fan.py b/tests/components/switchbot_cloud/test_fan.py index 61bf11b37a0d..11c3e79835b0 100644 --- a/tests/components/switchbot_cloud/test_fan.py +++ b/tests/components/switchbot_cloud/test_fan.py @@ -308,6 +308,29 @@ async def test_air_purifier( await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) +async def test_air_purifier_unknown_mode( + hass: HomeAssistant, + mock_list_devices: AsyncMock, + mock_get_status: AsyncMock, +) -> None: + """Test an air purifier reporting a mode that is not one of its presets. + + An unplugged purifier reports mode 0, which no preset maps to. + """ + mock_list_devices.return_value = [AIR_PURIFIER_INFO] + status = await async_load_json_object_fixture( + hass, "air_purifier_status.json", DOMAIN + ) + mock_get_status.return_value = {**status, "power": "OFF", "mode": 0} + + with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.FAN]): + await configure_integration(hass) + + state = hass.states.get("fan.air_purifier_1") + assert state.state == STATE_OFF + assert state.attributes[ATTR_PRESET_MODE] is None + + @pytest.mark.parametrize( ("service", "service_data", "expected_call_args"), [