Home Assistant Unofficial Reference 2024.12.1
vacuum.py
Go to the documentation of this file.
1 """Support for SwitchBot vacuum."""
2 
3 from typing import Any
4 
5 from switchbot_api import Device, Remote, SwitchBotAPI, VacuumCommands
6 
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 homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import SwitchbotCloudData
22 from .const import (
23  DOMAIN,
24  VACUUM_FAN_SPEED_MAX,
25  VACUUM_FAN_SPEED_QUIET,
26  VACUUM_FAN_SPEED_STANDARD,
27  VACUUM_FAN_SPEED_STRONG,
28 )
29 from .coordinator import SwitchBotCoordinator
30 from .entity import SwitchBotCloudEntity
31 
32 
34  hass: HomeAssistant,
35  config: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up SwitchBot Cloud entry."""
39  data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
41  _async_make_entity(data.api, device, coordinator)
42  for device, coordinator in data.devices.vacuums
43  )
44 
45 
46 VACUUM_SWITCHBOT_STATE_TO_HA_STATE: dict[str, str] = {
47  "StandBy": STATE_IDLE,
48  "Clearing": STATE_CLEANING,
49  "Paused": STATE_PAUSED,
50  "GotoChargeBase": STATE_RETURNING,
51  "Charging": STATE_DOCKED,
52  "ChargeDone": STATE_DOCKED,
53  "Dormant": STATE_IDLE,
54  "InTrouble": STATE_ERROR,
55  "InRemoteControl": STATE_CLEANING,
56  "InDustCollecting": STATE_DOCKED,
57 }
58 
59 VACUUM_FAN_SPEED_TO_SWITCHBOT_FAN_SPEED: dict[str, str] = {
60  VACUUM_FAN_SPEED_QUIET: "0",
61  VACUUM_FAN_SPEED_STANDARD: "1",
62  VACUUM_FAN_SPEED_STRONG: "2",
63  VACUUM_FAN_SPEED_MAX: "3",
64 }
65 
66 
67 # https://github.com/OpenWonderLabs/SwitchBotAPI?tab=readme-ov-file#robot-vacuum-cleaner-s1-plus-1
69  """Representation of a SwitchBot vacuum."""
70 
71  _attr_supported_features: VacuumEntityFeature = (
72  VacuumEntityFeature.BATTERY
73  | VacuumEntityFeature.FAN_SPEED
74  | VacuumEntityFeature.PAUSE
75  | VacuumEntityFeature.RETURN_HOME
76  | VacuumEntityFeature.START
77  | VacuumEntityFeature.STATE
78  )
79 
80  _attr_name = None
81  _attr_fan_speed_list: list[str] = list(
82  VACUUM_FAN_SPEED_TO_SWITCHBOT_FAN_SPEED.keys()
83  )
84 
85  async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
86  """Set fan speed."""
87  self._attr_fan_speed_attr_fan_speed = fan_speed
88  if fan_speed in VACUUM_FAN_SPEED_TO_SWITCHBOT_FAN_SPEED:
89  await self.send_api_commandsend_api_command(
90  VacuumCommands.POW_LEVEL,
91  parameters=VACUUM_FAN_SPEED_TO_SWITCHBOT_FAN_SPEED[fan_speed],
92  )
93  self.async_write_ha_stateasync_write_ha_state()
94 
95  async def async_pause(self) -> None:
96  """Pause the cleaning task."""
97  await self.send_api_commandsend_api_command(VacuumCommands.STOP)
98 
99  async def async_return_to_base(self, **kwargs: Any) -> None:
100  """Set the vacuum cleaner to return to the dock."""
101  await self.send_api_commandsend_api_command(VacuumCommands.DOCK)
102 
103  async def async_start(self) -> None:
104  """Start or resume the cleaning task."""
105  await self.send_api_commandsend_api_command(VacuumCommands.START)
106 
107  @callback
108  def _handle_coordinator_update(self) -> None:
109  """Handle updated data from the coordinator."""
110  if not self.coordinator.data:
111  return
112 
113  self._attr_battery_level_attr_battery_level = self.coordinator.data.get("battery")
114  self._attr_available_attr_available = self.coordinator.data.get("onlineStatus") == "online"
115 
116  switchbot_state = str(self.coordinator.data.get("workingStatus"))
117  self._attr_state_attr_state = VACUUM_SWITCHBOT_STATE_TO_HA_STATE.get(switchbot_state)
118 
119  self.async_write_ha_stateasync_write_ha_state()
120 
121 
122 @callback
124  api: SwitchBotAPI, device: Device | Remote, coordinator: SwitchBotCoordinator
125 ) -> SwitchBotCloudVacuum:
126  """Make a SwitchBotCloudVacuum."""
127  return SwitchBotCloudVacuum(api, device, coordinator)
None send_api_command(self, Commands command, str command_type="command", dict|str parameters="default")
Definition: entity.py:43
None async_set_fan_speed(self, str fan_speed, **Any kwargs)
Definition: vacuum.py:85
SwitchBotCloudVacuum _async_make_entity(SwitchBotAPI api, Device|Remote device, SwitchBotCoordinator coordinator)
Definition: vacuum.py:125
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Definition: vacuum.py:37