feat: connection-state callbacks for availability tracking - #18
feat: connection-state callbacks for availability tracking#18bluetoothbot wants to merge 4 commits into
Conversation
The auto-reconnect loop silently handled drops and restorations, leaving consumers (e.g. a Home Assistant integration) with no way to observe device availability. Add add_connection_callback(fn) and an available property: fires True after login (including auto-reconnect), False on connection loss, deduped to transitions only.
979c915 to
73f4d2e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Correction to this PR's own description: the auto-generated Quality Report footer says Re-verified today:
The same false footer is on all six open Kōan PRs here. Ignore it when reviewing. |
_close_transport() is the reconnect loop's teardown path and does not go through _on_connection_lost(), so availability stayed latched True while the connection was gone — including forever if reconnects keep failing. A login can also win a race against an already-fired _on_connection_lost and re-latch True on a dead transport. Notify unavailability from _close_transport() itself, which every teardown path (run loop, failed reconnect, disconnect) funnels through. The disconnect() notification is now redundant and removed.
|
Pushed Defect:
Fix: notify unavailability from Tests: two regression tests added, both failing before the fix. Branch: 162 pass, |
A TLS transport.close() completes asynchronously — the peer's close_notify (or the shutdown timeout) can land seconds later, well after the reconnect loop established a replacement connection. The detached protocol still held a reference to the connection, so its late connection_lost() cleared _connected, fired the availability callbacks with False, and set _connection_lost_event on a healthy connection, tearing it down and restarting the backoff loop. The protocol now drops the callback once close() has detached its transport.
|
Pushed a second fix to this branch ( Defect: a superseded transport could clobber a healthy connection's state.
Worst case is a reconnect loop that keeps killing its own successful reconnects, with Fix: Testing: new regression test Note: the auto-generated Quality Report block in the PR body above reads |
What
Add connection-state (availability) callbacks to
ThermostatConnection.Why
The auto-reconnect loop handled connection drops and restorations silently. Consumers — notably a Home Assistant integration — had no way to know when the thermostat went offline or came back, so entities couldn't reflect availability. Event callbacks only fire on incoming messages, never on lifecycle transitions.
How
add_connection_callback(fn)registersfn(available: bool), returning an unregister callable (mirrorsadd_event_callback).availableproperty: True when connected and authenticated.Trueafterlogin()succeeds (including post-reconnect logins),Falseon connection loss and explicitdisconnect().Testing
Quality Report
Changes: 3 files changed, 237 insertions(+), 12 deletions(-)
Code scan: 1 issue(s) found
README.md:141— debug print statementTests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan