fan.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """
  2. Setup for different kinds of Tuya fan devices
  3. """
  4. import logging
  5. from typing import Any
  6. from homeassistant.components.fan import FanEntity, FanEntityFeature
  7. from homeassistant.util.percentage import (
  8. percentage_to_ranged_value,
  9. ranged_value_to_percentage,
  10. )
  11. from .device import TuyaLocalDevice
  12. from .entity import TuyaLocalEntity
  13. from .helpers.config import async_tuya_setup_platform
  14. from .helpers.device_config import TuyaEntityConfig
  15. _LOGGER = logging.getLogger(__name__)
  16. async def async_setup_entry(hass, config_entry, async_add_entities):
  17. config = {**config_entry.data, **config_entry.options}
  18. await async_tuya_setup_platform(
  19. hass,
  20. async_add_entities,
  21. config,
  22. "fan",
  23. TuyaLocalFan,
  24. )
  25. class TuyaLocalFan(TuyaLocalEntity, FanEntity):
  26. """Representation of a Tuya Fan entity."""
  27. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  28. """
  29. Initialise the fan device.
  30. Args:
  31. device (TuyaLocalDevice): The device API instance.
  32. config (TuyaEntityConfig): The entity config.
  33. """
  34. super().__init__()
  35. dps_map = self._init_begin(device, config)
  36. self._switch_dps = dps_map.pop("switch", None)
  37. self._preset_dps = dps_map.pop("preset_mode", None)
  38. self._speed_dps = dps_map.pop("speed", None)
  39. self._oscillate_dps = dps_map.pop("oscillate", None)
  40. self._direction_dps = dps_map.pop("direction", None)
  41. self._init_end(dps_map)
  42. self._support_flags = FanEntityFeature(0)
  43. if self._preset_dps:
  44. self._support_flags |= FanEntityFeature.PRESET_MODE
  45. if self._speed_dps:
  46. self._support_flags |= FanEntityFeature.SET_SPEED
  47. if self._oscillate_dps:
  48. self._support_flags |= FanEntityFeature.OSCILLATE
  49. if self._direction_dps:
  50. self._support_flags |= FanEntityFeature.DIRECTION
  51. if self._switch_dps:
  52. self._support_flags |= FanEntityFeature.TURN_ON | FanEntityFeature.TURN_OFF
  53. elif self._speed_dps:
  54. r = self._speed_dps.range(self._device)
  55. if r and r[0] == 0:
  56. self._support_flags |= FanEntityFeature.TURN_OFF
  57. # Until the deprecation period ends (expected 2025.2)
  58. self._enable_turn_on_off_backwards_compatibility = False
  59. @property
  60. def supported_features(self):
  61. """Return the features supported by this climate device."""
  62. return self._support_flags
  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
  67. if self._switch_dps is None:
  68. return self.available
  69. return self._switch_dps.get_value(self._device)
  70. async def async_turn_on(
  71. self,
  72. percentage: int | None = None,
  73. preset_mode: str | None = None,
  74. **kwargs: Any,
  75. ):
  76. """Turn the fan on, setting any other parameters given"""
  77. settings = {}
  78. if self._switch_dps:
  79. settings = {
  80. **settings,
  81. **self._switch_dps.get_values_to_set(self._device, True, settings),
  82. }
  83. if percentage is not None and self._speed_dps:
  84. r = self._speed_dps.range(self._device)
  85. if r:
  86. if r[0] == 0:
  87. r = (1, r[1])
  88. percentage = percentage_to_ranged_value(r, percentage)
  89. settings = {
  90. **settings,
  91. **self._speed_dps.get_values_to_set(self._device, percentage, settings),
  92. }
  93. if preset_mode and self._preset_dps:
  94. settings = {
  95. **settings,
  96. **self._preset_dps.get_values_to_set(
  97. self._device, preset_mode, settings
  98. ),
  99. }
  100. # TODO: potentially handle other kwargs.
  101. if settings:
  102. await self._device.async_set_properties(settings)
  103. async def async_turn_off(self, **kwargs):
  104. """Turn the switch off"""
  105. if self._switch_dps:
  106. await self._switch_dps.async_set_value(self._device, False)
  107. elif (
  108. self._speed_dps
  109. and self._speed_dps.range(self._device)
  110. and self._speed_dps.range(self._device)[0] == 0
  111. ):
  112. await self._speed_dps.async_set_value(self._device, 0)
  113. else:
  114. raise NotImplementedError
  115. @property
  116. def percentage(self):
  117. """Return the currently set percentage."""
  118. if self._speed_dps is None:
  119. return None
  120. r = self._speed_dps.range(self._device)
  121. val = self._speed_dps.get_value(self._device)
  122. if r and val is not None:
  123. if r[0] == 0:
  124. r = (1, r[1])
  125. val = ranged_value_to_percentage(r, val)
  126. return val
  127. @property
  128. def percentage_step(self):
  129. """Return the step for percentage."""
  130. if self._speed_dps is None:
  131. return None
  132. if self._speed_dps.values(self._device):
  133. return 100 / len(self._speed_dps.values(self._device))
  134. r = self._speed_dps.range(self._device)
  135. scale = 100 / r[1] if r else 1.0
  136. return self._speed_dps.step(self._device) * scale
  137. @property
  138. def speed_count(self):
  139. """Return the number of speeds supported by the fan."""
  140. if self._speed_dps is None:
  141. return 0
  142. if self._speed_dps.values(self._device):
  143. return len(self._speed_dps.values(self._device))
  144. return int(round(100 / self.percentage_step))
  145. async def async_set_percentage(self, percentage):
  146. """Set the fan speed as a percentage."""
  147. # If speed is 0, turn the fan off
  148. if percentage == 0 and self._switch_dps:
  149. return await self.async_turn_off()
  150. if self._speed_dps is None:
  151. return None
  152. # If there is a fixed list of values, snap to the closest one
  153. if self._speed_dps.values(self._device):
  154. percentage = min(
  155. self._speed_dps.values(self._device),
  156. key=lambda x: abs(x - percentage),
  157. )
  158. elif self._speed_dps.range(self._device):
  159. r = self._speed_dps.range(self._device)
  160. if r[0] == 0:
  161. r = (1, r[1])
  162. percentage = percentage_to_ranged_value(r, percentage)
  163. values_to_set = self._speed_dps.get_values_to_set(self._device, percentage)
  164. if not self.is_on and self._switch_dps:
  165. values_to_set.update(
  166. self._switch_dps.get_values_to_set(self._device, True, values_to_set)
  167. )
  168. await self._device.async_set_properties(values_to_set)
  169. @property
  170. def preset_mode(self):
  171. """Return the current preset mode."""
  172. if self._preset_dps:
  173. return self._preset_dps.get_value(self._device)
  174. @property
  175. def preset_modes(self):
  176. """Return the list of presets that this device supports."""
  177. if self._preset_dps is None:
  178. return []
  179. return self._preset_dps.values(self._device)
  180. async def async_set_preset_mode(self, preset_mode):
  181. """Set the preset mode."""
  182. if self._preset_dps is None:
  183. raise NotImplementedError()
  184. await self._preset_dps.async_set_value(self._device, preset_mode)
  185. @property
  186. def current_direction(self):
  187. """Return the current direction [forward or reverse]."""
  188. if self._direction_dps:
  189. return self._direction_dps.get_value(self._device)
  190. async def async_set_direction(self, direction):
  191. """Set the direction of the fan."""
  192. if self._direction_dps is None:
  193. raise NotImplementedError()
  194. await self._direction_dps.async_set_value(self._device, direction)
  195. @property
  196. def oscillating(self):
  197. """Return whether or not the fan is oscillating."""
  198. if self._oscillate_dps:
  199. return self._oscillate_dps.get_value(self._device)
  200. async def async_oscillate(self, oscillating):
  201. """Oscillate the fan."""
  202. if self._oscillate_dps is None:
  203. raise NotImplementedError()
  204. await self._oscillate_dps.async_set_value(self._device, oscillating)