humidifier.py 5.2 KB

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