blesession 0.1.0a1__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.0a1/CHANGELOG.md +43 -0
  2. blesession-0.1.0a1/LICENSE +21 -0
  3. blesession-0.1.0a1/MANIFEST.in +3 -0
  4. blesession-0.1.0a1/PKG-INFO +113 -0
  5. blesession-0.1.0a1/README.md +83 -0
  6. blesession-0.1.0a1/docs/design.md +429 -0
  7. blesession-0.1.0a1/pyproject.toml +77 -0
  8. blesession-0.1.0a1/setup.cfg +4 -0
  9. blesession-0.1.0a1/src/blesession/__init__.py +62 -0
  10. blesession-0.1.0a1/src/blesession/attempts.py +155 -0
  11. blesession-0.1.0a1/src/blesession/causes.py +103 -0
  12. blesession-0.1.0a1/src/blesession/const.py +14 -0
  13. blesession-0.1.0a1/src/blesession/errors.py +86 -0
  14. blesession-0.1.0a1/src/blesession/hass.py +73 -0
  15. blesession-0.1.0a1/src/blesession/link.py +96 -0
  16. blesession-0.1.0a1/src/blesession/notifications.py +90 -0
  17. blesession-0.1.0a1/src/blesession/py.typed +0 -0
  18. blesession-0.1.0a1/src/blesession/report.py +116 -0
  19. blesession-0.1.0a1/src/blesession/session.py +125 -0
  20. blesession-0.1.0a1/src/blesession/stages.py +59 -0
  21. blesession-0.1.0a1/src/blesession/testing.py +110 -0
  22. blesession-0.1.0a1/src/blesession/trace.py +153 -0
  23. blesession-0.1.0a1/src/blesession.egg-info/PKG-INFO +113 -0
  24. blesession-0.1.0a1/src/blesession.egg-info/SOURCES.txt +29 -0
  25. blesession-0.1.0a1/src/blesession.egg-info/dependency_links.txt +1 -0
  26. blesession-0.1.0a1/src/blesession.egg-info/requires.txt +10 -0
  27. blesession-0.1.0a1/src/blesession.egg-info/top_level.txt +1 -0
  28. blesession-0.1.0a1/tests/test_attempts.py +172 -0
  29. blesession-0.1.0a1/tests/test_causes_and_link.py +66 -0
  30. blesession-0.1.0a1/tests/test_session.py +148 -0
  31. blesession-0.1.0a1/tests/test_trace.py +78 -0
@@ -0,0 +1,43 @@
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.0a1] — 2026-09-22
14
+
15
+ Pre-release for on-device testing with `hass-ble-esl`. The API is what the
16
+ ESL integration shaped; `hass-omron` is next and may still move it.
17
+
18
+ ### Added
19
+
20
+ - `ble_session()`: connect inside the block, bounded disconnect in `finally`,
21
+ `settle_s`, `keep`, `close_stale`; the client class is resolved at connect
22
+ time so Home Assistant's proxy-aware wrapper is picked up regardless of
23
+ import order.
24
+ - `Notifications`: queued replies from one characteristic; every wait names
25
+ its `step`, and `NotificationTimeout` carries it.
26
+ - `SessionTrace` / `traced()`: nested stage timings, the innermost stage a
27
+ failure escaped from, `note()` facts, `stage_map` to the primary vocabulary.
28
+ - Primary stage vocabulary: `unreachable`, `connect`, `session`, `auth`,
29
+ `transfer`, `finish`, `disconnect`.
30
+ - `run_attempts()`: lock held per attempt, attempt bound with `timed_out`,
31
+ `guard` under the lock, `retry_if`, `on_attempt`, `state` carried between
32
+ attempts.
33
+ - `build_report()` / `report_attempt()`: fixed attribute key order, device
34
+ cause callback first, generic sentences as fallback.
35
+ - Errors: `BleSessionError(ConnectionError)` with `Unreachable`,
36
+ `ConnectFailed`, `SessionDropped`, `NotificationTimeout`, `AttemptTimedOut`.
37
+ - `link.py`: the one place that probes bleak / habluetooth internals for the
38
+ radio a link took; `is_proxy()`.
39
+ - `blesession.hass.radio_facts()`: `via`, `via_type`, `rssi`, `paths`,
40
+ `advertised_via` as scanner names (imports Home Assistant lazily).
41
+ - `blesession.testing`: `FakeClient`, `FakeDevice`, `fake_connect()`.
42
+ - `SessionTrace.record()` for a stage measured elsewhere; `NotificationTimeout(message=)`
43
+ 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,113 @@
1
+ Metadata-Version: 2.4
2
+ Name: blesession
3
+ Version: 0.1.0a1
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 :: 2 - Pre-Alpha
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.0.1 — core written, no integration on it yet.** The library is tested
45
+ without Home Assistant (`pytest`); the first adopter is `hass-ble-esl`.
46
+ Read [`docs/design.md`](docs/design.md) for what belongs here, what
47
+ deliberately does not, how a Bluetooth-proxy route works without importing
48
+ Home Assistant, and the rollout plan.
49
+
50
+ The design is extracted from two integrations that already carry this
51
+ instrumentation and have drifted apart:
52
+
53
+ - [`hass-ble-esl`](https://github.com/eigger/hass-ble-esl) — e-paper shelf
54
+ labels (write an image, wait for the panel)
55
+ - [`hass-omron`](https://github.com/eigger/hass-omron) — blood pressure
56
+ monitors (bonded, unlock, read record memory)
57
+
58
+ and is meant to be adopted by the rest of the family (`hass-catprinter`,
59
+ `hass-niimbot`, `hass-gicisky`, `hass-zhsunyco`, `hass-lywsd02`,
60
+ `hass-marklife`, `hass-minibig`, `hass-vson`, …), which today each hand-roll
61
+ the same notification wait and have no failure attribution at all.
62
+
63
+ ## What it provides
64
+
65
+ | piece | one line |
66
+ |---|---|
67
+ | `ble_session()` | connect inside the block, bounded disconnect in `finally`, never masks the real error |
68
+ | `Notifications` | queued replies from one characteristic; every wait names its `step` |
69
+ | `SessionTrace` | nested stage timings; the innermost stage an exception escaped from |
70
+ | stage vocabulary | `unreachable · connect · session · auth · transfer · finish · disconnect`, plus a device `detail` |
71
+ | `run_attempts()` | the lock-per-attempt / fresh-handle-per-attempt contract; policy stays yours |
72
+ | `blesession.hass.radio_facts()` | `via`, `via_type`, `rssi`, `paths`, `advertised_via` as scanner names |
73
+ | `link.py` | the *one* place that probes bleak / habluetooth internals for the radio a link took |
74
+ | `blesession.testing` | `FakeClient` / `fake_connect()` so every integration's tests fake bleak the same way |
75
+ | `build_report()` | fixed attribute key order; generic likely-cause sentences, your device sentences first |
76
+ | errors | `ConnectionError` subclasses so an off device never becomes a traceback |
77
+
78
+ ## What it does not provide
79
+
80
+ Retry counts, backoff, packet pacing, lock scope, bonding and pairing,
81
+ cooldowns, advertisement parsing, protocol frames. Those are the parts each
82
+ integration learned from its own device and keeps.
83
+
84
+ ## Layout
85
+
86
+ ```
87
+ src/blesession/ pure Python + bleak, tested without Home Assistant
88
+ src/blesession/hass.py imports homeassistant lazily; only used inside HA
89
+ docs/design.md the design
90
+ ```
91
+
92
+ ```python
93
+ from blesession import Notifications, SessionTrace, ble_session, build_report, stages
94
+
95
+ trace = SessionTrace(stage_map={"start": stages.AUTH})
96
+ try:
97
+ async with ble_session(ble_device, trace=trace) as client:
98
+ async with Notifications(client, NOTIFY_UUID, settle=0.5) as replies:
99
+ with trace.timed("start"):
100
+ await client.write_gatt_char(WRITE_UUID, START, response=False)
101
+ await replies.next(timeout=5, step="start")
102
+ with trace.timed("transfer"):
103
+ ...
104
+ except ConnectionError as exc: # every session error is one
105
+ report = build_report(operation="write", trace=trace, exc=exc,
106
+ facts=radio_facts(hass, address, trace.link), noun="tag")
107
+ # {'operation': 'write', 'success': False, 'error': ..., 'failed_stage': 'auth',
108
+ # 'failed_detail': 'start', 'likely_cause': ..., 'via': ..., 'connect_s': ..., ...}
109
+ ```
110
+
111
+ ## License
112
+
113
+ MIT
@@ -0,0 +1,83 @@
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.0.1 — core written, no integration on it yet.** The library is tested
15
+ without Home Assistant (`pytest`); the first adopter is `hass-ble-esl`.
16
+ Read [`docs/design.md`](docs/design.md) for what belongs here, what
17
+ deliberately does not, how a Bluetooth-proxy route works without importing
18
+ Home Assistant, and the rollout plan.
19
+
20
+ The design is extracted from two integrations that already carry this
21
+ instrumentation and have drifted apart:
22
+
23
+ - [`hass-ble-esl`](https://github.com/eigger/hass-ble-esl) — e-paper shelf
24
+ labels (write an image, wait for the panel)
25
+ - [`hass-omron`](https://github.com/eigger/hass-omron) — blood pressure
26
+ monitors (bonded, unlock, read record memory)
27
+
28
+ and is meant to be adopted by the rest of the family (`hass-catprinter`,
29
+ `hass-niimbot`, `hass-gicisky`, `hass-zhsunyco`, `hass-lywsd02`,
30
+ `hass-marklife`, `hass-minibig`, `hass-vson`, …), which today each hand-roll
31
+ the same notification wait and have no failure attribution at all.
32
+
33
+ ## What it provides
34
+
35
+ | piece | one line |
36
+ |---|---|
37
+ | `ble_session()` | connect inside the block, bounded disconnect in `finally`, never masks the real error |
38
+ | `Notifications` | queued replies from one characteristic; every wait names its `step` |
39
+ | `SessionTrace` | nested stage timings; the innermost stage an exception escaped from |
40
+ | stage vocabulary | `unreachable · connect · session · auth · transfer · finish · disconnect`, plus a device `detail` |
41
+ | `run_attempts()` | the lock-per-attempt / fresh-handle-per-attempt contract; policy stays yours |
42
+ | `blesession.hass.radio_facts()` | `via`, `via_type`, `rssi`, `paths`, `advertised_via` as scanner names |
43
+ | `link.py` | the *one* place that probes bleak / habluetooth internals for the radio a link took |
44
+ | `blesession.testing` | `FakeClient` / `fake_connect()` so every integration's tests fake bleak the same way |
45
+ | `build_report()` | fixed attribute key order; generic likely-cause sentences, your device sentences first |
46
+ | errors | `ConnectionError` subclasses so an off device never becomes a traceback |
47
+
48
+ ## What it does not provide
49
+
50
+ Retry counts, backoff, packet pacing, lock scope, bonding and pairing,
51
+ cooldowns, advertisement parsing, protocol frames. Those are the parts each
52
+ integration learned from its own device and keeps.
53
+
54
+ ## Layout
55
+
56
+ ```
57
+ src/blesession/ pure Python + bleak, tested without Home Assistant
58
+ src/blesession/hass.py imports homeassistant lazily; only used inside HA
59
+ docs/design.md the design
60
+ ```
61
+
62
+ ```python
63
+ from blesession import Notifications, SessionTrace, ble_session, build_report, stages
64
+
65
+ trace = SessionTrace(stage_map={"start": stages.AUTH})
66
+ try:
67
+ async with ble_session(ble_device, trace=trace) as client:
68
+ async with Notifications(client, NOTIFY_UUID, settle=0.5) as replies:
69
+ with trace.timed("start"):
70
+ await client.write_gatt_char(WRITE_UUID, START, response=False)
71
+ await replies.next(timeout=5, step="start")
72
+ with trace.timed("transfer"):
73
+ ...
74
+ except ConnectionError as exc: # every session error is one
75
+ report = build_report(operation="write", trace=trace, exc=exc,
76
+ facts=radio_facts(hass, address, trace.link), noun="tag")
77
+ # {'operation': 'write', 'success': False, 'error': ..., 'failed_stage': 'auth',
78
+ # 'failed_detail': 'start', 'likely_cause': ..., 'via': ..., 'connect_s': ..., ...}
79
+ ```
80
+
81
+ ## License
82
+
83
+ MIT