humidifier.py 3.9 KB

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