feat: add optional validate_connection callback to establish_connection#225
Closed
cgoudie wants to merge 1 commit intoBluetooth-Devices:mainfrom
Closed
feat: add optional validate_connection callback to establish_connection#225cgoudie wants to merge 1 commit intoBluetooth-Devices:mainfrom
cgoudie wants to merge 1 commit intoBluetooth-Devices:mainfrom
Conversation
Allow callers to supply an async callback that verifies a connection is actually usable after connect() succeeds (e.g. send a command and check the response). If the callback returns False or raises any exception the client is disconnected and the attempt is retried until max_attempts is exhausted. The parameter is fully optional (defaults to None) so existing callers are unaffected. Co-authored-by: Cursor <cursoragent@cursor.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #225 +/- ##
==========================================
+ Coverage 79.64% 80.10% +0.45%
==========================================
Files 6 6
Lines 570 583 +13
Branches 112 114 +2
==========================================
+ Hits 454 467 +13
Misses 70 70
Partials 46 46 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
@cgoudie Please stop opening PRs. You are leaving me no choice but to ban you from the org |
Member
|
I turned off PRs for 24 hours to stop the spam |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
validate_connectionparameter toestablish_connection()— an async callbackCallable[[BleakClient], Awaitable[bool]]that is invoked after every successfulconnect().Falseor raises any exception, the client is disconnected and the attempt is retried untilmax_attemptsis exhausted.Noneso existing callers are completely unaffected — no API contract change.Motivation
A successful BLE
connect()does not guarantee the connection is actually usable. The device may accept the connection but fail to respond to GATT reads/writes. This callback lets the calling application verify the connection is live (e.g. read a characteristic, send a command and check the response) beforeestablish_connectionreturns.Example usage
Test plan
test_establish_connection_validate_connection_passes— callback returns True, client returnedtest_establish_connection_validate_connection_fails_then_passes— first call returns False (disconnect + retry), second call returns Truetest_establish_connection_validate_connection_exception_treated_as_false— callback raises RuntimeError, treated as failure, retry succeedstest_establish_connection_validate_connection_exhausts_retries— callback always returns False, raises BleakConnectionError after max_attemptstest_establish_connection_no_validate_connection— no callback provided, behaviour unchangedMade with Cursor