fan.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """
  2. Setup for different kinds of Tuya fan devices
  3. """
  4. from homeassistant.components.fan import (
  5. FanEntity,
  6. FanEntityFeature,
  7. )
  8. import logging
  9. from .device import TuyaLocalDevice
  10. from .helpers.config import async_tuya_setup_platform
  11. from .helpers.device_config import TuyaEntityConfig
  12. from .helpers.mixin import TuyaLocalEntity
  13. _LOGGER = logging.getLogger(__name__)
  14. async def async_setup_entry(hass, config_entry, async_add_entities):
  15. config = {**config_entry.data, **config_entry.options}
  16. await async_tuya_setup_platform(
  17. hass,
  18. async_add_entities,
  19. config,
  20. "fan",
  21. TuyaLocalFan,
  22. )
  23. class TuyaLocalFan(TuyaLocalEntity, FanEntity):
  24. """Representation of a Tuya Fan entity."""
  25. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  26. """
  27. Initialise the fan device.
  28. Args:
  29. device (TuyaLocalDevice): The device API instance.
  30. config (TuyaEntityConfig): The entity config.
  31. """
  32. super().__init__()
  33. dps_map = self._init_begin(device, config)
  34. self._switch_dps = dps_map.pop("switch", None)
  35. self._preset_dps = dps_map.pop("preset_mode", None)
  36. self._speed_dps = dps_map.pop("speed", None)
  37. self._oscillate_dps = dps_map.pop("oscillate", None)
  38. self._direction_dps = dps_map.pop("direction", None)
  39. self._init_end(dps_map)
  40. self._support_flags = 0
  41. if self._preset_dps:
  42. self._support_flags |= FanEntityFeature.PRESET_MODE
  43. if self._speed_dps:
  44. self._support_flags |= FanEntityFeature.SET_SPEED
  45. if self._oscillate_dps:
  46. self._support_flags |= FanEntityFeature.OSCILLATE
  47. if self._direction_dps:
  48. self._support_flags |= FanEntityFeature.DIRECTION
  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 is_on(self):
  55. """Return whether the switch is on or not."""
  56. # If there is no switch, it is always on
  57. if self._switch_dps is None:
  58. return self.available
  59. return self._switch_dps.get_value(self._device)
  60. async def async_turn_on(self, **kwargs):
  61. """Turn the switch on"""
  62. if self._switch_dps is None:
  63. raise NotImplementedError()
  64. await self._switch_dps.async_set_value(self._device, True)
  65. async def async_turn_off(self, **kwargs):
  66. """Turn the switch off"""
  67. if self._switch_dps is None:
  68. raise NotImplementedError
  69. await self._switch_dps.async_set_value(self._device, False)
  70. @property
  71. def percentage(self):
  72. """Return the currently set percentage."""
  73. if self._speed_dps is None:
  74. return None
  75. return self._speed_dps.get_value(self._device)
  76. @property
  77. def percentage_step(self):
  78. """Return the step for percentage."""
  79. if self._speed_dps is None:
  80. return None
  81. if self._speed_dps.values(self._device):
  82. return 100 / len(self._speed_dps.values(self._device))
  83. return self._speed_dps.step(self._device)
  84. @property
  85. def speed_count(self):
  86. """Return the number of speeds supported by the fan."""
  87. if self._speed_dps is None:
  88. return 0
  89. if self._speed_dps.values(self._device):
  90. return len(self._speed_dps.values(self._device))
  91. return int(round(100 / self.percentage_step))
  92. async def async_set_percentage(self, percentage):
  93. """Set the fan speed as a percentage."""
  94. if self._speed_dps is None:
  95. return None
  96. # If there is a fixed list of values, snap to the closest one
  97. if self._speed_dps.values(self._device):
  98. percentage = min(
  99. self._speed_dps.values(self._device),
  100. key=lambda x: abs(x - percentage),
  101. )
  102. await self._speed_dps.async_set_value(self._device, percentage)
  103. @property
  104. def preset_mode(self):
  105. """Return the current preset mode."""
  106. if self._preset_dps is None:
  107. return None
  108. return self._preset_dps.get_value(self._device)
  109. @property
  110. def preset_modes(self):
  111. """Return the list of presets that this device supports."""
  112. if self._preset_dps is None:
  113. return []
  114. return self._preset_dps.values(self._device)
  115. async def async_set_preset_mode(self, preset_mode):
  116. """Set the preset mode."""
  117. if self._preset_dps is None:
  118. raise NotImplementedError()
  119. await self._preset_dps.async_set_value(self._device, preset_mode)
  120. @property
  121. def current_direction(self):
  122. """Return the current direction [forward or reverse]."""
  123. if self._direction_dps is None:
  124. return None
  125. return self._direction_dps.get_value(self._device)
  126. async def async_set_direction(self, direction):
  127. """Set the direction of the fan."""
  128. if self._direction_dps is None:
  129. raise NotImplementedError()
  130. await self._direction_dps.async_set_value(self._device, direction)
  131. @property
  132. def oscillating(self):
  133. """Return whether or not the fan is oscillating."""
  134. if self._oscillate_dps is None:
  135. return None
  136. return self._oscillate_dps.get_value(self._device)
  137. async def async_oscillate(self, oscillating):
  138. """Oscillate the fan."""
  139. if self._oscillate_dps is None:
  140. raise NotImplementedError()
  141. await self._oscillate_dps.async_set_value(self._device, oscillating)