Skip to content

Commit cb21bfd

Browse files
authored
Improve error message when device has disappeared (#76)
1 parent 33af855 commit cb21bfd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

switchbot/devices/device.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
import asyncio
55
import binascii
66
import logging
7-
from ctypes import cast
8-
from typing import Any, Callable, TypeVar
7+
from typing import Any
98
from uuid import UUID
109

1110
import async_timeout
12-
import bleak
11+
1312
from bleak import BleakError
1413
from bleak.backends.device import BLEDevice
1514
from bleak.backends.service import BleakGATTCharacteristic, BleakGATTServiceCollection
1615
from bleak_retry_connector import (
1716
BleakClientWithServiceCache,
17+
BleakNotFoundError,
1818
ble_device_has_changed,
1919
establish_connection,
2020
)
@@ -106,10 +106,17 @@ async def _sendcommand(self, key: str, retry: int) -> bytes:
106106
for attempt in range(max_attempts):
107107
try:
108108
return await self._send_command_locked(key, command)
109+
except BleakNotFoundError:
110+
_LOGGER.error(
111+
"%s: device not found or no longer in range; Try restarting Bluetooth",
112+
self.name,
113+
exc_info=True,
114+
)
115+
return b"\x00"
109116
except BLEAK_EXCEPTIONS:
110117
if attempt == retry:
111118
_LOGGER.error(
112-
"%s: communication failed. Stopping trying",
119+
"%s: communication failed; Stopping trying",
113120
self.name,
114121
exc_info=True,
115122
)

0 commit comments

Comments
 (0)