feat(ble-proxy): add AdvertisementData.from_bleak() factory#680
Open
Apollon77 wants to merge 1 commit into
Open
feat(ble-proxy): add AdvertisementData.from_bleak() factory#680Apollon77 wants to merge 1 commit into
Apollon77 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the python_ble_proxy integration surface to accept Bleak’s native bleak.backends.scanner.AdvertisementData in the scan callback (with address and connectable passed explicitly), removing the library’s internal AdvertisementData dataclass while keeping the on-wire device_discovered JSON schema unchanged.
Changes:
- Replace
BleScanSourcecallback contract from a library dataclass to(address, connectable, bleak AdvertisementData). - Remove the internal
AdvertisementDatadataclass and builddevice_discoveredpayloads directly from Bleak’s shape. - Update Bleak backend + docs/exports/tests to match the new contract.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| python_ble_proxy/matter_ble_proxy/client.py | Updates callback typing/contract and rewires scan event mapping to use Bleak AdvertisementData + explicit address/connectable. |
| python_ble_proxy/matter_ble_proxy/bleak_backend.py | Stops building the removed dataclass; forwards (device.address, True, advertisement) and preserves prior name fallback via _replace(local_name=...). |
| python_ble_proxy/matter_ble_proxy/protocol.py | Deletes the internal AdvertisementData dataclass definition. |
| python_ble_proxy/matter_ble_proxy/init.py | Removes AdvertisementData from exports. |
| python_ble_proxy/README.md | Updates integrator docs to describe the new callback signature and Bleak type. |
| python_ble_proxy/tests/test_protocol.py | Removes the AdvertisementData defaulting test now that the type is deleted. |
| CHANGELOG.md | Adds a WIP entry describing the new callback signature. |
Comments suppressed due to low confidence (1)
python_ble_proxy/matter_ble_proxy/client.py:361
- The scan callback contract and the
device_discoveredpayload are now derived from bleak’sAdvertisementData(local_name→name, plus explicitaddress/connectable). There’s no unit test exercising this mapping/dedup behavior, so regressions (wrong field, missing connectable, incorrect dedup keying) won’t be caught. Consider adding a test that runs_handle_start_scanwith a fakeBleScanSourceand asserts the emitteddevice_discoveredpayload and duplicate suppression.
def _on_advertisement(address: str, connectable: bool, ad: BleakAdvertisementData) -> None:
if service_uuid_set is not None:
advertised = {_normalize_uuid(u) for u in ad.service_uuids}
# Some stacks only surface the service UUID via its service_data key,
# so include those too. _normalize_uuid collapses Bleak's canonical
21e9063 to
9566391
Compare
Add an additive `AdvertisementData.from_bleak(address, connectable, bleak_advertisement)` classmethod to the Python `matter_ble_proxy` library, so integrators that already have a `bleak.backends.scanner.AdvertisementData` (Home Assistant's `BluetoothServiceInfoBleak.advertisement`, raw Bleak wrappers, ESPHome BLE proxies, ...) can build the library's wire dataclass without copying seven fields by hand. The existing public API (`BleScanSource` callback contract, `AdvertisementData` constructor, `__init__` export surface) is unchanged — this is purely additive. The default `BleakScanSource` now uses the new factory internally; the `local_name or device.name` fallback is preserved via `advertisement._replace`. Resolves #679. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
9566391 to
5256f8e
Compare
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
AdvertisementData.from_bleak(address, connectable, bleak_advertisement)classmethod on the Pythonmatter_ble_proxylibrary. Integrators that already hold ableak.backends.scanner.AdvertisementData— Home Assistant'sBluetoothServiceInfoBleak.advertisement, rawbleak.BleakScannersetups, ESPHome BLE proxies — can build the library's wire dataclass with one call instead of copying seven fields by hand.tx_powerandplatform_data(not part of the wire schema) and requiresaddress+connectableexplicitly, becausebleak.AdvertisementDataintentionally carries neither (the first lives on the pairedBLEDevice, the second on the scan context).BleakScanSourceis updated to dogfood the factory; thelocal_name or device.namefallback is preserved by_replace-inglocal_nameon the bleakNamedTuplebefore forwarding.Non-breaking — the existing public
BleScanSourcecallback contract,AdvertisementDataconstructor, and__init__.pyexport surface are unchanged. Adopting the factory is opt-in.Resolves #679.
Follow-up: Home Assistant adjustments (after this PR merges + a
matter-ble-proxyrelease)homeassistant/components/matter/ble_proxy.pyin home-assistant/core#171384 can simplify_to_advertisement_datato a singleAdvertisementData.from_bleakcall:from bleak.backends.device import BLEDevice from home_assistant_bluetooth import BluetoothServiceInfoBleak from matter_ble_proxy import ( AdvertisementData, BleDeviceResolver, BleScanSource, MatterBleProxy, ) ... @callback def _on_advertisement( service_info: BluetoothServiceInfoBleak, _change: object, ) -> None: try: - callback_fn(_to_advertisement_data(service_info)) + callback_fn( + AdvertisementData.from_bleak( + service_info.address, + service_info.connectable, + service_info.advertisement, + ) + ) except Exception: _LOGGER.exception("BLE proxy advertisement forward failed")Then delete the
_to_advertisement_datahelper entirely (the wholedef _to_advertisement_data(...)block at the bottom of the file). Net result on HA side: three attribute reads + one factory call, instead of a seven-field manual translation. Renames or removals onbleak.AdvertisementDatawould surface as a typing error rather than silently dropping fields.Per @bdraco's review comment thread on
_to_advertisement_data.Test plan
python_ble_proxypytest suite passes (8/8, two new tests coverfrom_bleak)ruff check+ruff format --checkcleanmypycleanmatter-ble-proxyrelease: apply the HA diff above in #171384 and verify commissioning still works end-to-end🤖 Generated with Claude Code