|
| 1 | +"""Support for Switchbot humidifier.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from switchbot_api import CommonCommands, HumidifierCommands, HumidifierV2Commands |
| 7 | + |
| 8 | +from homeassistant.components.humidifier import ( |
| 9 | + MODE_AUTO, |
| 10 | + MODE_NORMAL, |
| 11 | + HumidifierDeviceClass, |
| 12 | + HumidifierEntity, |
| 13 | + HumidifierEntityFeature, |
| 14 | +) |
| 15 | +from homeassistant.config_entries import ConfigEntry |
| 16 | +from homeassistant.const import STATE_ON |
| 17 | +from homeassistant.core import HomeAssistant |
| 18 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 19 | + |
| 20 | +from . import SwitchbotCloudData |
| 21 | +from .const import AFTER_COMMAND_REFRESH, DOMAIN, HUMIDITY_LEVELS, Humidifier2Mode |
| 22 | +from .entity import SwitchBotCloudEntity |
| 23 | + |
| 24 | +PARALLEL_UPDATES = 0 |
| 25 | + |
| 26 | + |
| 27 | +async def async_setup_entry( |
| 28 | + hass: HomeAssistant, |
| 29 | + entry: ConfigEntry, |
| 30 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 31 | +) -> None: |
| 32 | + """Set up Switchbot based on a config entry.""" |
| 33 | + data: SwitchbotCloudData = hass.data[DOMAIN][entry.entry_id] |
| 34 | + async_add_entities( |
| 35 | + SwitchBotHumidifier(data.api, device, coordinator) |
| 36 | + if device.device_type == "Humidifier" |
| 37 | + else SwitchBotEvaporativeHumidifier(data.api, device, coordinator) |
| 38 | + for device, coordinator in data.devices.humidifiers |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +class SwitchBotHumidifier(SwitchBotCloudEntity, HumidifierEntity): |
| 43 | + """Representation of a Switchbot humidifier.""" |
| 44 | + |
| 45 | + _attr_supported_features = HumidifierEntityFeature.MODES |
| 46 | + _attr_device_class = HumidifierDeviceClass.HUMIDIFIER |
| 47 | + _attr_available_modes = [MODE_NORMAL, MODE_AUTO] |
| 48 | + _attr_min_humidity = 1 |
| 49 | + _attr_translation_key = "humidifier" |
| 50 | + _attr_name = None |
| 51 | + _attr_target_humidity = 50 |
| 52 | + |
| 53 | + def _set_attributes(self) -> None: |
| 54 | + """Set attributes from coordinator data.""" |
| 55 | + if coord_data := self.coordinator.data: |
| 56 | + self._attr_is_on = coord_data.get("power") == STATE_ON |
| 57 | + self._attr_mode = MODE_AUTO if coord_data.get("auto") else MODE_NORMAL |
| 58 | + self._attr_current_humidity = coord_data.get("humidity") |
| 59 | + |
| 60 | + async def async_set_humidity(self, humidity: int) -> None: |
| 61 | + """Set new target humidity.""" |
| 62 | + self.target_humidity, parameters = self._map_humidity_to_supported_level( |
| 63 | + humidity |
| 64 | + ) |
| 65 | + await self.send_api_command( |
| 66 | + HumidifierCommands.SET_MODE, parameters=str(parameters) |
| 67 | + ) |
| 68 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 69 | + await self.coordinator.async_request_refresh() |
| 70 | + |
| 71 | + async def async_set_mode(self, mode: str) -> None: |
| 72 | + """Set new target humidity.""" |
| 73 | + if mode == MODE_AUTO: |
| 74 | + await self.send_api_command(HumidifierCommands.SET_MODE, parameters=mode) |
| 75 | + else: |
| 76 | + await self.send_api_command( |
| 77 | + HumidifierCommands.SET_MODE, parameters=str(102) |
| 78 | + ) |
| 79 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 80 | + await self.coordinator.async_request_refresh() |
| 81 | + |
| 82 | + async def async_turn_on(self, **kwargs: Any) -> None: |
| 83 | + """Turn the device on.""" |
| 84 | + await self.send_api_command(CommonCommands.ON) |
| 85 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 86 | + await self.coordinator.async_request_refresh() |
| 87 | + |
| 88 | + async def async_turn_off(self, **kwargs: Any) -> None: |
| 89 | + """Turn the device off.""" |
| 90 | + await self.send_api_command(CommonCommands.OFF) |
| 91 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 92 | + await self.coordinator.async_request_refresh() |
| 93 | + |
| 94 | + def _map_humidity_to_supported_level(self, humidity: int) -> tuple[int, int]: |
| 95 | + """Map any humidity to the closest supported level and its parameter.""" |
| 96 | + if humidity <= 34: |
| 97 | + return 34, HUMIDITY_LEVELS[34] |
| 98 | + if humidity <= 67: |
| 99 | + return 67, HUMIDITY_LEVELS[67] |
| 100 | + return 100, HUMIDITY_LEVELS[100] |
| 101 | + |
| 102 | + |
| 103 | +class SwitchBotEvaporativeHumidifier(SwitchBotCloudEntity, HumidifierEntity): |
| 104 | + """Representation of a Switchbot humidifier v2.""" |
| 105 | + |
| 106 | + _attr_supported_features = HumidifierEntityFeature.MODES |
| 107 | + _attr_device_class = HumidifierDeviceClass.HUMIDIFIER |
| 108 | + _attr_available_modes = Humidifier2Mode.get_modes() |
| 109 | + _attr_translation_key = "evaporative_humidifier" |
| 110 | + _attr_name = None |
| 111 | + _attr_target_humidity = 50 |
| 112 | + |
| 113 | + def _set_attributes(self) -> None: |
| 114 | + """Set attributes from coordinator data.""" |
| 115 | + if coord_data := self.coordinator.data: |
| 116 | + self._attr_is_on = coord_data.get("power") == STATE_ON |
| 117 | + self._attr_mode = ( |
| 118 | + Humidifier2Mode(coord_data.get("mode")).name.lower() |
| 119 | + if coord_data.get("mode") is not None |
| 120 | + else None |
| 121 | + ) |
| 122 | + self._attr_current_humidity = ( |
| 123 | + coord_data.get("humidity") |
| 124 | + if coord_data.get("humidity") != 127 |
| 125 | + else None |
| 126 | + ) |
| 127 | + |
| 128 | + async def async_set_humidity(self, humidity: int) -> None: |
| 129 | + """Set new target humidity.""" |
| 130 | + assert self.coordinator.data is not None |
| 131 | + self._attr_target_humidity = humidity |
| 132 | + params = {"mode": self.coordinator.data["mode"], "humidity": humidity} |
| 133 | + await self.send_api_command(HumidifierV2Commands.SET_MODE, parameters=params) |
| 134 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 135 | + await self.coordinator.async_request_refresh() |
| 136 | + |
| 137 | + async def async_set_mode(self, mode: str) -> None: |
| 138 | + """Set new target mode.""" |
| 139 | + assert self.coordinator.data is not None |
| 140 | + params = {"mode": Humidifier2Mode[mode.upper()].value} |
| 141 | + await self.send_api_command(HumidifierV2Commands.SET_MODE, parameters=params) |
| 142 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 143 | + await self.coordinator.async_request_refresh() |
| 144 | + |
| 145 | + async def async_turn_on(self, **kwargs: Any) -> None: |
| 146 | + """Turn the device on.""" |
| 147 | + await self.send_api_command(CommonCommands.ON) |
| 148 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 149 | + await self.coordinator.async_request_refresh() |
| 150 | + |
| 151 | + async def async_turn_off(self, **kwargs: Any) -> None: |
| 152 | + """Turn the device off.""" |
| 153 | + await self.send_api_command(CommonCommands.OFF) |
| 154 | + await asyncio.sleep(AFTER_COMMAND_REFRESH) |
| 155 | + await self.coordinator.async_request_refresh() |
0 commit comments