Skip to content

Commit 451aace

Browse files
committed
add missing timeout options
1 parent ac0d6bf commit 451aace

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Contributors
1919
* Bernie Conrad <[email protected]>
2020
* Jonathan Soto <[email protected]>
2121
* Kyle J. Williams <[email protected]>
22+
* Vyacheslav Linnik <[email protected]>

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed
1616
of the same characteristic are used. Fixes #675.
1717
* Fixed reading a characteristic on CoreBluetooth backend also triggers notification
1818
callback.
19+
* Removed hardcoded timeout values in all backends.
1920

2021

2122
`0.13.0`_ (2021-10-20)

bleak/backends/bluezdbus/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def connect(self, **kwargs) -> bool:
9090
"""Connect to the specified GATT server.
9191
9292
Keyword Args:
93-
timeout (float): Timeout for required ``BleakScanner.find_device_by_address`` call. Defaults to 10.0.
93+
timeout (float): Defaults to 10.0.
9494
9595
Returns:
9696
Boolean representing connection status.
@@ -377,9 +377,12 @@ def _cleanup_all(self) -> None:
377377
self.services = BleakGATTServiceCollection()
378378
self._services_resolved = False
379379

380-
async def disconnect(self) -> bool:
380+
async def disconnect(self, **kwargs) -> bool:
381381
"""Disconnect from the specified GATT server.
382382
383+
Keyword Args:
384+
timeout (float): Defaults to 10.0.
385+
383386
Returns:
384387
Boolean representing if device is disconnected.
385388
@@ -396,10 +399,11 @@ async def disconnect(self) -> bool:
396399
logger.debug(f"already disconnected ({self._device_path})")
397400
return True
398401

402+
timeout = kwargs.get("timeout", self._timeout)
399403
if self._disconnecting_event:
400404
# another call to disconnect() is already in progress
401405
logger.debug(f"already in progress ({self._device_path})")
402-
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=10)
406+
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=timeout)
403407
elif self.is_connected:
404408
self._disconnecting_event = asyncio.Event()
405409
try:
@@ -413,7 +417,7 @@ async def disconnect(self) -> bool:
413417
)
414418
)
415419
assert_reply(reply)
416-
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=10)
420+
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=timeout)
417421
finally:
418422
self._disconnecting_event = None
419423

@@ -575,6 +579,9 @@ def mtu_size(self) -> int:
575579
async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
576580
"""Get all services registered for this GATT server.
577581
582+
Keyword Args:
583+
timeout (float): Timeout for required ``BleakScanner.find_device_by_address`` call. Defaults to 10.0.
584+
578585
Returns:
579586
A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree.
580587
@@ -585,11 +592,12 @@ async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
585592
if self._services_resolved:
586593
return self.services
587594

595+
timeout = kwargs.get("timeout", self._timeout)
588596
if not self._properties["ServicesResolved"]:
589597
logger.debug(f"Waiting for ServicesResolved ({self._device_path})")
590598
self._services_resolved_event = asyncio.Event()
591599
try:
592-
await asyncio.wait_for(self._services_resolved_event.wait(), 5)
600+
await asyncio.wait_for(self._services_resolved_event.wait(), timeout)
593601
finally:
594602
self._services_resolved_event = None
595603

bleak/backends/corebluetooth/PeripheralDelegate.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import asyncio
1010
import itertools
1111
import logging
12-
from typing import Callable, Any, Dict, Iterable, NewType, Optional
12+
from typing import Callable, Any, Dict, Iterable, NewType, Optional, Union
1313

1414
import objc
1515
from Foundation import NSNumber, NSObject, NSArray, NSData, NSError, NSUUID, NSString
@@ -125,17 +125,20 @@ async def discover_descriptors(self, characteristic: CBCharacteristic) -> NSArra
125125

126126
@objc.python_method
127127
async def read_characteristic(
128-
self, characteristic: CBCharacteristic, use_cached: bool = True
128+
self, characteristic: CBCharacteristic, use_cached: bool = True,
129+
timeout: Optional[Union[int, float]] = None
129130
) -> NSData:
130131
if characteristic.value() is not None and use_cached:
131132
return characteristic.value()
133+
if timeout is None:
134+
timeout = self._timeout
132135

133136
future = self._event_loop.create_future()
134137

135138
self._characteristic_read_futures[characteristic.handle()] = future
136139
try:
137140
self.peripheral.readValueForCharacteristic_(characteristic)
138-
return await asyncio.wait_for(future, timeout=5)
141+
return await asyncio.wait_for(future, timeout=timeout)
139142
finally:
140143
del self._characteristic_read_futures[characteristic.handle()]
141144

bleak/backends/winrt/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,18 @@ def _ConnectionStatusChanged_Handler(sender, args):
258258

259259
return True
260260

261-
async def disconnect(self) -> bool:
261+
async def disconnect(self, **kwargs) -> bool:
262262
"""Disconnect from the specified GATT server.
263263
264+
Keyword Args:
265+
timeout (float): Defaults to 10.0.
266+
264267
Returns:
265268
Boolean representing if device is disconnected.
266269
267270
"""
268271
logger.debug("Disconnecting from BLE device...")
272+
timeout = kwargs.get("timeout", self._timeout)
269273
# Remove notifications.
270274
for handle, event_handler_token in list(self._notification_callbacks.items()):
271275
char = self.services.get_characteristic(handle)
@@ -289,7 +293,7 @@ async def disconnect(self) -> bool:
289293
self._disconnect_events.append(event)
290294
try:
291295
self._requester.close()
292-
await asyncio.wait_for(event.wait(), timeout=10)
296+
await asyncio.wait_for(event.wait(), timeout=timeout)
293297
finally:
294298
self._disconnect_events.remove(event)
295299

0 commit comments

Comments
 (0)