humidifier.py 4.4 KB

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