vacuum.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. """
  2. Setup for different kinds of Tuya vacuum cleaners
  3. """
  4. import logging
  5. from homeassistant.components.vacuum import (
  6. SERVICE_CLEAN_SPOT,
  7. SERVICE_RETURN_TO_BASE,
  8. SERVICE_STOP,
  9. StateVacuumEntity,
  10. VacuumActivity,
  11. VacuumEntityFeature,
  12. )
  13. from .device import TuyaLocalDevice
  14. from .entity import TuyaLocalEntity
  15. from .helpers.config import async_tuya_setup_platform
  16. from .helpers.device_config import TuyaEntityConfig
  17. _LOGGER = logging.getLogger(__name__)
  18. async def async_setup_entry(hass, config_entry, async_add_entities):
  19. config = {**config_entry.data, **config_entry.options}
  20. await async_tuya_setup_platform(
  21. hass,
  22. async_add_entities,
  23. config,
  24. "vacuum",
  25. TuyaLocalVacuum,
  26. )
  27. class TuyaLocalVacuum(TuyaLocalEntity, StateVacuumEntity):
  28. """Representation of a Tuya Vacuum Cleaner"""
  29. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  30. """
  31. Initialise the sensor.
  32. Args:
  33. device (TuyaLocalDevice): the device API instance.
  34. config (TuyaEntityConfig): the configuration for this entity
  35. """
  36. super().__init__()
  37. dps_map = self._init_begin(device, config)
  38. self._status_dps = dps_map.get("status")
  39. self._command_dps = dps_map.get("command")
  40. self._locate_dps = dps_map.get("locate")
  41. self._power_dps = dps_map.get("power")
  42. self._activate_dps = dps_map.get("activate")
  43. self._direction_dps = dps_map.get("direction_control")
  44. self._error_dps = dps_map.get("error")
  45. self._fan_dps = dps_map.pop("fan_speed", None)
  46. if self._status_dps is None:
  47. raise AttributeError(f"{config.config_id} is missing a status dps")
  48. self._init_end(dps_map)
  49. @property
  50. def supported_features(self):
  51. """Return the features supported by this vacuum cleaner."""
  52. support = (
  53. VacuumEntityFeature.STATE
  54. | VacuumEntityFeature.STATUS
  55. | VacuumEntityFeature.SEND_COMMAND
  56. )
  57. if self._fan_dps:
  58. support |= VacuumEntityFeature.FAN_SPEED
  59. if self._power_dps:
  60. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  61. if self._locate_dps:
  62. support |= VacuumEntityFeature.LOCATE
  63. cmd_dps = self._command_dps or self._status_dps
  64. cmd_support = cmd_dps.values(self._device)
  65. if SERVICE_RETURN_TO_BASE in cmd_support:
  66. support |= VacuumEntityFeature.RETURN_HOME
  67. if SERVICE_CLEAN_SPOT in cmd_support:
  68. support |= VacuumEntityFeature.CLEAN_SPOT
  69. if SERVICE_STOP in cmd_support:
  70. support |= VacuumEntityFeature.STOP
  71. if self._activate_dps:
  72. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  73. else:
  74. if "start" in cmd_support:
  75. support |= VacuumEntityFeature.START
  76. if "pause" in cmd_support:
  77. support |= VacuumEntityFeature.PAUSE
  78. return support
  79. @property
  80. def status(self):
  81. """Return the status of the vacuum cleaner."""
  82. return self._status_dps.get_value(self._device)
  83. @property
  84. def activity(self):
  85. """Return the state of the vacuum cleaner."""
  86. status = self.status
  87. if self._error_dps and self._error_dps.get_value(self._device):
  88. return VacuumActivity.ERROR
  89. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  90. return VacuumActivity.RETURNING
  91. elif status in ["standby", "sleep"]:
  92. return VacuumActivity.IDLE
  93. elif status == "paused":
  94. return VacuumActivity.PAUSED
  95. elif status in ["charging", "charged", "docked"]:
  96. return VacuumActivity.DOCKED
  97. elif self._power_dps and self._power_dps.get_value(self._device) is False:
  98. return VacuumActivity.IDLE
  99. elif self._activate_dps and self._activate_dps.get_value(self._device) is False:
  100. return VacuumActivity.PAUSED
  101. else:
  102. return VacuumActivity.CLEANING
  103. async def async_turn_on(self, **kwargs):
  104. """Turn on the vacuum cleaner."""
  105. if self._power_dps:
  106. async with self._device.set_lock:
  107. _LOGGER.info("%s turning on", self._config.config_id)
  108. await self._power_dps.async_set_value(self._device, True)
  109. async def async_turn_off(self, **kwargs):
  110. """Turn off the vacuum cleaner."""
  111. if self._power_dps:
  112. async with self._device.set_lock:
  113. _LOGGER.info("%s turning off", self._config.config_id)
  114. await self._power_dps.async_set_value(self._device, False)
  115. async def async_toggle(self, **kwargs):
  116. """Toggle the vacuum cleaner."""
  117. dps = self._power_dps or self._activate_dps
  118. if dps:
  119. async with self._device.set_lock:
  120. switch_to = not dps.get_value(self._device)
  121. _LOGGER.info("%s toggling to %s", self._config.config_id, switch_to)
  122. await dps.async_set_value(self._device, switch_to)
  123. async def async_start(self):
  124. dps = self._command_dps or self._status_dps
  125. async with self._device.set_lock:
  126. if dps and "start" in dps.values(self._device):
  127. _LOGGER.info("%s starting by command", self._config.config_id)
  128. await dps.async_set_value(self._device, "start")
  129. elif self._activate_dps:
  130. _LOGGER.info("%s activating", self._config.config_id)
  131. await self._activate_dps.async_set_value(self._device, True)
  132. async def async_pause(self):
  133. """Pause the vacuum cleaner."""
  134. dps = self._command_dps or self._status_dps
  135. async with self._device.set_lock:
  136. if dps and "pause" in dps.values(self._device):
  137. _LOGGER.info("%s pausing by command", self._config.config_id)
  138. await dps.async_set_value(self._device, "pause")
  139. elif self._activate_dps:
  140. _LOGGER.info("%s deactivating", self._config.config_id)
  141. await self._activate_dps.async_set_value(self._device, False)
  142. async def async_return_to_base(self, **kwargs):
  143. """Tell the vacuum cleaner to return to its base."""
  144. dps = self._command_dps or self._status_dps
  145. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  146. async with self._device.set_lock:
  147. _LOGGER.info("%s returning to base", self._config.config_id)
  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. async with self._device.set_lock:
  154. _LOGGER.info("%s doing spot clean", self._config.config_id)
  155. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  156. async def async_stop(self, **kwargs):
  157. """Tell the vacuum cleaner to stop."""
  158. dps = self._command_dps or self._status_dps
  159. if dps and SERVICE_STOP in dps.values(self._device):
  160. async with self._device.set_lock:
  161. _LOGGER.info("%s stopping", self._config.config_id)
  162. await dps.async_set_value(self._device, SERVICE_STOP)
  163. async def async_locate(self, **kwargs):
  164. """Locate the vacuum cleaner."""
  165. if self._locate_dps:
  166. async with self._device.set_lock:
  167. _LOGGER.info("%s locating", self._config.config_id)
  168. await self._locate_dps.async_set_value(self._device, True)
  169. async def async_send_command(self, command, params=None, **kwargs):
  170. """Send a command to the vacuum cleaner."""
  171. dps = self._command_dps or self._status_dps
  172. # stop command is often present in both command and direction dps
  173. # in that case, prefer the direction dp as async_stop will cover
  174. # the commad dp seperately.
  175. if (
  176. command == SERVICE_STOP
  177. and self._direction_dps
  178. and SERVICE_STOP in self._direction_dps.values(self._device)
  179. ):
  180. dps = self._direction_dps
  181. async with self._device.set_lock:
  182. if command in dps.values(self._device):
  183. _LOGGER.info(
  184. "%s sending %s %s",
  185. self._config.config_id,
  186. "direction" if dps is self._direction_dps else "command",
  187. command,
  188. )
  189. await dps.async_set_value(self._device, command)
  190. elif self._direction_dps and command in self._direction_dps.values(
  191. self._device
  192. ):
  193. _LOGGER.info("%s sending direction %s", self._config.config_id, command)
  194. await self._direction_dps.async_set_value(self._device, command)
  195. @property
  196. def fan_speed_list(self):
  197. """Return the list of fan speeds supported"""
  198. if self._fan_dps:
  199. return self._fan_dps.values(self._device)
  200. @property
  201. def fan_speed(self):
  202. """Return the current fan speed"""
  203. if self._fan_dps:
  204. return self._fan_dps.get_value(self._device)
  205. async def async_set_fan_speed(self, fan_speed, **kwargs):
  206. """Set the fan speed of the vacuum."""
  207. if self._fan_dps:
  208. async with self._device.set_lock:
  209. _LOGGER.info(
  210. "%s setting fan speed to %s", self._config.config_id, fan_speed
  211. )
  212. await self._fan_dps.async_set_value(self._device, fan_speed)