pymammotion 0.0.37__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pymammotion might be problematic. Click here for more details.

Files changed (106) hide show
  1. pymammotion/__init__.py +43 -0
  2. pymammotion/aliyun/cloud_gateway.py +549 -0
  3. pymammotion/aliyun/cloud_service.py +65 -0
  4. pymammotion/aliyun/dataclass/aep_response.py +18 -0
  5. pymammotion/aliyun/dataclass/connect_response.py +51 -0
  6. pymammotion/aliyun/dataclass/dev_by_account_response.py +43 -0
  7. pymammotion/aliyun/dataclass/login_by_oauth_response.py +65 -0
  8. pymammotion/aliyun/dataclass/regions_response.py +26 -0
  9. pymammotion/aliyun/dataclass/session_by_authcode_response.py +18 -0
  10. pymammotion/aliyun/tmp_constant.py +175 -0
  11. pymammotion/bluetooth/__init__.py +1 -0
  12. pymammotion/bluetooth/ble.py +74 -0
  13. pymammotion/bluetooth/ble_message.py +430 -0
  14. pymammotion/bluetooth/const.py +27 -0
  15. pymammotion/bluetooth/data/__init__.py +0 -0
  16. pymammotion/bluetooth/data/convert.py +26 -0
  17. pymammotion/bluetooth/data/framectrldata.py +40 -0
  18. pymammotion/bluetooth/data/notifydata.py +63 -0
  19. pymammotion/const.py +9 -0
  20. pymammotion/data/__init__.py +0 -0
  21. pymammotion/data/model/__init__.py +8 -0
  22. pymammotion/data/model/device.py +157 -0
  23. pymammotion/data/model/enums.py +67 -0
  24. pymammotion/data/model/excute_boarder_params.py +48 -0
  25. pymammotion/data/model/execute_boarder.py +36 -0
  26. pymammotion/data/model/generate_route_information.py +133 -0
  27. pymammotion/data/model/hash_list.py +17 -0
  28. pymammotion/data/model/mowing_modes.py +37 -0
  29. pymammotion/data/model/plan.py +58 -0
  30. pymammotion/data/model/rapid_state.py +45 -0
  31. pymammotion/data/model/region_data.py +99 -0
  32. pymammotion/data/mqtt/__init__.py +1 -0
  33. pymammotion/data/mqtt/event.py +90 -0
  34. pymammotion/data/mqtt/properties.py +140 -0
  35. pymammotion/data/mqtt/status.py +52 -0
  36. pymammotion/event/__init__.py +6 -0
  37. pymammotion/event/event.py +50 -0
  38. pymammotion/http/_init_.py +0 -0
  39. pymammotion/http/http.py +76 -0
  40. pymammotion/luba/_init_.py +0 -0
  41. pymammotion/luba/base.py +52 -0
  42. pymammotion/mammotion/__init__.py +0 -0
  43. pymammotion/mammotion/commands/__init__.py +0 -0
  44. pymammotion/mammotion/commands/abstract_message.py +7 -0
  45. pymammotion/mammotion/commands/mammotion_command.py +34 -0
  46. pymammotion/mammotion/commands/messages/__init__.py +0 -0
  47. pymammotion/mammotion/commands/messages/driver.py +108 -0
  48. pymammotion/mammotion/commands/messages/media.py +36 -0
  49. pymammotion/mammotion/commands/messages/navigation.py +535 -0
  50. pymammotion/mammotion/commands/messages/network.py +236 -0
  51. pymammotion/mammotion/commands/messages/ota.py +34 -0
  52. pymammotion/mammotion/commands/messages/system.py +266 -0
  53. pymammotion/mammotion/commands/messages/video.py +27 -0
  54. pymammotion/mammotion/control/__init__.py +0 -0
  55. pymammotion/mammotion/control/joystick.py +184 -0
  56. pymammotion/mammotion/devices/__init__.py +1 -0
  57. pymammotion/mammotion/devices/luba.py +564 -0
  58. pymammotion/mqtt/mqtt.py +230 -0
  59. pymammotion/proto/__init__.py +0 -0
  60. pymammotion/proto/common.proto +7 -0
  61. pymammotion/proto/common.py +12 -0
  62. pymammotion/proto/common_pb2.py +25 -0
  63. pymammotion/proto/common_pb2.pyi +13 -0
  64. pymammotion/proto/dev_net.proto +297 -0
  65. pymammotion/proto/dev_net.py +381 -0
  66. pymammotion/proto/dev_net_pb2.py +107 -0
  67. pymammotion/proto/dev_net_pb2.pyi +472 -0
  68. pymammotion/proto/luba_msg.proto +73 -0
  69. pymammotion/proto/luba_msg.py +80 -0
  70. pymammotion/proto/luba_msg_pb2.py +40 -0
  71. pymammotion/proto/luba_msg_pb2.pyi +93 -0
  72. pymammotion/proto/luba_mul.proto +68 -0
  73. pymammotion/proto/luba_mul.py +76 -0
  74. pymammotion/proto/luba_mul_pb2.py +45 -0
  75. pymammotion/proto/luba_mul_pb2.pyi +91 -0
  76. pymammotion/proto/mctrl_driver.proto +67 -0
  77. pymammotion/proto/mctrl_driver.py +100 -0
  78. pymammotion/proto/mctrl_driver_pb2.py +45 -0
  79. pymammotion/proto/mctrl_driver_pb2.pyi +112 -0
  80. pymammotion/proto/mctrl_nav.proto +485 -0
  81. pymammotion/proto/mctrl_nav.py +589 -0
  82. pymammotion/proto/mctrl_nav_pb2.py +116 -0
  83. pymammotion/proto/mctrl_nav_pb2.pyi +875 -0
  84. pymammotion/proto/mctrl_ota.proto +42 -0
  85. pymammotion/proto/mctrl_ota.py +48 -0
  86. pymammotion/proto/mctrl_ota_pb2.py +35 -0
  87. pymammotion/proto/mctrl_ota_pb2.pyi +65 -0
  88. pymammotion/proto/mctrl_pept.proto +29 -0
  89. pymammotion/proto/mctrl_pept.py +41 -0
  90. pymammotion/proto/mctrl_pept_pb2.py +31 -0
  91. pymammotion/proto/mctrl_pept_pb2.pyi +50 -0
  92. pymammotion/proto/mctrl_sys.proto +487 -0
  93. pymammotion/proto/mctrl_sys.py +574 -0
  94. pymammotion/proto/mctrl_sys_pb2.py +142 -0
  95. pymammotion/proto/mctrl_sys_pb2.pyi +787 -0
  96. pymammotion/py.typed +0 -0
  97. pymammotion/utility/constant/__init__.py +1 -0
  98. pymammotion/utility/constant/device_constant.py +238 -0
  99. pymammotion/utility/datatype_converter.py +80 -0
  100. pymammotion/utility/device_type.py +152 -0
  101. pymammotion/utility/periodic.py +41 -0
  102. pymammotion/utility/rocker_util.py +135 -0
  103. pymammotion-0.0.37.dist-info/LICENSE +674 -0
  104. pymammotion-0.0.37.dist-info/METADATA +92 -0
  105. pymammotion-0.0.37.dist-info/RECORD +106 -0
  106. pymammotion-0.0.37.dist-info/WHEEL +4 -0
@@ -0,0 +1,564 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import codecs
5
+ import logging
6
+ from abc import abstractmethod
7
+ from enum import Enum
8
+ from typing import Any
9
+ from uuid import UUID
10
+
11
+ import betterproto
12
+ from bleak import BleakClient
13
+ from bleak.backends.device import BLEDevice
14
+ from bleak.backends.service import BleakGATTCharacteristic, BleakGATTServiceCollection
15
+ from bleak.exc import BleakDBusError
16
+ from bleak_retry_connector import (
17
+ BLEAK_RETRY_EXCEPTIONS,
18
+ BleakClientWithServiceCache,
19
+ BleakNotFoundError,
20
+ establish_connection,
21
+ )
22
+
23
+ from pyluba.bluetooth import BleMessage
24
+ from pyluba.data.model.device import MowingDevice
25
+ from pyluba.mammotion.commands.mammotion_command import MammotionCommand
26
+ from pyluba.proto.dev_net import DevNet
27
+ from pyluba.proto.luba_msg import LubaMsg
28
+
29
+
30
+ class CharacteristicMissingError(Exception):
31
+ """Raised when a characteristic is missing."""
32
+
33
+
34
+ def _sb_uuid(comms_type: str = "service") -> UUID | str:
35
+ """Return Mammotion UUID."""
36
+
37
+ _uuid = {"tx": "ff01", "rx": "ff02", "service": "2A05"}
38
+
39
+ if comms_type in _uuid:
40
+ return UUID(f"0000{_uuid[comms_type]}-0000-1000-8000-00805f9b34fb")
41
+
42
+ return "Incorrect type, choose between: tx, rx or service"
43
+
44
+
45
+ READ_CHAR_UUID = _sb_uuid(comms_type="rx")
46
+ WRITE_CHAR_UUID = _sb_uuid(comms_type="tx")
47
+
48
+ DBUS_ERROR_BACKOFF_TIME = 0.25
49
+
50
+ DISCONNECT_DELAY = 10
51
+
52
+ _LOGGER = logging.getLogger(__name__)
53
+
54
+
55
+ def slashescape(err):
56
+ """Codecs error handler. err is UnicodeDecode instance. return
57
+ a tuple with a replacement for the unencodable part of the input
58
+ and a position where encoding should continue
59
+ """
60
+ # print err, dir(err), err.start, err.end, err.object[:err.start]
61
+ thebyte = err.object[err.start : err.end]
62
+ repl = "\\x" + hex(ord(thebyte))[2:]
63
+ return (repl, err.end)
64
+
65
+
66
+ codecs.register_error("slashescape", slashescape)
67
+
68
+
69
+ def _handle_timeout(fut: asyncio.Future[None]) -> None:
70
+ """Handle a timeout."""
71
+ if not fut.done():
72
+ fut.set_exception(asyncio.TimeoutError)
73
+
74
+
75
+ class ConnectionPreference(Enum):
76
+ EITHER = 0
77
+ WIFI = 1
78
+ BLUETOOTH = 2
79
+
80
+
81
+ class MammotionDevice:
82
+ _ble_device: MammotionBaseBLEDevice | None = None
83
+
84
+ def __init__(
85
+ self,
86
+ ble_device: BLEDevice,
87
+ preference: ConnectionPreference = ConnectionPreference.EITHER,
88
+ ) -> None:
89
+ if ble_device:
90
+ self._ble_device = MammotionBaseBLEDevice(ble_device)
91
+ self._preference = preference
92
+
93
+ async def send_command(self, key: str):
94
+ return await self._ble_device.command(key)
95
+
96
+
97
+ def has_field(message: betterproto.Message) -> bool:
98
+ return betterproto.serialized_on_wire(message)
99
+
100
+
101
+ class MammotionBaseDevice:
102
+ def __init__(self) -> None:
103
+ self.loop = asyncio.get_event_loop()
104
+ self._raw_data = LubaMsg().to_dict(casing=betterproto.Casing.SNAKE)
105
+ self._luba_msg = LubaMsg()
106
+ self._notify_future: asyncio.Future[bytes] | None = None
107
+
108
+ def _update_raw_data(self, data: bytes) -> None:
109
+ """Update raw and model data from notifications."""
110
+ # proto_luba = luba_msg_pb2.LubaMsg()
111
+ # proto_luba.ParseFromString(data)
112
+ tmp_msg = LubaMsg().parse(data)
113
+ res = betterproto.which_one_of(tmp_msg, "LubaSubMsg")
114
+ match res[0]:
115
+ case "nav":
116
+ nav_sub_msg = betterproto.which_one_of(tmp_msg.nav, "SubNavMsg")
117
+ nav = self._raw_data.get("nav")
118
+ if nav is None:
119
+ self._raw_data["nav"] = {}
120
+ if isinstance(nav_sub_msg[1], int):
121
+ self._raw_data["net"][nav_sub_msg[0]] = nav_sub_msg[1]
122
+ else:
123
+ self._raw_data["nav"][nav_sub_msg[0]] = nav_sub_msg[1].to_dict(
124
+ casing=betterproto.Casing.SNAKE
125
+ )
126
+ case "sys":
127
+ sys_sub_msg = betterproto.which_one_of(tmp_msg.sys, "SubSysMsg")
128
+ sys = self._raw_data.get("sys")
129
+ if sys is None:
130
+ self._raw_data["sys"] = {}
131
+ self._raw_data["sys"][sys_sub_msg[0]] = sys_sub_msg[1].to_dict(
132
+ casing=betterproto.Casing.SNAKE
133
+ )
134
+ case "driver":
135
+ drv_sub_msg = betterproto.which_one_of(tmp_msg.driver, "SubDrvMsg")
136
+ drv = self._raw_data.get("driver")
137
+ if drv is None:
138
+ self._raw_data["driver"] = {}
139
+ self._raw_data["driver"][drv_sub_msg[0]] = drv_sub_msg[1].to_dict(
140
+ casing=betterproto.Casing.SNAKE
141
+ )
142
+ case "net":
143
+ net_sub_msg = betterproto.which_one_of(tmp_msg.net, "NetSubType")
144
+ net = self._raw_data.get("net")
145
+ if net is None:
146
+ self._raw_data["net"] = {}
147
+ if isinstance(net_sub_msg[1], int):
148
+ self._raw_data["net"][net_sub_msg[0]] = net_sub_msg[1]
149
+ else:
150
+ self._raw_data["net"][net_sub_msg[0]] = net_sub_msg[1].to_dict(
151
+ casing=betterproto.Casing.SNAKE
152
+ )
153
+
154
+ case "mul":
155
+ mul_sub_msg = betterproto.which_one_of(tmp_msg.mul, "SubMul")
156
+ mul = self._raw_data.get("mul")
157
+ if mul is None:
158
+ self._raw_data["mul"] = {}
159
+ self._raw_data["mul"][mul_sub_msg[0]] = mul_sub_msg[1].to_dict(
160
+ casing=betterproto.Casing.SNAKE
161
+ )
162
+ case "ota":
163
+ ota_sub_msg = betterproto.which_one_of(tmp_msg.ota, "SubOtaMsg")
164
+ ota = self._raw_data.get("ota")
165
+ if ota is None:
166
+ self._raw_data["ota"] = {}
167
+ self._raw_data["ota"][ota_sub_msg[0]] = ota_sub_msg[1].to_dict(
168
+ casing=betterproto.Casing.SNAKE
169
+ )
170
+
171
+ self._luba_msg = MowingDevice.from_raw(self._raw_data)
172
+
173
+ @property
174
+ def raw_data(self) -> dict[str, Any]:
175
+ return self._raw_data
176
+
177
+ @property
178
+ def luba_msg(self) -> LubaMsg:
179
+ return self._luba_msg
180
+
181
+ @abstractmethod
182
+ async def _send_command(self, key: str, retry: int | None = None) -> bytes | None:
183
+ """Send command to device and read response."""
184
+
185
+ @abstractmethod
186
+ async def _send_command_with_args(self, key: str, **kwargs: any) -> bytes | None:
187
+ """Send command to device and read response."""
188
+
189
+ async def start_sync(self, retry: int):
190
+ await self._send_command("get_device_base_info", retry)
191
+ await self._send_command("get_report_cfg", retry)
192
+ await self._send_command_with_args("read_plan", sub_cmd=2, plan_index=0)
193
+
194
+
195
+ RW = await self._send_command_with_args(
196
+ "allpowerfull_rw", id=5, context=1, rw=1
197
+ )
198
+ # RW_proto = luba_msg_pb2.LubaMsg()
199
+ # RW_proto.ParseFromString(RW)
200
+ # print(json_format.MessageToDict(RW_proto))
201
+
202
+ async def command(self, key: str, **kwargs):
203
+ return await self._send_command_with_args(key, **kwargs)
204
+
205
+
206
+ class MammotionBaseBLEDevice(MammotionBaseDevice):
207
+ def __init__(self, device: BLEDevice, interface: int = 0, **kwargs: Any) -> None:
208
+ super().__init__()
209
+ self._interface = f"hci{interface}"
210
+ self._device = device
211
+ self._client: BleakClientWithServiceCache | None = None
212
+ self._read_char: BleakGATTCharacteristic | None = None
213
+ self._write_char: BleakGATTCharacteristic | None = None
214
+ self._disconnect_timer: asyncio.TimerHandle | None = None
215
+ self._message: BleMessage | None = None
216
+ self._commands: MammotionCommand = MammotionCommand(device.name)
217
+ self._expected_disconnect = False
218
+ self._connect_lock = asyncio.Lock()
219
+ self._operation_lock = asyncio.Lock()
220
+ self._key: str | None = None
221
+
222
+ def update_device(self, device: BLEDevice) -> None:
223
+ self._device = device
224
+
225
+ async def _send_command_with_args(self, key: str, **kwargs) -> bytes | None:
226
+ """Send command to device and read response."""
227
+ if self._operation_lock.locked():
228
+ _LOGGER.debug(
229
+ "%s: Operation already in progress, waiting for it to complete; RSSI: %s",
230
+ self.name,
231
+ self.rssi,
232
+ )
233
+ async with self._operation_lock:
234
+ try:
235
+ command_bytes = getattr(self._commands, key)(**kwargs)
236
+ return await self._send_command_locked(key, command_bytes)
237
+ except BleakNotFoundError:
238
+ _LOGGER.error(
239
+ "%s: device not found, no longer in range, or poor RSSI: %s",
240
+ self.name,
241
+ self.rssi,
242
+ exc_info=True,
243
+ )
244
+ raise
245
+ except CharacteristicMissingError as ex:
246
+ _LOGGER.debug(
247
+ "%s: characteristic missing: %s; RSSI: %s",
248
+ self.name,
249
+ ex,
250
+ self.rssi,
251
+ exc_info=True,
252
+ )
253
+ except BLEAK_RETRY_EXCEPTIONS:
254
+ _LOGGER.debug(
255
+ "%s: communication failed with:", self.name, exc_info=True
256
+ )
257
+ # raise RuntimeError("Unreachable")
258
+
259
+ async def _send_command(self, key: str, retry: int | None = None) -> bytes | None:
260
+ """Send command to device and read response."""
261
+ if self._operation_lock.locked():
262
+ _LOGGER.debug(
263
+ "%s: Operation already in progress, waiting for it to complete; RSSI: %s",
264
+ self.name,
265
+ self.rssi,
266
+ )
267
+ async with self._operation_lock:
268
+ try:
269
+ command_bytes = getattr(self._commands, key)()
270
+ return await self._send_command_locked(key, command_bytes)
271
+ except BleakNotFoundError:
272
+ _LOGGER.error(
273
+ "%s: device not found, no longer in range, or poor RSSI: %s",
274
+ self.name,
275
+ self.rssi,
276
+ exc_info=True,
277
+ )
278
+ raise
279
+ except CharacteristicMissingError as ex:
280
+ _LOGGER.debug(
281
+ "%s: characteristic missing: %s; RSSI: %s",
282
+ self.name,
283
+ ex,
284
+ self.rssi,
285
+ exc_info=True,
286
+ )
287
+ except BLEAK_RETRY_EXCEPTIONS:
288
+ _LOGGER.debug(
289
+ "%s: communication failed with:", self.name, exc_info=True
290
+ )
291
+ # raise RuntimeError("Unreachable")
292
+
293
+ @property
294
+ def name(self) -> str:
295
+ """Return device name."""
296
+ return f"{self._device.name} ({self._device.address})"
297
+
298
+ @property
299
+ def rssi(self) -> int:
300
+ """Return RSSI of device."""
301
+ return 0
302
+
303
+ async def _ensure_connected(self):
304
+ """Ensure connection to device is established."""
305
+ if self._connect_lock.locked():
306
+ _LOGGER.debug(
307
+ "%s: Connection already in progress, waiting for it to complete; RSSI: %s",
308
+ self.name,
309
+ self.rssi,
310
+ )
311
+ if self._client and self._client.is_connected:
312
+ _LOGGER.debug(
313
+ "%s: Already connected before obtaining lock, resetting timer; RSSI: %s",
314
+ self.name,
315
+ self.rssi,
316
+ )
317
+ self._reset_disconnect_timer()
318
+ return
319
+ async with self._connect_lock:
320
+ # Check again while holding the lock
321
+ if self._client and self._client.is_connected:
322
+ _LOGGER.debug(
323
+ "%s: Already connected after obtaining lock, resetting timer; RSSI: %s",
324
+ self.name,
325
+ self.rssi,
326
+ )
327
+ self._reset_disconnect_timer()
328
+ return
329
+ _LOGGER.debug("%s: Connecting; RSSI: %s", self.name, self.rssi)
330
+ client: BleakClientWithServiceCache = await establish_connection(
331
+ BleakClient,
332
+ self._device,
333
+ self.name,
334
+ self._disconnected,
335
+ max_attempts=10,
336
+ use_services_cache=True,
337
+ ble_device_callback=lambda: self._device,
338
+ )
339
+ _LOGGER.debug("%s: Connected; RSSI: %s", self.name, self.rssi)
340
+ self._client = client
341
+ self._message = BleMessage(client)
342
+
343
+ try:
344
+ self._resolve_characteristics(client.services)
345
+ except CharacteristicMissingError as ex:
346
+ _LOGGER.debug(
347
+ "%s: characteristic missing, clearing cache: %s; RSSI: %s",
348
+ self.name,
349
+ ex,
350
+ self.rssi,
351
+ exc_info=True,
352
+ )
353
+ await client.clear_cache()
354
+ self._cancel_disconnect_timer()
355
+ await self._execute_disconnect_with_lock()
356
+ raise
357
+
358
+ _LOGGER.debug(
359
+ "%s: Starting notify and disconnect timer; RSSI: %s",
360
+ self.name,
361
+ self.rssi,
362
+ )
363
+ self._reset_disconnect_timer()
364
+ await self._start_notify()
365
+
366
+ command_bytes = self._commands.send_todev_ble_sync(2)
367
+ await self._message.post_custom_data_bytes(command_bytes)
368
+
369
+ async def _send_command_locked(self, key: str, command: bytes) -> bytes:
370
+ """Send command to device and read response."""
371
+ await self._ensure_connected()
372
+ try:
373
+ return await self._execute_command_locked(key, command)
374
+ except BleakDBusError as ex:
375
+ # Disconnect so we can reset state and try again
376
+ await asyncio.sleep(DBUS_ERROR_BACKOFF_TIME)
377
+ _LOGGER.debug(
378
+ "%s: RSSI: %s; Backing off %ss; Disconnecting due to error: %s",
379
+ self.name,
380
+ self.rssi,
381
+ DBUS_ERROR_BACKOFF_TIME,
382
+ ex,
383
+ )
384
+ await self._execute_forced_disconnect()
385
+ raise
386
+ except BLEAK_RETRY_EXCEPTIONS as ex:
387
+ # Disconnect so we can reset state and try again
388
+ _LOGGER.debug(
389
+ "%s: RSSI: %s; Disconnecting due to error: %s", self.name, self.rssi, ex
390
+ )
391
+ await self._execute_forced_disconnect()
392
+ raise
393
+
394
+ async def _notification_handler(
395
+ self, _sender: BleakGATTCharacteristic, data: bytearray
396
+ ) -> None:
397
+ """Handle notification responses."""
398
+ _LOGGER.debug("%s: Received notification: %s", self.name, data)
399
+ result = self._message.parseNotification(data)
400
+ if result == 0:
401
+ data = await self._message.parseBlufiNotifyData(True)
402
+ self._update_raw_data(data)
403
+ self._message.clearNotification()
404
+ else:
405
+ return
406
+ new_msg = LubaMsg().parse(data)
407
+ if betterproto.serialized_on_wire(new_msg.net):
408
+ if new_msg.net.todev_ble_sync != 0 or has_field(
409
+ new_msg.net.toapp_wifi_iot_status
410
+ ):
411
+ # TODO occasionally respond with ble sync
412
+ return
413
+
414
+ if self._notify_future and not self._notify_future.done():
415
+ self._notify_future.set_result(data)
416
+ return
417
+
418
+ async def _start_notify(self) -> None:
419
+ """Start notification."""
420
+ _LOGGER.debug("%s: Subscribe to notifications; RSSI: %s", self.name, self.rssi)
421
+ await self._client.start_notify(self._read_char, self._notification_handler)
422
+
423
+ async def _execute_command_locked(self, key: str, command: bytes) -> bytes:
424
+ """Execute command and read response."""
425
+ assert self._client is not None
426
+ assert self._read_char is not None
427
+ assert self._write_char is not None
428
+ self._notify_future = self.loop.create_future()
429
+ self._key = key
430
+ _LOGGER.debug("%s: Sending command: %s", self.name, key)
431
+ await self._message.post_custom_data_bytes(command)
432
+
433
+ timeout = 5
434
+ timeout_handle = self.loop.call_at(
435
+ self.loop.time() + timeout, _handle_timeout, self._notify_future
436
+ )
437
+ timeout_expired = False
438
+ try:
439
+ notify_msg = await self._notify_future
440
+ except asyncio.TimeoutError:
441
+ timeout_expired = True
442
+ raise
443
+ finally:
444
+ if not timeout_expired:
445
+ timeout_handle.cancel()
446
+ self._notify_future = None
447
+
448
+ _LOGGER.debug("%s: Notification received: %s", self.name, notify_msg.hex())
449
+ return notify_msg
450
+
451
+ def get_address(self) -> str:
452
+ """Return address of device."""
453
+ return self._device.address
454
+
455
+ def _resolve_characteristics(self, services: BleakGATTServiceCollection) -> None:
456
+ """Resolve characteristics."""
457
+ self._read_char = services.get_characteristic(READ_CHAR_UUID)
458
+ if not self._read_char:
459
+ raise CharacteristicMissingError(READ_CHAR_UUID)
460
+ self._write_char = services.get_characteristic(WRITE_CHAR_UUID)
461
+ if not self._write_char:
462
+ raise CharacteristicMissingError(WRITE_CHAR_UUID)
463
+
464
+ def _reset_disconnect_timer(self):
465
+ """Reset disconnect timer."""
466
+ self._cancel_disconnect_timer()
467
+ self._expected_disconnect = False
468
+ self._disconnect_timer = self.loop.call_later(
469
+ DISCONNECT_DELAY, self._disconnect_from_timer
470
+ )
471
+
472
+ def _disconnected(self, client: BleakClientWithServiceCache) -> None:
473
+ """Disconnected callback."""
474
+ if self._expected_disconnect:
475
+ _LOGGER.debug(
476
+ "%s: Disconnected from device; RSSI: %s", self.name, self.rssi
477
+ )
478
+ return
479
+ _LOGGER.warning(
480
+ "%s: Device unexpectedly disconnected; RSSI: %s",
481
+ self.name,
482
+ self.rssi,
483
+ )
484
+ self._cancel_disconnect_timer()
485
+
486
+ def _disconnect_from_timer(self):
487
+ """Disconnect from device."""
488
+ if self._operation_lock.locked() and self._client.is_connected:
489
+ _LOGGER.debug(
490
+ "%s: Operation in progress, resetting disconnect timer; RSSI: %s",
491
+ self.name,
492
+ self.rssi,
493
+ )
494
+ self._reset_disconnect_timer()
495
+ return
496
+ self._cancel_disconnect_timer()
497
+ self._timed_disconnect_task = asyncio.create_task(
498
+ self._execute_timed_disconnect()
499
+ )
500
+
501
+ def _cancel_disconnect_timer(self):
502
+ """Cancel disconnect timer."""
503
+ if self._disconnect_timer:
504
+ self._disconnect_timer.cancel()
505
+ self._disconnect_timer = None
506
+
507
+ async def _execute_forced_disconnect(self) -> None:
508
+ """Execute forced disconnection."""
509
+ self._cancel_disconnect_timer()
510
+ _LOGGER.debug(
511
+ "%s: Executing forced disconnect",
512
+ self.name,
513
+ )
514
+ await self._execute_disconnect()
515
+
516
+ async def _execute_timed_disconnect(self) -> None:
517
+ """Execute timed disconnection."""
518
+ _LOGGER.debug(
519
+ "%s: Executing timed disconnect after timeout of %s",
520
+ self.name,
521
+ DISCONNECT_DELAY,
522
+ )
523
+ await self._execute_disconnect()
524
+
525
+ async def _execute_disconnect(self) -> None:
526
+ """Execute disconnection."""
527
+ _LOGGER.debug("%s: Executing disconnect", self.name)
528
+ async with self._connect_lock:
529
+ await self._execute_disconnect_with_lock()
530
+
531
+ async def _execute_disconnect_with_lock(self) -> None:
532
+ """Execute disconnection while holding the lock."""
533
+ assert self._connect_lock.locked(), "Lock not held"
534
+ _LOGGER.debug("%s: Executing disconnect with lock", self.name)
535
+ if self._disconnect_timer: # If the timer was reset, don't disconnect
536
+ _LOGGER.debug("%s: Skipping disconnect as timer reset", self.name)
537
+ return
538
+ client = self._client
539
+ self._expected_disconnect = True
540
+
541
+ if not client:
542
+ _LOGGER.debug("%s: Already disconnected", self.name)
543
+ return
544
+ _LOGGER.debug("%s: Disconnecting", self.name)
545
+ try:
546
+ """We reset what command the robot last heard before disconnecting."""
547
+ command_bytes = self._commands.send_todev_ble_sync(2)
548
+ await self._message.post_custom_data_bytes(command_bytes)
549
+ await client.stop_notify(self._read_char)
550
+ await client.disconnect()
551
+ except BLEAK_RETRY_EXCEPTIONS as ex:
552
+ _LOGGER.warning(
553
+ "%s: Error disconnecting: %s; RSSI: %s",
554
+ self.name,
555
+ ex,
556
+ self.rssi,
557
+ )
558
+ else:
559
+ _LOGGER.debug("%s: Disconnect completed successfully", self.name)
560
+ self._client = None
561
+
562
+ async def _disconnect(self) -> bool:
563
+ if self._client is not None:
564
+ return await self._client.disconnect()