humidifier.py 5.4 KB

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