Debian Codegraph @

docs/home_assistant_integration.md

raw
1 # Wiring the DBus-free local scanner into Home Assistant (experimental)
2
3 > **Experimental.** The management/L2CAP path (`HaScannerMgmt`, `HaMgmtClient`,
4 > `create_local_scanner`) is a work in progress; the API may change or be
5 > renamed. Nothing here is on by default — Home Assistant still constructs the
6 > bleak-backed `HaScanner` until this wiring lands.
7
8 The DBus-free path drives a local adapter entirely over the BlueZ management
9 socket and a raw L2CAP ATT channel (discovery, connect, pairing/bonding),
10 bypassing bluetoothd/DBus. `habluetooth` exposes everything Home Assistant needs
11 to opt in; the only change is in `homeassistant/components/bluetooth`.
12
13 ## 1. Select the scanner via the factory
14
15 Today the bluetooth component constructs the scanner directly
16 (`homeassistant/components/bluetooth/__init__.py`):
17
18 ```python
19 scanner = HaScanner(mode, adapter, address)
20 ```
21
22 Swap that for the factory. It returns `HaScannerMgmt` when the management path is
23 usable, and falls back to the bleak-backed `HaScanner` otherwise, so it is safe
24 everywhere:
25
26 ```python
27 from habluetooth import create_local_scanner
28
29 scanner = create_local_scanner(mode, adapter, address)
30 ```
31
32 The returned object is a `BaseHaScanner`, so the rest of the flow is unchanged:
33 `scanner.async_setup()`, `async_register_scanner(hass, scanner, connection_slots=slots)`,
34 then `await scanner.async_start()`. The advertisement side-channel, connection
35 slots, and registration all work as before.
36
37 ### When the factory picks the mgmt scanner
38
39 `create_local_scanner` returns `HaScannerMgmt` only when **all** hold; otherwise
40 it returns `HaScanner`:
41
42 - the host is Linux,
43 - the management socket is available and reports discovery capability
44 (`CAP_NET_ADMIN`/`CAP_NET_RAW`),
45 - the adapter is an `hciN` adapter,
46 - a real adapter BD_ADDR is known (not `DEFAULT_ADDRESS`),
47 - the mode is not `AUTO` (the mgmt scanner does not yet implement
48 active-window promotion, so `AUTO` stays on the bleak path).
49
50 Note Home Assistant already converts `AUTO` to `ACTIVE` for adapters that cannot
51 do passive scanning before this call, so on those adapters the mgmt scanner is
52 eligible.
53
54 ### Recommended gating
55
56 Because the path is experimental, gate the swap behind an opt-in (a config/YAML
57 flag or an environment check) so it is off by default and a user can enable it
58 per install. Fall back to the plain `HaScanner` constructor when the flag is
59 off.
60
61 ### Controller ownership (required before enabling the connect path)
62
63 The LE long-term-key table belongs to the **controller and its kernel**, not to
64 a process or container. `AF_BLUETOOTH`/HCI control sockets are not
65 network-namespaced, so a `habluetooth` running in the Home Assistant container
66 and a `bluetoothd` running on the host share the **same** `hciN` and the **same**
67 kernel bond table. The typical deployment (HA in a container, host `bluetoothd`)
68 is therefore a single shared controller, not two isolated ones.
69
70 The mgmt **discovery** side already coexists with host `bluetoothd` (it only
71 reads advertisement events; that is the existing side channel). The mgmt
72 **connect/pair** path does not: `LOAD_LONG_TERM_KEYS`, mgmt pairing, and the
73 L2CAP ACL/GATT are stateful on the shared controller and collide with a
74 `bluetoothd` that is still managing the same `hciN`. In particular
75 `LOAD_LONG_TERM_KEYS` replaces the controller's whole key list, clearing the
76 bonds `bluetoothd` loaded; those keys cannot be recovered (DBus never exposes
77 key material, and `bluetoothd`'s `/var/lib/bluetooth` store is not reachable from
78 the HA container).
79
80 So the model is **per-adapter partitioning**, never co-management of one `hciN`:
81
82 - Controllers `bluetoothd` manages stay on the bleak/DBus path (today's
83 behavior); their bonds remain `bluetoothd`'s and are untouched.
84 - The mgmt connect path may own a controller only if `bluetoothd` is **not**
85 managing it (for example a dedicated adapter the host bluetooth service is
86 configured to ignore). It then creates its own bonds from the first pairing,
87 so `LOAD_LONG_TERM_KEYS` only ever replaces its own list.
88
89 > **Factory gap.** `create_local_scanner` currently gates only on mgmt discovery
90 > capability and L2CAP availability; it has **no** signal for "this controller is
91 > not also managed by `bluetoothd`." Until that exclusivity gate exists, treat
92 > the connect path as safe only on a controller you know `bluetoothd` does not
93 > manage, and keep the opt-in above scoped to such adapters.
94
95 ## 2. Persist bonds across restarts
96
97 Pairing over mgmt yields long-term keys that `habluetooth` keeps **in memory**
98 only. To survive a restart without re-pairing, the component persists them with
99 its own `Store` and seeds them back on startup. The scanner exposes the hooks;
100 see [Bond key persistence](bond_persistence.md) for the full worked example. In
101 short, for each scanner that is an `HaScannerMgmt`:
102
103 ```python
104 from habluetooth import HaScannerMgmt
105
106 if isinstance(scanner, HaScannerMgmt):
107 bond_storage.async_attach(scanner) # restore_long_term_keys + change callback
108 ```
109
110 Attach **before** `async_start()` so restored keys are loaded before the first
111 connection, and clear the change callback (`set_long_term_keys_changed_callback(None)`)
112 when the scanner is torn down.
113
114 ## What does not change
115
116 - Scanner registration, connection-slot accounting, and the unavailable-tracking
117 timer are inherited from `BaseHaScanner` and behave as before.
118 - Remote (ESPHome) scanners and non-Linux/macOS local adapters are untouched;
119 the factory returns the existing `HaScanner` for them.
120 - No public API is removed; `HaScanner` remains available for the fallback path.