vacuum.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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._active_dps:
  64. support |= VacuumEntityFeature.START | VacuumEntityFeature.PAUSE
  65. if self._locate_dps:
  66. support |= VacuumEntityFeature.LOCATE
  67. cmd_dps = self._command_dps or self._status_dps
  68. cmd_support = cmd_dps.values(self._device)
  69. if SERVICE_RETURN_TO_BASE in cmd_support:
  70. support |= VacuumEntityFeature.RETURN_HOME
  71. if SERVICE_CLEAN_SPOT in cmd_support:
  72. support |= VacuumEntityFeature.CLEAN_SPOT
  73. return support
  74. @property
  75. def battery_level(self):
  76. """Return the battery level of the vacuum cleaner."""
  77. if self._battery_dps:
  78. return self._battery_dps.get_value(self._device)
  79. @property
  80. def status(self):
  81. """Return the status of the vacuum cleaner."""
  82. return self._status_dps.get_value(self._device)
  83. @property
  84. def state(self):
  85. """Return the state of the vacuum cleaner."""
  86. status = self.status
  87. if self._error_dps and self._error_dps.get_value(self._device) != 0:
  88. return STATE_ERROR
  89. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  90. return STATE_RETURNING
  91. elif status in ["standby", "charging"]:
  92. return STATE_DOCKED
  93. elif self._power_dps and not self._power_dps.get_value(self._device):
  94. return STATE_DOCKED
  95. elif self._active_dps and not self._active_dps.get_value(self._device):
  96. return STATE_DOCKED
  97. else:
  98. return STATE_CLEANING
  99. async def async_turn_on(self, **kwargs):
  100. """Turn on the vacuum cleaner."""
  101. if self._power_dps:
  102. await self._power_dps.async_set_value(self._device, True)
  103. async def async_turn_off(self, **kwargs):
  104. """Turn off the vacuum cleaner."""
  105. if self._power_dps:
  106. await self._power_dps.async_set_value(self._device, False)
  107. async def async_toggle(self, **kwargs):
  108. """Toggle the vacuum cleaner."""
  109. dps = self._power_dps
  110. if not dps:
  111. dps = self._activate_dps
  112. if dps:
  113. switch_to = not dps.get_value(self._device)
  114. await dps.async_set_value(self._device, switch_to)
  115. async def async_start(self):
  116. if self._active_dps:
  117. await self._active_dps.async_set_value(self._device, True)
  118. async def async_pause(self):
  119. """Pause the vacuum cleaner."""
  120. if self._active_dps:
  121. await self._active_dps.async_set_value(self._device, False)
  122. async def async_return_to_base(self, **kwargs):
  123. """Tell the vacuum cleaner to return to its base."""
  124. dps = self._command_dps or self._status_dps
  125. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  126. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  127. async def async_clean_spot(self, **kwargs):
  128. """Tell the vacuum cleaner do a spot clean."""
  129. dps = self._command_dps or self._status_dps
  130. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  131. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  132. async def async_locate(self, **kwargs):
  133. """Locate the vacuum cleaner."""
  134. if self._locate_dps:
  135. await self._locate_dps.async_set_value(self._device, True)
  136. async def async_send_command(self, command, params=None, **kwargs):
  137. """Send a command to the vacuum cleaner."""
  138. dps = self._command_dps or self._status_dps
  139. if command in dps.values(self._device):
  140. await dps.async_set_value(self._device, command)
  141. elif self._direction_dps and command in self._direction_dps.values(
  142. self._device
  143. ):
  144. await self._direction_dps.async_set_value(self._device, command)
  145. @property
  146. def fan_speed_list(self):
  147. """Return the list of fan speeds supported"""
  148. if self._fan_dps:
  149. return self._fan_dps.values(self._device)
  150. @property
  151. def fan_speed(self):
  152. """Return the current fan speed"""
  153. if self._fan_dps:
  154. return self._fan_dps.get_value(self._device)
  155. async def async_set_fan_speed(self, fan_speed, **kwargs):
  156. """Set the fan speed of the vacuum."""
  157. if self._fan_dps:
  158. await self._fan_dps.async_set_value(self._device, fan_speed)