vacuum.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. super().__init__()
  40. dps_map = self._init_begin(device, config)
  41. self._status_dps = dps_map.get("status")
  42. self._command_dps = dps_map.get("command")
  43. self._locate_dps = dps_map.get("locate")
  44. self._power_dps = dps_map.get("power")
  45. self._activate_dps = dps_map.get("activate")
  46. self._battery_dps = dps_map.pop("battery", None)
  47. self._direction_dps = dps_map.get("direction_control")
  48. self._error_dps = dps_map.get("error")
  49. self._fan_dps = dps_map.pop("fan_speed", None)
  50. if self._status_dps is None:
  51. raise AttributeError(f"{config.name} is missing a status dps")
  52. self._init_end(dps_map)
  53. @property
  54. def supported_features(self):
  55. """Return the features supported by this vacuum cleaner."""
  56. support = (
  57. VacuumEntityFeature.STATE
  58. | VacuumEntityFeature.STATUS
  59. | VacuumEntityFeature.SEND_COMMAND
  60. )
  61. if self._battery_dps:
  62. support |= VacuumEntityFeature.BATTERY
  63. if self._fan_dps:
  64. support |= VacuumEntityFeature.FAN_SPEED
  65. if self._power_dps:
  66. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  67. if self._locate_dps:
  68. support |= VacuumEntityFeature.LOCATE
  69. cmd_dps = self._command_dps or self._status_dps
  70. cmd_support = cmd_dps.values(self._device)
  71. if SERVICE_RETURN_TO_BASE in cmd_support:
  72. support |= VacuumEntityFeature.RETURN_HOME
  73. if SERVICE_CLEAN_SPOT in cmd_support:
  74. support |= VacuumEntityFeature.CLEAN_SPOT
  75. if SERVICE_STOP in cmd_support:
  76. support |= VacuumEntityFeature.STOP
  77. if self._activate_dps:
  78. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  79. else:
  80. if "start" in cmd_support:
  81. support |= VacuumEntityFeature.START
  82. if "pause" in cmd_support:
  83. support |= VacuumEntityFeature.PAUSE
  84. return support
  85. @property
  86. def battery_level(self):
  87. """Return the battery level of the vacuum cleaner."""
  88. if self._battery_dps:
  89. return self._battery_dps.get_value(self._device)
  90. @property
  91. def status(self):
  92. """Return the status of the vacuum cleaner."""
  93. return self._status_dps.get_value(self._device)
  94. @property
  95. def state(self):
  96. """Return the state of the vacuum cleaner."""
  97. status = self.status
  98. if self._error_dps and self._error_dps.get_value(self._device):
  99. return STATE_ERROR
  100. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  101. return STATE_RETURNING
  102. elif status in ["standby", "sleep"]:
  103. return STATE_IDLE
  104. elif status == "paused":
  105. return STATE_PAUSED
  106. elif status in ["charging", "charged"]:
  107. return STATE_DOCKED
  108. elif self._power_dps and self._power_dps.get_value(self._device) is False:
  109. return STATE_IDLE
  110. elif self._activate_dps and self._activate_dps.get_value(self._device) is False:
  111. return STATE_PAUSED
  112. else:
  113. return STATE_CLEANING
  114. async def async_turn_on(self, **kwargs):
  115. """Turn on the vacuum cleaner."""
  116. if self._power_dps:
  117. await self._power_dps.async_set_value(self._device, True)
  118. async def async_turn_off(self, **kwargs):
  119. """Turn off the vacuum cleaner."""
  120. if self._power_dps:
  121. await self._power_dps.async_set_value(self._device, False)
  122. async def async_toggle(self, **kwargs):
  123. """Toggle the vacuum cleaner."""
  124. dps = self._power_dps
  125. if not dps:
  126. dps = self._activate_dps
  127. if dps:
  128. switch_to = not dps.get_value(self._device)
  129. await dps.async_set_value(self._device, switch_to)
  130. async def async_start(self):
  131. if self._activate_dps:
  132. await self._activate_dps.async_set_value(self._device, True)
  133. else:
  134. dps = self._command_dps or self._status_dps
  135. if "start" in dps.values(self._device):
  136. await dps.async_set_value(self._device, "start")
  137. async def async_pause(self):
  138. """Pause the vacuum cleaner."""
  139. if self._activate_dps:
  140. await self._activate_dps.async_set_value(self._device, False)
  141. else:
  142. dps = self._command_dps or self._status_dps
  143. if "pause" in dps.values(self._device):
  144. await dps.async_set_value(self._device, "pause")
  145. async def async_return_to_base(self, **kwargs):
  146. """Tell the vacuum cleaner to return to its base."""
  147. dps = self._command_dps or self._status_dps
  148. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  149. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  150. async def async_clean_spot(self, **kwargs):
  151. """Tell the vacuum cleaner do a spot clean."""
  152. dps = self._command_dps or self._status_dps
  153. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  154. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  155. async def async_stop(self, **kwargs):
  156. """Tell the vacuum cleaner to stop."""
  157. dps = self._command_dps or self._status_dps
  158. if dps and SERVICE_STOP in dps.values(self._device):
  159. await dps.async_set_value(self._device, SERVICE_STOP)
  160. async def async_locate(self, **kwargs):
  161. """Locate the vacuum cleaner."""
  162. if self._locate_dps:
  163. await self._locate_dps.async_set_value(self._device, True)
  164. async def async_send_command(self, command, params=None, **kwargs):
  165. """Send a command to the vacuum cleaner."""
  166. dps = self._command_dps or self._status_dps
  167. if command in dps.values(self._device):
  168. await dps.async_set_value(self._device, command)
  169. elif self._direction_dps and command in self._direction_dps.values(
  170. self._device
  171. ):
  172. await self._direction_dps.async_set_value(self._device, command)
  173. @property
  174. def fan_speed_list(self):
  175. """Return the list of fan speeds supported"""
  176. if self._fan_dps:
  177. return self._fan_dps.values(self._device)
  178. @property
  179. def fan_speed(self):
  180. """Return the current fan speed"""
  181. if self._fan_dps:
  182. return self._fan_dps.get_value(self._device)
  183. async def async_set_fan_speed(self, fan_speed, **kwargs):
  184. """Set the fan speed of the vacuum."""
  185. if self._fan_dps:
  186. await self._fan_dps.async_set_value(self._device, fan_speed)