| 1 |
# Bond key persistence (experimental) |
| 2 |
|
| 3 |
> **Experimental.** This covers the DBus-free management/L2CAP path |
| 4 |
> (`HaScannerMgmt` + `HaMgmtClient`). The API is not stable and may change or be |
| 5 |
> renamed. Home Assistant does not select this path yet. |
| 6 |
|
| 7 |
When pairing over the BlueZ management socket, the kernel hands back a |
| 8 |
long-term key (LTK) per bond. `habluetooth` keeps these **in memory only** (it is |
| 9 |
a library and does no disk I/O), so without help they are lost on restart and |
| 10 |
the peer must be re-paired. To survive a restart, the consumer (Home Assistant) |
| 11 |
persists them and seeds them back on startup. |
| 12 |
|
| 13 |
Keys are **per adapter**: an LTK is tied to the local adapter identity it paired |
| 14 |
with, so it is only valid on that adapter. Persist keyed by adapter. |
| 15 |
|
| 16 |
## What `habluetooth` provides |
| 17 |
|
| 18 |
- `LongTermKey` — the bonded key (`habluetooth.LongTermKey`). |
| 19 |
- `long_term_key_to_dict()` / `long_term_key_from_dict()` — JSON-safe |
| 20 |
(de)serialization (the `bytes` fields become hex strings), shaped like the |
| 21 |
discovery-cache helpers. |
| 22 |
- On `HaScannerMgmt`: |
| 23 |
- `export_long_term_keys() -> list[LongTermKey]` — snapshot for saving. |
| 24 |
- `restore_long_term_keys(keys)` — seed the store on startup. |
| 25 |
- `set_long_term_keys_changed_callback(callback)` — `callback` fires whenever |
| 26 |
the store changes (a pair captured a key, or an unpair removed one), so the |
| 27 |
consumer can schedule a debounced save. |
| 28 |
|
| 29 |
## Wiring it in Home Assistant |
| 30 |
|
| 31 |
Mirror the existing remote-scanner storage (`Store` + `async_delay_save`), but |
| 32 |
keyed by adapter and holding a list of serialized keys: |
| 33 |
|
| 34 |
```python |
| 35 |
from habluetooth import ( |
| 36 |
HaScannerMgmt, |
| 37 |
LongTermKeyDict, |
| 38 |
long_term_key_from_dict, |
| 39 |
long_term_key_to_dict, |
| 40 |
) |
| 41 |
from homeassistant.helpers.storage import Store |
| 42 |
|
| 43 |
BOND_STORAGE_VERSION = 1 |
| 44 |
BOND_STORAGE_KEY = "bluetooth.bonds" |
| 45 |
SAVE_DELAY = 5 |
| 46 |
|
| 47 |
StoredBonds = dict[str, list[LongTermKeyDict]] # keyed by adapter (e.g. "hci0") |
| 48 |
|
| 49 |
|
| 50 |
class BondStorage: |
| 51 |
def __init__(self, hass): |
| 52 |
self._store: Store[StoredBonds] = Store( |
| 53 |
hass, BOND_STORAGE_VERSION, BOND_STORAGE_KEY |
| 54 |
) |
| 55 |
self._data: StoredBonds = {} |
| 56 |
|
| 57 |
async def async_setup(self): |
| 58 |
self._data = await self._store.async_load() or {} |
| 59 |
|
| 60 |
def async_attach(self, scanner: HaScannerMgmt): |
| 61 |
adapter = scanner.adapter |
| 62 |
# Seed the scanner with any keys we saved last run. |
| 63 |
if saved := self._data.get(adapter): |
| 64 |
scanner.restore_long_term_keys( |
| 65 |
long_term_key_from_dict(key) for key in saved |
| 66 |
) |
| 67 |
# Save (debounced) whenever the scanner's bonds change. |
| 68 |
scanner.set_long_term_keys_changed_callback(lambda: self._async_save(scanner)) |
| 69 |
|
| 70 |
def _async_save(self, scanner: HaScannerMgmt): |
| 71 |
self._data[scanner.adapter] = [ |
| 72 |
long_term_key_to_dict(key) for key in scanner.export_long_term_keys() |
| 73 |
] |
| 74 |
self._store.async_delay_save(lambda: self._data, SAVE_DELAY) |
| 75 |
``` |
| 76 |
|
| 77 |
Call `async_attach()` when the scanner is created and `set_long_term_keys_changed_callback(None)` if it is torn down. |