Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Roborock device base class."""
2 
3 from typing import Any
4 
5 from roborock.api import RoborockClient
6 from roborock.command_cache import CacheableAttribute
7 from roborock.containers import Consumable, Status
8 from roborock.exceptions import RoborockException
9 from roborock.roborock_message import RoborockDataProtocol
10 from roborock.roborock_typing import RoborockCommand
11 from roborock.version_1_apis.roborock_client_v1 import AttributeCache, RoborockClientV1
12 from roborock.version_1_apis.roborock_mqtt_client_v1 import RoborockMqttClientV1
13 from roborock.version_a01_apis import RoborockClientA01
14 
15 from homeassistant.exceptions import HomeAssistantError
16 from homeassistant.helpers.device_registry import DeviceInfo
17 from homeassistant.helpers.entity import Entity
18 from homeassistant.helpers.update_coordinator import CoordinatorEntity
19 
20 from .const import DOMAIN
21 from .coordinator import RoborockDataUpdateCoordinator, RoborockDataUpdateCoordinatorA01
22 
23 
25  """Representation of a base Roborock Entity."""
26 
27  _attr_has_entity_name = True
28 
29  def __init__(
30  self,
31  unique_id: str,
32  device_info: DeviceInfo,
33  api: RoborockClient,
34  ) -> None:
35  """Initialize the Roborock Device."""
36  self._attr_unique_id_attr_unique_id = unique_id
37  self._attr_device_info_attr_device_info = device_info
38  self._api_api = api
39 
40 
42  """Representation of a base Roborock V1 Entity."""
43 
44  _api: RoborockClientV1
45 
46  def __init__(
47  self, unique_id: str, device_info: DeviceInfo, api: RoborockClientV1
48  ) -> None:
49  """Initialize the Roborock Device."""
50  super().__init__(unique_id, device_info, api)
51 
52  def get_cache(self, attribute: CacheableAttribute) -> AttributeCache:
53  """Get an item from the api cache."""
54  return self._api_api.cache[attribute]
55 
56  async def send(
57  self,
58  command: RoborockCommand | str,
59  params: dict[str, Any] | list[Any] | int | None = None,
60  ) -> dict:
61  """Send a command to a vacuum cleaner."""
62  try:
63  response: dict = await self._api_api.send_command(command, params)
64  except RoborockException as err:
65  if isinstance(command, RoborockCommand):
66  command_name = command.name
67  else:
68  command_name = command
69  raise HomeAssistantError(
70  translation_domain=DOMAIN,
71  translation_key="command_failed",
72  translation_placeholders={
73  "command": command_name,
74  },
75  ) from err
76  return response
77 
78  @property
79  def api(self) -> RoborockClientV1:
80  """Returns the api."""
81  return self._api_api
82 
83 
85  """Representation of a base Roborock Entity for A01 devices."""
86 
87  _api: RoborockClientA01
88 
89  def __init__(
90  self, unique_id: str, device_info: DeviceInfo, api: RoborockClientA01
91  ) -> None:
92  """Initialize the Roborock Device."""
93  super().__init__(unique_id, device_info, api)
94 
95 
97  RoborockEntityV1, CoordinatorEntity[RoborockDataUpdateCoordinator]
98 ):
99  """Representation of a base a coordinated Roborock Entity."""
100 
101  _attr_has_entity_name = True
102 
103  def __init__(
104  self,
105  unique_id: str,
106  coordinator: RoborockDataUpdateCoordinator,
107  listener_request: list[RoborockDataProtocol]
108  | RoborockDataProtocol
109  | None = None,
110  ) -> None:
111  """Initialize the coordinated Roborock Device."""
112  RoborockEntityV1.__init__(
113  self,
114  unique_id=unique_id,
115  device_info=coordinator.device_info,
116  api=coordinator.api,
117  )
118  CoordinatorEntity.__init__(self, coordinator=coordinator)
119  self._attr_unique_id_attr_unique_id_attr_unique_id = unique_id
120  if isinstance(listener_request, RoborockDataProtocol):
121  listener_request = [listener_request]
122  self.listener_requestslistener_requests = listener_request or []
123 
124  async def async_added_to_hass(self) -> None:
125  """Add listeners when the device is added to hass."""
126  await super().async_added_to_hass()
127  for listener_request in self.listener_requestslistener_requests:
128  self.apiapiapi.add_listener(
129  listener_request, self._update_from_listener_update_from_listener, cache=self.apiapiapi.cache
130  )
131 
132  async def async_will_remove_from_hass(self) -> None:
133  """Remove listeners when the device is removed from hass."""
134  for listener_request in self.listener_requestslistener_requests:
135  self.apiapiapi.remove_listener(listener_request, self._update_from_listener_update_from_listener)
136  await super().async_will_remove_from_hass()
137 
138  @property
139  def _device_status(self) -> Status:
140  """Return the status of the device."""
141  data = self.coordinator.data
142  return data.status
143 
144  @property
145  def cloud_api(self) -> RoborockMqttClientV1:
146  """Return the cloud api."""
147  return self.coordinator.cloud_api
148 
149  async def send(
150  self,
151  command: RoborockCommand | str,
152  params: dict[str, Any] | list[Any] | int | None = None,
153  ) -> dict:
154  """Overloads normal send command but refreshes coordinator."""
155  res = await super().send(command, params)
156  await self.coordinator.async_refresh()
157  return res
158 
159  def _update_from_listener(self, value: Status | Consumable) -> None:
160  """Update the status or consumable data from a listener and then write the new entity state."""
161  if isinstance(value, Status):
162  self.coordinator.roborock_device_info.props.status = value
163  else:
164  self.coordinator.roborock_device_info.props.consumable = value
165  self.coordinator.data = self.coordinator.roborock_device_info.props
166  self.schedule_update_ha_stateschedule_update_ha_state()
167 
168 
170  RoborockEntityA01, CoordinatorEntity[RoborockDataUpdateCoordinatorA01]
171 ):
172  """Representation of a base a coordinated Roborock Entity."""
173 
174  def __init__(
175  self,
176  unique_id: str,
177  coordinator: RoborockDataUpdateCoordinatorA01,
178  ) -> None:
179  """Initialize the coordinated Roborock Device."""
180  RoborockEntityA01.__init__(
181  self,
182  unique_id=unique_id,
183  device_info=coordinator.device_info,
184  api=coordinator.api,
185  )
186  CoordinatorEntity.__init__(self, coordinator=coordinator)
187  self._attr_unique_id_attr_unique_id_attr_unique_id = unique_id
None __init__(self, str unique_id, RoborockDataUpdateCoordinatorA01 coordinator)
Definition: entity.py:178
None __init__(self, str unique_id, RoborockDataUpdateCoordinator coordinator, list[RoborockDataProtocol]|RoborockDataProtocol|None listener_request=None)
Definition: entity.py:110
None _update_from_listener(self, Status|Consumable value)
Definition: entity.py:159
dict send(self, RoborockCommand|str command, dict[str, Any]|list[Any]|int|None params=None)
Definition: entity.py:153
None __init__(self, str unique_id, DeviceInfo device_info, RoborockClientA01 api)
Definition: entity.py:91
None __init__(self, str unique_id, DeviceInfo device_info, RoborockClientV1 api)
Definition: entity.py:48
AttributeCache get_cache(self, CacheableAttribute attribute)
Definition: entity.py:52
dict send(self, RoborockCommand|str command, dict[str, Any]|list[Any]|int|None params=None)
Definition: entity.py:60
None __init__(self, str unique_id, DeviceInfo device_info, RoborockClient api)
Definition: entity.py:34
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244