blesession 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. blesession-0.1.0/CHANGELOG.md +59 -0
  2. blesession-0.1.0/LICENSE +21 -0
  3. blesession-0.1.0/MANIFEST.in +3 -0
  4. blesession-0.1.0/PKG-INFO +114 -0
  5. blesession-0.1.0/README.md +84 -0
  6. blesession-0.1.0/docs/design.md +429 -0
  7. blesession-0.1.0/pyproject.toml +77 -0
  8. blesession-0.1.0/setup.cfg +4 -0
  9. blesession-0.1.0/src/blesession/__init__.py +62 -0
  10. blesession-0.1.0/src/blesession/attempts.py +147 -0
  11. blesession-0.1.0/src/blesession/causes.py +103 -0
  12. blesession-0.1.0/src/blesession/const.py +14 -0
  13. blesession-0.1.0/src/blesession/errors.py +86 -0
  14. blesession-0.1.0/src/blesession/hass.py +73 -0
  15. blesession-0.1.0/src/blesession/link.py +96 -0
  16. blesession-0.1.0/src/blesession/notifications.py +90 -0
  17. blesession-0.1.0/src/blesession/py.typed +0 -0
  18. blesession-0.1.0/src/blesession/report.py +113 -0
  19. blesession-0.1.0/src/blesession/session.py +125 -0
  20. blesession-0.1.0/src/blesession/stages.py +59 -0
  21. blesession-0.1.0/src/blesession/testing.py +110 -0
  22. blesession-0.1.0/src/blesession/trace.py +177 -0
  23. blesession-0.1.0/src/blesession.egg-info/PKG-INFO +114 -0
  24. blesession-0.1.0/src/blesession.egg-info/SOURCES.txt +29 -0
  25. blesession-0.1.0/src/blesession.egg-info/dependency_links.txt +1 -0
  26. blesession-0.1.0/src/blesession.egg-info/requires.txt +10 -0
  27. blesession-0.1.0/src/blesession.egg-info/top_level.txt +1 -0
  28. blesession-0.1.0/tests/test_attempts.py +188 -0
  29. blesession-0.1.0/tests/test_causes_and_link.py +66 -0
  30. blesession-0.1.0/tests/test_session.py +148 -0
  31. blesession-0.1.0/tests/test_trace.py +112 -0
@@ -0,0 +1,59 @@
1
+ # Changelog
2
+
3
+ All notable changes to blesession. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); the project uses
5
+ [Semantic Versioning](https://semver.org/) — until 1.0, a minor bump may carry
6
+ a behaviour change, and every such change is listed under **Changed** with the
7
+ old and new behaviour.
8
+
9
+ Report keys (`failed_stage`, `likely_cause`, `via`, `<stage>_s`, …) and the
10
+ primary stage names are part of the contract: troubleshooting docs quote
11
+ them, so any change to them is at least a minor bump and is listed here.
12
+
13
+ ## [0.1.0] — 2026-09-22
14
+
15
+ First release. Same code as 0.1.0a2, verified on device with `hass-ble-esl`
16
+ 0.10.0b1 over a Bluetooth proxy: `via` / `via_type` / `advertised_via`,
17
+ `failed_stage` / `failed_detail` / `likely_cause` as designed.
18
+
19
+ ## [0.1.0a2] — 2026-09-22
20
+
21
+ ### Fixed
22
+
23
+ - `build_report()` dropped an error's `detail` whenever the trace had a
24
+ failed stage, so a `ConnectFailed(detail="settle")` inside `connect` lost
25
+ it on the direct path while `report_attempt()` kept it. Both now use one
26
+ rule, `SessionTrace.failure(exc)`: the trace names the stage; the error's
27
+ detail applies when it is about that same stage or the trace has none.
28
+
29
+ ## [0.1.0a1] — 2026-09-22
30
+
31
+ Pre-release for on-device testing with `hass-ble-esl`. The API is what the
32
+ ESL integration shaped; `hass-omron` is next and may still move it.
33
+
34
+ ### Added
35
+
36
+ - `ble_session()`: connect inside the block, bounded disconnect in `finally`,
37
+ `settle_s`, `keep`, `close_stale`; the client class is resolved at connect
38
+ time so Home Assistant's proxy-aware wrapper is picked up regardless of
39
+ import order.
40
+ - `Notifications`: queued replies from one characteristic; every wait names
41
+ its `step`, and `NotificationTimeout` carries it.
42
+ - `SessionTrace` / `traced()`: nested stage timings, the innermost stage a
43
+ failure escaped from, `note()` facts, `stage_map` to the primary vocabulary.
44
+ - Primary stage vocabulary: `unreachable`, `connect`, `session`, `auth`,
45
+ `transfer`, `finish`, `disconnect`.
46
+ - `run_attempts()`: lock held per attempt, attempt bound with `timed_out`,
47
+ `guard` under the lock, `retry_if`, `on_attempt`, `state` carried between
48
+ attempts.
49
+ - `build_report()` / `report_attempt()`: fixed attribute key order, device
50
+ cause callback first, generic sentences as fallback.
51
+ - Errors: `BleSessionError(ConnectionError)` with `Unreachable`,
52
+ `ConnectFailed`, `SessionDropped`, `NotificationTimeout`, `AttemptTimedOut`.
53
+ - `link.py`: the one place that probes bleak / habluetooth internals for the
54
+ radio a link took; `is_proxy()`.
55
+ - `blesession.hass.radio_facts()`: `via`, `via_type`, `rssi`, `paths`,
56
+ `advertised_via` as scanner names (imports Home Assistant lazily).
57
+ - `blesession.testing`: `FakeClient`, `FakeDevice`, `fake_connect()`.
58
+ - `SessionTrace.record()` for a stage measured elsewhere; `NotificationTimeout(message=)`
59
+ for protocols that word their own probe timeouts.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 eigger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include LICENSE README.md CHANGELOG.md
2
+ recursive-include tests *.py
3
+ recursive-include docs *.md
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.4
2
+ Name: blesession
3
+ Version: 0.1.0
4
+ Summary: One BLE session, instrumented: connect, notifications, stage timings, where it failed and why — for Home Assistant BLE integrations.
5
+ Author: eigger
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/eigger/blesession
8
+ Project-URL: Repository, https://github.com/eigger/blesession
9
+ Keywords: ble,bluetooth,bleak,home-assistant,session,diagnostics
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Framework :: AsyncIO
15
+ Classifier: Topic :: System :: Hardware
16
+ Classifier: Operating System :: OS Independent
17
+ Requires-Python: >=3.13
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: bleak>=0.22
21
+ Requires-Dist: bleak-retry-connector>=3.6
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=8.0; extra == "dev"
24
+ Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
25
+ Requires-Dist: pytest-cov>=5.0; extra == "dev"
26
+ Requires-Dist: ruff>=0.6; extra == "dev"
27
+ Requires-Dist: build>=1.2; extra == "dev"
28
+ Requires-Dist: mypy>=1.14; extra == "dev"
29
+ Dynamic: license-file
30
+
31
+ # blesession
32
+
33
+ One BLE session, instrumented — for Home Assistant BLE integrations built on
34
+ [bleak](https://github.com/hbldh/bleak).
35
+
36
+ Connect, subscribe to notifications, exchange frames with per-step timeouts,
37
+ disconnect — and come out the other side with **where the time went, where
38
+ it failed, which radio it went over, and one sentence on what that most
39
+ likely means**, ready to publish as sensor attributes so a failed session at
40
+ 3 am can be read off the entity without debug logging.
41
+
42
+ ## Status
43
+
44
+ **0.1.0 — first release.** `hass-ble-esl` 0.10.0 runs on it, verified on
45
+ device over a Bluetooth proxy; `hass-omron` is next. The library is tested
46
+ without Home Assistant (`pytest`).
47
+ Read [`docs/design.md`](docs/design.md) for what belongs here, what
48
+ deliberately does not, how a Bluetooth-proxy route works without importing
49
+ Home Assistant, and the rollout plan.
50
+
51
+ The design is extracted from two integrations that already carry this
52
+ instrumentation and have drifted apart:
53
+
54
+ - [`hass-ble-esl`](https://github.com/eigger/hass-ble-esl) — e-paper shelf
55
+ labels (write an image, wait for the panel)
56
+ - [`hass-omron`](https://github.com/eigger/hass-omron) — blood pressure
57
+ monitors (bonded, unlock, read record memory)
58
+
59
+ and is meant to be adopted by the rest of the family (`hass-catprinter`,
60
+ `hass-niimbot`, `hass-gicisky`, `hass-zhsunyco`, `hass-lywsd02`,
61
+ `hass-marklife`, `hass-minibig`, `hass-vson`, …), which today each hand-roll
62
+ the same notification wait and have no failure attribution at all.
63
+
64
+ ## What it provides
65
+
66
+ | piece | one line |
67
+ |---|---|
68
+ | `ble_session()` | connect inside the block, bounded disconnect in `finally`, never masks the real error |
69
+ | `Notifications` | queued replies from one characteristic; every wait names its `step` |
70
+ | `SessionTrace` | nested stage timings; the innermost stage an exception escaped from |
71
+ | stage vocabulary | `unreachable · connect · session · auth · transfer · finish · disconnect`, plus a device `detail` |
72
+ | `run_attempts()` | the lock-per-attempt / fresh-handle-per-attempt contract; policy stays yours |
73
+ | `blesession.hass.radio_facts()` | `via`, `via_type`, `rssi`, `paths`, `advertised_via` as scanner names |
74
+ | `link.py` | the *one* place that probes bleak / habluetooth internals for the radio a link took |
75
+ | `blesession.testing` | `FakeClient` / `fake_connect()` so every integration's tests fake bleak the same way |
76
+ | `build_report()` | fixed attribute key order; generic likely-cause sentences, your device sentences first |
77
+ | errors | `ConnectionError` subclasses so an off device never becomes a traceback |
78
+
79
+ ## What it does not provide
80
+
81
+ Retry counts, backoff, packet pacing, lock scope, bonding and pairing,
82
+ cooldowns, advertisement parsing, protocol frames. Those are the parts each
83
+ integration learned from its own device and keeps.
84
+
85
+ ## Layout
86
+
87
+ ```
88
+ src/blesession/ pure Python + bleak, tested without Home Assistant
89
+ src/blesession/hass.py imports homeassistant lazily; only used inside HA
90
+ docs/design.md the design
91
+ ```
92
+
93
+ ```python
94
+ from blesession import Notifications, SessionTrace, ble_session, build_report, stages
95
+
96
+ trace = SessionTrace(stage_map={"start": stages.AUTH})
97
+ try:
98
+ async with ble_session(ble_device, trace=trace) as client:
99
+ async with Notifications(client, NOTIFY_UUID, settle=0.5) as replies:
100
+ with trace.timed("start"):
101
+ await client.write_gatt_char(WRITE_UUID, START, response=False)
102
+ await replies.next(timeout=5, step="start")
103
+ with trace.timed("transfer"):
104
+ ...
105
+ except ConnectionError as exc: # every session error is one
106
+ report = build_report(operation="write", trace=trace, exc=exc,
107
+ facts=radio_facts(hass, address, trace.link), noun="tag")
108
+ # {'operation': 'write', 'success': False, 'error': ..., 'failed_stage': 'auth',
109
+ # 'failed_detail': 'start', 'likely_cause': ..., 'via': ..., 'connect_s': ..., ...}
110
+ ```
111
+
112
+ ## License
113
+
114
+ MIT
@@ -0,0 +1,84 @@
1
+ # blesession
2
+
3
+ One BLE session, instrumented — for Home Assistant BLE integrations built on
4
+ [bleak](https://github.com/hbldh/bleak).
5
+
6
+ Connect, subscribe to notifications, exchange frames with per-step timeouts,
7
+ disconnect — and come out the other side with **where the time went, where
8
+ it failed, which radio it went over, and one sentence on what that most
9
+ likely means**, ready to publish as sensor attributes so a failed session at
10
+ 3 am can be read off the entity without debug logging.
11
+
12
+ ## Status
13
+
14
+ **0.1.0 — first release.** `hass-ble-esl` 0.10.0 runs on it, verified on
15
+ device over a Bluetooth proxy; `hass-omron` is next. The library is tested
16
+ without Home Assistant (`pytest`).
17
+ Read [`docs/design.md`](docs/design.md) for what belongs here, what
18
+ deliberately does not, how a Bluetooth-proxy route works without importing
19
+ Home Assistant, and the rollout plan.
20
+
21
+ The design is extracted from two integrations that already carry this
22
+ instrumentation and have drifted apart:
23
+
24
+ - [`hass-ble-esl`](https://github.com/eigger/hass-ble-esl) — e-paper shelf
25
+ labels (write an image, wait for the panel)
26
+ - [`hass-omron`](https://github.com/eigger/hass-omron) — blood pressure
27
+ monitors (bonded, unlock, read record memory)
28
+
29
+ and is meant to be adopted by the rest of the family (`hass-catprinter`,
30
+ `hass-niimbot`, `hass-gicisky`, `hass-zhsunyco`, `hass-lywsd02`,
31
+ `hass-marklife`, `hass-minibig`, `hass-vson`, …), which today each hand-roll
32
+ the same notification wait and have no failure attribution at all.
33
+
34
+ ## What it provides
35
+
36
+ | piece | one line |
37
+ |---|---|
38
+ | `ble_session()` | connect inside the block, bounded disconnect in `finally`, never masks the real error |
39
+ | `Notifications` | queued replies from one characteristic; every wait names its `step` |
40
+ | `SessionTrace` | nested stage timings; the innermost stage an exception escaped from |
41
+ | stage vocabulary | `unreachable · connect · session · auth · transfer · finish · disconnect`, plus a device `detail` |
42
+ | `run_attempts()` | the lock-per-attempt / fresh-handle-per-attempt contract; policy stays yours |
43
+ | `blesession.hass.radio_facts()` | `via`, `via_type`, `rssi`, `paths`, `advertised_via` as scanner names |
44
+ | `link.py` | the *one* place that probes bleak / habluetooth internals for the radio a link took |
45
+ | `blesession.testing` | `FakeClient` / `fake_connect()` so every integration's tests fake bleak the same way |
46
+ | `build_report()` | fixed attribute key order; generic likely-cause sentences, your device sentences first |
47
+ | errors | `ConnectionError` subclasses so an off device never becomes a traceback |
48
+
49
+ ## What it does not provide
50
+
51
+ Retry counts, backoff, packet pacing, lock scope, bonding and pairing,
52
+ cooldowns, advertisement parsing, protocol frames. Those are the parts each
53
+ integration learned from its own device and keeps.
54
+
55
+ ## Layout
56
+
57
+ ```
58
+ src/blesession/ pure Python + bleak, tested without Home Assistant
59
+ src/blesession/hass.py imports homeassistant lazily; only used inside HA
60
+ docs/design.md the design
61
+ ```
62
+
63
+ ```python
64
+ from blesession import Notifications, SessionTrace, ble_session, build_report, stages
65
+
66
+ trace = SessionTrace(stage_map={"start": stages.AUTH})
67
+ try:
68
+ async with ble_session(ble_device, trace=trace) as client:
69
+ async with Notifications(client, NOTIFY_UUID, settle=0.5) as replies:
70
+ with trace.timed("start"):
71
+ await client.write_gatt_char(WRITE_UUID, START, response=False)
72
+ await replies.next(timeout=5, step="start")
73
+ with trace.timed("transfer"):
74
+ ...
75
+ except ConnectionError as exc: # every session error is one
76
+ report = build_report(operation="write", trace=trace, exc=exc,
77
+ facts=radio_facts(hass, address, trace.link), noun="tag")
78
+ # {'operation': 'write', 'success': False, 'error': ..., 'failed_stage': 'auth',
79
+ # 'failed_detail': 'start', 'likely_cause': ..., 'via': ..., 'connect_s': ..., ...}
80
+ ```
81
+
82
+ ## License
83
+
84
+ MIT