humidifier.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. await self._switch_dp.async_set_value(self._device, True)
  87. async def async_turn_off(self, **kwargs):
  88. """Turn the switch off"""
  89. await self._switch_dp.async_set_value(self._device, False)
  90. @property
  91. def current_humidity(self):
  92. """Return the current humidity if available."""
  93. if self._current_humidity_dp:
  94. return self._current_humidity_dp.get_value(self._device)
  95. @property
  96. def target_humidity(self):
  97. """Return the currently set target humidity."""
  98. if self._humidity_dp is None:
  99. raise NotImplementedError()
  100. return self._humidity_dp.get_value(self._device)
  101. @property
  102. def min_humidity(self):
  103. """Return the minimum supported target humidity."""
  104. if self._humidity_dp is None:
  105. return None
  106. r = self._humidity_dp.range(self._device)
  107. return DEFAULT_MIN_HUMIDITY if r is None else r[0]
  108. @property
  109. def max_humidity(self):
  110. """Return the maximum supported target humidity."""
  111. if self._humidity_dp is None:
  112. return None
  113. r = self._humidity_dp.range(self._device)
  114. return DEFAULT_MAX_HUMIDITY if r is None else r[1]
  115. async def async_set_humidity(self, humidity):
  116. if self._humidity_dp is None:
  117. raise NotImplementedError()
  118. await self._humidity_dp.async_set_value(self._device, humidity)
  119. @property
  120. def mode(self):
  121. """Return the current preset mode."""
  122. if self._mode_dp is None:
  123. raise NotImplementedError()
  124. return self._mode_dp.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_dp:
  129. return self._mode_dp.values(self._device)
  130. async def async_set_mode(self, mode):
  131. """Set the preset mode."""
  132. if self._mode_dp is None:
  133. raise NotImplementedError()
  134. await self._mode_dp.async_set_value(self._device, mode)