feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677
feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677Apollon77 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.
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. |
|
Independent validation on macOS 26.5.1 arm64, Node v26.3.1, Python 3.13.14: clean build; |
|
As also posted in the other issue ... try HA 2026.7 please and report back |
|
@Apollon77 Reported back on #704 with full detail |
|
Yes and repeated: Issue discussion on PRs is wrong place and do not double post please. Thanks |
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