fan.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 .helpers.config import async_tuya_setup_platform
  13. from .helpers.device_config import TuyaEntityConfig
  14. from .helpers.mixin import TuyaLocalEntity
  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. @property
  52. def supported_features(self):
  53. """Return the features supported by this climate device."""
  54. return self._support_flags
  55. @property
  56. def is_on(self):
  57. """Return whether the switch is on or not."""
  58. # If there is no switch, it is always on
  59. if self._switch_dps is None:
  60. return self.available
  61. return self._switch_dps.get_value(self._device)
  62. async def async_turn_on(
  63. self,
  64. percentage: int | None = None,
  65. preset_mode: str | None = None,
  66. **kwargs: Any,
  67. ):
  68. """Turn the fan on, setting any other parameters given"""
  69. settings = {}
  70. if self._switch_dps:
  71. settings = {
  72. **settings,
  73. **self._switch_dps.get_values_to_set(self._device, True),
  74. }
  75. if percentage is not None and self._speed_dps:
  76. r = self._speed_dps.range(self._device)
  77. if r:
  78. percentage = percentage_to_ranged_value(r, percentage)
  79. settings = {
  80. **settings,
  81. **self._speed_dps.get_values_to_set(self._device, percentage),
  82. }
  83. if preset_mode and self._preset_dps:
  84. settings = {
  85. **settings,
  86. **self._preset_dps.get_values_to_set(self._device, preset_mode),
  87. }
  88. # TODO: potentially handle other kwargs.
  89. if settings:
  90. await self._device.async_set_properties(settings)
  91. async def async_turn_off(self, **kwargs):
  92. """Turn the switch off"""
  93. if self._switch_dps is None:
  94. raise NotImplementedError
  95. await self._switch_dps.async_set_value(self._device, False)
  96. @property
  97. def percentage(self):
  98. """Return the currently set percentage."""
  99. if self._speed_dps is None:
  100. return None
  101. r = self._speed_dps.range(self._device)
  102. val = self._speed_dps.get_value(self._device)
  103. if r and val is not None:
  104. val = ranged_value_to_percentage(r, val)
  105. return val
  106. @property
  107. def percentage_step(self):
  108. """Return the step for percentage."""
  109. if self._speed_dps is None:
  110. return None
  111. if self._speed_dps.values(self._device):
  112. return 100 / len(self._speed_dps.values(self._device))
  113. r = self._speed_dps.range(self._device)
  114. scale = 100 / r[1] if r else 1.0
  115. return self._speed_dps.step(self._device) * scale
  116. @property
  117. def speed_count(self):
  118. """Return the number of speeds supported by the fan."""
  119. if self._speed_dps is None:
  120. return 0
  121. if self._speed_dps.values(self._device):
  122. return len(self._speed_dps.values(self._device))
  123. return int(round(100 / self.percentage_step))
  124. async def async_set_percentage(self, percentage):
  125. """Set the fan speed as a percentage."""
  126. # If speed is 0, turn the fan off
  127. if percentage == 0 and self._switch_dps:
  128. return await self.async_turn_off()
  129. if self._speed_dps is None:
  130. return None
  131. # If there is a fixed list of values, snap to the closest one
  132. if self._speed_dps.values(self._device):
  133. percentage = min(
  134. self._speed_dps.values(self._device),
  135. key=lambda x: abs(x - percentage),
  136. )
  137. elif self._speed_dps.range(self._device):
  138. r = self._speed_dps.range(self._device)
  139. percentage = percentage_to_ranged_value(r, percentage)
  140. values_to_set = self._speed_dps.get_values_to_set(self._device, percentage)
  141. if not self.is_on and self._switch_dps:
  142. values_to_set.update(self._switch_dps.get_values_to_set(self._device, True))
  143. await self._device.async_set_properties(values_to_set)
  144. @property
  145. def preset_mode(self):
  146. """Return the current preset mode."""
  147. if self._preset_dps:
  148. return self._preset_dps.get_value(self._device)
  149. @property
  150. def preset_modes(self):
  151. """Return the list of presets that this device supports."""
  152. if self._preset_dps is None:
  153. return []
  154. return self._preset_dps.values(self._device)
  155. async def async_set_preset_mode(self, preset_mode):
  156. """Set the preset mode."""
  157. if self._preset_dps is None:
  158. raise NotImplementedError()
  159. await self._preset_dps.async_set_value(self._device, preset_mode)
  160. @property
  161. def current_direction(self):
  162. """Return the current direction [forward or reverse]."""
  163. if self._direction_dps:
  164. return self._direction_dps.get_value(self._device)
  165. async def async_set_direction(self, direction):
  166. """Set the direction of the fan."""
  167. if self._direction_dps is None:
  168. raise NotImplementedError()
  169. await self._direction_dps.async_set_value(self._device, direction)
  170. @property
  171. def oscillating(self):
  172. """Return whether or not the fan is oscillating."""
  173. if self._oscillate_dps:
  174. return self._oscillate_dps.get_value(self._device)
  175. async def async_oscillate(self, oscillating):
  176. """Oscillate the fan."""
  177. if self._oscillate_dps is None:
  178. raise NotImplementedError()
  179. await self._oscillate_dps.async_set_value(self._device, oscillating)