microdlms 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.
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -0,0 +1,97 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ spellcheck:
16
+ name: Spell check
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - name: Checkout code
20
+ uses: actions/checkout@v7
21
+
22
+ - name: Install uv
23
+ uses: astral-sh/setup-uv@v10.3.0
24
+ with:
25
+ enable-cache: true
26
+ python-version: "3.14"
27
+
28
+ - name: Run spellcheck
29
+ run: uv run codespell
30
+
31
+ lint:
32
+ name: Lint & Format
33
+ runs-on: ubuntu-latest
34
+ steps:
35
+ - name: Checkout code
36
+ uses: actions/checkout@v7
37
+
38
+ - name: Install uv
39
+ uses: astral-sh/setup-uv@v10.3.0
40
+ with:
41
+ enable-cache: true
42
+ python-version: "3.14"
43
+
44
+ - name: Run linter
45
+ run: uv run ruff check
46
+
47
+ - name: Check formatting
48
+ run: uv run ruff format --check
49
+
50
+ typecheck:
51
+ name: Type check
52
+ runs-on: ubuntu-latest
53
+ steps:
54
+ - name: Checkout code
55
+ uses: actions/checkout@v7
56
+
57
+ - name: Install uv
58
+ uses: astral-sh/setup-uv@v10.3.0
59
+ with:
60
+ enable-cache: true
61
+ python-version: "3.14"
62
+
63
+ - name: Run mypy
64
+ run: uv run mypy src tests
65
+
66
+ complexity:
67
+ name: Code complexity
68
+ runs-on: ubuntu-latest
69
+ steps:
70
+ - name: Checkout code
71
+ uses: actions/checkout@v7
72
+
73
+ - name: Install uv
74
+ uses: astral-sh/setup-uv@v10.3.0
75
+ with:
76
+ enable-cache: true
77
+ python-version: "3.14"
78
+
79
+ - name: Run complexipy
80
+ run: uv run complexipy . --max-complexity-allowed 15
81
+
82
+ test:
83
+ name: Tests & Coverage
84
+ runs-on: ubuntu-latest
85
+ steps:
86
+ - name: Checkout code
87
+ uses: actions/checkout@v7
88
+
89
+ - name: Install uv
90
+ uses: astral-sh/setup-uv@v10.3.0
91
+ with:
92
+ enable-cache: true
93
+ python-version: "3.14"
94
+
95
+ - name: Run pytest
96
+ run: uv run pytest
97
+
@@ -0,0 +1,41 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v[0-9]+.[0-9]+.[0-9]+*"
7
+
8
+ permissions:
9
+ contents: write
10
+ id-token: write
11
+
12
+ jobs:
13
+ release:
14
+ name: Release
15
+ runs-on: ubuntu-latest
16
+ permissions:
17
+ contents: write
18
+ id-token: write
19
+ steps:
20
+ - name: Checkout code
21
+ uses: actions/checkout@v7
22
+
23
+ - name: Install uv
24
+ uses: astral-sh/setup-uv@v10.3.0
25
+ with:
26
+ enable-cache: false
27
+ python-version: "3.14"
28
+
29
+ - name: Build package
30
+ run: uv build
31
+
32
+ - name: Publish to PyPI
33
+ run: uv publish
34
+
35
+ - name: Create GitHub Release
36
+ uses: softprops/action-gh-release@v3
37
+ with:
38
+ tag_name: ${{ github.ref_name }}
39
+ name: Release ${{ github.ref_name }}
40
+ generate_release_notes: true
41
+ files: dist/*
@@ -0,0 +1,181 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
175
+
176
+ # Cursor
177
+ # Cursor is an AI-powered code editor.`.cursorignore` specifies files/directories to
178
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
179
+ # refer to https://docs.cursor.com/context/ignore-files
180
+ .cursorignore
181
+ .cursorindexingignore
@@ -0,0 +1,27 @@
1
+ repos:
2
+ - repo: https://github.com/astral-sh/ruff-pre-commit
3
+ rev: v0.17.0
4
+ hooks:
5
+ - id: ruff-check
6
+ - id: ruff-format
7
+
8
+ - repo: https://github.com/codespell-project/codespell
9
+ rev: v2.4.3
10
+ hooks:
11
+ - id: codespell
12
+ additional_dependencies:
13
+ - tomli
14
+
15
+ - repo: https://github.com/pre-commit/mirrors-mypy
16
+ rev: v2.4.0
17
+ hooks:
18
+ - id: mypy
19
+ additional_dependencies:
20
+ - pytest>=8.0
21
+ args: [--config-file=pyproject.toml]
22
+
23
+ - repo: https://github.com/rohaquinlop/complexipy-pre-commit
24
+ rev: v8.0.1
25
+ hooks:
26
+ - id: complexipy
27
+ args: [--max-complexity-allowed=15]
@@ -0,0 +1,126 @@
1
+ # Agent Guidelines for `microdlms`
2
+
3
+ This document outlines architecture principles, operational standards, hardware constraints, and testing patterns for AI agents developing or maintaining `microdlms`.
4
+
5
+ ---
6
+
7
+ ## 1. Project Overview & Identity
8
+
9
+ `microdlms` is an MIT-licensed, zero-core-dependency Python 3.14+ library for read-only DLMS/COSEM utility meter communication.
10
+
11
+ ### Role & Ecosystem
12
+ - **Lightweight Alternative**: Designed for Home Assistant and standalone IoT metering systems.
13
+ - **Permissive Licensing**: `microdlms` provides an MIT-licensed library implementing the required read-only DLMS subset, suitable for open-source integration.
14
+ - **Transport Independence**: Core protocol codecs and state machines operate sans-I/O. Serial/socket I/O is segregated into `microdlms.client` via [`serialx`](https://github.com/home-assistant-libs/serialx), supporting standard serial ports (`/dev/ttyUSB*`, `COM*`), network URLs (`socket://`, `tcp://`), and ESPHome `serial_proxy` bridges.
15
+
16
+ ---
17
+
18
+ ## 2. Architectural Rules & Design Principles
19
+
20
+ ### 2.1 Sans-I/O Purity in Core
21
+ - **Zero I/O in Core**: Modules `microdlms.hdlc`, `microdlms.axdr`, `microdlms.apdu`, `microdlms.obis`, `microdlms.types`, and `microdlms.session` must remain **100% sans-I/O**.
22
+ - **No I/O Imports**: Never import `socket`, `asyncio`, `serial`, `serialx`, `urllib`, or any networking libraries in core modules.
23
+ - **State Machine Pattern**: The session engine (`DlmsSession`) operates strictly on in-memory buffers:
24
+ - Ingestion: `receive_bytes(data: bytes) -> list[SessionEvent]`
25
+ - Outbound data: `data_to_send() -> bytes`
26
+ - **I/O Isolation**: All async I/O, timeouts, task orchestration, and socket management reside exclusively in `microdlms.client.AsyncMeterClient`.
27
+
28
+ ### 2.2 Specification-Driven Design
29
+ - **Open Standards**: Base protocol implementations on official open DLMS Blue Book and Green Book specifications.
30
+ - **Test Vectors**: Validate behavior against verified physical meter frame captures in `tests/meter_fixtures.py`.
31
+
32
+ ---
33
+
34
+ ## 3. Git & Operational Conventions
35
+
36
+ ### 3.1 Commit Guidelines
37
+ - **Commit Message Prefix**: Always prefix agent-generated commits with `[ai]` (e.g., `[ai] Support multi-frame A-XDR structures.`).
38
+ - **Commit Format**: Keep commit messages concise (under 50 characters including prefix) and end with a full stop (`.`).
39
+ - **Branching**: Conduct feature and bugfix work on dedicated branches (`feature/<name>` or `fix/<name>`). Never commit experimental scaffolding directly to the main branch.
40
+
41
+ ### 3.2 Code Hygiene Tools
42
+ Before submitting changes, all checks must pass cleanly with zero errors:
43
+
44
+ ```bash
45
+ # Code formatting and linting
46
+ uv run ruff check
47
+ uv run ruff format --check
48
+
49
+ # Strict static type checking
50
+ uv run mypy src tests
51
+
52
+ # Cognitive complexity verification (max allowed: 15)
53
+ uv run complexipy .
54
+
55
+ # Spelling verification
56
+ uv run codespell
57
+
58
+ # Test suite execution with coverage enforcement
59
+ uv run pytest
60
+ ```
61
+
62
+ > **Note on Pytest Plugins**: If your local environment has third-party pytest plugins installed (such as `pytest-homeassistant-custom-component` or `pytest-socket`), they may interfere with test execution. You can disable them by adding `-p no:homeassistant -p no:socket`:
63
+ > ```bash
64
+ > uv run pytest -p no:homeassistant -p no:socket
65
+ > ```
66
+
67
+
68
+ When using `prek` (Astral's Git hook manager):
69
+ ```bash
70
+ prek run --all-files
71
+ ```
72
+
73
+ ### 3.3 Code Style & Naming Conventions
74
+ - **Self-Descriptive Naming**: Ensure human-readable, self-descriptive names for variables, parameters, and attributes across both codebase and test suites. Avoid terse or cryptic abbreviations (e.g., use `position` over `pos`, `message` over `msg`, `frame_1` over `f1`).
75
+
76
+ ---
77
+
78
+ ## 4. Hardware Realities & DLMS Communication
79
+
80
+ ### 4.1 Optical Probe Timing & Constraints
81
+ - **Low Baud Rate**: Optical IR utility meter communication runs predominantly at 9600 baud (8N1).
82
+ - **Latency**: Each attribute read request-response exchange takes between 0.5 and 1.5 seconds over physical optical links.
83
+ - **Exclusive Access**: Serial connections and optical probes are strictly exclusive. Only one active session may use the serial link or TCP socket proxy at a time.
84
+ - **Frame Slicing**: Optical links frequently fragment or concatenate byte streams. Codecs must handle partial frames across multiple chunk arrivals and cleanly identify frame boundaries (`0x7E` framing flags).
85
+
86
+ ### 4.2 Sequence Tracking & Protocol Fields
87
+ - **HDLC Sequence Counters**: $N(S)$ (send sequence number) and $N(R)$ (receive sequence number) are cyclic counters modulo 8 ($0..7$).
88
+ - **DLMS Invoke IDs**: DLMS invocation identifiers are cyclic counters modulo 16 ($0..15$).
89
+ - **LLC Sublayer**: DLMS frames over HDLC I-frames use LLC encapsulation:
90
+ - Client LLC header: `0xE6 0xE6 0x00`
91
+ - Server LLC header: `0xE6 0xE7 0x00`
92
+ - **Dual Error Model**:
93
+ - The sans-I/O `DlmsSession` yields typed events (`AssociationRejectedEvent`, `GetAccessErrorEvent`, `ProtocolErrorEvent`) rather than raising exceptions, maintaining continuous byte consumption.
94
+ - The async `AsyncMeterClient` converts error events into appropriate Python exceptions:
95
+ - `CommunicationError`: physical link, timeout, or framing errors.
96
+ - `AuthenticationError`: LLS password or association rejections.
97
+ - `DataAccessError`: COSEM data access errors (carrying `DataAccessResult`).
98
+
99
+ ---
100
+
101
+ ## 5. Testing & Coverage Rules
102
+
103
+ ### 5.1 Simulated & Mocked I/O
104
+ - **No Real Hardware or Network I/O**: Tests must never open real physical serial ports or external network connections.
105
+ - **Mock Transport**: Test client interactions using mock readers/writers or mocked `serialx.open_serial_connection` factories.
106
+ - **Deterministic Sans-I/O Testing**: Primary protocol logic must be tested by feeding synthetic or captured byte vectors directly to `DlmsSession.receive_bytes()`.
107
+
108
+ ### 5.2 Test Vectors & Replay Verification
109
+ - **Physical Meter Fixtures**: The repository includes physical meter captures in `tests/meter_fixtures.py` and `tests/meter_data.json` covering:
110
+ - HDLC Connection (`SNRM` $\rightarrow$ `UA`)
111
+ - AARQ Application Association with LLS password authentication
112
+ - Authentication failure responses (`REJECTED_PERMANENT`, diagnostic `13`)
113
+ - Logical Device Name (`0.0.42.0.0.255`, attr 2)
114
+ - Meter Serial Number (`0.0.96.1.0.255`, attr 2)
115
+ - Voltage, current, power readings and `ScalerUnit` structures
116
+ - Error responses (`OBJECT_UNDEFINED` for non-existent OBIS or attribute)
117
+ - Disconnect sequence (`DISC` $\rightarrow$ `UA`)
118
+ - `tests/test_fixture_replay.py` must continually validate all 46 captured frame exchanges.
119
+
120
+ ### 5.3 Coverage Enforcement
121
+ - **Minimum 95% Coverage**: Branch and statement coverage across `src/microdlms/` must remain at or above 95% at all times.
122
+ - Pytest enforces this via:
123
+ ```bash
124
+ uv run pytest --cov=microdlms --cov-fail-under=95
125
+ ```
126
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 denpamusic
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.