vacuum.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. STATE_CLEANING,
  8. STATE_DOCKED,
  9. STATE_RETURNING,
  10. STATE_ERROR,
  11. StateVacuumEntity,
  12. VacuumEntityFeature,
  13. )
  14. from .device import TuyaLocalDevice
  15. from .helpers.config import async_tuya_setup_platform
  16. from .helpers.device_config import TuyaEntityConfig
  17. from .helpers.mixin import TuyaLocalEntity
  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. dps_map = self._init_begin(device, config)
  37. self._status_dps = dps_map.get("status")
  38. self._command_dps = dps_map.get("command")
  39. self._locate_dps = dps_map.get("locate")
  40. self._power_dps = dps_map.get("power")
  41. self._active_dps = dps_map.get("activate")
  42. self._battery_dps = dps_map.pop("battery", None)
  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.name} 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._battery_dps:
  58. support |= VacuumEntityFeature.BATTERY
  59. if self._fan_dps:
  60. support |= VacuumEntityFeature.FAN_SPEED
  61. if self._power_dps:
  62. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  63. if self._locate_dps:
  64. support |= VacuumEntityFeature.LOCATE
  65. cmd_dps = self._command_dps or self._status_dps
  66. cmd_support = cmd_dps.values(self._device)
  67. if SERVICE_RETURN_TO_BASE in cmd_support:
  68. support |= VacuumEntityFeature.RETURN_HOME
  69. if SERVICE_CLEAN_SPOT in cmd_support:
  70. support |= VacuumEntityFeature.CLEAN_SPOT
  71. if self._active_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 battery_level(self):
  81. """Return the battery level of the vacuum cleaner."""
  82. if self._battery_dps:
  83. return self._battery_dps.get_value(self._device)
  84. @property
  85. def status(self):
  86. """Return the status of the vacuum cleaner."""
  87. return self._status_dps.get_value(self._device)
  88. @property
  89. def state(self):
  90. """Return the state of the vacuum cleaner."""
  91. status = self.status
  92. if self._error_dps and self._error_dps.get_value(self._device) != 0:
  93. return STATE_ERROR
  94. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  95. return STATE_RETURNING
  96. elif status in ["standby", "charging"]:
  97. return STATE_DOCKED
  98. elif self._power_dps and not self._power_dps.get_value(self._device):
  99. return STATE_DOCKED
  100. elif self._active_dps and not self._active_dps.get_value(self._device):
  101. return STATE_DOCKED
  102. else:
  103. return STATE_CLEANING
  104. async def async_turn_on(self, **kwargs):
  105. """Turn on the vacuum cleaner."""
  106. if self._power_dps:
  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. await self._power_dps.async_set_value(self._device, False)
  112. async def async_toggle(self, **kwargs):
  113. """Toggle the vacuum cleaner."""
  114. dps = self._power_dps
  115. if not dps:
  116. dps = self._activate_dps
  117. if dps:
  118. switch_to = not dps.get_value(self._device)
  119. await dps.async_set_value(self._device, switch_to)
  120. async def async_start(self):
  121. if self._active_dps:
  122. await self._active_dps.async_set_value(self._device, True)
  123. else:
  124. dps = self._command_dps or self._status_dps
  125. if "start" in dps.values(self._device):
  126. await dps.async_set_value(self._device, "start")
  127. async def async_pause(self):
  128. """Pause the vacuum cleaner."""
  129. if self._active_dps:
  130. await self._active_dps.async_set_value(self._device, False)
  131. else:
  132. dps = self._command_dps or self._status_dps
  133. if "pause" in dps.values(self._device):
  134. await dps.async_set_value(self._device, "pause")
  135. async def async_return_to_base(self, **kwargs):
  136. """Tell the vacuum cleaner to return to its base."""
  137. dps = self._command_dps or self._status_dps
  138. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  139. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  140. async def async_clean_spot(self, **kwargs):
  141. """Tell the vacuum cleaner do a spot clean."""
  142. dps = self._command_dps or self._status_dps
  143. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  144. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  145. async def async_locate(self, **kwargs):
  146. """Locate the vacuum cleaner."""
  147. if self._locate_dps:
  148. await self._locate_dps.async_set_value(self._device, True)
  149. async def async_send_command(self, command, params=None, **kwargs):
  150. """Send a command to the vacuum cleaner."""
  151. dps = self._command_dps or self._status_dps
  152. if command in dps.values(self._device):
  153. await dps.async_set_value(self._device, command)
  154. elif self._direction_dps and command in self._direction_dps.values(
  155. self._device
  156. ):
  157. await self._direction_dps.async_set_value(self._device, command)
  158. @property
  159. def fan_speed_list(self):
  160. """Return the list of fan speeds supported"""
  161. if self._fan_dps:
  162. return self._fan_dps.values(self._device)
  163. @property
  164. def fan_speed(self):
  165. """Return the current fan speed"""
  166. if self._fan_dps:
  167. return self._fan_dps.get_value(self._device)
  168. async def async_set_fan_speed(self, fan_speed, **kwargs):
  169. """Set the fan speed of the vacuum."""
  170. if self._fan_dps:
  171. await self._fan_dps.async_set_value(self._device, fan_speed)