Skip to content

Commit af820a7

Browse files
committed
add missing timeout options
1 parent ac0d6bf commit af820a7

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

Diff for: AUTHORS.rst

+1
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]>

Diff for: CHANGELOG.rst

+1
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)

Diff for: bleak/backends/bluezdbus/client.py

+15-5
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,9 @@ 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(
421+
self._disconnecting_event.wait(), timeout=timeout
422+
)
417423
finally:
418424
self._disconnecting_event = None
419425

@@ -575,6 +581,9 @@ def mtu_size(self) -> int:
575581
async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
576582
"""Get all services registered for this GATT server.
577583
584+
Keyword Args:
585+
timeout (float): Defaults to 10.0.
586+
578587
Returns:
579588
A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree.
580589
@@ -585,11 +594,12 @@ async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
585594
if self._services_resolved:
586595
return self.services
587596

597+
timeout = kwargs.get("timeout", self._timeout)
588598
if not self._properties["ServicesResolved"]:
589599
logger.debug(f"Waiting for ServicesResolved ({self._device_path})")
590600
self._services_resolved_event = asyncio.Event()
591601
try:
592-
await asyncio.wait_for(self._services_resolved_event.wait(), 5)
602+
await asyncio.wait_for(self._services_resolved_event.wait(), timeout)
593603
finally:
594604
self._services_resolved_event = None
595605

Diff for: bleak/backends/corebluetooth/PeripheralDelegate.py

+8-3
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,22 @@ 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,
129+
characteristic: CBCharacteristic,
130+
use_cached: bool = True,
131+
timeout: Optional[Union[int, float]] = None,
129132
) -> NSData:
130133
if characteristic.value() is not None and use_cached:
131134
return characteristic.value()
135+
if timeout is None:
136+
timeout = self._timeout
132137

133138
future = self._event_loop.create_future()
134139

135140
self._characteristic_read_futures[characteristic.handle()] = future
136141
try:
137142
self.peripheral.readValueForCharacteristic_(characteristic)
138-
return await asyncio.wait_for(future, timeout=5)
143+
return await asyncio.wait_for(future, timeout=timeout)
139144
finally:
140145
del self._characteristic_read_futures[characteristic.handle()]
141146

Diff for: bleak/backends/winrt/client.py

+6-2
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)