fan.py 6.6 KB

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