humidifier.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if self._humidity_dp is not None:
  47. self._attr_target_humidity_step = self._humidity_dp.step(device)
  48. self._support_flags = HumidifierEntityFeature(0)
  49. if self._mode_dp:
  50. self._support_flags |= HumidifierEntityFeature.MODES
  51. @property
  52. def supported_features(self):
  53. """Return the features supported by this climate device."""
  54. return self._support_flags
  55. @property
  56. def device_class(self):
  57. """Return the class of this device"""
  58. return (
  59. HumidifierDeviceClass.DEHUMIDIFIER
  60. if self._config.device_class == "dehumidifier"
  61. else HumidifierDeviceClass.HUMIDIFIER
  62. )
  63. @property
  64. def is_on(self):
  65. """Return whether the switch is on or not."""
  66. # If there is no switch, it is always on if available
  67. if self._switch_dp is None:
  68. return self.available
  69. return self._switch_dp.get_value(self._device)
  70. @property
  71. def action(self):
  72. """Return the current action."""
  73. if self._action_dp:
  74. if not self.is_on:
  75. return HumidifierAction.OFF
  76. action = self._action_dp.get_value(self._device)
  77. try:
  78. return HumidifierAction(action) if action else None
  79. except ValueError:
  80. _LOGGER.warning(
  81. "%s/%s: Unrecognised action %s ignored",
  82. self._config._device.config,
  83. self.name or "humidifier",
  84. action,
  85. )
  86. async def async_turn_on(self, **kwargs):
  87. """Turn the switch on"""
  88. _LOGGER.info("%s turning on", self._config.config_id)
  89. await self._switch_dp.async_set_value(self._device, True)
  90. async def async_turn_off(self, **kwargs):
  91. """Turn the switch off"""
  92. _LOGGER.info("%s turning off", self._config.config_id)
  93. await self._switch_dp.async_set_value(self._device, False)
  94. @property
  95. def current_humidity(self):
  96. """Return the current humidity if available."""
  97. if self._current_humidity_dp:
  98. return self._current_humidity_dp.get_value(self._device)
  99. @property
  100. def target_humidity(self):
  101. """Return the currently set target humidity."""
  102. if self._humidity_dp is None:
  103. raise NotImplementedError()
  104. return self._humidity_dp.get_value(self._device)
  105. @property
  106. def min_humidity(self):
  107. """Return the minimum supported target humidity."""
  108. if self._humidity_dp is None:
  109. return None
  110. r = self._humidity_dp.range(self._device)
  111. return DEFAULT_MIN_HUMIDITY if r is None else r[0]
  112. @property
  113. def max_humidity(self):
  114. """Return the maximum supported target humidity."""
  115. if self._humidity_dp is None:
  116. return None
  117. r = self._humidity_dp.range(self._device)
  118. return DEFAULT_MAX_HUMIDITY if r is None else r[1]
  119. async def async_set_humidity(self, humidity):
  120. if self._humidity_dp is None:
  121. raise NotImplementedError()
  122. _LOGGER.info("%s setting humidity to %s", self._config.config_id, humidity)
  123. await self._humidity_dp.async_set_value(self._device, humidity)
  124. @property
  125. def mode(self):
  126. """Return the current preset mode."""
  127. if self._mode_dp is None:
  128. raise NotImplementedError()
  129. return self._mode_dp.get_value(self._device)
  130. @property
  131. def available_modes(self):
  132. """Return the list of presets that this device supports."""
  133. if self._mode_dp:
  134. return self._mode_dp.values(self._device)
  135. async def async_set_mode(self, mode):
  136. """Set the preset mode."""
  137. if self._mode_dp is None:
  138. raise NotImplementedError()
  139. _LOGGER.info("%s setting mode to %s", self._config.config_id, mode)
  140. await self._mode_dp.async_set_value(self._device, mode)