1 """Support for Xiaomi Mi Air Purifier and Xiaomi Mi Air Humidifier with humidifier entity."""
7 from miio.integrations.humidifier.deerma.airhumidifier_mjjsq
import (
8 OperationMode
as AirhumidifierMjjsqOperationMode,
10 from miio.integrations.humidifier.zhimi.airhumidifier
import (
11 OperationMode
as AirhumidifierOperationMode,
13 from miio.integrations.humidifier.zhimi.airhumidifier_miot
import (
14 OperationMode
as AirhumidifierMiotOperationMode,
19 HumidifierDeviceClass,
21 HumidifierEntityFeature,
34 MODEL_AIRHUMIDIFIER_CA1,
35 MODEL_AIRHUMIDIFIER_CA4,
36 MODEL_AIRHUMIDIFIER_CB1,
37 MODELS_HUMIDIFIER_MIOT,
38 MODELS_HUMIDIFIER_MJJSQ,
40 from .entity
import XiaomiCoordinatedMiioEntity
42 _LOGGER = logging.getLogger(__name__)
45 ATTR_TARGET_HUMIDITY =
"target_humidity"
47 AVAILABLE_ATTRIBUTES = {
49 ATTR_TARGET_HUMIDITY:
"target_humidity",
50 ATTR_HUMIDITY:
"humidity",
53 AVAILABLE_MODES_CA1_CB1 = [
55 for mode
in AirhumidifierOperationMode
56 if mode
is not AirhumidifierOperationMode.Strong
58 AVAILABLE_MODES_CA4 = [mode.name
for mode
in AirhumidifierMiotOperationMode]
59 AVAILABLE_MODES_MJJSQ = [
61 for mode
in AirhumidifierMjjsqOperationMode
62 if mode
is not AirhumidifierMjjsqOperationMode.WetAndProtect
64 AVAILABLE_MODES_OTHER = [
66 for mode
in AirhumidifierOperationMode
67 if mode
is not AirhumidifierOperationMode.Auto
73 config_entry: ConfigEntry,
74 async_add_entities: AddEntitiesCallback,
76 """Set up the Humidifier from a config entry."""
77 if config_entry.data[CONF_FLOW_TYPE] != CONF_DEVICE:
80 entities: list[HumidifierEntity] = []
81 entity: HumidifierEntity
82 model = config_entry.data[CONF_MODEL]
83 unique_id = config_entry.unique_id
84 coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
86 if model
in MODELS_HUMIDIFIER_MIOT:
87 air_humidifier = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE]
94 elif model
in MODELS_HUMIDIFIER_MJJSQ:
95 air_humidifier = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE]
103 air_humidifier = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE]
111 entities.append(entity)
117 """Representation of a generic Xiaomi humidifier device."""
119 _attr_device_class = HumidifierDeviceClass.HUMIDIFIER
120 _attr_supported_features = HumidifierEntityFeature.MODES
123 def __init__(self, device, entry, unique_id, coordinator):
124 """Initialize the generic Xiaomi device."""
125 super().
__init__(device, entry, unique_id, coordinator=coordinator)
135 """Return true if device is on."""
140 """Get the current mode."""
141 return self.
_mode_mode
144 """Turn the device on."""
145 result = await self._try_command(
146 "Turning the miio device on failed.", self._device.on
153 """Turn the device off."""
154 result = await self._try_command(
155 "Turning the miio device off failed.", self._device.off
163 """Translate the target humidity to the first valid step."""
168 if 0 < humidity <= 100
174 """Representation of a Xiaomi Air Humidifier."""
176 available_modes: list[str]
178 def __init__(self, device, entry, unique_id, coordinator):
179 """Initialize the plug switch."""
180 super().
__init__(device, entry, unique_id, coordinator)
184 if self._model
in [MODEL_AIRHUMIDIFIER_CA1, MODEL_AIRHUMIDIFIER_CB1]:
187 elif self._model
in [MODEL_AIRHUMIDIFIER_CA4]:
190 elif self._model
in MODELS_HUMIDIFIER_MJJSQ:
200 key: self._extract_value_from_attribute(self.coordinator.data, value)
201 for key, value
in AVAILABLE_ATTRIBUTES.items()
210 """Return true if device is on."""
215 """Fetch state from the device."""
219 key: self._extract_value_from_attribute(self.coordinator.data, value)
220 for key, value
in AVAILABLE_ATTRIBUTES.items()
230 """Return the current mode."""
231 return AirhumidifierOperationMode(self.
_mode_mode_mode).name
235 """Return the target humidity."""
238 if self.
_mode_mode_mode == AirhumidifierOperationMode.Auto.value
239 or AirhumidifierOperationMode.Auto.name
not in self.
available_modesavailable_modes
244 """Set the target humidity of the humidifier and set the mode to auto."""
246 if not target_humidity:
249 _LOGGER.debug(
"Setting the target humidity to: %s", target_humidity)
250 if await self._try_command(
251 "Setting target humidity of the miio device failed.",
252 self._device.set_target_humidity,
258 or AirhumidifierOperationMode(self.
_attributes_attributes[ATTR_MODE])
259 == AirhumidifierOperationMode.Auto
260 or AirhumidifierOperationMode.Auto.name
not in self.
available_modesavailable_modes
264 _LOGGER.debug(
"Setting the operation mode to: Auto")
265 if await self._try_command(
266 "Setting operation mode of the miio device to MODE_AUTO failed.",
267 self._device.set_mode,
268 AirhumidifierOperationMode.Auto,
270 self.
_mode_mode_mode = AirhumidifierOperationMode.Auto.value
274 """Set the mode of the humidifier."""
279 _LOGGER.warning(
"Mode %s is not a valid operation mode", mode)
282 _LOGGER.debug(
"Setting the operation mode to: %s", mode)
283 if await self._try_command(
284 "Setting operation mode of the miio device failed.",
285 self._device.set_mode,
286 AirhumidifierOperationMode[mode],
293 """Representation of a Xiaomi Air Humidifier (MiOT protocol)."""
296 AirhumidifierMiotOperationMode.Auto:
"Auto",
297 AirhumidifierMiotOperationMode.Low:
"Low",
298 AirhumidifierMiotOperationMode.Mid:
"Mid",
299 AirhumidifierMiotOperationMode.High:
"High",
302 REVERSE_MODE_MAPPING = {v: k
for k, v
in MODE_MAPPING.items()}
306 """Return the current mode."""
311 """Return the target humidity."""
316 == AirhumidifierMiotOperationMode.Auto
322 """Set the target humidity of the humidifier and set the mode to auto."""
324 if not target_humidity:
327 _LOGGER.debug(
"Setting the humidity to: %s", target_humidity)
328 if await self._try_command(
329 "Setting operation mode of the miio device failed.",
330 self._device.set_target_humidity,
336 or AirhumidifierMiotOperationMode(self.
_attributes_attributes[ATTR_MODE])
337 == AirhumidifierMiotOperationMode.Auto
341 _LOGGER.debug(
"Setting the operation mode to: Auto")
342 if await self._try_command(
343 "Setting operation mode of the miio device to MODE_AUTO failed.",
344 self._device.set_mode,
345 AirhumidifierMiotOperationMode.Auto,
351 """Set the mode of the fan."""
356 _LOGGER.warning(
"Mode %s is not a valid operation mode", mode)
359 _LOGGER.debug(
"Setting the operation mode to: %s", mode)
361 if await self._try_command(
362 "Setting operation mode of the miio device failed.",
363 self._device.set_mode,
371 """Representation of a Xiaomi Air MJJSQ Humidifier."""
374 "Low": AirhumidifierMjjsqOperationMode.Low,
375 "Medium": AirhumidifierMjjsqOperationMode.Medium,
376 "High": AirhumidifierMjjsqOperationMode.High,
377 "Humidity": AirhumidifierMjjsqOperationMode.Humidity,
382 """Return the current mode."""
387 """Return the target humidity."""
391 == AirhumidifierMjjsqOperationMode.Humidity
397 """Set the target humidity of the humidifier and set the mode to Humidity."""
399 if not target_humidity:
402 _LOGGER.debug(
"Setting the humidity to: %s", target_humidity)
403 if await self._try_command(
404 "Setting operation mode of the miio device failed.",
405 self._device.set_target_humidity,
411 or AirhumidifierMjjsqOperationMode(self.
_attributes_attributes[ATTR_MODE])
412 == AirhumidifierMjjsqOperationMode.Humidity
416 _LOGGER.debug(
"Setting the operation mode to: Humidity")
417 if await self._try_command(
418 "Setting operation mode of the miio device to MODE_HUMIDITY failed.",
419 self._device.set_mode,
420 AirhumidifierMjjsqOperationMode.Humidity,
426 """Set the mode of the fan."""
428 _LOGGER.warning(
"Mode %s is not a valid operation mode", mode)
431 _LOGGER.debug(
"Setting the operation mode to: %s", mode)
433 if await self._try_command(
434 "Setting operation mode of the miio device failed.",
435 self._device.set_mode,
list[str]|None available_modes(self)
HumidifierEntityFeature supported_features(self)
None async_set_mode(self, str mode)
def target_humidity(self)
dictionary REVERSE_MODE_MAPPING
None async_set_humidity(self, float humidity)
def target_humidity(self)
None async_set_mode(self, str mode)
None async_set_humidity(self, float humidity)
None async_set_mode(self, str mode)
def target_humidity(self)
def __init__(self, device, entry, unique_id, coordinator)
def _handle_coordinator_update(self)
None async_set_humidity(self, float humidity)
None async_turn_off(self, **Any kwargs)
float|None translate_humidity(self, float humidity)
None async_turn_on(self, **Any kwargs)
def __init__(self, device, entry, unique_id, coordinator)
None async_write_ha_state(self)
int|None supported_features(self)
IssData update(pyiss.ISS iss)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
float percentage_to_ranged_value(tuple[float, float] low_high_range, float percentage)