Leave a SwitchBot air purifier without a preset it does not report (#180560)

This commit is contained in:
Franck Nijhof
2026-08-29 08:20:39 +00:00
parent 813af6d07d
commit 1c983cd5e3
2 changed files with 31 additions and 3 deletions
@@ -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
@@ -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"),
[