vacuum.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """
  2. Platform to control Tuya robot vacuums.
  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.device_config import TuyaEntityConfig
  16. from ..helpers.mixin import TuyaLocalEntity
  17. class TuyaLocalVacuum(TuyaLocalEntity, StateVacuumEntity):
  18. """Representation of a Tuya Vacuum Cleaner"""
  19. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  20. """
  21. Initialise the sensor.
  22. Args:
  23. device (TuyaLocalDevice): the device API instance.
  24. config (TuyaEntityConfig): the configuration for this entity
  25. """
  26. dps_map = self._init_begin(device, config)
  27. self._status_dps = dps_map.get("status")
  28. self._command_dps = dps_map.get("command")
  29. self._locate_dps = dps_map.get("locate")
  30. self._power_dps = dps_map.get("power")
  31. self._active_dps = dps_map.get("activate")
  32. self._battery_dps = dps_map.pop("battery", None)
  33. self._direction_dps = dps_map.get("direction_control")
  34. self._error_dps = dps_map.get("error")
  35. self._fan_dps = dps_map.pop("fan_speed", None)
  36. if self._status_dps is None:
  37. raise AttributeError(f"{config.name} is missing a status dps")
  38. self._init_end(dps_map)
  39. @property
  40. def supported_features(self):
  41. """Return the features supported by this vacuum cleaner."""
  42. support = (
  43. VacuumEntityFeature.STATE
  44. | VacuumEntityFeature.STATUS
  45. | VacuumEntityFeature.SEND_COMMAND
  46. )
  47. if self._battery_dps:
  48. support |= VacuumEntityFeature.BATTERY
  49. if self._fan_dps:
  50. support |= VacuumEntityFeature.FAN_SPEED
  51. if self._power_dps:
  52. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  53. if self._active_dps:
  54. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  55. if self._locate_dps:
  56. support |= VacuumEntityFeature.LOCATE
  57. cmd_dps = self._command_dps or self._status_dps
  58. cmd_support = cmd_dps.values(self._device)
  59. if SERVICE_RETURN_TO_BASE in cmd_support:
  60. support |= VacuumEntityFeature.RETURN_HOME
  61. if SERVICE_CLEAN_SPOT in cmd_support:
  62. support |= VacuumEntityFeature.CLEAN_SPOT
  63. return support
  64. @property
  65. def battery_level(self):
  66. """Return the battery level of the vacuum cleaner."""
  67. if self._battery_dps:
  68. return self._battery_dps.get_value(self._device)
  69. @property
  70. def status(self):
  71. """Return the status of the vacuum cleaner."""
  72. return self._status_dps.get_value(self._device)
  73. @property
  74. def state(self):
  75. """Return the state of the vacuum cleaner."""
  76. status = self.status
  77. if self._error_dps and self._error_dps.get_value(self._device) != 0:
  78. return STATE_ERROR
  79. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  80. return STATE_RETURNING
  81. elif status in ["standby", "charging"]:
  82. return STATE_DOCKED
  83. elif self._power_dps and not self._power_dps.get_value(self._device):
  84. return STATE_DOCKED
  85. elif self._active_dps and not self._active_dps.get_value(self._device):
  86. return STATE_DOCKED
  87. else:
  88. return STATE_CLEANING
  89. async def async_turn_on(self, **kwargs):
  90. """Turn on the vacuum cleaner."""
  91. if self._power_dps:
  92. await self._power_dps.async_set_value(self._device, True)
  93. async def async_turn_off(self, **kwargs):
  94. """Turn off the vacuum cleaner."""
  95. if self._power_dps:
  96. await self._power_dps.async_set_value(self._device, False)
  97. async def async_toggle(self, **kwargs):
  98. """Toggle the vacuum cleaner."""
  99. dps = self._power_dps
  100. if not dps:
  101. dps = self._activate_dps
  102. if dps:
  103. switch_to = not dps.get_value(self._device)
  104. await dps.async_set_value(self._device, switch_to)
  105. async def async_start(self):
  106. if self._active_dps:
  107. await self._active_dps.async_set_value(self._device, True)
  108. async def async_pause(self):
  109. """Pause the vacuum cleaner."""
  110. if self._active_dps:
  111. await self._active_dps.async_set_value(self._device, False)
  112. async def async_return_to_base(self, **kwargs):
  113. """Tell the vacuum cleaner to return to its base."""
  114. dps = self._command_dps or self._status_dps
  115. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  116. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  117. async def async_clean_spot(self, **kwargs):
  118. """Tell the vacuum cleaner do a spot clean."""
  119. dps = self._command_dps or self._status_dps
  120. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  121. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  122. async def async_locate(self, **kwargs):
  123. """Locate the vacuum cleaner."""
  124. if self._locate_dps:
  125. await self._locate_dps.async_set_value(self._device, True)
  126. async def async_send_command(self, command, params=None, **kwargs):
  127. """Send a command to the vacuum cleaner."""
  128. dps = self._command_dps or self._status_dps
  129. if command in dps.values(self._device):
  130. await dps.async_set_value(self._device, command)
  131. elif self._direction_dps and command in self._direction_dps.values(
  132. self._device
  133. ):
  134. await self._direction_dps.async_set_value(self._device, command)
  135. @property
  136. def fan_speed_list(self):
  137. """Return the list of fan speeds supported"""
  138. if self._fan_dps:
  139. return self._fan_dps.values(self._device)
  140. @property
  141. def fan_speed(self):
  142. """Return the current fan speed"""
  143. if self._fan_dps:
  144. return self._fan_dps.get_value(self._device)
  145. async def async_set_fan_speed(self, fan_speed, **kwargs):
  146. """Set the fan speed of the vacuum."""
  147. if self._fan_dps:
  148. await self._fan_dps.async_set_value(self._device, fan_speed)