vacuum.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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._locate_dps = dps_map.get("locate")
  29. self._power_dps = dps_map.get("power")
  30. self._active_dps = dps_map.get("activate")
  31. self._battery_dps = dps_map.pop("battery", None)
  32. self._direction_dps = dps_map.get("direction_control")
  33. self._error_dps = dps_map.get("error")
  34. self._fan_dps = dps_map.pop("fan_speed", None)
  35. if self._status_dps is None:
  36. raise AttributeError(f"{config.name} is missing a status dps")
  37. self._init_end(dps_map)
  38. @property
  39. def supported_features(self):
  40. """Return the features supported by this vacuum cleaner."""
  41. support = (
  42. VacuumEntityFeature.STATE
  43. | VacuumEntityFeature.STATUS
  44. | VacuumEntityFeature.SEND_COMMAND
  45. )
  46. if self._battery_dps:
  47. support |= VacuumEntityFeature.BATTERY
  48. if self._fan_dps:
  49. support |= VacuumEntityFeature.FAN_SPEED
  50. if self._power_dps:
  51. support |= VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
  52. if self._active_dps:
  53. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  54. if self._locate_dps:
  55. support |= VacuumEntityFeature.LOCATE
  56. status_support = self._status_dps.values(self._device)
  57. if SERVICE_RETURN_TO_BASE in status_support:
  58. support |= VacuumEntityFeature.RETURN_HOME
  59. if SERVICE_CLEAN_SPOT in status_support:
  60. support |= VacuumEntityFeature.CLEAN_SPOT
  61. return support
  62. @property
  63. def battery_level(self):
  64. """Return the battery level of the vacuum cleaner."""
  65. if self._battery_dps:
  66. return self._battery_dps.get_value(self._device)
  67. @property
  68. def status(self):
  69. """Return the status of the vacuum cleaner."""
  70. return self._status_dps.get_value(self._device)
  71. @property
  72. def state(self):
  73. """Return the state of the vacuum cleaner."""
  74. status = self._status_dps.get_value(self._device)
  75. if self._error_dps and self._error_dps.get_value(self._device) != 0:
  76. return STATE_ERROR
  77. elif status == SERVICE_RETURN_TO_BASE:
  78. return STATE_RETURNING
  79. elif status == "standby":
  80. return STATE_DOCKED
  81. elif self._power_dps and not self._power_dps.get_value(self._device):
  82. return STATE_DOCKED
  83. elif self._active_dps and not self._active_dps.get_value(self._device):
  84. return STATE_DOCKED
  85. else:
  86. return STATE_CLEANING
  87. async def async_turn_on(self, **kwargs):
  88. """Turn on the vacuum cleaner."""
  89. if self._power_dps:
  90. await self._power_dps.async_set_value(self._device, True)
  91. async def async_turn_off(self, **kwargs):
  92. """Turn off the vacuum cleaner."""
  93. if self._power_dps:
  94. await self._power_dps.async_set_value(self._device, False)
  95. async def async_toggle(self, **kwargs):
  96. """Toggle the vacuum cleaner."""
  97. dps = self._power_dps
  98. if not dps:
  99. dps = self._activate_dps
  100. if dps:
  101. switch_to = not dps.get_value(self._device)
  102. await dps.async_set_value(self._device, switch_to)
  103. async def async_start(self):
  104. if self._active_dps:
  105. await self._active_dps.async_set_value(self._device, True)
  106. async def async_pause(self):
  107. """Pause the vacuum cleaner."""
  108. if self._active_dps:
  109. await self._active_dps.async_set_value(self._device, False)
  110. async def async_return_to_base(self, **kwargs):
  111. """Tell the vacuum cleaner to return to its base."""
  112. if self._status_dps and SERVICE_RETURN_TO_BASE in self._status_dps.values(
  113. self._device
  114. ):
  115. await self._status_dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  116. async def async_clean_spot(self, **kwargs):
  117. """Tell the vacuum cleaner do a spot clean."""
  118. if self._status_dps and SERVICE_CLEAN_SPOT in self._status_dps.values(
  119. self._device
  120. ):
  121. await self._status_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. if command in self._status_dps.values(self._device):
  129. await self._status_dps.async_set_value(self._device, command)
  130. elif self._direction_dps and command in self._direction_dps.values(
  131. self._device
  132. ):
  133. await self._direction_dps.async_set_value(self._device, command)
  134. @property
  135. def fan_speed_list(self):
  136. """Return the list of fan speeds supported"""
  137. if self._fan_dps:
  138. return self._fan_dps.values(self._device)
  139. @property
  140. def fan_speed(self):
  141. """Return the current fan speed"""
  142. if self._fan_dps:
  143. return self._fan_dps.get_value(self._device)
  144. async def async_set_fan_speed(self, fan_speed, **kwargs):
  145. """Set the fan speed of the vacuum."""
  146. if self._fan_dps:
  147. await self._fan_dps.async_set_value(self._device, fan_speed)