fan.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """
  2. Platform to control tuya fan devices.
  3. """
  4. import logging
  5. from homeassistant.components.fan import (
  6. FanEntity,
  7. SUPPORT_DIRECTION,
  8. SUPPORT_OSCILLATE,
  9. SUPPORT_PRESET_MODE,
  10. SUPPORT_SET_SPEED,
  11. )
  12. from homeassistant.const import (
  13. STATE_UNAVAILABLE,
  14. )
  15. from ..device import TuyaLocalDevice
  16. from ..helpers.device_config import TuyaEntityConfig
  17. _LOGGER = logging.getLogger(__name__)
  18. class TuyaLocalFan(FanEntity):
  19. """Representation of a Tuya Fan entity."""
  20. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  21. """
  22. Initialise the fan device.
  23. Args:
  24. device (TuyaLocalDevice): The device API instance.
  25. config (TuyaEntityConfig): The entity config.
  26. """
  27. self._device = device
  28. self._config = config
  29. self._support_flags = 0
  30. self._attr_dps = []
  31. dps_map = {c.name: c for c in config.dps()}
  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. for d in dps_map.values():
  38. if not d.hidden:
  39. self._attr_dps.append(d)
  40. if self._preset_dps:
  41. self._support_flags |= SUPPORT_PRESET_MODE
  42. if self._speed_dps:
  43. self._support_flags |= SUPPORT_SET_SPEED
  44. if self._oscillate_dps:
  45. self._support_flags |= SUPPORT_OSCILLATE
  46. if self._direction_dps:
  47. self._support_flags |= SUPPORT_DIRECTION
  48. @property
  49. def supported_features(self):
  50. """Return the features supported by this climate device."""
  51. return self._support_flags
  52. @property
  53. def should_poll(self):
  54. """Return the polling state."""
  55. return True
  56. @property
  57. def name(self):
  58. """Return the friendly name of the entity for the UI."""
  59. return self._config.name(self._device.name)
  60. @property
  61. def unique_id(self):
  62. """Return the unique id for this climate device."""
  63. return self._config.unique_id(self._device.unique_id)
  64. @property
  65. def device_info(self):
  66. """Return device information about this heater."""
  67. return self._device.device_info
  68. @property
  69. def icon(self):
  70. """Return the icon to use in the frontend for this device."""
  71. icon = self._config.icon(self._device)
  72. if icon:
  73. return icon
  74. else:
  75. return super().icon
  76. @property
  77. def is_on(self):
  78. """Return whether the switch is on or not."""
  79. # If there is no switch, it is always on
  80. if self._switch_dps is None:
  81. return True
  82. is_switched_on = self._switch_dps.get_value(self._device)
  83. if is_switched_on is None:
  84. return STATE_UNAVAILABLE
  85. else:
  86. return bool(is_switched_on)
  87. async def async_turn_on(self, **kwargs):
  88. """Turn the switch on"""
  89. if self._switch_dps is None:
  90. raise NotImplementedError()
  91. await self._switch_dps.async_set_value(self._device, True)
  92. async def async_turn_off(self, **kwargs):
  93. """Turn the switch off"""
  94. if self._switch_dps is None:
  95. raise NotImplementedError
  96. await self._switch_dps.async_set_value(self._device, False)
  97. @property
  98. def percentage(self):
  99. """Return the currently set percentage."""
  100. if self._speed_dps is None:
  101. return None
  102. return self._speed_dps.get_value(self._device)
  103. @property
  104. def percentage_step(self):
  105. """Return the step for percentage."""
  106. if self._speed_dps is None:
  107. return None
  108. if self._speed_dps.values(self._device) is None:
  109. return self._speed_dps.step(self._device)
  110. else:
  111. return 100 / len(self._speed_dps.values(self._device))
  112. @property
  113. def speed_count(self):
  114. """Return the number of speeds supported by the fan."""
  115. if self._speed_dps is None:
  116. return 0
  117. if self._speed_dps.values(self._device) is not None:
  118. return len(self._speed_dps.values(self._device))
  119. return int(round(100 / self.percentage_step))
  120. async def async_set_percentage(self, percentage):
  121. """Set the fan speed as a percentage."""
  122. if self._speed_dps is None:
  123. return None
  124. # If there is a fixed list of values, snap to the closest one
  125. if self._speed_dps.values(self._device) is not None:
  126. percentage = min(
  127. self._speed_dps.values(self._device), key=lambda x: abs(x - percentage)
  128. )
  129. await self._speed_dps.async_set_value(self._device, percentage)
  130. @property
  131. def preset_mode(self):
  132. """Return the current preset mode."""
  133. if self._preset_dps is None:
  134. return None
  135. return self._preset_dps.get_value(self._device)
  136. @property
  137. def preset_modes(self):
  138. """Return the list of presets that this device supports."""
  139. if self._preset_dps is None:
  140. return []
  141. return self._preset_dps.values(self._device)
  142. async def async_set_preset_mode(self, preset_mode):
  143. """Set the preset mode."""
  144. if self._preset_dps is None:
  145. raise NotImplementedError()
  146. await self._preset_dps.async_set_value(self._device, preset_mode)
  147. @property
  148. def current_direction(self):
  149. """Return the current direction [forward or reverse]."""
  150. if self._direction_dps is None:
  151. return None
  152. return self._direction_dps.get_value(self._device)
  153. async def async_set_direction(self, direction):
  154. """Set the direction of the fan."""
  155. if self._direction_dps is None:
  156. raise NotImplementedError()
  157. await self._direction_dps.async_set_value(self._device, direction)
  158. @property
  159. def oscillating(self):
  160. """Return whether or not the fan is oscillating."""
  161. if self._oscillate_dps is None:
  162. return None
  163. return self._oscillate_dps.get_value(self._device)
  164. async def async_oscillate(self, oscillating):
  165. """Oscillate the fan."""
  166. if self._oscillate_dps is None:
  167. raise NotImplementedError()
  168. await self._oscillate_dps.async_set_value(self._device, oscillating)
  169. @property
  170. def device_state_attributes(self):
  171. """Get additional attributes that the integration itself does not support."""
  172. attr = {}
  173. for a in self._attr_dps:
  174. attr[a.name] = a.get_value(self._device)
  175. return attr
  176. async def async_update(self):
  177. await self._device.async_refresh()