pylumencache 0.0.1__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 (46) hide show
  1. pylumencache-0.0.1/.github/workflows/ci.yml +37 -0
  2. pylumencache-0.0.1/.github/workflows/release.yml +30 -0
  3. pylumencache-0.0.1/.gitignore +93 -0
  4. pylumencache-0.0.1/LICENSE +202 -0
  5. pylumencache-0.0.1/PKG-INFO +165 -0
  6. pylumencache-0.0.1/README.md +131 -0
  7. pylumencache-0.0.1/SECURITY.md +16 -0
  8. pylumencache-0.0.1/docs/asyncapi.yaml +1088 -0
  9. pylumencache-0.0.1/docs/protocol.md +376 -0
  10. pylumencache-0.0.1/pyproject.toml +92 -0
  11. pylumencache-0.0.1/src/pylumencache/__init__.py +13 -0
  12. pylumencache-0.0.1/src/pylumencache/api.py +73 -0
  13. pylumencache-0.0.1/src/pylumencache/domain/__init__.py +1 -0
  14. pylumencache-0.0.1/src/pylumencache/domain/commands.py +20 -0
  15. pylumencache-0.0.1/src/pylumencache/domain/events.py +48 -0
  16. pylumencache-0.0.1/src/pylumencache/domain/model.py +178 -0
  17. pylumencache-0.0.1/src/pylumencache/obfuscate.py +84 -0
  18. pylumencache-0.0.1/src/pylumencache/py.typed +0 -0
  19. pylumencache-0.0.1/src/pylumencache/transport.py +46 -0
  20. pylumencache-0.0.1/src/pylumencache/transports/__init__.py +1 -0
  21. pylumencache-0.0.1/src/pylumencache/transports/fallback.py +181 -0
  22. pylumencache-0.0.1/src/pylumencache/transports/mqtt/__init__.py +4 -0
  23. pylumencache-0.0.1/src/pylumencache/transports/mqtt/adapter.py +59 -0
  24. pylumencache-0.0.1/src/pylumencache/transports/mqtt/client.py +200 -0
  25. pylumencache-0.0.1/src/pylumencache/transports/mqtt/fake.py +118 -0
  26. pylumencache-0.0.1/src/pylumencache/transports/mqtt/wire/__init__.py +1 -0
  27. pylumencache-0.0.1/src/pylumencache/transports/mqtt/wire/codes.py +27 -0
  28. pylumencache-0.0.1/src/pylumencache/transports/mqtt/wire/commands.py +192 -0
  29. pylumencache-0.0.1/src/pylumencache/transports/mqtt/wire/models.py +219 -0
  30. pylumencache-0.0.1/src/pylumencache/transports/mqtt/wire/parser.py +136 -0
  31. pylumencache-0.0.1/tests/conftest.py +12 -0
  32. pylumencache-0.0.1/tests/domain/test_model.py +131 -0
  33. pylumencache-0.0.1/tests/fixtures/inventory.jsonl +15 -0
  34. pylumencache-0.0.1/tests/fixtures/messages.jsonl +11 -0
  35. pylumencache-0.0.1/tests/test_api.py +70 -0
  36. pylumencache-0.0.1/tests/test_obfuscation.py +66 -0
  37. pylumencache-0.0.1/tests/transports/mqtt/test_adapter.py +60 -0
  38. pylumencache-0.0.1/tests/transports/mqtt/test_client.py +209 -0
  39. pylumencache-0.0.1/tests/transports/mqtt/test_commands.py +38 -0
  40. pylumencache-0.0.1/tests/transports/mqtt/test_fake.py +88 -0
  41. pylumencache-0.0.1/tests/transports/mqtt/test_generator.py +36 -0
  42. pylumencache-0.0.1/tests/transports/mqtt/test_parser.py +110 -0
  43. pylumencache-0.0.1/tests/transports/test_fallback.py +249 -0
  44. pylumencache-0.0.1/tools/capture.py +273 -0
  45. pylumencache-0.0.1/tools/gen_protocol.py +353 -0
  46. pylumencache-0.0.1/tools/watch.py +90 -0
@@ -0,0 +1,37 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ matrix:
13
+ python-version: ["3.12", "3.13"]
14
+ steps:
15
+ - uses: actions/checkout@v5
16
+ - uses: actions/setup-python@v6
17
+ with:
18
+ python-version: ${{ matrix.python-version }}
19
+ - run: pip install -e ".[dev]"
20
+ - run: ruff check .
21
+ - run: mypy
22
+ - name: Layers respect their import boundaries
23
+ run: lint-imports
24
+ - name: Generated files are up to date and the spec is valid
25
+ run: python tools/gen_protocol.py --check
26
+ - run: pytest
27
+
28
+ build:
29
+ runs-on: ubuntu-latest
30
+ steps:
31
+ - uses: actions/checkout@v5
32
+ - uses: actions/setup-python@v6
33
+ with:
34
+ python-version: "3.12"
35
+ - run: pip install build twine
36
+ - run: python -m build
37
+ - run: twine check dist/*
@@ -0,0 +1,30 @@
1
+ name: Release
2
+
3
+ # Publishes to PyPI when a tag such as v0.1.0 is pushed. Uses PyPI trusted
4
+ # publishing, so no token is stored. One-time setup on pypi.org: add a pending
5
+ # publisher for this repository, workflow "release.yml", environment "pypi".
6
+
7
+ on:
8
+ push:
9
+ tags: ["v*"]
10
+
11
+ jobs:
12
+ publish:
13
+ runs-on: ubuntu-latest
14
+ environment: pypi
15
+ permissions:
16
+ id-token: write
17
+ steps:
18
+ - uses: actions/checkout@v5
19
+ - uses: actions/setup-python@v6
20
+ with:
21
+ python-version: "3.12"
22
+ - run: pip install -e ".[dev]" build
23
+ - run: python tools/gen_protocol.py --check
24
+ - run: pytest
25
+ - name: Tag matches the package version
26
+ run: |
27
+ version=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
28
+ test "v$version" = "$GITHUB_REF_NAME" || { echo "tag $GITHUB_REF_NAME does not match version $version"; exit 1; }
29
+ - run: python -m build
30
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,93 @@
1
+ # ---------------------------------------------------------------------
2
+ # Secrets and credentials (never commit these)
3
+ # ---------------------------------------------------------------------
4
+ .env
5
+ .env.*
6
+ !.env.example
7
+ secrets.yaml
8
+ *.pem
9
+ *.key
10
+ *.crt
11
+ *.p12
12
+ passwd
13
+ mosquitto_passwd
14
+
15
+ # ---------------------------------------------------------------------
16
+ # Raw protocol captures
17
+ # Raw captures can contain MACs, names and credentials-adjacent data.
18
+ # Only redacted, reviewed files belong in fixtures/ (which IS committed).
19
+ # ---------------------------------------------------------------------
20
+ capture*.jsonl
21
+ captures/
22
+ *.capture
23
+ *.pcap
24
+
25
+ # ---------------------------------------------------------------------
26
+ # Python (pylumencache, integration, tools)
27
+ # ---------------------------------------------------------------------
28
+ __pycache__/
29
+ *.py[cod]
30
+ *.egg-info/
31
+ .eggs/
32
+ build/
33
+ dist/
34
+ .venv/
35
+ venv/
36
+ .python-version
37
+
38
+ # Test, lint and type-check caches
39
+ .pytest_cache/
40
+ .mypy_cache/
41
+ .ruff_cache/
42
+ .hypothesis/
43
+ .coverage
44
+ .coverage.*
45
+ coverage.xml
46
+ htmlcov/
47
+
48
+ # ---------------------------------------------------------------------
49
+ # Home Assistant development instance (devcontainer / docker)
50
+ # ---------------------------------------------------------------------
51
+ /config/*
52
+ !/config/configuration.yaml
53
+ home-assistant.log*
54
+ home-assistant_v2.db*
55
+ .storage/
56
+ .cloud/
57
+ .HA_VERSION
58
+ OZW_Log.txt
59
+
60
+ # ---------------------------------------------------------------------
61
+ # Lovelace card (TypeScript + Lit)
62
+ # The final built bundle is committed under
63
+ # custom_components/lumencache/www/ (HACS needs it); intermediate
64
+ # build output and tooling are not.
65
+ # ---------------------------------------------------------------------
66
+ node_modules/
67
+ card/dist/
68
+ card/.vite/
69
+ card/coverage/
70
+ *.tsbuildinfo
71
+ npm-debug.log*
72
+ yarn-debug.log*
73
+ yarn-error.log*
74
+ .pnpm-debug.log*
75
+
76
+ # Browser tests (Playwright)
77
+ test-results/
78
+ playwright-report/
79
+ playwright/.cache/
80
+
81
+ # ---------------------------------------------------------------------
82
+ # Editors, OS and misc
83
+ # ---------------------------------------------------------------------
84
+ .vscode/*
85
+ !.vscode/extensions.json
86
+ !.vscode/settings.json
87
+ .idea/
88
+ *.swp
89
+ *~
90
+ .DS_Store
91
+ Thumbs.db
92
+ *.log
93
+
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,165 @@
1
+ Metadata-Version: 2.5
2
+ Name: pylumencache
3
+ Version: 0.0.1
4
+ Summary: Unofficial Python client for LumenCache ReNetA lighting
5
+ Project-URL: Homepage, https://github.com/vamsi765/pylumencache
6
+ Project-URL: Issues, https://github.com/vamsi765/pylumencache/issues
7
+ Project-URL: Integration, https://github.com/vamsi765/ha-lumencache
8
+ Author: Vamsi Krishna Adsumilli
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: home-assistant,lighting,lumencache,mqtt,reneta
12
+ Classifier: Development Status :: 2 - Pre-Alpha
13
+ Classifier: Framework :: AsyncIO
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Home Automation
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: aiomqtt>=2.0
22
+ Provides-Extra: capture
23
+ Requires-Dist: paho-mqtt; extra == 'capture'
24
+ Provides-Extra: dev
25
+ Requires-Dist: import-linter; extra == 'dev'
26
+ Requires-Dist: jsonschema; extra == 'dev'
27
+ Requires-Dist: mypy; extra == 'dev'
28
+ Requires-Dist: paho-mqtt; extra == 'dev'
29
+ Requires-Dist: pytest; extra == 'dev'
30
+ Requires-Dist: pyyaml; extra == 'dev'
31
+ Requires-Dist: ruff; extra == 'dev'
32
+ Requires-Dist: types-pyyaml; extra == 'dev'
33
+ Description-Content-Type: text/markdown
34
+
35
+ # pylumencache
36
+
37
+ Unofficial Python library for [LumenCache](https://www.lumencache.com) ReNetA lighting, which is controlled over a hosted MQTT broker. It is the protocol layer behind the [ha-lumencache](https://github.com/vamsi765/ha-lumencache) Home Assistant integration and has no Home Assistant dependency.
38
+
39
+ > **Unofficial.** This is an independent, community project. It is not made, endorsed or supported by LumenCache.
40
+
41
+ > **Status: pre-alpha.** The protocol was worked out from captures, and the library covers it: typed messages and commands, an async client, a system model and a fake controller. The API may change before the first release, and the library is not on PyPI yet.
42
+
43
+ ## Contents
44
+
45
+ | Path | What it is |
46
+ |---|---|
47
+ | `docs/asyncapi.yaml` | The protocol, as an AsyncAPI 3.0 description. The single source of truth for message codes and payloads |
48
+ | `docs/protocol.md` | Prose notes and open questions. Its code tables are generated from the YAML |
49
+ | `src/pylumencache/api.py` | `LumenCache`: the public entry point. Owns a `SystemModel`, keeps it fed from a transport, and sends commands. Use `LumenCache.mqtt(...)` for the hosted broker |
50
+ | `src/pylumencache/domain/` | What the library is about, with no wire format in it: `model.py` (`SystemModel`: devices, and the last known state of zones and lights; saves to and loads from JSON, because the system does not retain its inventory), `events.py` (`DeviceAnnounced`, `DeviceSeen`, `LightChanged`) and `commands.py` (`SetLight`) |
51
+ | `src/pylumencache/transport.py` | The `Transport` interface: what a way of reaching a system must provide (start, stop, connection state, domain events in, domain commands out) |
52
+ | `src/pylumencache/transports/mqtt/` | The MQTT transport. `client.py` (`MqttTransport`: TLS, reconnects, publishes to `cmd`), `adapter.py` (messages to domain events, domain commands to messages), `fake.py` (`FakeController`: a fake ReNetA system that replays a capture, for developing and testing without lights; pass `controller.factory` as `client_factory`) |
53
+ | `src/pylumencache/transports/mqtt/wire/` | The MQTT wire format: `codes.py` (the `Code` enum), `commands.py` (one builder per documented command, such as `zone_control(zone_id=250, brightness=50)`) and `models.py` (a dataclass per received message), all generated from the YAML, and `parser.py` (`parse(topic, payload)` returns a typed message, or an `UnknownMessage` for anything unrecognised) |
54
+ | `src/pylumencache/obfuscate.py` | `Obfuscator`: replaces ids, serials, free text and MACs with stable fakes. Used by the capture tool for its files and by `watch.py` for the screen |
55
+ | `tools/watch.py` | Prints every parsed message, from a real system or, with `--replay FILE`, from a capture. Ids are replaced with fakes unless `--no-obfuscate` |
56
+ | `tools/gen_protocol.py` | Regenerates the tables in `protocol.md` and `codes.py`, `commands.py` and `models.py`; `--check` fails when either is stale |
57
+ | `tools/capture.py` | Records and sends MQTT traffic |
58
+
59
+ ## Setup
60
+
61
+ ```bash
62
+ python3 -m venv .venv
63
+ source .venv/bin/activate # Windows: .venv\Scripts\activate
64
+ pip install -e ".[dev]"
65
+ python3 tools/gen_protocol.py --check
66
+ ruff check . && mypy && lint-imports && pytest # what CI runs
67
+ ```
68
+
69
+ ## Layers
70
+
71
+ ```
72
+ api.py LumenCache: what an integration uses
73
+ transports/ ways of reaching a system (MQTT today)
74
+ mqtt/ client, adapter, wire format
75
+ transport.py the Transport interface
76
+ domain/ model, events, commands: no wire format
77
+ ```
78
+
79
+ A module imports only from the layers below it. A transport turns its wire format into domain events and domain commands into wire messages, so the model and the code above it never see a topic, an event code or a URL. `lint-imports` checks this in CI (see `[tool.importlinter]` in `pyproject.toml`), and only the MQTT transport may import `aiomqtt`. A new transport is a new folder under `transports/` that implements `Transport`.
80
+
81
+ ## Several transports
82
+
83
+ A `LumenCache` has one transport, and that transport can be a `FallbackTransport`: an ordered list of transports in which a later one is connected only while the earlier ones are down.
84
+
85
+ ```python
86
+ from pylumencache import LumenCache
87
+
88
+ cache = LumenCache.with_fallback([first, second], fail_after=30) # the list is the priority order
89
+ ```
90
+
91
+ - Only the first transport is started at first. If nothing is connected after `fail_after` seconds, the next one is started, and so on down the list.
92
+ - The earlier transports keep trying in the background. As soon as one connects, every transport after it is stopped, so a later transport is connected only while all earlier ones are down.
93
+ - Events and commands use the highest-priority transport that is connected, so the model never gets two sources at once. If a command is not supported by that transport, the next connected one is tried.
94
+ - "Down" means not connected. A transport that is connected but silent counts as up.
95
+ - `cache.transport.active` is the transport in use. Connection listeners hear `True` when some transport is connected and `False` when none is, not each switch.
96
+
97
+ The fallback code (`transports/fallback.py`) knows nothing about any particular transport. It works with anything that implements `Transport`, and the import check enforces that.
98
+
99
+ ## Using the library
100
+
101
+ ```python
102
+ import asyncio
103
+ from pylumencache import LumenCache, SetLight
104
+
105
+ async def main():
106
+ cache = LumenCache.mqtt("<host>", "<Prefix Topic>", username="<user>", password="<pass>")
107
+ cache.model.add_listener(print) # called with a Change when a device, zone or light changes
108
+ await cache.start() # connects in the background and reconnects if it drops
109
+ await cache.wait_connected(30)
110
+ await cache.send(SetLight("zone", 250, brightness=50, fade=800))
111
+ await asyncio.sleep(60)
112
+ await cache.stop()
113
+
114
+ asyncio.run(main())
115
+ ```
116
+
117
+ Model listeners are plain functions; transport listeners can also be coroutines. One that raises does not affect the others. `cache.model` holds the devices and the last known state of each zone and light. `cache.send_raw(...)` sends any MQTT command as a fallback. For the raw typed messages, use `cache.transport.add_message_listener(...)` with `wire.commands` builders. `tools/watch.py` does that from the command line.
118
+
119
+ ## Capturing the protocol
120
+
121
+ The protocol is documented from real traffic, in `docs/protocol.md`, with the message definitions in `docs/asyncapi.yaml` (an AsyncAPI description; the code tables in the markdown are generated from it by `tools/gen_protocol.py`). `tools/capture.py` records everything the system publishes (read-only by default) and can also send commands by hand. After a firmware or backend update, re-capture and refresh the spec and fixtures:
122
+
123
+ ```bash
124
+ # one-time setup: create a virtual environment and install the dependency
125
+ python3 -m venv .venv
126
+ source .venv/bin/activate # Windows: .venv\Scripts\activate
127
+ pip install -e ".[capture]"
128
+
129
+ export LC_HOST=<host> LC_USER=<user> LC_PASS=<pass> LC_TOPIC_PREFIX='<Prefix Topic>'
130
+ python3 tools/capture.py --hail # record for 2 minutes
131
+ python3 tools/capture.py --interactive # type commands, see replies live
132
+ ```
133
+
134
+ While it runs, press a wall switch and change a light from the PowerPlane display, so `status` and `events` messages appear in the capture.
135
+
136
+ **What it does.** It connects to the broker over TLS (MQTT 5, port 8883), subscribes to everything under your Prefix Topic (`<prefix>/#`), and writes every message to a new `capture.<date>_<time>.jsonl` file. By default it is read-only: the only thing it can send is one optional hail (`{"e":9,"g":0}`, which lists uncommissioned devices) to `<prefix>/cmd` when you pass `--hail`. With `--interactive` it also sends what you type (see below), so it can change your lights and settings.
137
+
138
+ **Options**
139
+
140
+ | Option | Meaning |
141
+ |---|---|
142
+ | `--host`, `--port`, `--user`, `--password` | Connection details. Defaults come from `LC_HOST`, `LC_PORT` (8883), `LC_USER`, `LC_PASS`; prefer the environment variables so the password stays out of shell history |
143
+ | `--topic-prefix P` | The Prefix Topic from your LumenCache connection details (env `LC_TOPIC_PREFIX`). Required. Quote it in the shell |
144
+ | `--topic T` | Subscribe to `T` (relative to the prefix) instead of `<prefix>/#`; repeat for several. `+` matches one level, `#` matches everything below |
145
+ | `-i`, `--interactive` | Type JSON commands to send to `<prefix>/cmd`; ends at `--duration` or when you quit |
146
+ | `--hail` | Send the one read-only hail described above |
147
+ | `--duration N` | Seconds to run, interactive mode included (default 120). `0` runs until you stop it (Ctrl-C, or `quit` in interactive mode) |
148
+ | `--name NAME` | Start of the output file name (default `capture`). The file is `NAME.YYYY.MM.DD_HH_MM_SS.jsonl`, using the local start time, so every run gets its own file, for example `capture.2026.10.10_08_11_00.jsonl` |
149
+ | `--no-obfuscate` | Keep real ids, serials and addresses in the capture. Obfuscation is on by default (see Output) |
150
+
151
+ **Interactive mode.** After it connects, type one JSON command per line and press Enter, for example `{"e":8,"g":2}`. Each line is published to `<prefix>/cmd` exactly as typed; lines that are not a JSON object are refused and nothing is sent. Messages from the system print as they arrive, each with a time, and are also written to the capture file. Because the script subscribes to the whole prefix, your own commands come back from the broker as `cmd` messages, which confirms they were published. Type `quit` or press Ctrl-C to exit. The only guard is on factory reset (`e:15` with `c:1`): it asks you to type `RESET` first. Everything else, including commands that persist settings, is sent as typed.
152
+
153
+ **Output.** Each run writes a new file named `capture.YYYY.MM.DD_HH_MM_SS.jsonl` (local start time; `--name` replaces `capture`), so earlier captures are never overwritten. It has one JSON object per message with the timestamp, direction (`recv` for messages from the broker, `sent` for commands this script published), topic, retained flag, and the payload (parsed as JSON when possible). Topics are written with the prefix removed, so the file does not contain your identifier and the protocol spec and fixtures do not depend on one system's prefix. Credentials are never written.
154
+
155
+ **Obfuscation** is on by default and applies to the file, not the screen. System ids and device serials (`sys`, `hsn`) are replaced with stable fakes (`SYS0001`, `HSN0001`; the same real value always gets the same fake, and a socket's `_S1` suffix is kept, so relationships between messages survive), the free-text fields in `meta` (name, address, zip, phone, note) are blanked, MAC-shaped strings are masked, and the Prefix Topic is replaced by `<prefix>` wherever it appears in text. Commands you send are obfuscated the same way, so a `sent` line for a command that uses a real `hsn` shows the fake. The mapping is kept in memory only, so fakes are consistent within one capture and not between captures. Pass `--no-obfuscate` to keep the real values (for example to debug against your own system). Obfuscation is best effort: still review a capture for personal details before sharing it.
156
+
157
+ **Troubleshooting**
158
+
159
+ - *A subscription is refused:* the script prints the broker's reason. Check that `--topic-prefix` matches your Prefix Topic exactly, or use `--topic` for a topic you know you may read.
160
+ - *Cannot connect:* check host, port, username and password.
161
+ - *No messages arrive:* trigger activity while it runs (press a switch), and check the prefix.
162
+
163
+ ## License
164
+
165
+ Apache License 2.0. See [LICENSE](LICENSE).