Skip to content

Commit 2e19327

Browse files
authored
Log RSSI on unexpected disconnects (#77)
1 parent cbe64df commit 2e19327

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

switchbot/devices/device.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
# How long to hold the connection
3939
# to wait for additional commands for
4040
# disconnecting the device.
41-
DISCONNECT_DELAY = 59
41+
DISCONNECT_DELAY = 49
4242

4343

4444
def _sb_uuid(comms_type: str = "service") -> UUID | str:
@@ -81,6 +81,7 @@ def __init__(
8181
self._read_char: BleakGATTCharacteristic | None = None
8282
self._write_char: BleakGATTCharacteristic | None = None
8383
self._disconnect_timer: asyncio.TimerHandle | None = None
84+
self._expected_disconnect = False
8485
self.loop = asyncio.get_event_loop()
8586

8687
def _commandkey(self, key: str) -> str:
@@ -97,8 +98,9 @@ async def _sendcommand(self, key: str, retry: int) -> bytes:
9798
_LOGGER.debug("%s: Sending command %s", self.name, command)
9899
if self._operation_lock.locked():
99100
_LOGGER.debug(
100-
"%s: Operation already in progress, waiting for it to complete.",
101+
"%s: Operation already in progress, waiting for it to complete; RSSI: %s",
101102
self.name,
103+
self.rssi,
102104
)
103105

104106
max_attempts = retry + 1
@@ -116,8 +118,9 @@ async def _sendcommand(self, key: str, retry: int) -> bytes:
116118
except BLEAK_EXCEPTIONS:
117119
if attempt == retry:
118120
_LOGGER.error(
119-
"%s: communication failed; Stopping trying",
121+
"%s: communication failed; Stopping trying; RSSI: %s",
120122
self.name,
123+
self.rssi,
121124
exc_info=True,
122125
)
123126
return b"\x00"
@@ -133,12 +136,18 @@ def name(self) -> str:
133136
"""Return device name."""
134137
return f"{self._device.name} ({self._device.address})"
135138

139+
@property
140+
def rssi(self) -> int:
141+
"""Return RSSI of device."""
142+
return self._get_adv_value("rssi")
143+
136144
async def _ensure_connected(self):
137145
"""Ensure connection to device is established."""
138146
if self._connect_lock.locked():
139147
_LOGGER.debug(
140-
"%s: Connection already in progress, waiting for it to complete.",
148+
"%s: Connection already in progress, waiting for it to complete; RSSI: %s",
141149
self.name,
150+
self.rssi,
142151
)
143152
if self._client and self._client.is_connected:
144153
self._reset_disconnect_timer()
@@ -148,14 +157,16 @@ async def _ensure_connected(self):
148157
if self._client and self._client.is_connected:
149158
self._reset_disconnect_timer()
150159
return
160+
_LOGGER.debug("%s: Connecting; RSSI: %s", self.name, self.rssi)
151161
client = await establish_connection(
152162
BleakClientWithServiceCache,
153163
self._device,
154164
self.name,
165+
self._disconnected,
155166
cached_services=self._cached_services,
156167
)
157168
self._cached_services = client.services
158-
_LOGGER.debug("%s: Connected", self.name)
169+
_LOGGER.debug("%s: Connected; RSSI: %s", self.name, self.rssi)
159170
services = client.services
160171
self._read_char = services.get_characteristic(_sb_uuid(comms_type="rx"))
161172
self._write_char = services.get_characteristic(_sb_uuid(comms_type="tx"))
@@ -166,10 +177,24 @@ def _reset_disconnect_timer(self):
166177
"""Reset disconnect timer."""
167178
if self._disconnect_timer:
168179
self._disconnect_timer.cancel()
180+
self._expected_disconnect = False
169181
self._disconnect_timer = self.loop.call_later(
170182
DISCONNECT_DELAY, self._disconnect
171183
)
172184

185+
def _disconnected(self, client: BleakClientWithServiceCache) -> None:
186+
"""Disconnected callback."""
187+
if self._expected_disconnect:
188+
_LOGGER.debug(
189+
"%s: Disconnected from device; RSSI: %s", self.name, self.rssi
190+
)
191+
return
192+
_LOGGER.warning(
193+
"%s: Device unexpectedly disconnected; RSSI: %s",
194+
self.name,
195+
self.rssi,
196+
)
197+
173198
def _disconnect(self):
174199
"""Disconnect from device."""
175200
self._disconnect_timer = None
@@ -185,6 +210,7 @@ async def _execute_disconnect(self):
185210
async with self._connect_lock:
186211
if not self._client or not self._client.is_connected:
187212
return
213+
self._expected_disconnect = True
188214
await self._client.disconnect()
189215
self._client = None
190216
self._read_char = None
@@ -206,7 +232,7 @@ def _notification_handler(_sender: int, data: bytearray) -> None:
206232
return
207233
future.set_result(data)
208234

209-
_LOGGER.debug("%s: Subscribe to notifications", self.name)
235+
_LOGGER.debug("%s: Subscribe to notifications; RSSI: %s", self.name, self.rssi)
210236
await client.start_notify(self._read_char, _notification_handler)
211237

212238
_LOGGER.debug("%s: Sending command: %s", self.name, key)

0 commit comments

Comments
 (0)