humidifier.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 homeassistant.const import (
  14. STATE_UNAVAILABLE,
  15. )
  16. from ..device import TuyaLocalDevice
  17. from ..helpers.device_config import TuyaEntityConfig
  18. _LOGGER = logging.getLogger(__name__)
  19. class TuyaLocalHumidifier(HumidifierEntity):
  20. """Representation of a Tuya Humidifier entity."""
  21. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  22. """
  23. Initialise the humidifier device.
  24. Args:
  25. device (TuyaLocalDevice): The device API instance.
  26. config (TuyaEntityConfig): The entity config.
  27. """
  28. self._device = device
  29. self._config = config
  30. self._support_flags = 0
  31. self._attr_dps = []
  32. dps_map = {c.name: c for c in config.dps()}
  33. self._humidity_dps = dps_map.pop("humidity", None)
  34. self._mode_dps = dps_map.pop("mode", None)
  35. self._switch_dps = dps_map.pop("switch", None)
  36. for d in dps_map.values():
  37. if not d.hidden:
  38. self._attr_dps.append(d)
  39. if self._mode_dps:
  40. self._support_flags |= SUPPORT_MODES
  41. @property
  42. def supported_features(self):
  43. """Return the features supported by this climate device."""
  44. return self._support_flags
  45. @property
  46. def should_poll(self):
  47. """Return the polling state."""
  48. return True
  49. @property
  50. def name(self):
  51. """Return the name of the climate device."""
  52. return self._device.name
  53. @property
  54. def friendly_name(self):
  55. """Return the friendly name of the climate entity for the UI."""
  56. return self._config.name
  57. @property
  58. def unique_id(self):
  59. """Return the unique id for this climate device."""
  60. return self._device.unique_id
  61. @property
  62. def device_info(self):
  63. """Return device information about this heater."""
  64. return self._device.device_info
  65. @property
  66. def device_class(self):
  67. """Return the class of this device"""
  68. return (
  69. DEVICE_CLASS_DEHUMIDIFIER
  70. if self._config.device_class == "dehumidifier"
  71. else DEVICE_CLASS_HUMIDIFIER
  72. )
  73. @property
  74. def icon(self):
  75. """Return the icon to use in the frontend for this device."""
  76. icon = self._config.icon(self._device)
  77. if icon:
  78. return icon
  79. else:
  80. return super().icon
  81. @property
  82. def is_on(self):
  83. """Return whether the switch is on or not."""
  84. is_switched_on = self._switch_dps.get_value(self._device)
  85. if is_switched_on is None:
  86. return STATE_UNAVAILABLE
  87. else:
  88. return is_switched_on
  89. async def async_turn_on(self, **kwargs):
  90. """Turn the switch on"""
  91. await self._switch_dps.async_set_value(self._device, True)
  92. async def async_turn_off(self, **kwargs):
  93. """Turn the switch off"""
  94. await self._switch_dps.async_set_value(self._device, False)
  95. @property
  96. def target_humidity(self):
  97. """Return the currently set target humidity."""
  98. if self._humidity_dps is None:
  99. raise NotImplementedError()
  100. return self._humidity_dps.get_value(self._device)
  101. @property
  102. def min_humidity(self):
  103. """Return the minimum supported target humidity."""
  104. if self._humidity_dps is None:
  105. return None
  106. r = self._humidity_dps.range(self._device)
  107. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  108. @property
  109. def max_humidity(self):
  110. """Return the maximum supported target humidity."""
  111. if self._humidity_dps is None:
  112. return None
  113. r = self._humidity_dps.range(self._device)
  114. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  115. async def async_set_humidity(self, humidity):
  116. if self._humidity_dps is None:
  117. raise NotImplementedError()
  118. await self._humidity_dps.async_set_value(self._device, humidity)
  119. @property
  120. def mode(self):
  121. """Return the current preset mode."""
  122. if self._mode_dps is None:
  123. raise NotImplementedError()
  124. return self._mode_dps.get_value(self._device)
  125. @property
  126. def available_modes(self):
  127. """Return the list of presets that this device supports."""
  128. if self._mode_dps is None:
  129. return None
  130. return self._mode_dps.values(self._device)
  131. async def async_set_mode(self, mode):
  132. """Set the preset mode."""
  133. if self._mode_dps is None:
  134. raise NotImplementedError()
  135. await self._mode_dps.async_set_value(self._device, mode)
  136. @property
  137. def device_state_attributes(self):
  138. """Get additional attributes that the integration itself does not support."""
  139. attr = {}
  140. for a in self._attr_dps:
  141. attr[a.name] = a.get_value(self._device)
  142. return attr
  143. async def async_update(self):
  144. await self._device.async_refresh()