feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677
Open
Apollon77 wants to merge 1 commit into
Open
feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677Apollon77 wants to merge 1 commit into
Apollon77 wants to merge 1 commit into
Conversation
Adds an optional connect_strategy callable to MatterBleProxy so embedders can plug in bleak_retry_connector.establish_connection (or any other connection helper) without making it a hard dependency of the library. - Public API: MatterBleProxy(connect_strategy=...) and the default matter_ble_proxy.default_connect_strategy implementation - _handle_connect delegates to the strategy; the existing TimeoutError / generic-Exception mapping to "connection_failed" is preserved - Tests cover constructor wiring, success path, device-not-found short-circuit, and timeout/exception mapping Background: Home Assistant's habluetooth.wrappers warns that BleakClient.connect() was called without bleak-retry-connector during BLE proxy pairing. With this hook HA can pass establish_connection and get adapter path scoring + retry for free.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a pluggable BLE connection hook to the Python BLE proxy client so embedders (notably Home Assistant) can supply a custom connection-establishment routine (e.g., bleak_retry_connector.establish_connection) without introducing a hard dependency, while keeping the existing on-the-wire error semantics intact.
Changes:
- Introduces
connect_strategy(optional) onMatterBleProxy, defaulting to a newdefault_connect_strategy. - Refactors
_handle_connectto delegate connection establishment to the selected strategy while preservingTimeoutError/ generic exception →connection_failedmappings. - Adds unit tests covering default vs custom strategy behavior and error mapping.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python_ble_proxy/matter_ble_proxy/client.py | Adds default_connect_strategy, threads optional connect_strategy through MatterBleProxy, and delegates _handle_connect to it while keeping existing error mapping. |
| python_ble_proxy/matter_ble_proxy/init.py | Exposes default_connect_strategy as part of the public package API. |
| python_ble_proxy/tests/test_connect_strategy.py | Adds unit tests validating strategy wiring, invocation, and error mapping behavior. |
3 tasks
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
connect_strategycallable toMatterBleProxyso embedders can plug inbleak_retry_connector.establish_connection(or any other connection helper) without making it a hard dependency of the library.matter_ble_proxy.default_connect_strategy(target, on_disconnect, timeout_ms)keeps the plain-Bleak behavior for the standalone CLI._handle_connectdelegates to the strategy; existingTimeoutError/ generic-Exception→connection_failedmapping is preserved, so error semantics on the wire don't change.Why
Home Assistant's
habluetooth.wrapperswarns wheneverBleakClient.connect()runs outsidebleak_retry_connector.establish_connection. During BLE proxy pairing through HA you see:Functionally pairing succeeds, but we lose adapter path scoring + the retry policy HA uses everywhere else. With this hook the HA integration can pass
establish_connectionwhile the library stays Bleak-only.Strategy contract
Strategy receives
(target, disconnected_callback, timeout_ms)and returns a connectedBleakClient. The library applies no extra timeout — the strategy owns its retry/timeout policy.Follow-up
The Home Assistant matter integration PR (home-assistant/core#171384) will pin to the released version of this change and pass an
establish_connection-backed strategy fromhomeassistant/components/matter/ble_proxy.py.Test plan
npm run python-ble-proxy:lintnpm run python-ble-proxy:typechecknpm run python-ble-proxy:test(12 passed, including 6 new intest_connect_strategy.py)npm run python-ble-proxy:build