humidifier.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. Setup for different kinds of Tuya humidifier devices
  3. """
  4. from homeassistant.components.humidifier import (
  5. HumidifierDeviceClass,
  6. HumidifierEntity,
  7. HumidifierEntityFeature,
  8. )
  9. from homeassistant.components.humidifier.const import (
  10. DEFAULT_MAX_HUMIDITY,
  11. DEFAULT_MIN_HUMIDITY,
  12. )
  13. import logging
  14. from .device import TuyaLocalDevice
  15. from .helpers.device_config import TuyaEntityConfig
  16. from .helpers.mixin import TuyaLocalEntity
  17. from .helpers.config import async_tuya_setup_platform
  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._humidity_dps = dps_map.pop("humidity", None)
  40. self._mode_dps = dps_map.pop("mode", None)
  41. self._switch_dps = dps_map.pop("switch", None)
  42. self._init_end(dps_map)
  43. self._support_flags = 0
  44. if self._mode_dps:
  45. self._support_flags |= HumidifierEntityFeature.MODES
  46. @property
  47. def supported_features(self):
  48. """Return the features supported by this climate device."""
  49. return self._support_flags
  50. @property
  51. def device_class(self):
  52. """Return the class of this device"""
  53. return (
  54. HumidifierDeviceClass.DEHUMIDIFIER
  55. if self._config.device_class == "dehumidifier"
  56. else HumidifierDeviceClass.HUMIDIFIER
  57. )
  58. @property
  59. def is_on(self):
  60. """Return whether the switch is on or not."""
  61. # If there is no switch, it is always on if available
  62. if self._switch_dps is None:
  63. return self.available
  64. return self._switch_dps.get_value(self._device)
  65. async def async_turn_on(self, **kwargs):
  66. """Turn the switch on"""
  67. await self._switch_dps.async_set_value(self._device, True)
  68. async def async_turn_off(self, **kwargs):
  69. """Turn the switch off"""
  70. await self._switch_dps.async_set_value(self._device, False)
  71. @property
  72. def target_humidity(self):
  73. """Return the currently set target humidity."""
  74. if self._humidity_dps is None:
  75. raise NotImplementedError()
  76. return self._humidity_dps.get_value(self._device)
  77. @property
  78. def min_humidity(self):
  79. """Return the minimum supported target humidity."""
  80. if self._humidity_dps is None:
  81. return None
  82. r = self._humidity_dps.range(self._device)
  83. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  84. @property
  85. def max_humidity(self):
  86. """Return the maximum supported target humidity."""
  87. if self._humidity_dps is None:
  88. return None
  89. r = self._humidity_dps.range(self._device)
  90. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  91. async def async_set_humidity(self, humidity):
  92. if self._humidity_dps is None:
  93. raise NotImplementedError()
  94. await self._humidity_dps.async_set_value(self._device, humidity)
  95. @property
  96. def mode(self):
  97. """Return the current preset mode."""
  98. if self._mode_dps is None:
  99. raise NotImplementedError()
  100. return self._mode_dps.get_value(self._device)
  101. @property
  102. def available_modes(self):
  103. """Return the list of presets that this device supports."""
  104. if self._mode_dps is None:
  105. return None
  106. return self._mode_dps.values(self._device)
  107. async def async_set_mode(self, mode):
  108. """Set the preset mode."""
  109. if self._mode_dps is None:
  110. raise NotImplementedError()
  111. await self._mode_dps.async_set_value(self._device, mode)