Skip to main content

ble-bond

CI PyPI License: MIT

Bonded Bluetooth LE sessions for devices that pair in a button window and answer one request at a time.

Some devices accept a new bond only for a few seconds after a button is pressed, drop any link that is not encrypted, and then answer requests with no request id, so a late or stray reply can be taken for the wrong request. Reaching them through a Home Assistant Bluetooth proxy adds its own traps: the bond lives on one proxy, Home Assistant picks the proxy for each attempt, and a refusal through the wrong proxy says nothing about the bond. ble-bond handles that part, built on bleak and bleak-retry-connector, and leaves framing and the device's protocol to the device library.

Used by grundfos-ble (Grundfos ALPHA2 GO pumps) and mira-ble (Mira digital showers).

What it does

  • Pairs on every connection: on an ESPHome proxy a quick re-encrypt with the stored bond, real pairing otherwise. A refusal (ESP-IDF reason 82, BlueZ AuthenticationFailed) raises PairingRequired; a proxy that cannot pair at all raises PairingUnsupported.
  • Records which adapter or proxy each attempt went through (last_source), refused attempts included, so a refusal can be pinned on the path that saw it.
  • On a local Linux adapter, registers a BlueZ pairing agent, one at a time, only while a new bond is expected; remove_bluez_bond makes BlueZ forget a bond the device dropped.
  • Runs one request at a time. A frame is taken as the reply only when the caller's check accepts it; a request that does not finish (timeout, cancellation, failed write) closes the link, so its late reply cannot answer the next request.
  • What differs between devices (pauses, retries, how weak evidence about the bond is reported, write size, connection parameters) is one LinkPolicy.

Usage

import asyncio

from bleak import BleakScanner
from ble_bond import BondedLink, LinkPolicy, PairingRequired

REPLY_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb"


class LengthFramed:
    """The device's framing: here, a length byte that counts the whole frame."""

    def __init__(self) -> None:
        self.buffer = bytearray()

    def feed(self, data: bytes | bytearray) -> list[bytes]:
        self.buffer += data
        frames = []
        while self.buffer and len(self.buffer) >= self.buffer[0] > 0:
            frames.append(bytes(self.buffer[: self.buffer[0]]))
            del self.buffer[: self.buffer[0]]
        return frames

    def reset(self) -> None:
        self.buffer.clear()


async def main() -> None:
    device = await BleakScanner.find_device_by_address("AA:BB:CC:DD:EE:FF")
    policy = LinkPolicy(settle_after_subscribe=0.5)
    try:
        async with BondedLink(
            device, notify_uuid=REPLY_UUID, assembler=LengthFramed(), policy=policy
        ) as link:
            reply = await link.request(b"\x02\x01", accept=lambda frame: frame[1] == 0x01)
            print(reply.hex(" "), "via", link.last_source)
    except PairingRequired:
        print("Press the device's button, then connect with expect_new_bond=True.")


asyncio.run(main())

ble_bond.testing has a fake bleak client and a patch for the connection call, so a device library can test its transport without hardware.

Compatibility

Every 1.x release is backward compatible: several device libraries may be installed in one Home Assistant, which then holds a single copy of this package. Depend on ble-bond>=1.0,<2.

Acknowledgements

The BlueZ pairing agent is adapted from alpha-hwr (MIT); its notice is in THIRD_PARTY_NOTICES.md. The pause after subscribing follows ESPHome's measurement in esphome/esphome#22651.

Licence

MIT

Metadata

Release files for ble-bond 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ble-bond 1.0.0
File Size Uploaded Provenance Actions
ble_bond-1.0.0.tar.gz 25.0 kB With provenance Details for ble_bond-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ble-bond 1.0.0
File Interpreter ABI Platform Actions
ble_bond-1.0.0-py3-none-any.whl Python 3 none any Details for ble_bond-1.0.0-py3-none-any.whl

Total release size: 45.8 kB

Release files / ble_bond-1.0.0.tar.gz

Download URL ble_bond-1.0.0.tar.gz
Size 25.0 kB
Tags Source
SHA-256 checksum
How to use checksums
00b836e7fbdb7971ff6029fb81ebf15230e63ada4c7b9c665e405c3f4e98b7b0
BLAKE2b-256 checksum
How to use checksums
e73c1bcf2e6d3b339796f7498992ce871193e0c42450e69f1f94eb633d15ae5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / ble_bond-1.0.0-py3-none-any.whl

Download URL ble_bond-1.0.0-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8bf3d7b09077d4cc88c7e547e0f9a4049787f9cd1f9484c2140e109b69d6591
BLAKE2b-256 checksum
How to use checksums
28fa61fd5e34d5a982ae05e7802026a4c4618c487bff8ce458658925fa66e615
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for ble-bond 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ble_bond-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page