Skip to content

Commit 9c83650

Browse files
authored
Fix disconnect race on subsequent operations (#151)
1 parent bd25e98 commit 9c83650

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

switchbot/devices/device.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _reset_disconnect_timer(self):
256256
self._cancel_disconnect_timer()
257257
self._expected_disconnect = False
258258
self._disconnect_timer = self.loop.call_later(
259-
DISCONNECT_DELAY, self._disconnect
259+
DISCONNECT_DELAY, self._disconnect_from_timer
260260
)
261261

262262
def _disconnected(self, client: BleakClientWithServiceCache) -> None:
@@ -272,8 +272,16 @@ def _disconnected(self, client: BleakClientWithServiceCache) -> None:
272272
self.rssi,
273273
)
274274

275-
def _disconnect(self):
275+
def _disconnect_from_timer(self):
276276
"""Disconnect from device."""
277+
if self._operation_lock.locked() and self._client.is_connected:
278+
_LOGGER.debug(
279+
"%s: Operation in progress, resetting disconnect timer; RSSI: %s",
280+
self.name,
281+
self.rssi,
282+
)
283+
self._reset_disconnect_timer()
284+
return
277285
self._cancel_disconnect_timer()
278286
asyncio.create_task(self._execute_timed_disconnect())
279287

0 commit comments

Comments
 (0)