fan.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 name of the climate device."""
  59. return self._device.name
  60. @property
  61. def friendly_name(self):
  62. """Return the friendly name of the climate entity for the UI."""
  63. return self._config.name
  64. @property
  65. def unique_id(self):
  66. """Return the unique id for this climate device."""
  67. return self._device.unique_id
  68. @property
  69. def device_info(self):
  70. """Return device information about this heater."""
  71. return self._device.device_info
  72. @property
  73. def icon(self):
  74. """Return the icon to use in the frontend for this device."""
  75. if self.is_on:
  76. return "mdi:fan"
  77. else:
  78. return "mdi:fan-off"
  79. @property
  80. def is_on(self):
  81. """Return whether the switch is on or not."""
  82. # If there is no switch, it is always on
  83. if self._switch_dps is None:
  84. return True
  85. is_switched_on = self._switch_dps.get_value(self._device)
  86. if is_switched_on is None:
  87. return STATE_UNAVAILABLE
  88. else:
  89. return bool(is_switched_on)
  90. async def async_turn_on(self, **kwargs):
  91. """Turn the switch on"""
  92. if self._switch_dps is None:
  93. raise NotImplementedError()
  94. await self._switch_dps.async_set_value(self._device, True)
  95. async def async_turn_off(self, **kwargs):
  96. """Turn the switch off"""
  97. if self._switch_dps is None:
  98. raise NotImplementedError
  99. await self._switch_dps.async_set_value(self._device, False)
  100. @property
  101. def percentage(self):
  102. """Return the currently set percentage."""
  103. if self._speed_dps is None:
  104. return None
  105. return self._speed_dps.get_value(self._device)
  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 is None:
  112. return self._speed_dps.step(self._device)
  113. else:
  114. return 100 / len(self._speed_dps.values)
  115. @property
  116. def speed_count(self):
  117. """Return the number of speeds supported by the fan."""
  118. if self._speed_dps is None:
  119. return 0
  120. if self._speed_dps.values is not None:
  121. return len(self._speed_dps.values)
  122. return int(round(100 / self.percentage_step))
  123. async def async_set_percentage(self, percentage):
  124. """Set the fan speed as a percentage."""
  125. if self._speed_dps is None:
  126. return None
  127. # If there is a fixed list of values, snap to the closest one
  128. if self._speed_dps.values is not None:
  129. percentage = min(self._speed_dps.values, key=lambda x: abs(x - percentage))
  130. await self._speed_dps.async_set_value(self._device, percentage)
  131. @property
  132. def preset_mode(self):
  133. """Return the current preset mode."""
  134. if self._preset_dps is None:
  135. return None
  136. return self._preset_dps.get_value(self._device)
  137. @property
  138. def preset_modes(self):
  139. """Return the list of presets that this device supports."""
  140. if self._preset_dps is None:
  141. return []
  142. return self._preset_dps.values
  143. async def async_set_preset_mode(self, preset_mode):
  144. """Set the preset mode."""
  145. if self._preset_dps is None:
  146. raise NotImplementedError()
  147. await self._preset_dps.async_set_value(self._device, preset_mode)
  148. @property
  149. def current_direction(self):
  150. """Return the current direction [forward or reverse]."""
  151. if self._direction_dps is None:
  152. return None
  153. return self._direction_dps.get_value(self._device)
  154. async def async_set_direction(self, direction):
  155. """Set the direction of the fan."""
  156. if self._direction_dps is None:
  157. raise NotImplementedError()
  158. await self._direction_dps.async_set_value(self._device, direction)
  159. @property
  160. def oscillating(self):
  161. """Return whether or not the fan is oscillating."""
  162. if self._oscillate_dps is None:
  163. return None
  164. return self._oscillate_dps.get_value(self._device)
  165. async def async_oscillate(self, oscillating):
  166. """Oscillate the fan."""
  167. if self._oscillate_dps is None:
  168. raise NotImplementedError()
  169. await self._oscillate_dps.async_set_value(self._device, oscillating)
  170. @property
  171. def device_state_attributes(self):
  172. """Get additional attributes that the integration itself does not support."""
  173. attr = {}
  174. for a in self._attr_dps:
  175. attr[a.name] = a.get_value(self._device)
  176. return attr
  177. async def async_update(self):
  178. await self._device.async_refresh()