vacuum.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. _LOGGER.info("%s turning on", self._config.config_id)
  107. await self._power_dps.async_set_value(self._device, True)
  108. async def async_turn_off(self, **kwargs):
  109. """Turn off the vacuum cleaner."""
  110. if self._power_dps:
  111. _LOGGER.info("%s turning off", self._config.config_id)
  112. await self._power_dps.async_set_value(self._device, False)
  113. async def async_toggle(self, **kwargs):
  114. """Toggle the vacuum cleaner."""
  115. dps = self._power_dps or self._activate_dps
  116. if dps:
  117. switch_to = not dps.get_value(self._device)
  118. _LOGGER.info("%s toggling to %s", self._config.config_id, switch_to)
  119. await dps.async_set_value(self._device, switch_to)
  120. async def async_start(self):
  121. dps = self._command_dps or self._status_dps
  122. if dps and "start" in dps.values(self._device):
  123. _LOGGER.info("%s starting by command", self._config.config_id)
  124. await dps.async_set_value(self._device, "start")
  125. elif self._activate_dps:
  126. _LOGGER.info("%s activating", self._config.config_id)
  127. await self._activate_dps.async_set_value(self._device, True)
  128. async def async_pause(self):
  129. """Pause the vacuum cleaner."""
  130. dps = self._command_dps or self._status_dps
  131. if dps and "pause" in dps.values(self._device):
  132. _LOGGER.info("%s pausing by command", self._config.config_id)
  133. await dps.async_set_value(self._device, "pause")
  134. elif self._activate_dps:
  135. _LOGGER.info("%s deactivating", self._config.config_id)
  136. await self._activate_dps.async_set_value(self._device, False)
  137. async def async_return_to_base(self, **kwargs):
  138. """Tell the vacuum cleaner to return to its base."""
  139. dps = self._command_dps or self._status_dps
  140. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  141. _LOGGER.info("%s returning to base", self._config.config_id)
  142. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  143. async def async_clean_spot(self, **kwargs):
  144. """Tell the vacuum cleaner do a spot clean."""
  145. dps = self._command_dps or self._status_dps
  146. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  147. _LOGGER.info("%s doing spot clean", self._config.config_id)
  148. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  149. async def async_stop(self, **kwargs):
  150. """Tell the vacuum cleaner to stop."""
  151. dps = self._command_dps or self._status_dps
  152. if dps and SERVICE_STOP in dps.values(self._device):
  153. _LOGGER.info("%s stopping", self._config.config_id)
  154. await dps.async_set_value(self._device, SERVICE_STOP)
  155. async def async_locate(self, **kwargs):
  156. """Locate the vacuum cleaner."""
  157. if self._locate_dps:
  158. _LOGGER.info("%s locating", self._config.config_id)
  159. await self._locate_dps.async_set_value(self._device, True)
  160. async def async_send_command(self, command, params=None, **kwargs):
  161. """Send a command to the vacuum cleaner."""
  162. dps = self._command_dps or self._status_dps
  163. # stop command is often present in both command and direction dps
  164. # in that case, prefer the direction dp as async_stop will cover
  165. # the commad dp seperately.
  166. if (
  167. command == SERVICE_STOP
  168. and self._direction_dps
  169. and SERVICE_STOP in self._direction_dps.values(self._device)
  170. ):
  171. dps = self._direction_dps
  172. if command in dps.values(self._device):
  173. _LOGGER.info(
  174. "%s sending %s %s",
  175. self._config.config_id,
  176. "direction" if dps is self._direction_dps else "command",
  177. command,
  178. )
  179. await dps.async_set_value(self._device, command)
  180. elif self._direction_dps and command in self._direction_dps.values(
  181. self._device
  182. ):
  183. _LOGGER.info("%s sending direction %s", self._config.config_id, command)
  184. await self._direction_dps.async_set_value(self._device, command)
  185. @property
  186. def fan_speed_list(self):
  187. """Return the list of fan speeds supported"""
  188. if self._fan_dps:
  189. return self._fan_dps.values(self._device)
  190. @property
  191. def fan_speed(self):
  192. """Return the current fan speed"""
  193. if self._fan_dps:
  194. return self._fan_dps.get_value(self._device)
  195. async def async_set_fan_speed(self, fan_speed, **kwargs):
  196. """Set the fan speed of the vacuum."""
  197. if self._fan_dps:
  198. _LOGGER.info(
  199. "%s setting fan speed to %s", self._config.config_id, fan_speed
  200. )
  201. await self._fan_dps.async_set_value(self._device, fan_speed)