vacuum.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_ERROR,
  11. STATE_IDLE,
  12. STATE_PAUSED,
  13. STATE_RETURNING,
  14. StateVacuumEntity,
  15. VacuumEntityFeature,
  16. )
  17. from .device import TuyaLocalDevice
  18. from .helpers.config import async_tuya_setup_platform
  19. from .helpers.device_config import TuyaEntityConfig
  20. from .helpers.mixin import TuyaLocalEntity
  21. async def async_setup_entry(hass, config_entry, async_add_entities):
  22. config = {**config_entry.data, **config_entry.options}
  23. await async_tuya_setup_platform(
  24. hass,
  25. async_add_entities,
  26. config,
  27. "vacuum",
  28. TuyaLocalVacuum,
  29. )
  30. class TuyaLocalVacuum(TuyaLocalEntity, StateVacuumEntity):
  31. """Representation of a Tuya Vacuum Cleaner"""
  32. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  33. """
  34. Initialise the sensor.
  35. Args:
  36. device (TuyaLocalDevice): the device API instance.
  37. config (TuyaEntityConfig): the configuration for this entity
  38. """
  39. super().__init__()
  40. dps_map = self._init_begin(device, config)
  41. self._status_dps = dps_map.get("status")
  42. self._command_dps = dps_map.get("command")
  43. self._locate_dps = dps_map.get("locate")
  44. self._power_dps = dps_map.get("power")
  45. self._activate_dps = dps_map.get("activate")
  46. self._direction_dps = dps_map.get("direction_control")
  47. self._error_dps = dps_map.get("error")
  48. self._fan_dps = dps_map.pop("fan_speed", None)
  49. if self._status_dps is None:
  50. raise AttributeError(f"{config.config_id} is missing a status dps")
  51. self._init_end(dps_map)
  52. @property
  53. def supported_features(self):
  54. """Return the features supported by this vacuum cleaner."""
  55. support = (
  56. VacuumEntityFeature.STATE
  57. | VacuumEntityFeature.STATUS
  58. | VacuumEntityFeature.SEND_COMMAND
  59. )
  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._activate_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 status(self):
  84. """Return the status of the vacuum cleaner."""
  85. return self._status_dps.get_value(self._device)
  86. @property
  87. def state(self):
  88. """Return the state of the vacuum cleaner."""
  89. status = self.status
  90. if self._error_dps and self._error_dps.get_value(self._device):
  91. return STATE_ERROR
  92. elif status in [SERVICE_RETURN_TO_BASE, "returning"]:
  93. return STATE_RETURNING
  94. elif status in ["standby", "sleep"]:
  95. return STATE_IDLE
  96. elif status == "paused":
  97. return STATE_PAUSED
  98. elif status in ["charging", "charged"]:
  99. return STATE_DOCKED
  100. elif self._power_dps and self._power_dps.get_value(self._device) is False:
  101. return STATE_IDLE
  102. elif self._activate_dps and self._activate_dps.get_value(self._device) is False:
  103. return STATE_PAUSED
  104. else:
  105. return STATE_CLEANING
  106. async def async_turn_on(self, **kwargs):
  107. """Turn on the vacuum cleaner."""
  108. if self._power_dps:
  109. await self._power_dps.async_set_value(self._device, True)
  110. async def async_turn_off(self, **kwargs):
  111. """Turn off the vacuum cleaner."""
  112. if self._power_dps:
  113. await self._power_dps.async_set_value(self._device, False)
  114. async def async_toggle(self, **kwargs):
  115. """Toggle the vacuum cleaner."""
  116. dps = self._power_dps or 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. dps = self._command_dps or self._status_dps
  122. if dps and "start" in dps.values(self._device):
  123. await dps.async_set_value(self._device, "start")
  124. elif self._activate_dps:
  125. await self._activate_dps.async_set_value(self._device, True)
  126. async def async_pause(self):
  127. """Pause the vacuum cleaner."""
  128. dps = self._command_dps or self._status_dps
  129. if dps and "pause" in dps.values(self._device):
  130. await dps.async_set_value(self._device, "pause")
  131. elif self._activate_dps:
  132. await self._activate_dps.async_set_value(self._device, False)
  133. async def async_return_to_base(self, **kwargs):
  134. """Tell the vacuum cleaner to return to its base."""
  135. dps = self._command_dps or self._status_dps
  136. if dps and SERVICE_RETURN_TO_BASE in dps.values(self._device):
  137. await dps.async_set_value(self._device, SERVICE_RETURN_TO_BASE)
  138. async def async_clean_spot(self, **kwargs):
  139. """Tell the vacuum cleaner do a spot clean."""
  140. dps = self._command_dps or self._status_dps
  141. if dps and SERVICE_CLEAN_SPOT in dps.values(self._device):
  142. await dps.async_set_value(self._device, SERVICE_CLEAN_SPOT)
  143. async def async_stop(self, **kwargs):
  144. """Tell the vacuum cleaner to stop."""
  145. dps = self._command_dps or self._status_dps
  146. if dps and SERVICE_STOP in dps.values(self._device):
  147. await dps.async_set_value(self._device, SERVICE_STOP)
  148. async def async_locate(self, **kwargs):
  149. """Locate the vacuum cleaner."""
  150. if self._locate_dps:
  151. await self._locate_dps.async_set_value(self._device, True)
  152. async def async_send_command(self, command, params=None, **kwargs):
  153. """Send a command to the vacuum cleaner."""
  154. dps = self._command_dps or self._status_dps
  155. if command in dps.values(self._device):
  156. await dps.async_set_value(self._device, command)
  157. elif self._direction_dps and command in self._direction_dps.values(
  158. self._device
  159. ):
  160. await self._direction_dps.async_set_value(self._device, command)
  161. @property
  162. def fan_speed_list(self):
  163. """Return the list of fan speeds supported"""
  164. if self._fan_dps:
  165. return self._fan_dps.values(self._device)
  166. @property
  167. def fan_speed(self):
  168. """Return the current fan speed"""
  169. if self._fan_dps:
  170. return self._fan_dps.get_value(self._device)
  171. async def async_set_fan_speed(self, fan_speed, **kwargs):
  172. """Set the fan speed of the vacuum."""
  173. if self._fan_dps:
  174. await self._fan_dps.async_set_value(self._device, fan_speed)