-
-
Notifications
You must be signed in to change notification settings - Fork 84
Add bluetooth_gatt_stop_notify and auto-cleanup notify callbacks on disconnect #1487
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
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1487 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 3488 3506 +18
=========================================
+ Hits 3488 3506 +18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Pull request overview
This pull request adds synchronous methods to stop Bluetooth GATT notify callbacks and implements automatic cleanup of notify callbacks when a Bluetooth device disconnects. This addresses issue #667 where the previous API design made it easy for callers to leak notify callbacks.
Changes:
- Added internal tracking of notify callbacks via
_notify_callbacksdictionary inAPIClientBase - Introduced
bluetooth_gatt_stop_notify()andbluetooth_gatt_stop_notify_for_address()synchronous methods for cleanup - Modified
bluetooth_device_connect()to automatically clean up notify callbacks on disconnect
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| aioesphomeapi/client_base.py | Added _notify_callbacks dictionary to __slots__ and initialized it in __init__ to track active notify sessions |
| aioesphomeapi/client_base.pxd | Added Cython declaration for _notify_callbacks dictionary |
| aioesphomeapi/client.py | Implemented notify cleanup methods, added auto-cleanup wrapper in bluetooth_device_connect(), and integrated with bluetooth_gatt_start_notify() |
| tests/test_client.py | Added comprehensive tests for the new stop methods and auto-cleanup on disconnect functionality |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughThe changes implement a mechanism to prevent Bluetooth notify callback leaks on device disconnect by introducing an internal registry keyed by (address, handle) to track active notify callbacks, automatically cleaning them up on connection state changes and providing public methods to stop individual or address-wide notify sessions. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Application
participant Client as APIClient
participant Registry as Notify Registry<br/>(_notify_callbacks)
participant GATT as GATT Layer
participant ConnMgr as Connection Manager
App->>Client: bluetooth_gatt_start_notify(address, handle)
Client->>GATT: Enable notification
GATT-->>Client: removal_callback
Client->>Registry: Store removal_callback<br/>at (address, handle)
Client-->>App: stop_fn(), wrapped_remove_fn()
Note over Client,ConnMgr: Device disconnects...
ConnMgr->>Client: _on_bluetooth_device_connection_response<br/>(disconnected)
Client->>Client: on_bluetooth_connection_state_with_notify_cleanup()
Client->>Client: bluetooth_gatt_stop_notify_for_address(address)
Client->>Registry: Fetch all callbacks for address
Registry-->>Client: List of removal_callbacks
Client->>GATT: Invoke each removal_callback
Client->>Registry: Clear callbacks for address
Note over App,Client: Alternative: App stops notify
App->>Client: stop_fn()
Client->>Client: bluetooth_gatt_stop_notify(address, handle)
Client->>Registry: Lookup callback at (address, handle)
Client->>GATT: Invoke removal_callback
Client->>Registry: Clear (address, handle) entry
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
What does this implement/fix?
Add synchronous
bluetooth_gatt_stop_notifyandbluetooth_gatt_stop_notify_for_addressmethods to safely clean up Bluetooth GATT notify callbacks, and automatically clean up notify callbacks when a Bluetooth device disconnects.Previously, callers had to track and manage the two callables returned by
bluetooth_gatt_start_notify, which made it easy to leak callbacks on disconnect. This change:(address, handle)in_notify_callbacksdictbluetooth_gatt_stop_notify(address, handle)- a sync method to stop a single notify sessionbluetooth_gatt_stop_notify_for_address(address)- a sync method to stop all notify sessions for an addressbluetooth_device_connectto automatically callbluetooth_gatt_stop_notify_for_addresswhen a device disconnectsThe sync methods are safe to call from exception handlers without awaiting, which is needed for proper cleanup in bleak-esphome (see Bluetooth-Devices/bleak-esphome#259).
Types of changes
Related issue or feature (if applicable):
Pull request in esphome (if applicable):
Checklist:
tests/folder).