humidifier.py 3.9 KB

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