Skip to content

feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677

Open
Apollon77 wants to merge 1 commit into
mainfrom
feat/ble-proxy-connect-strategy
Open

feat(ble-proxy): pluggable connect_strategy hook for MatterBleProxy#677
Apollon77 wants to merge 1 commit into
mainfrom
feat/ble-proxy-connect-strategy

Conversation

@Apollon77

@Apollon77 Apollon77 commented May 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • 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.
  • New public helper matter_ble_proxy.default_connect_strategy(target, on_disconnect, timeout_ms) keeps the plain-Bleak behavior for the standalone CLI.
  • _handle_connect delegates to the strategy; existing TimeoutError / generic-Exceptionconnection_failed mapping is preserved, so error semantics on the wire don't change.

Generic hook, not HA-specific. Connection establishment is already the library's responsibility (_handle_connect constructs the BleakClient and calls .connect()); the hook makes that one step pluggable so any embedder — bleak-retry-connector users, custom connection pools, telemetry wrappers — can supply their own helper. Home Assistant is the first consumer; the library stays dependency-free.

Why

Home Assistant's habluetooth.wrappers warns whenever BleakClient.connect() runs outside bleak_retry_connector.establish_connection. During BLE proxy pairing through HA you see:

habluetooth.wrappers C8:8B:A4:F7:57:32: BleakClient.connect() called without
  bleak-retry-connector. For reliable connection establishment, use
  bleak_retry_connector.establish_connection().

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_connection while the library stays Bleak-only.

Strategy contract

ConnectStrategy = Callable[
    [BLEDevice | str, Callable[[BleakClient], None], int],
    Awaitable[BleakClient],
]

Strategy receives (target, disconnected_callback, timeout_ms) and returns a connected BleakClient. 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 from homeassistant/components/matter/ble_proxy.py.

Test plan

  • npm run python-ble-proxy:lint
  • npm run python-ble-proxy:typecheck
  • npm run python-ble-proxy:test (12 passed, including 6 new in test_connect_strategy.py)
  • npm run python-ble-proxy:build
  • Real-device pairing through HA against a 0.7.2 build of the library — verifies the warning is gone end to end (deferred to the HA-side PR once the lib release is out)

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.
Copilot AI review requested due to automatic review settings May 22, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) on MatterBleProxy, defaulting to a new default_connect_strategy.
  • Refactors _handle_connect to delegate connection establishment to the selected strategy while preserving TimeoutError / generic exception → connection_failed mappings.
  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants