vacuum.py 7.7 KB

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