humidifier.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """
  2. Setup for different kinds of Tuya humidifier devices
  3. """
  4. import logging
  5. from homeassistant.components.humidifier import (
  6. HumidifierAction,
  7. HumidifierDeviceClass,
  8. HumidifierEntity,
  9. HumidifierEntityFeature,
  10. )
  11. from homeassistant.components.humidifier.const import (
  12. DEFAULT_MAX_HUMIDITY,
  13. DEFAULT_MIN_HUMIDITY,
  14. )
  15. from .device import TuyaLocalDevice
  16. from .entity import TuyaLocalEntity
  17. from .helpers.config import async_tuya_setup_platform
  18. from .helpers.device_config import TuyaEntityConfig
  19. _LOGGER = logging.getLogger(__name__)
  20. async def async_setup_entry(hass, config_entry, async_add_entities):
  21. config = {**config_entry.data, **config_entry.options}
  22. await async_tuya_setup_platform(
  23. hass,
  24. async_add_entities,
  25. config,
  26. "humidifier",
  27. TuyaLocalHumidifier,
  28. )
  29. class TuyaLocalHumidifier(TuyaLocalEntity, HumidifierEntity):
  30. """Representation of a Tuya Humidifier entity."""
  31. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  32. """
  33. Initialise the humidifier device.
  34. Args:
  35. device (TuyaLocalDevice): The device API instance.
  36. config (TuyaEntityConfig): The entity config.
  37. """
  38. super().__init__()
  39. dps_map = self._init_begin(device, config)
  40. self._current_humidity_dp = dps_map.pop("current_humidity", None)
  41. self._humidity_dp = dps_map.pop("humidity", None)
  42. self._mode_dp = dps_map.pop("mode", None)
  43. self._switch_dp = dps_map.pop("switch", None)
  44. self._action_dp = dps_map.pop("action", None)
  45. self._init_end(dps_map)
  46. self._support_flags = HumidifierEntityFeature(0)
  47. if self._mode_dp:
  48. self._support_flags |= HumidifierEntityFeature.MODES
  49. @property
  50. def supported_features(self):
  51. """Return the features supported by this climate device."""
  52. return self._support_flags
  53. @property
  54. def device_class(self):
  55. """Return the class of this device"""
  56. return (
  57. HumidifierDeviceClass.DEHUMIDIFIER
  58. if self._config.device_class == "dehumidifier"
  59. else HumidifierDeviceClass.HUMIDIFIER
  60. )
  61. @property
  62. def is_on(self):
  63. """Return whether the switch is on or not."""
  64. # If there is no switch, it is always on if available
  65. if self._switch_dp is None:
  66. return self.available
  67. return self._switch_dp.get_value(self._device)
  68. @property
  69. def action(self):
  70. """Return the current action."""
  71. if self._action_dp:
  72. if not self.is_on:
  73. return HumidifierAction.OFF
  74. action = self._action_dp.get_value(self._device)
  75. try:
  76. return HumidifierAction(action) if action else None
  77. except ValueError:
  78. _LOGGER.warning(
  79. "%s/%s: Unrecognised action %s ignored",
  80. self._config._device.config,
  81. self.name or "humidifier",
  82. action,
  83. )
  84. async def async_turn_on(self, **kwargs):
  85. """Turn the switch on"""
  86. _LOGGER.info("%s turning on", self._config.config_id)
  87. await self._switch_dp.async_set_value(self._device, True)
  88. async def async_turn_off(self, **kwargs):
  89. """Turn the switch off"""
  90. _LOGGER.info("%s turning off", self._config.config_id)
  91. await self._switch_dp.async_set_value(self._device, False)
  92. @property
  93. def current_humidity(self):
  94. """Return the current humidity if available."""
  95. if self._current_humidity_dp:
  96. return self._current_humidity_dp.get_value(self._device)
  97. @property
  98. def target_humidity(self):
  99. """Return the currently set target humidity."""
  100. if self._humidity_dp is None:
  101. raise NotImplementedError()
  102. return self._humidity_dp.get_value(self._device)
  103. @property
  104. def min_humidity(self):
  105. """Return the minimum supported target humidity."""
  106. if self._humidity_dp is None:
  107. return None
  108. r = self._humidity_dp.range(self._device)
  109. return DEFAULT_MIN_HUMIDITY if r is None else r[0]
  110. @property
  111. def max_humidity(self):
  112. """Return the maximum supported target humidity."""
  113. if self._humidity_dp is None:
  114. return None
  115. r = self._humidity_dp.range(self._device)
  116. return DEFAULT_MAX_HUMIDITY if r is None else r[1]
  117. async def async_set_humidity(self, humidity):
  118. if self._humidity_dp is None:
  119. raise NotImplementedError()
  120. _LOGGER.info("%s setting humidity to %s", self._config.config_id, humidity)
  121. await self._humidity_dp.async_set_value(self._device, humidity)
  122. @property
  123. def mode(self):
  124. """Return the current preset mode."""
  125. if self._mode_dp is None:
  126. raise NotImplementedError()
  127. return self._mode_dp.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_dp:
  132. return self._mode_dp.values(self._device)
  133. async def async_set_mode(self, mode):
  134. """Set the preset mode."""
  135. if self._mode_dp is None:
  136. raise NotImplementedError()
  137. _LOGGER.info("%s setting mode to %s", self._config.config_id, mode)
  138. await self._mode_dp.async_set_value(self._device, mode)