-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove hardcoded timeout values #679
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ Contributors | |
* Bernie Conrad <[email protected]> | ||
* Jonathan Soto <[email protected]> | ||
* Kyle J. Williams <[email protected]> | ||
* Vyacheslav Linnik <[email protected]> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ async def connect(self, **kwargs) -> bool: | |
"""Connect to the specified GATT server. | ||
|
||
Keyword Args: | ||
timeout (float): Timeout for required ``BleakScanner.find_device_by_address`` call. Defaults to 10.0. | ||
timeout (float): Defaults to 10.0. | ||
|
||
Returns: | ||
Boolean representing connection status. | ||
|
@@ -377,9 +377,12 @@ def _cleanup_all(self) -> None: | |
self.services = BleakGATTServiceCollection() | ||
self._services_resolved = False | ||
|
||
async def disconnect(self) -> bool: | ||
async def disconnect(self, **kwargs) -> bool: | ||
"""Disconnect from the specified GATT server. | ||
|
||
Keyword Args: | ||
timeout (float): Defaults to 10.0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps specify the unit as seconds; so "timeout (float): Defaults to 10.0 seconds." |
||
|
||
Returns: | ||
Boolean representing if device is disconnected. | ||
|
||
|
@@ -396,10 +399,11 @@ async def disconnect(self) -> bool: | |
logger.debug(f"already disconnected ({self._device_path})") | ||
return True | ||
|
||
timeout = kwargs.get("timeout", self._timeout) | ||
if self._disconnecting_event: | ||
# another call to disconnect() is already in progress | ||
logger.debug(f"already in progress ({self._device_path})") | ||
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=10) | ||
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=timeout) | ||
elif self.is_connected: | ||
self._disconnecting_event = asyncio.Event() | ||
try: | ||
|
@@ -413,7 +417,9 @@ async def disconnect(self) -> bool: | |
) | ||
) | ||
assert_reply(reply) | ||
await asyncio.wait_for(self._disconnecting_event.wait(), timeout=10) | ||
await asyncio.wait_for( | ||
self._disconnecting_event.wait(), timeout=timeout | ||
) | ||
finally: | ||
self._disconnecting_event = None | ||
|
||
|
@@ -575,6 +581,9 @@ def mtu_size(self) -> int: | |
async def get_services(self, **kwargs) -> BleakGATTServiceCollection: | ||
"""Get all services registered for this GATT server. | ||
|
||
Keyword Args: | ||
timeout (float): Defaults to 10.0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps specify the unit as seconds; so "timeout (float): Defaults to 10.0 seconds." |
||
|
||
Returns: | ||
A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree. | ||
|
||
|
@@ -585,11 +594,12 @@ async def get_services(self, **kwargs) -> BleakGATTServiceCollection: | |
if self._services_resolved: | ||
return self.services | ||
|
||
timeout = kwargs.get("timeout", self._timeout) | ||
if not self._properties["ServicesResolved"]: | ||
logger.debug(f"Waiting for ServicesResolved ({self._device_path})") | ||
self._services_resolved_event = asyncio.Event() | ||
try: | ||
await asyncio.wait_for(self._services_resolved_event.wait(), 5) | ||
await asyncio.wait_for(self._services_resolved_event.wait(), timeout) | ||
finally: | ||
self._services_resolved_event = None | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,14 +258,18 @@ def _ConnectionStatusChanged_Handler(sender, args): | |
|
||
return True | ||
|
||
async def disconnect(self) -> bool: | ||
async def disconnect(self, **kwargs) -> bool: | ||
"""Disconnect from the specified GATT server. | ||
Keyword Args: | ||
timeout (float): Defaults to 10.0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps specify the unit as seconds; so "timeout (float): Defaults to 10.0 seconds." |
||
Returns: | ||
Boolean representing if device is disconnected. | ||
""" | ||
logger.debug("Disconnecting from BLE device...") | ||
timeout = kwargs.get("timeout", self._timeout) | ||
# Remove notifications. | ||
for handle, event_handler_token in list(self._notification_callbacks.items()): | ||
char = self.services.get_characteristic(handle) | ||
|
@@ -289,7 +293,7 @@ async def disconnect(self) -> bool: | |
self._disconnect_events.append(event) | ||
try: | ||
self._requester.close() | ||
await asyncio.wait_for(event.wait(), timeout=10) | ||
await asyncio.wait_for(event.wait(), timeout=timeout) | ||
finally: | ||
self._disconnect_events.remove(event) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps specify the unit as seconds; so "timeout (float): Defaults to 10.0 seconds."