vacuum.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. Setup for different kinds of Tuya vacuum cleaners
  3. """
  4. from homeassistant.components.vacuum import (
  5. SERVICE_CLEAN_SPOT,
  6. SERVICE_RETURN_TO_BASE,
  7. SERVICE_STOP,
  8. STATE_CLEANING,
  9. STATE_DOCKED,
  10. STATE_ERROR,
  11. STATE_IDLE,
  12. STATE_PAUSED,
  13. STATE_RETURNING,
  14. StateVacuumEntity,
  15. VacuumEntityFeature,
  16. )
  17. from .device import TuyaLocalDevice
  18. from .helpers.config import async_tuya_setup_platform
  19. from .helpers.device_config import TuyaEntityConfig
  20. from .helpers.mixin import TuyaLocalEntity
  21. async def async_setup_entry(hass, config_entry, async_add_entities):
  22. config = {**config_entry.data, **config_entry.options}
  23. await async_tuya_setup_platform(
  24. hass,
  25. async_add_entities,
  26. config,
  27. "vacuum",
  28. TuyaLocalVacuum,
  29. )
  30. class TuyaLocalVacuum(TuyaLocalEntity, StateVacuumEntity):
  31. """Representation of a Tuya Vacuum Cleaner"""
  32. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  33. """
  34. Initialise the sensor.
  35. Args:
  36. device (TuyaLocalDevice): the device API instance.
  37. config (TuyaEntityConfig): the configuration for this entity
  38. """
  39. dps_map = self._init_begin(device, config)
  40. self._status_dps = dps_map.get("status")
  41. self._command_dps = dps_map.get("command")
  42. self._locate_dps = dps_map.get("locate")
  43. self._power_dps = dps_map.get("power")
  44. self._active_dps = dps_map.get("activate")
  45. self._battery_dps = dps_map.pop("battery", None)
  46. self._direction_dps = dps_map.get("direction_control")
  47. self._error_dps = dps_map.get("error")
  48. self._fan_dps = dps_map.pop("fan_speed", None)
  49. if self._status_dps is None:
  50. raise AttributeError(f"{config.name} is missing a status dps")
  51. self._init_end(dps_map)
  52. @property
  53. def supported_features(self):
  54. """Return the features supported by this vacuum cleaner."""
  55. support = (
  56. VacuumEntityFeature.STATE
  57. | VacuumEntityFeature.STATUS
  58. | VacuumEntityFeature.SEND_COMMAND
  59. )
  60. if self._battery_dps:
  61. support |= VacuumEntityFeature.BATTERY
  62. if self._fan_dps:
  63. support |= VacuumEntityFeature.FAN_SPEED
  64. if self._power_dps:
  65. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  66. if self._locate_dps:
  67. support |= VacuumEntityFeature.LOCATE
  68. cmd_dps = self._command_dps or self._status_dps
  69. cmd_support = cmd_dps.values(self._device)
  70. if SERVICE_RETURN_TO_BASE in cmd_support:
  71. support |= VacuumEntityFeature.RETURN_HOME
  72. if SERVICE_CLEAN_SPOT in cmd_support:
  73. support |= VacuumEntityFeature.CLEAN_SPOT
  74. if SERVICE_STOP in cmd_support:
  75. support |= VacuumEntityFeature.STOP
  76. if self._active_dps:
  77. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  78. else:
  79. if "start" in cmd_support:
  80. support |= VacuumEntityFeature.START
  81. if "pause" in cmd_support:
  82. support |= VacuumEntityFeature.PAUSE
  83. return support
  84. @property
  85. def battery_level(self):
  86. """Return the battery level of the vacuum cleaner."""
  87. if self._battery_dps:
  88. return self._battery_dps.get_value(self._device)
  89. @property
  90. def status(self):
  91. """Return the status of the vacuum cleaner."""
  92. return self._status_dps.get_value(self._device)
  93. @property
  94. def state(self):
  95. """Return the state of the vacuum cleaner."""
  96. status = self.status
  97. if self._error_dps and self._error_dps.get_value(self._device):
  98. return STATE_ERROR
  99. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  100. return STATE_RETURNING
  101. elif status == "standby":
  102. return STATE_IDLE
  103. elif status == "paused":
  104. return STATE_PAUSED
  105. elif status in ["charging", "charged"]:
  106. return STATE_DOCKED
  107. elif self._power_dps and self._power_dps.get_value(self._device) is False:
  108. return STATE_DOCKED
  109. elif self._active_dps and self._active_dps.get_value(self._device) is False:
  110. return STATE_DOCKED
  111. else:
  112. return STATE_CLEANING
  113. async def async_turn_on(self, **kwargs):
  114. """Turn on the vacuum cleaner."""
  115. if self._power_dps:
  116. await self._power_dps.async_set_value(self._device, True)
  117. async def async_turn_off(self, **kwargs):
  118. """Turn off the vacuum cleaner."""
  119. if self._power_dps:
  120. await self._power_dps.async_set_value(self._device, False)
  121. async def async_toggle(self, **kwargs):
  122. """Toggle the vacuum cleaner."""
  123. dps = self._power_dps
  124. if not dps:
  125. dps = self._activate_dps
  126. if dps:
  127. switch_to = not dps.get_value(self._device)
  128. await dps.async_set_value(self._device, switch_to)
  129. async def async_start(self):
  130. if self._active_dps:
  131. await self._active_dps.async_set_value(self._device, True)
  132. else:
  133. dps = self._command_dps or self._status_dps
  134. if "start" in dps.values(self._device):
  135. await dps.async_set_value(self._device, "start")
  136. async def async_pause(self):
  137. """Pause the vacuum cleaner."""
  138. if self._active_dps:
  139. await self._active_dps.async_set_value(self._device, False)
  140. else:
  141. dps = self._command_dps or self._status_dps
  142. if "pause" in dps.values(self._device):
  143. await dps.async_set_value(self._device, "pause")
  144. async def async_return_to_base(self, **kwargs):
  145. """Tell the vacuum cleaner to return to its base."""
  146. dps = self._command_dps or self._status_dps
  147. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  148. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  149. async def async_clean_spot(self, **kwargs):
  150. """Tell the vacuum cleaner do a spot clean."""
  151. dps = self._command_dps or self._status_dps
  152. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  153. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  154. async def async_stop(self, **kwargs):
  155. """Tell the vacuum cleaner to stop."""
  156. dps = self._command_dps or self._status_dps
  157. if dps and SERVICE_STOP in dps.values(self._device):
  158. await dps.async_set_value(self._device, SERVICE_STOP)
  159. async def async_locate(self, **kwargs):
  160. """Locate the vacuum cleaner."""
  161. if self._locate_dps:
  162. await self._locate_dps.async_set_value(self._device, True)
  163. async def async_send_command(self, command, params=None, **kwargs):
  164. """Send a command to the vacuum cleaner."""
  165. dps = self._command_dps or self._status_dps
  166. if command in dps.values(self._device):
  167. await dps.async_set_value(self._device, command)
  168. elif self._direction_dps and command in self._direction_dps.values(
  169. self._device
  170. ):
  171. await self._direction_dps.async_set_value(self._device, command)
  172. @property
  173. def fan_speed_list(self):
  174. """Return the list of fan speeds supported"""
  175. if self._fan_dps:
  176. return self._fan_dps.values(self._device)
  177. @property
  178. def fan_speed(self):
  179. """Return the current fan speed"""
  180. if self._fan_dps:
  181. return self._fan_dps.get_value(self._device)
  182. async def async_set_fan_speed(self, fan_speed, **kwargs):
  183. """Set the fan speed of the vacuum."""
  184. if self._fan_dps:
  185. await self._fan_dps.async_set_value(self._device, fan_speed)