humidifier.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. Platform to control tuya humidifier and dehumidifier devices.
  3. """
  4. import logging
  5. from homeassistant.components.humidifier import HumidifierEntity
  6. from homeassistant.components.humidifier.const import (
  7. DEFAULT_MAX_HUMIDITY,
  8. DEFAULT_MIN_HUMIDITY,
  9. DEVICE_CLASS_DEHUMIDIFIER,
  10. DEVICE_CLASS_HUMIDIFIER,
  11. SUPPORT_MODES,
  12. )
  13. from ..device import TuyaLocalDevice
  14. from ..helpers.device_config import TuyaEntityConfig
  15. from ..helpers.mixin import TuyaLocalEntity
  16. _LOGGER = logging.getLogger(__name__)
  17. class TuyaLocalHumidifier(TuyaLocalEntity, HumidifierEntity):
  18. """Representation of a Tuya Humidifier entity."""
  19. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  20. """
  21. Initialise the humidifier device.
  22. Args:
  23. device (TuyaLocalDevice): The device API instance.
  24. config (TuyaEntityConfig): The entity config.
  25. """
  26. dps_map = self._init_begin(device, config)
  27. self._humidity_dps = dps_map.pop("humidity", None)
  28. self._mode_dps = dps_map.pop("mode", None)
  29. self._switch_dps = dps_map.pop("switch", None)
  30. self._init_end(dps_map)
  31. self._support_flags = 0
  32. if self._mode_dps:
  33. self._support_flags |= SUPPORT_MODES
  34. @property
  35. def supported_features(self):
  36. """Return the features supported by this climate device."""
  37. return self._support_flags
  38. @property
  39. def device_class(self):
  40. """Return the class of this device"""
  41. return (
  42. DEVICE_CLASS_DEHUMIDIFIER
  43. if self._config.device_class == "dehumidifier"
  44. else DEVICE_CLASS_HUMIDIFIER
  45. )
  46. @property
  47. def is_on(self):
  48. """Return whether the switch is on or not."""
  49. # If there is no switch, it is always on if available
  50. if self._switch_dps is None:
  51. return self.available
  52. return self._switch_dps.get_value(self._device)
  53. async def async_turn_on(self, **kwargs):
  54. """Turn the switch on"""
  55. await self._switch_dps.async_set_value(self._device, True)
  56. async def async_turn_off(self, **kwargs):
  57. """Turn the switch off"""
  58. await self._switch_dps.async_set_value(self._device, False)
  59. @property
  60. def target_humidity(self):
  61. """Return the currently set target humidity."""
  62. if self._humidity_dps is None:
  63. raise NotImplementedError()
  64. return self._humidity_dps.get_value(self._device)
  65. @property
  66. def min_humidity(self):
  67. """Return the minimum supported target humidity."""
  68. if self._humidity_dps is None:
  69. return None
  70. r = self._humidity_dps.range(self._device)
  71. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  72. @property
  73. def max_humidity(self):
  74. """Return the maximum supported target humidity."""
  75. if self._humidity_dps is None:
  76. return None
  77. r = self._humidity_dps.range(self._device)
  78. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  79. async def async_set_humidity(self, humidity):
  80. if self._humidity_dps is None:
  81. raise NotImplementedError()
  82. await self._humidity_dps.async_set_value(self._device, humidity)
  83. @property
  84. def mode(self):
  85. """Return the current preset mode."""
  86. if self._mode_dps is None:
  87. raise NotImplementedError()
  88. return self._mode_dps.get_value(self._device)
  89. @property
  90. def available_modes(self):
  91. """Return the list of presets that this device supports."""
  92. if self._mode_dps is None:
  93. return None
  94. return self._mode_dps.values(self._device)
  95. async def async_set_mode(self, mode):
  96. """Set the preset mode."""
  97. if self._mode_dps is None:
  98. raise NotImplementedError()
  99. await self._mode_dps.async_set_value(self._device, mode)