sofar-modbus 0.1.4__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.
- sofar_modbus-0.1.4/.github/workflows/ci.yml +35 -0
- sofar_modbus-0.1.4/.github/workflows/publish.yml +45 -0
- sofar_modbus-0.1.4/.gitignore +10 -0
- sofar_modbus-0.1.4/LICENSE +202 -0
- sofar_modbus-0.1.4/PKG-INFO +144 -0
- sofar_modbus-0.1.4/README.md +129 -0
- sofar_modbus-0.1.4/pyproject.toml +60 -0
- sofar_modbus-0.1.4/src/sofar_modbus/__init__.py +44 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/__init__.py +29 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/device.py +168 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/enums.py +28 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/identity.py +17 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/pv.py +92 -0
- sofar_modbus-0.1.4/src/sofar_modbus/legacy/storage.py +102 -0
- sofar_modbus-0.1.4/src/sofar_modbus/model.py +52 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/__init__.py +116 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/battery.py +97 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/battery_pack.py +99 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/device.py +321 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/energy.py +34 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/enums.py +336 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/inverter.py +143 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/offgrid.py +63 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/pv.py +81 -0
- sofar_modbus-0.1.4/src/sofar_modbus/modern/settings.py +238 -0
- sofar_modbus-0.1.4/src/sofar_modbus/py.typed +0 -0
- sofar_modbus-0.1.4/src/sofar_modbus/variants.py +106 -0
- sofar_modbus-0.1.4/tests/__init__.py +0 -0
- sofar_modbus-0.1.4/tests/conftest.py +205 -0
- sofar_modbus-0.1.4/tests/test_legacy.py +169 -0
- sofar_modbus-0.1.4/tests/test_modern.py +335 -0
- sofar_modbus-0.1.4/tests/test_read_patterns.py +257 -0
- sofar_modbus-0.1.4/tests/test_resilience.py +99 -0
- sofar_modbus-0.1.4/tests/test_variants.py +76 -0
- sofar_modbus-0.1.4/uv.lock +447 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
lint:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: astral-sh/setup-uv@v6
|
|
18
|
+
- name: Install dependencies
|
|
19
|
+
run: uv sync
|
|
20
|
+
- name: Ruff format
|
|
21
|
+
run: uv run ruff format --check .
|
|
22
|
+
- name: Ruff lint
|
|
23
|
+
run: uv run ruff check .
|
|
24
|
+
- name: Mypy
|
|
25
|
+
run: uv run mypy
|
|
26
|
+
|
|
27
|
+
test:
|
|
28
|
+
runs-on: ubuntu-latest
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v4
|
|
31
|
+
- uses: astral-sh/setup-uv@v6
|
|
32
|
+
- name: Install dependencies
|
|
33
|
+
run: uv sync
|
|
34
|
+
- name: Run tests
|
|
35
|
+
run: uv run pytest
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
inputs:
|
|
8
|
+
tag:
|
|
9
|
+
description: Existing release tag to build and publish (e.g. 4.0.1)
|
|
10
|
+
required: true
|
|
11
|
+
type: string
|
|
12
|
+
|
|
13
|
+
env:
|
|
14
|
+
RELEASE_TAG: ${{ inputs.tag || github.event.release.tag_name }}
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
build:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v7
|
|
21
|
+
with:
|
|
22
|
+
ref: ${{ env.RELEASE_TAG }}
|
|
23
|
+
- uses: astral-sh/setup-uv@v7
|
|
24
|
+
- name: Set package version from release tag
|
|
25
|
+
run: sed -i "s/^version = \"0\\.0\\.0\"$/version = \"${RELEASE_TAG}\"/" pyproject.toml
|
|
26
|
+
- name: Verify the version was substituted
|
|
27
|
+
run: grep -qx "version = \"${RELEASE_TAG}\"" pyproject.toml
|
|
28
|
+
- run: uv build
|
|
29
|
+
- uses: actions/upload-artifact@v7
|
|
30
|
+
with:
|
|
31
|
+
name: dist
|
|
32
|
+
path: dist/
|
|
33
|
+
|
|
34
|
+
publish:
|
|
35
|
+
needs: build
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
environment: pypi
|
|
38
|
+
permissions:
|
|
39
|
+
id-token: write
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/download-artifact@v8
|
|
42
|
+
with:
|
|
43
|
+
name: dist
|
|
44
|
+
path: dist/
|
|
45
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 William Swann
|
|
190
|
+
Copyright 2026 darkrain-nl
|
|
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,144 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sofar-modbus
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: Read Sofar Solar inverters (HYD, KTL-X and the older PV/storage range) over Modbus.
|
|
5
|
+
Project-URL: Homepage, https://github.com/darkrain-nl/sofar-modbus
|
|
6
|
+
Author: sofar-modbus contributors
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: home-assistant,inverter,modbus,sofar,sofar-solar,solar
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Home Automation
|
|
12
|
+
Requires-Python: >=3.12
|
|
13
|
+
Requires-Dist: modbus-connection[tmodbus]>=4.6.1
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# sofar-modbus
|
|
17
|
+
|
|
18
|
+
Read Sofar Solar inverters over Modbus, as typed Python objects rather than
|
|
19
|
+
register numbers.
|
|
20
|
+
|
|
21
|
+
The library maps Sofar's register set onto
|
|
22
|
+
[modbus-connection](https://github.com/balloob/modbus-connection)'s device model:
|
|
23
|
+
you hand it a `ModbusUnit`, call `async_update()`, and read sub-systems as
|
|
24
|
+
attributes. It owns no connection and no I/O policy — the caller does.
|
|
25
|
+
|
|
26
|
+
## Supported devices
|
|
27
|
+
|
|
28
|
+
Sofar ships two quite different register maps, so there are two device objects.
|
|
29
|
+
|
|
30
|
+
**`SofarInverter` — the current generation.** HYD hybrids and KTL-X / KTLM PV
|
|
31
|
+
inverters, over the 0x0400 (state and identity), 0x0480 (grid), 0x0500
|
|
32
|
+
(off-grid), 0x0580 (PV), 0x0600 (battery), 0x0680 (energy), 0x1000 (settings)
|
|
33
|
+
and 0x9000 (BTS battery tower) blocks. Serial prefixes: `SP1`, `SP2`, `ZP1`,
|
|
34
|
+
`ZP2`, `SM2E`, `ZM2E`, `SH3E`, `SS2E`, `ZS2E`, `SQ1ES1`, `SA1`, `SB1`, `SC1`,
|
|
35
|
+
`SD1`, `SF4`, `SH1`, `SL1`, `SJ2`, `SS1`. Includes the Azzurro and ZCS
|
|
36
|
+
rebadges. This is the only generation with writable registers.
|
|
37
|
+
|
|
38
|
+
**`SofarLegacyInverter` — the older generation.** The earlier PV inverters
|
|
39
|
+
(`SA1`, `SA3`, `SB1`, `ZA3`, `SC1`, `SD1`, `SF4`, `SH1`, `SJ2`, `SL1`, `SM1`)
|
|
40
|
+
and the `SE1E` / `SM1E` / `ZE1E` / `ZM1E` storage inverters, over the 0x0000 and
|
|
41
|
+
0x0200 blocks, with the serial number in the input-register space. Read-only.
|
|
42
|
+
|
|
43
|
+
Within a generation, what an inverter serves depends on its model: single or
|
|
44
|
+
three phase, PV-only or hybrid, how many MPPT trackers, whether off-grid (EPS)
|
|
45
|
+
and parallel-system registers exist. The first update reads the serial number
|
|
46
|
+
and settles this into an `InverterType` bitmask; each component declares the
|
|
47
|
+
mask it applies to, and a poll reads only the matching ones. Nothing else is
|
|
48
|
+
touched — an inverter without batteries never sees a battery register.
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import asyncio
|
|
54
|
+
|
|
55
|
+
from modbus_connection import ModbusTcpParams
|
|
56
|
+
from modbus_connection.tmodbus import ModbusConnection
|
|
57
|
+
from sofar_modbus import SofarInverter
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def main() -> None:
|
|
61
|
+
connection = ModbusConnection(
|
|
62
|
+
ModbusTcpParams(host="192.168.1.50", port=502, framer="rtu")
|
|
63
|
+
)
|
|
64
|
+
try:
|
|
65
|
+
inverter = SofarInverter(connection.for_unit(1), read_eps=True)
|
|
66
|
+
await inverter.async_update()
|
|
67
|
+
|
|
68
|
+
print("Model:", inverter.model, inverter.serial_number)
|
|
69
|
+
print("State:", inverter.state.system_state)
|
|
70
|
+
print("Grid power:", inverter.grid.active_power_output_total, "kW")
|
|
71
|
+
print("PV power:", inverter.pv_1_2.pv_power_total, "kW")
|
|
72
|
+
print("Battery SoC:", inverter.battery_totals.battery_capacity_total, "%")
|
|
73
|
+
print("Solar today:", inverter.energy.solar_generation_today, "kWh")
|
|
74
|
+
finally:
|
|
75
|
+
await connection.close()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
asyncio.run(main())
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A poll reads each sub-system independently, the way the integration reads its
|
|
82
|
+
blocks: one slow or refused block does not take the rest of the poll with it.
|
|
83
|
+
`async_update()` returns an `UpdateReport` — a failed component keeps its
|
|
84
|
+
previous values, does not notify its listeners, and is listed by attribute
|
|
85
|
+
name with its error, while every other component refreshes and notifies once
|
|
86
|
+
the whole poll is done. Only a dead link (`ModbusConnectionError`) raises:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
report = await inverter.async_update()
|
|
90
|
+
for name, error in report.failed.items():
|
|
91
|
+
print(f"{name} kept its previous values: {error}")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Writing works the same way — a plain field write for the registers that take
|
|
95
|
+
one, and a method for the registers the device insists on receiving as a block:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from sofar_modbus.modern import ChargerUseMode, FeedinLimitationMode
|
|
99
|
+
|
|
100
|
+
await inverter.charger.write("charger_use_mode", ChargerUseMode.PASSIVE_MODE)
|
|
101
|
+
await inverter.feed_in.async_write_limit(FeedinLimitationMode.DISABLED, 3000)
|
|
102
|
+
await inverter.passive.async_write_power(
|
|
103
|
+
grid_power=-2000, battery_min=0, battery_max=5000
|
|
104
|
+
)
|
|
105
|
+
await inverter.active_power_control.async_write_active_power_limit(True, 70)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`active_power_control` is a live throttle on the inverter's own output — distinct
|
|
109
|
+
from `feed_in`, which caps power exported to the grid. It applies to PV-only
|
|
110
|
+
inverters as well as hybrids, and takes effect within seconds.
|
|
111
|
+
|
|
112
|
+
A BTS battery tower multiplexes every pack onto one register block, so packs are
|
|
113
|
+
read one at a time rather than polled:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
if inverter.has_battery_tower:
|
|
117
|
+
pack = await inverter.async_read_pack(string_nr=0, pack_nr=0)
|
|
118
|
+
print(pack.pack_serial_number, pack.soc, pack.cell_1_voltage)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
For an issue report, `async_read_raw()` dumps every register the inverter reads
|
|
122
|
+
undecoded, keyed by address space and address — every block a poll covers, for
|
|
123
|
+
the sub-systems this model serves. The pack block is not in it: a dump of it
|
|
124
|
+
would be whichever pack happened to be selected, with nothing to say which.
|
|
125
|
+
|
|
126
|
+
## ASCII over TCP is not supported
|
|
127
|
+
|
|
128
|
+
Sofar inverters are reached over RTU or RTU-over-TCP. This library never accepts
|
|
129
|
+
or forwards `framer="ascii"`, and it exposes no connect helper that could: the
|
|
130
|
+
caller builds the `ModbusUnit` and hands it over. Build it from an RTU serial or
|
|
131
|
+
RTU-over-TCP connection — an ASCII-framed TCP connection is unsupported and
|
|
132
|
+
untested, and nothing here works around it.
|
|
133
|
+
|
|
134
|
+
## Attribution
|
|
135
|
+
|
|
136
|
+
The register maps are derived from
|
|
137
|
+
[homeassistant-solax-modbus](https://github.com/wills106/homeassistant-solax-modbus)
|
|
138
|
+
(Apache-2.0), specifically its `plugin_sofar.py` and `plugin_sofar_old.py`. This
|
|
139
|
+
library keeps that project's field keys, scale factors, units and per-model
|
|
140
|
+
filtering, and is released under the same licence.
|
|
141
|
+
|
|
142
|
+
Where upstream declares two entities on one register, or the same key twice,
|
|
143
|
+
this library keeps both rather than picking a winner — the docstring on each
|
|
144
|
+
field carries upstream's name, and the tests spell out the cases.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# sofar-modbus
|
|
2
|
+
|
|
3
|
+
Read Sofar Solar inverters over Modbus, as typed Python objects rather than
|
|
4
|
+
register numbers.
|
|
5
|
+
|
|
6
|
+
The library maps Sofar's register set onto
|
|
7
|
+
[modbus-connection](https://github.com/balloob/modbus-connection)'s device model:
|
|
8
|
+
you hand it a `ModbusUnit`, call `async_update()`, and read sub-systems as
|
|
9
|
+
attributes. It owns no connection and no I/O policy — the caller does.
|
|
10
|
+
|
|
11
|
+
## Supported devices
|
|
12
|
+
|
|
13
|
+
Sofar ships two quite different register maps, so there are two device objects.
|
|
14
|
+
|
|
15
|
+
**`SofarInverter` — the current generation.** HYD hybrids and KTL-X / KTLM PV
|
|
16
|
+
inverters, over the 0x0400 (state and identity), 0x0480 (grid), 0x0500
|
|
17
|
+
(off-grid), 0x0580 (PV), 0x0600 (battery), 0x0680 (energy), 0x1000 (settings)
|
|
18
|
+
and 0x9000 (BTS battery tower) blocks. Serial prefixes: `SP1`, `SP2`, `ZP1`,
|
|
19
|
+
`ZP2`, `SM2E`, `ZM2E`, `SH3E`, `SS2E`, `ZS2E`, `SQ1ES1`, `SA1`, `SB1`, `SC1`,
|
|
20
|
+
`SD1`, `SF4`, `SH1`, `SL1`, `SJ2`, `SS1`. Includes the Azzurro and ZCS
|
|
21
|
+
rebadges. This is the only generation with writable registers.
|
|
22
|
+
|
|
23
|
+
**`SofarLegacyInverter` — the older generation.** The earlier PV inverters
|
|
24
|
+
(`SA1`, `SA3`, `SB1`, `ZA3`, `SC1`, `SD1`, `SF4`, `SH1`, `SJ2`, `SL1`, `SM1`)
|
|
25
|
+
and the `SE1E` / `SM1E` / `ZE1E` / `ZM1E` storage inverters, over the 0x0000 and
|
|
26
|
+
0x0200 blocks, with the serial number in the input-register space. Read-only.
|
|
27
|
+
|
|
28
|
+
Within a generation, what an inverter serves depends on its model: single or
|
|
29
|
+
three phase, PV-only or hybrid, how many MPPT trackers, whether off-grid (EPS)
|
|
30
|
+
and parallel-system registers exist. The first update reads the serial number
|
|
31
|
+
and settles this into an `InverterType` bitmask; each component declares the
|
|
32
|
+
mask it applies to, and a poll reads only the matching ones. Nothing else is
|
|
33
|
+
touched — an inverter without batteries never sees a battery register.
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import asyncio
|
|
39
|
+
|
|
40
|
+
from modbus_connection import ModbusTcpParams
|
|
41
|
+
from modbus_connection.tmodbus import ModbusConnection
|
|
42
|
+
from sofar_modbus import SofarInverter
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async def main() -> None:
|
|
46
|
+
connection = ModbusConnection(
|
|
47
|
+
ModbusTcpParams(host="192.168.1.50", port=502, framer="rtu")
|
|
48
|
+
)
|
|
49
|
+
try:
|
|
50
|
+
inverter = SofarInverter(connection.for_unit(1), read_eps=True)
|
|
51
|
+
await inverter.async_update()
|
|
52
|
+
|
|
53
|
+
print("Model:", inverter.model, inverter.serial_number)
|
|
54
|
+
print("State:", inverter.state.system_state)
|
|
55
|
+
print("Grid power:", inverter.grid.active_power_output_total, "kW")
|
|
56
|
+
print("PV power:", inverter.pv_1_2.pv_power_total, "kW")
|
|
57
|
+
print("Battery SoC:", inverter.battery_totals.battery_capacity_total, "%")
|
|
58
|
+
print("Solar today:", inverter.energy.solar_generation_today, "kWh")
|
|
59
|
+
finally:
|
|
60
|
+
await connection.close()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
asyncio.run(main())
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
A poll reads each sub-system independently, the way the integration reads its
|
|
67
|
+
blocks: one slow or refused block does not take the rest of the poll with it.
|
|
68
|
+
`async_update()` returns an `UpdateReport` — a failed component keeps its
|
|
69
|
+
previous values, does not notify its listeners, and is listed by attribute
|
|
70
|
+
name with its error, while every other component refreshes and notifies once
|
|
71
|
+
the whole poll is done. Only a dead link (`ModbusConnectionError`) raises:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
report = await inverter.async_update()
|
|
75
|
+
for name, error in report.failed.items():
|
|
76
|
+
print(f"{name} kept its previous values: {error}")
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Writing works the same way — a plain field write for the registers that take
|
|
80
|
+
one, and a method for the registers the device insists on receiving as a block:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from sofar_modbus.modern import ChargerUseMode, FeedinLimitationMode
|
|
84
|
+
|
|
85
|
+
await inverter.charger.write("charger_use_mode", ChargerUseMode.PASSIVE_MODE)
|
|
86
|
+
await inverter.feed_in.async_write_limit(FeedinLimitationMode.DISABLED, 3000)
|
|
87
|
+
await inverter.passive.async_write_power(
|
|
88
|
+
grid_power=-2000, battery_min=0, battery_max=5000
|
|
89
|
+
)
|
|
90
|
+
await inverter.active_power_control.async_write_active_power_limit(True, 70)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`active_power_control` is a live throttle on the inverter's own output — distinct
|
|
94
|
+
from `feed_in`, which caps power exported to the grid. It applies to PV-only
|
|
95
|
+
inverters as well as hybrids, and takes effect within seconds.
|
|
96
|
+
|
|
97
|
+
A BTS battery tower multiplexes every pack onto one register block, so packs are
|
|
98
|
+
read one at a time rather than polled:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
if inverter.has_battery_tower:
|
|
102
|
+
pack = await inverter.async_read_pack(string_nr=0, pack_nr=0)
|
|
103
|
+
print(pack.pack_serial_number, pack.soc, pack.cell_1_voltage)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For an issue report, `async_read_raw()` dumps every register the inverter reads
|
|
107
|
+
undecoded, keyed by address space and address — every block a poll covers, for
|
|
108
|
+
the sub-systems this model serves. The pack block is not in it: a dump of it
|
|
109
|
+
would be whichever pack happened to be selected, with nothing to say which.
|
|
110
|
+
|
|
111
|
+
## ASCII over TCP is not supported
|
|
112
|
+
|
|
113
|
+
Sofar inverters are reached over RTU or RTU-over-TCP. This library never accepts
|
|
114
|
+
or forwards `framer="ascii"`, and it exposes no connect helper that could: the
|
|
115
|
+
caller builds the `ModbusUnit` and hands it over. Build it from an RTU serial or
|
|
116
|
+
RTU-over-TCP connection — an ASCII-framed TCP connection is unsupported and
|
|
117
|
+
untested, and nothing here works around it.
|
|
118
|
+
|
|
119
|
+
## Attribution
|
|
120
|
+
|
|
121
|
+
The register maps are derived from
|
|
122
|
+
[homeassistant-solax-modbus](https://github.com/wills106/homeassistant-solax-modbus)
|
|
123
|
+
(Apache-2.0), specifically its `plugin_sofar.py` and `plugin_sofar_old.py`. This
|
|
124
|
+
library keeps that project's field keys, scale factors, units and per-model
|
|
125
|
+
filtering, and is released under the same licence.
|
|
126
|
+
|
|
127
|
+
Where upstream declares two entities on one register, or the same key twice,
|
|
128
|
+
this library keeps both rather than picking a winner — the docstring on each
|
|
129
|
+
field carries upstream's name, and the tests spell out the cases.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sofar-modbus"
|
|
7
|
+
version = "0.1.4"
|
|
8
|
+
description = "Read Sofar Solar inverters (HYD, KTL-X and the older PV/storage range) over Modbus."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [{ name = "sofar-modbus contributors" }]
|
|
13
|
+
keywords = ["modbus", "sofar", "sofar-solar", "inverter", "solar", "home-assistant"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Topic :: Home Automation",
|
|
17
|
+
]
|
|
18
|
+
# The caller injects a ModbusUnit; tmodbus is the backend this is exercised against.
|
|
19
|
+
dependencies = ["modbus-connection[tmodbus]>=4.6.1"]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/darkrain-nl/sofar-modbus"
|
|
23
|
+
|
|
24
|
+
[dependency-groups]
|
|
25
|
+
dev = [
|
|
26
|
+
"mypy>=1.11",
|
|
27
|
+
"pytest>=8",
|
|
28
|
+
"pytest-asyncio>=0.24",
|
|
29
|
+
"ruff>=0.6",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.wheel]
|
|
33
|
+
packages = ["src/sofar_modbus"]
|
|
34
|
+
|
|
35
|
+
[tool.pytest.ini_options]
|
|
36
|
+
asyncio_mode = "auto"
|
|
37
|
+
testpaths = ["tests"]
|
|
38
|
+
|
|
39
|
+
[tool.ruff]
|
|
40
|
+
target-version = "py312"
|
|
41
|
+
src = ["src"]
|
|
42
|
+
|
|
43
|
+
[tool.ruff.lint]
|
|
44
|
+
select = ["E", "F", "W", "I", "UP", "B", "D"]
|
|
45
|
+
# D401 wants imperative docstrings; this codebase names things ("The inverter
|
|
46
|
+
# type a serial implies"), matching modbus-connection's own style. D107 is
|
|
47
|
+
# covered by the class docstring.
|
|
48
|
+
ignore = ["D107", "D401"]
|
|
49
|
+
|
|
50
|
+
[tool.ruff.lint.pydocstyle]
|
|
51
|
+
convention = "pep257"
|
|
52
|
+
|
|
53
|
+
[tool.mypy]
|
|
54
|
+
python_version = "3.12"
|
|
55
|
+
files = ["src", "tests"]
|
|
56
|
+
strict = true
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint.per-file-ignores]
|
|
59
|
+
# A test's name is its documentation; docstrings are for the ones that need a why.
|
|
60
|
+
"tests/*" = ["D100", "D103", "D104"]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""sofar-modbus — read Sofar Solar inverters over Modbus.
|
|
2
|
+
|
|
3
|
+
Two protocol generations live side by side, each with its own top-level device
|
|
4
|
+
object built on ``modbus_connection.model``::
|
|
5
|
+
|
|
6
|
+
from modbus_connection import ModbusTcpParams
|
|
7
|
+
from modbus_connection.tmodbus import ModbusConnection
|
|
8
|
+
from sofar_modbus import SofarInverter
|
|
9
|
+
|
|
10
|
+
connection = ModbusConnection(ModbusTcpParams(host="10.0.0.5", framer="rtu"))
|
|
11
|
+
inverter = SofarInverter(connection.for_unit(1))
|
|
12
|
+
await inverter.async_update()
|
|
13
|
+
inverter.grid.active_power_output_total
|
|
14
|
+
|
|
15
|
+
- :class:`SofarInverter` — the current HYD / KTL-X generation.
|
|
16
|
+
- :class:`SofarLegacyInverter` — the older SA/SB/SC/SM1E generation.
|
|
17
|
+
|
|
18
|
+
Which registers an inverter serves depends on its model, encoded as an
|
|
19
|
+
:class:`InverterType` bitmask on every component. See :mod:`sofar_modbus.variants`.
|
|
20
|
+
|
|
21
|
+
ASCII framing over TCP is not supported: build the ``ModbusUnit`` from an RTU or
|
|
22
|
+
RTU-over-TCP connection.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from .legacy import SofarLegacyInverter
|
|
26
|
+
from .model import (
|
|
27
|
+
SofarComponent,
|
|
28
|
+
SofarComponentBase,
|
|
29
|
+
SofarLegacyComponent,
|
|
30
|
+
UpdateReport,
|
|
31
|
+
)
|
|
32
|
+
from .modern import SofarInverter
|
|
33
|
+
from .variants import InverterType, matches
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"InverterType",
|
|
37
|
+
"SofarComponent",
|
|
38
|
+
"SofarComponentBase",
|
|
39
|
+
"SofarInverter",
|
|
40
|
+
"SofarLegacyComponent",
|
|
41
|
+
"SofarLegacyInverter",
|
|
42
|
+
"UpdateReport",
|
|
43
|
+
"matches",
|
|
44
|
+
]
|