pysunsynkweb 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.
- pysunsynkweb-0.0.1/.github/workflows/python-package.yml +42 -0
- pysunsynkweb-0.0.1/.gitignore +6 -0
- pysunsynkweb-0.0.1/.vscode/settings.json +7 -0
- pysunsynkweb-0.0.1/LICENSE +201 -0
- pysunsynkweb-0.0.1/LICENSE.txt +9 -0
- pysunsynkweb-0.0.1/PKG-INFO +44 -0
- pysunsynkweb-0.0.1/README.md +21 -0
- pysunsynkweb-0.0.1/pyproject.toml +255 -0
- pysunsynkweb-0.0.1/requirements.txt +5 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/__about__.py +6 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/__init__.py +4 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/const.py +10 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/exceptions.py +5 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/model.py +181 -0
- pysunsynkweb-0.0.1/src/pysunsynkweb/session.py +77 -0
- pysunsynkweb-0.0.1/tests/__init__.py +4 -0
- pysunsynkweb-0.0.1/tests/conftest.py +87 -0
- pysunsynkweb-0.0.1/tests/test_coordinator.py +57 -0
- pysunsynkweb-0.0.1/tests/test_model.py +28 -0
- pysunsynkweb-0.0.1/tests/test_session.py +66 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
|
|
3
|
+
|
|
4
|
+
name: Python package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "main" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "main" ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
strategy:
|
|
17
|
+
fail-fast: true
|
|
18
|
+
matrix:
|
|
19
|
+
python-version: ["3.9", "3.10", "3.11"]
|
|
20
|
+
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v4
|
|
23
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
24
|
+
uses: actions/setup-python@v3
|
|
25
|
+
with:
|
|
26
|
+
python-version: ${{ matrix.python-version }}
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: |
|
|
29
|
+
python -m pip install --upgrade pip
|
|
30
|
+
python -m pip install flake8 pytest
|
|
31
|
+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
|
32
|
+
- name: Lint with flake8
|
|
33
|
+
run: |
|
|
34
|
+
# stop the build if there are Python syntax errors or undefined names
|
|
35
|
+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
|
36
|
+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
|
37
|
+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
|
38
|
+
- name: Test with pytest
|
|
39
|
+
run: |
|
|
40
|
+
export PYTHONPATH=${PYTHONPATH}:src
|
|
41
|
+
pytest tests
|
|
42
|
+
|
|
@@ -0,0 +1,201 @@
|
|
|
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 [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Francois <f@flve.uk>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pysunsynkweb
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Project-URL: Documentation, https://github.com/francoisverbeek/pysunsynkweb
|
|
5
|
+
Project-URL: Issues, https://github.com/francoisverbeek/pysunsynkweb/issues
|
|
6
|
+
Project-URL: Source, https://github.com/francoisverbeek/pysunsynkweb
|
|
7
|
+
Author-email: Francois <f@flve.uk>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
19
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
20
|
+
Requires-Python: >=3.8
|
|
21
|
+
Requires-Dist: aiohttp
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Sunsynk Web
|
|
25
|
+
|
|
26
|
+
[](https://pypi.org/project/sunsynk-web)
|
|
27
|
+
[](https://pypi.org/project/sunsynk-web)
|
|
28
|
+
|
|
29
|
+
-----
|
|
30
|
+
|
|
31
|
+
## Table of Contents
|
|
32
|
+
|
|
33
|
+
- [Installation](#installation)
|
|
34
|
+
- [License](#license)
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```console
|
|
39
|
+
pip install sunsynk-web
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
`sunsynk-web` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Sunsynk Web
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/sunsynk-web)
|
|
4
|
+
[](https://pypi.org/project/sunsynk-web)
|
|
5
|
+
|
|
6
|
+
-----
|
|
7
|
+
|
|
8
|
+
## Table of Contents
|
|
9
|
+
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [License](#license)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
pip install sunsynk-web
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
`sunsynk-web` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pysunsynkweb"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = ''
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
keywords = []
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Francois", email = "f@flve.uk" },
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Programming Language :: Python :: 3.8",
|
|
20
|
+
"Programming Language :: Python :: 3.9",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
25
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"aiohttp"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Documentation = "https://github.com/francoisverbeek/pysunsynkweb"
|
|
33
|
+
Issues = "https://github.com/francoisverbeek/pysunsynkweb/issues"
|
|
34
|
+
Source = "https://github.com/francoisverbeek/pysunsynkweb"
|
|
35
|
+
|
|
36
|
+
[tool.hatch.envs.test]
|
|
37
|
+
extra-dependencies = [
|
|
38
|
+
"pytest-asyncio",
|
|
39
|
+
"aioresponses",
|
|
40
|
+
"asyncio",
|
|
41
|
+
"ruff","coverage"
|
|
42
|
+
]
|
|
43
|
+
[tool.hatch.envs.test.scripts]
|
|
44
|
+
test = "pytest {args:tests}"
|
|
45
|
+
test-cov = "coverage run -m pytest {args:tests}"
|
|
46
|
+
cov-report = [
|
|
47
|
+
"- coverage combine",
|
|
48
|
+
"coverage xml",
|
|
49
|
+
"coverage report"
|
|
50
|
+
]
|
|
51
|
+
cov = [
|
|
52
|
+
"test-cov",
|
|
53
|
+
"cov-report",
|
|
54
|
+
]
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
asyncio_mode = "auto"
|
|
57
|
+
[tool.hatch.version]
|
|
58
|
+
|
|
59
|
+
path = "src/pysunsynkweb/__about__.py"
|
|
60
|
+
[tool.hatch.envs.types]
|
|
61
|
+
extra-dependencies = [
|
|
62
|
+
"mypy>=1.0.0",
|
|
63
|
+
"aioresponses"
|
|
64
|
+
]
|
|
65
|
+
[tool.hatch.envs.types.scripts]
|
|
66
|
+
check = "mypy --install-types --non-interactive {args:src/pysunsynkweb tests}"
|
|
67
|
+
|
|
68
|
+
[tool.coverage.run]
|
|
69
|
+
source_pkgs = ["pysunsynkweb", "tests"]
|
|
70
|
+
branch = true
|
|
71
|
+
parallel = true
|
|
72
|
+
omit = [
|
|
73
|
+
"src/pysunsynkweb/__about__.py",
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
[tool.coverage.paths]
|
|
77
|
+
pysunsynkweb = ["src/pysunsynkweb", "*/pysunsynkweb/src/pysunsynkweb"]
|
|
78
|
+
tests = ["tests", "*/pysunsynkweb/tests"]
|
|
79
|
+
|
|
80
|
+
[tool.coverage.report]
|
|
81
|
+
exclude_lines = [
|
|
82
|
+
"no cov",
|
|
83
|
+
"if __name__ == .__main__.:",
|
|
84
|
+
"if TYPE_CHECKING:",
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
[tool.ruff]
|
|
88
|
+
required-version = ">=0.4.2"
|
|
89
|
+
|
|
90
|
+
[tool.ruff.lint]
|
|
91
|
+
select = [
|
|
92
|
+
"A001", # Variable {name} is shadowing a Python builtin
|
|
93
|
+
"B002", # Python does not support the unary prefix increment
|
|
94
|
+
"B005", # Using .strip() with multi-character strings is misleading
|
|
95
|
+
"B007", # Loop control variable {name} not used within loop body
|
|
96
|
+
"B014", # Exception handler with duplicate exception
|
|
97
|
+
"B015", # Pointless comparison. Did you mean to assign a value? Otherwise, prepend assert or remove it.
|
|
98
|
+
"B017", # pytest.raises(BaseException) should be considered evil
|
|
99
|
+
"B018", # Found useless attribute access. Either assign it to a variable or remove it.
|
|
100
|
+
"B023", # Function definition does not bind loop variable {name}
|
|
101
|
+
"B026", # Star-arg unpacking after a keyword argument is strongly discouraged
|
|
102
|
+
"B032", # Possible unintentional type annotation (using :). Did you mean to assign (using =)?
|
|
103
|
+
"B904", # Use raise from to specify exception cause
|
|
104
|
+
"B905", # zip() without an explicit strict= parameter
|
|
105
|
+
"BLE",
|
|
106
|
+
"C", # complexity
|
|
107
|
+
"COM818", # Trailing comma on bare tuple prohibited
|
|
108
|
+
"D", # docstrings
|
|
109
|
+
"DTZ003", # Use datetime.now(tz=) instead of datetime.utcnow()
|
|
110
|
+
"DTZ004", # Use datetime.fromtimestamp(ts, tz=) instead of datetime.utcfromtimestamp(ts)
|
|
111
|
+
"E", # pycodestyle
|
|
112
|
+
"F", # pyflakes/autoflake
|
|
113
|
+
"FLY", # flynt
|
|
114
|
+
"G", # flake8-logging-format
|
|
115
|
+
"I", # isort
|
|
116
|
+
"INP", # flake8-no-pep420
|
|
117
|
+
"ISC", # flake8-implicit-str-concat
|
|
118
|
+
"ICN001", # import concentions; {name} should be imported as {asname}
|
|
119
|
+
"LOG", # flake8-logging
|
|
120
|
+
"N804", # First argument of a class method should be named cls
|
|
121
|
+
"N805", # First argument of a method should be named self
|
|
122
|
+
"N815", # Variable {name} in class scope should not be mixedCase
|
|
123
|
+
"PERF", # Perflint
|
|
124
|
+
"PGH", # pygrep-hooks
|
|
125
|
+
"PIE", # flake8-pie
|
|
126
|
+
"PL", # pylint
|
|
127
|
+
"PT", # flake8-pytest-style
|
|
128
|
+
"PYI", # flake8-pyi
|
|
129
|
+
"RET", # flake8-return
|
|
130
|
+
"RSE", # flake8-raise
|
|
131
|
+
"RUF005", # Consider iterable unpacking instead of concatenation
|
|
132
|
+
"RUF006", # Store a reference to the return value of asyncio.create_task
|
|
133
|
+
"RUF010", # Use explicit conversion flag
|
|
134
|
+
"RUF013", # PEP 484 prohibits implicit Optional
|
|
135
|
+
"RUF018", # Avoid assignment expressions in assert statements
|
|
136
|
+
"RUF019", # Unnecessary key check before dictionary access
|
|
137
|
+
# "RUF100", # Unused `noqa` directive; temporarily every now and then to clean them up
|
|
138
|
+
"S102", # Use of exec detected
|
|
139
|
+
"S103", # bad-file-permissions
|
|
140
|
+
"S108", # hardcoded-temp-file
|
|
141
|
+
"S306", # suspicious-mktemp-usage
|
|
142
|
+
"S307", # suspicious-eval-usage
|
|
143
|
+
"S313", # suspicious-xmlc-element-tree-usage
|
|
144
|
+
"S314", # suspicious-xml-element-tree-usage
|
|
145
|
+
"S315", # suspicious-xml-expat-reader-usage
|
|
146
|
+
"S316", # suspicious-xml-expat-builder-usage
|
|
147
|
+
"S317", # suspicious-xml-sax-usage
|
|
148
|
+
"S318", # suspicious-xml-mini-dom-usage
|
|
149
|
+
"S319", # suspicious-xml-pull-dom-usage
|
|
150
|
+
"S320", # suspicious-xmle-tree-usage
|
|
151
|
+
"S601", # paramiko-call
|
|
152
|
+
"S602", # subprocess-popen-with-shell-equals-true
|
|
153
|
+
"S604", # call-with-shell-equals-true
|
|
154
|
+
"S608", # hardcoded-sql-expression
|
|
155
|
+
"S609", # unix-command-wildcard-injection
|
|
156
|
+
"SIM", # flake8-simplify
|
|
157
|
+
"SLF", # flake8-self
|
|
158
|
+
"SLOT", # flake8-slots
|
|
159
|
+
"T100", # Trace found: {name} used
|
|
160
|
+
"T20", # flake8-print
|
|
161
|
+
"TID251", # Banned imports
|
|
162
|
+
"TRY", # tryceratops
|
|
163
|
+
"UP", # pyupgrade
|
|
164
|
+
"W", # pycodestyle
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
ignore = [
|
|
168
|
+
"D202", # No blank lines allowed after function docstring
|
|
169
|
+
"D203", # 1 blank line required before class docstring
|
|
170
|
+
"D213", # Multi-line docstring summary should start at the second line
|
|
171
|
+
"D406", # Section name should end with a newline
|
|
172
|
+
"D407", # Section name underlining
|
|
173
|
+
"D415",
|
|
174
|
+
"D400",
|
|
175
|
+
|
|
176
|
+
"PLC1901", # {existing} can be simplified to {replacement} as an empty string is falsey; too many false positives
|
|
177
|
+
"PLR0911", # Too many return statements ({returns} > {max_returns})
|
|
178
|
+
"PLR0912", # Too many branches ({branches} > {max_branches})
|
|
179
|
+
"PLR0913", # Too many arguments to function call ({c_args} > {max_args})
|
|
180
|
+
"PLR0915", # Too many statements ({statements} > {max_statements})
|
|
181
|
+
"PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable
|
|
182
|
+
"PLW2901", # Outer {outer_kind} variable {name} overwritten by inner {inner_kind} target
|
|
183
|
+
"PT004", # Fixture {fixture} does not return anything, add leading underscore
|
|
184
|
+
"PT011", # pytest.raises({exception}) is too broad, set the `match` parameter or use a more specific exception
|
|
185
|
+
"PT012", # `pytest.raises()` block should contain a single simple statement
|
|
186
|
+
"PT018", # Assertion should be broken down into multiple parts
|
|
187
|
+
"RUF001", # String contains ambiguous unicode character.
|
|
188
|
+
"RUF002", # Docstring contains ambiguous unicode character.
|
|
189
|
+
"RUF003", # Comment contains ambiguous unicode character.
|
|
190
|
+
"RUF015", # Prefer next(...) over single element slice
|
|
191
|
+
"SIM102", # Use a single if statement instead of nested if statements
|
|
192
|
+
"SIM103", # Return the condition {condition} directly
|
|
193
|
+
"SIM108", # Use ternary operator {contents} instead of if-else-block
|
|
194
|
+
"SIM115", # Use context handler for opening files
|
|
195
|
+
"TRY003", # Avoid specifying long messages outside the exception class
|
|
196
|
+
"TRY400", # Use `logging.exception` instead of `logging.error`
|
|
197
|
+
# Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923
|
|
198
|
+
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
|
|
199
|
+
|
|
200
|
+
# May conflict with the formatter, https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
|
|
201
|
+
"W191",
|
|
202
|
+
"E111",
|
|
203
|
+
"E114",
|
|
204
|
+
"E117",
|
|
205
|
+
"D206",
|
|
206
|
+
"D300",
|
|
207
|
+
"Q",
|
|
208
|
+
"COM812",
|
|
209
|
+
"COM819",
|
|
210
|
+
"ISC001",
|
|
211
|
+
|
|
212
|
+
# Disabled because ruff does not understand type of __all__ generated by a function
|
|
213
|
+
"PLE0605",
|
|
214
|
+
|
|
215
|
+
# temporarily disabled
|
|
216
|
+
"PT019",
|
|
217
|
+
"PYI024", # Use typing.NamedTuple instead of collections.namedtuple
|
|
218
|
+
"RET503",
|
|
219
|
+
"RET501",
|
|
220
|
+
"TRY002",
|
|
221
|
+
"TRY301"
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
[tool.ruff.lint.flake8-import-conventions.extend-aliases]
|
|
225
|
+
voluptuous = "vol"
|
|
226
|
+
"homeassistant.helpers.area_registry" = "ar"
|
|
227
|
+
"homeassistant.helpers.category_registry" = "cr"
|
|
228
|
+
"homeassistant.helpers.config_validation" = "cv"
|
|
229
|
+
"homeassistant.helpers.device_registry" = "dr"
|
|
230
|
+
"homeassistant.helpers.entity_registry" = "er"
|
|
231
|
+
"homeassistant.helpers.floor_registry" = "fr"
|
|
232
|
+
"homeassistant.helpers.issue_registry" = "ir"
|
|
233
|
+
"homeassistant.helpers.label_registry" = "lr"
|
|
234
|
+
"homeassistant.util.dt" = "dt_util"
|
|
235
|
+
|
|
236
|
+
[tool.ruff.lint.flake8-pytest-style]
|
|
237
|
+
fixture-parentheses = false
|
|
238
|
+
mark-parentheses = false
|
|
239
|
+
|
|
240
|
+
[tool.ruff.lint.flake8-tidy-imports.banned-api]
|
|
241
|
+
"async_timeout".msg = "use asyncio.timeout instead"
|
|
242
|
+
"pytz".msg = "use zoneinfo instead"
|
|
243
|
+
|
|
244
|
+
[tool.ruff.lint.isort]
|
|
245
|
+
force-sort-within-sections = true
|
|
246
|
+
known-first-party = [
|
|
247
|
+
"sunsynkweb",
|
|
248
|
+
]
|
|
249
|
+
combine-as-imports = true
|
|
250
|
+
split-on-trailing-comma = false
|
|
251
|
+
|
|
252
|
+
[tool.ruff.lint.per-file-ignores]
|
|
253
|
+
|
|
254
|
+
[tool.ruff.lint.mccabe]
|
|
255
|
+
max-complexity = 25
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Constants for pysunsynkweb."""
|
|
2
|
+
|
|
3
|
+
BASE_URL = "https://api.sunsynk.net"
|
|
4
|
+
BASE_API = BASE_URL + "/api/v1"
|
|
5
|
+
BASE_HEADERS = {
|
|
6
|
+
"accept": "application/json",
|
|
7
|
+
"content-type": "application/json",
|
|
8
|
+
"accept-language": "en-US,en;q=0.5",
|
|
9
|
+
"accept-encoding": "gzip, deflate, br",
|
|
10
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Top level data model for the sunsynk web api."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
import decimal
|
|
5
|
+
import logging
|
|
6
|
+
import pprint
|
|
7
|
+
from typing import List, Union
|
|
8
|
+
|
|
9
|
+
from .const import BASE_API
|
|
10
|
+
from .session import SunsynkwebSession
|
|
11
|
+
|
|
12
|
+
_LOGGER = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class Plant:
|
|
17
|
+
"""Proxy for the 'Plant' object in sunsynk web api.
|
|
18
|
+
|
|
19
|
+
A plant can host multiple inverters and other devices. Our plant object
|
|
20
|
+
carries a summary of the data from all inverters in the plant.
|
|
21
|
+
|
|
22
|
+
In the limited sample i've got, there is one plant per inverter.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
id: int
|
|
27
|
+
master_id: int
|
|
28
|
+
name: str
|
|
29
|
+
status: int
|
|
30
|
+
battery_power: int = 0
|
|
31
|
+
state_of_charge: float = 0
|
|
32
|
+
load_power: int = 0
|
|
33
|
+
grid_power: int = 0
|
|
34
|
+
pv_power: int = 0
|
|
35
|
+
inverter_sn: Union[int, None] = None
|
|
36
|
+
acc_pv: decimal.Decimal = decimal.Decimal(0)
|
|
37
|
+
acc_grid_export: decimal.Decimal = decimal.Decimal(0)
|
|
38
|
+
acc_grid_import: decimal.Decimal = decimal.Decimal(0)
|
|
39
|
+
acc_battery_discharge: decimal.Decimal = decimal.Decimal(0)
|
|
40
|
+
acc_battery_charge: decimal.Decimal = decimal.Decimal(0)
|
|
41
|
+
acc_load: decimal.Decimal = decimal.Decimal(0)
|
|
42
|
+
session: Union[SunsynkwebSession , None] = None
|
|
43
|
+
|
|
44
|
+
def __repr__(self):
|
|
45
|
+
"""Summary of the plant"""
|
|
46
|
+
return f"""Plant {id}
|
|
47
|
+
|
|
48
|
+
{self.name}
|
|
49
|
+
Battery power {self.battery_power} W ({self.state_of_charge})%
|
|
50
|
+
Grid Power: {self.grid_power} W
|
|
51
|
+
PV Power: {self.pv_power} W
|
|
52
|
+
Accumulated PV Energy {self.acc_pv} KWh
|
|
53
|
+
Accumulated Grid export {self.acc_grid_export} KWh
|
|
54
|
+
Accumulated Grid import {self.acc_grid_import} KWh
|
|
55
|
+
Accumulated Load {self.acc_load} KWh
|
|
56
|
+
Accumulated Battery discharge {self.acc_battery_discharge} KWh
|
|
57
|
+
Accumulated Battery charge {self.acc_battery_charge} KWh
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def ismaster(self):
|
|
62
|
+
"""Is the plant a master plant.
|
|
63
|
+
|
|
64
|
+
Unused at the moment, but required when introducing read-write calls
|
|
65
|
+
(for instance to command to charge batteries from the grid).
|
|
66
|
+
"""
|
|
67
|
+
return self.master_id == self.id
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def from_api(cls, api_return, session):
|
|
71
|
+
"""Create the plant from the return of the web api."""
|
|
72
|
+
return cls(
|
|
73
|
+
name=api_return["name"],
|
|
74
|
+
id=api_return["id"],
|
|
75
|
+
master_id=api_return["masterId"],
|
|
76
|
+
status=api_return["status"],
|
|
77
|
+
session=session,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
async def enrich_inverters(self):
|
|
81
|
+
"""Populate inverters' serial numbers.
|
|
82
|
+
|
|
83
|
+
The plant summary doesn't contain the inverters, so we have a
|
|
84
|
+
separate call to populate inverter's serial numbers.
|
|
85
|
+
"""
|
|
86
|
+
returned = await self.session.get(
|
|
87
|
+
BASE_API + f"/plant/{self.id}/inverters",
|
|
88
|
+
params={"page": 1, "limit": 20, "type": -1, "status": -1},
|
|
89
|
+
)
|
|
90
|
+
assert len(returned["data"]["infos"]) == 1
|
|
91
|
+
self.inverter_sn = returned["data"]["infos"][0]["sn"]
|
|
92
|
+
|
|
93
|
+
async def _get_instantaneous_data(self):
|
|
94
|
+
"""Populate instantaneous data.
|
|
95
|
+
|
|
96
|
+
Instantaneous data is conveniently summarized in the 'flow' api end point.
|
|
97
|
+
"""
|
|
98
|
+
returned = await self.session.get(
|
|
99
|
+
BASE_API + f"/plant/energy/{self.id}/flow",
|
|
100
|
+
params={"page": 1, "limit": 20},
|
|
101
|
+
)
|
|
102
|
+
_LOGGER.debug("Flow Api returned %s", pprint.pformat(returned))
|
|
103
|
+
|
|
104
|
+
self.battery_power = returned["data"]["battPower"]
|
|
105
|
+
if returned["data"]["toBat"]:
|
|
106
|
+
self.battery_power *= -1
|
|
107
|
+
self.state_of_charge = returned["data"]["soc"]
|
|
108
|
+
self.load_power = returned["data"]["loadOrEpsPower"]
|
|
109
|
+
self.grid_power = returned["data"]["gridOrMeterPower"]
|
|
110
|
+
if returned["data"]["toGrid"]:
|
|
111
|
+
self.grid_power *= -1
|
|
112
|
+
self.pv_power = returned["data"]["pvPower"]
|
|
113
|
+
|
|
114
|
+
async def _get_total_grid(self):
|
|
115
|
+
returned = await self.session.get(
|
|
116
|
+
BASE_API + f"/inverter/grid/{self.inverter_sn}/realtime",
|
|
117
|
+
params={"lan": "en"},
|
|
118
|
+
)
|
|
119
|
+
self.acc_grid_export = returned["data"]["etotalTo"]
|
|
120
|
+
self.acc_grid_import = returned["data"]["etotalFrom"]
|
|
121
|
+
|
|
122
|
+
async def _get_total_battery(self):
|
|
123
|
+
returned = await self.session.get(
|
|
124
|
+
BASE_API + f"/inverter/battery/{self.inverter_sn}/realtime",
|
|
125
|
+
params={"lan": "en"},
|
|
126
|
+
)
|
|
127
|
+
self.acc_battery_charge = returned["data"]["etotalChg"]
|
|
128
|
+
self.acc_battery_discharge = returned["data"]["etotalDischg"]
|
|
129
|
+
|
|
130
|
+
async def _get_total_pv(self):
|
|
131
|
+
returned = await self.session.get(
|
|
132
|
+
BASE_API + f"/inverter/{self.inverter_sn}/total",
|
|
133
|
+
params={"lan": "en"},
|
|
134
|
+
)
|
|
135
|
+
self.acc_pv = sum([decimal.Decimal(i["value"]) for i in returned["data"]["infos"][0]["records"]])
|
|
136
|
+
|
|
137
|
+
async def _get_total_load(self):
|
|
138
|
+
returned = await self.session.get(
|
|
139
|
+
BASE_API + f"/inverter/load/{self.inverter_sn}/realtime",
|
|
140
|
+
params={"lan": "en"},
|
|
141
|
+
)
|
|
142
|
+
self.acc_load = returned["data"]["totalUsed"]
|
|
143
|
+
|
|
144
|
+
async def update(self):
|
|
145
|
+
"""Update all sensors."""
|
|
146
|
+
await self._get_instantaneous_data()
|
|
147
|
+
await self._get_total_pv()
|
|
148
|
+
await self._get_total_grid()
|
|
149
|
+
await self._get_total_battery()
|
|
150
|
+
await self._get_total_load()
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclass
|
|
154
|
+
class Installation:
|
|
155
|
+
"""An installation is a series of plants.
|
|
156
|
+
|
|
157
|
+
This integration presents the plants as a single entity.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
plants: List[Plant]
|
|
161
|
+
|
|
162
|
+
@classmethod
|
|
163
|
+
def from_api(cls, api_return, session):
|
|
164
|
+
"""Create the installation from the sunsynk web api."""
|
|
165
|
+
assert "data" in api_return
|
|
166
|
+
assert api_return["msg"] == "Success"
|
|
167
|
+
return cls(plants=[Plant.from_api(ret, session) for ret in api_return["data"]["infos"]])
|
|
168
|
+
|
|
169
|
+
async def update(self):
|
|
170
|
+
"""Update all the plants. They will in turn update their sensors."""
|
|
171
|
+
for plant in self.plants:
|
|
172
|
+
await plant.update()
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
async def get_plants(session: SunsynkwebSession):
|
|
176
|
+
"""Start walking the plant composition."""
|
|
177
|
+
returned = await session.get(BASE_API + "/plants", params={"page": 1, "limit": 20})
|
|
178
|
+
installation = Installation.from_api(returned, session)
|
|
179
|
+
for plant in installation.plants:
|
|
180
|
+
await plant.enrich_inverters()
|
|
181
|
+
return installation
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""A session object
|
|
2
|
+
|
|
3
|
+
Maintaining the authentication with the sunsynk api and
|
|
4
|
+
wrapping repetitive things
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import pprint
|
|
9
|
+
|
|
10
|
+
import aiohttp
|
|
11
|
+
|
|
12
|
+
from .const import BASE_HEADERS, BASE_URL
|
|
13
|
+
from .exceptions import AuthenticationFailed
|
|
14
|
+
|
|
15
|
+
_LOGGER = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class SunsynkwebSession:
|
|
19
|
+
"""the main entry point to sunsynk api.
|
|
20
|
+
|
|
21
|
+
Maintains http headers,
|
|
22
|
+
authentication token, etc.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, session: aiohttp.ClientSession, username: str, password: str) -> None:
|
|
26
|
+
"""Pass an aiohttp client session and authentication items"""
|
|
27
|
+
self.session = session
|
|
28
|
+
self.bearer = None
|
|
29
|
+
self.username = username
|
|
30
|
+
self.password = password
|
|
31
|
+
|
|
32
|
+
async def get(self, *args, **kwargs):
|
|
33
|
+
"""Run a GET query against the sunsynk api"""
|
|
34
|
+
if self.bearer is None:
|
|
35
|
+
await self._get_bearer_token()
|
|
36
|
+
headers = BASE_HEADERS.copy()
|
|
37
|
+
headers.update({"Authorization": f"Bearer{self.bearer}"})
|
|
38
|
+
kwargs["headers"] = headers
|
|
39
|
+
result = await self.session.get(*args, **kwargs)
|
|
40
|
+
result = await result.json()
|
|
41
|
+
if result.get("msg") != "Success" and result.get("code") == 401:
|
|
42
|
+
# expired token
|
|
43
|
+
await self._get_bearer_token()
|
|
44
|
+
result = await self.get(*args, **kwargs)
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
async def _get_bearer_token(self):
|
|
48
|
+
"""Get the bearer token for the sunsynk api."""
|
|
49
|
+
params = {
|
|
50
|
+
"username": self.username,
|
|
51
|
+
"password": self.password,
|
|
52
|
+
"grant_type": "password",
|
|
53
|
+
"client_id": "csp-web",
|
|
54
|
+
"source": "sunsynk",
|
|
55
|
+
"areaCode": "sunsynk",
|
|
56
|
+
}
|
|
57
|
+
returned = await self.session.post(BASE_URL + "/oauth/token", json=params, headers=BASE_HEADERS)
|
|
58
|
+
|
|
59
|
+
returned = await returned.json()
|
|
60
|
+
_LOGGER.debug("authentication attempt returned %s", pprint.pformat(returned))
|
|
61
|
+
# returned data looks like the below
|
|
62
|
+
# {
|
|
63
|
+
# "code": 0,
|
|
64
|
+
# "msg": "Success",
|
|
65
|
+
# "data": {
|
|
66
|
+
# "access_token": "VALUE",
|
|
67
|
+
# "token_type": "bearer",
|
|
68
|
+
# "refresh_token": "VALUE",
|
|
69
|
+
# "expires_in": 604799,
|
|
70
|
+
# "scope": "all",
|
|
71
|
+
# },
|
|
72
|
+
# "success": True,
|
|
73
|
+
# }
|
|
74
|
+
try:
|
|
75
|
+
self.bearer = returned["data"]["access_token"]
|
|
76
|
+
except KeyError as exc:
|
|
77
|
+
raise AuthenticationFailed from exc
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Common fixtures for the Sunsynk Inverter Web tests."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
from pysunsynkweb.const import BASE_API, BASE_URL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def populatemocked(mocked):
|
|
9
|
+
"""Return the normal start of coordinator minimal api required from the api."""
|
|
10
|
+
mocked.post(BASE_URL + "/oauth/token", payload={"msg": "Success", "data": {"access_token": "12345"}})
|
|
11
|
+
mocked.get(
|
|
12
|
+
re.compile(BASE_API + "/plants.*"),
|
|
13
|
+
payload={
|
|
14
|
+
"msg": "Success",
|
|
15
|
+
"data": {
|
|
16
|
+
"infos": [
|
|
17
|
+
{"name": "plant1", "id": 1, "masterId": 1, "status": 0},
|
|
18
|
+
{"name": "plant2", "id": 2, "masterId": 1, "status": 0},
|
|
19
|
+
]
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
)
|
|
23
|
+
mocked.get(
|
|
24
|
+
re.compile(BASE_API + r"/plant/\d/inverters.*"),
|
|
25
|
+
payload={"msg": "Success", "data": {"infos": [{"sn": 123}]}},
|
|
26
|
+
repeat=True,
|
|
27
|
+
)
|
|
28
|
+
mocked.get(
|
|
29
|
+
re.compile(BASE_API + r"/plant/energy/\d/flow.*"),
|
|
30
|
+
payload={
|
|
31
|
+
"code": 200,
|
|
32
|
+
"msg": "Success",
|
|
33
|
+
"data": {
|
|
34
|
+
"battPower": 1,
|
|
35
|
+
"toBat": True,
|
|
36
|
+
"soc": 2,
|
|
37
|
+
"loadOrEpsPower": 3,
|
|
38
|
+
"gridOrMeterPower": 4,
|
|
39
|
+
"toGrid": True,
|
|
40
|
+
"pvPower": 5,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
mocked.get(
|
|
45
|
+
re.compile(BASE_API + r"/plant/energy/\d/flow.*"),
|
|
46
|
+
payload={
|
|
47
|
+
"code": 200,
|
|
48
|
+
"msg": "Success",
|
|
49
|
+
"data": {
|
|
50
|
+
"battPower": 1,
|
|
51
|
+
"toBat": False,
|
|
52
|
+
"soc": 2,
|
|
53
|
+
"loadOrEpsPower": 3,
|
|
54
|
+
"gridOrMeterPower": 4,
|
|
55
|
+
"toGrid": False,
|
|
56
|
+
"pvPower": 5,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
)
|
|
60
|
+
mocked.get(
|
|
61
|
+
re.compile(BASE_API + "/inverter/123/total.*"),
|
|
62
|
+
repeat=True,
|
|
63
|
+
# PV total
|
|
64
|
+
payload={
|
|
65
|
+
"data": {
|
|
66
|
+
"infos": [
|
|
67
|
+
{
|
|
68
|
+
"records": [
|
|
69
|
+
{"year": 2024, "value": 1},
|
|
70
|
+
{"year": 2023, "value": 2},
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
mocked.get(
|
|
78
|
+
re.compile(BASE_API + "/inverter/grid/123/realtime.*"),
|
|
79
|
+
payload={"data": {"etotalTo": 2, "etotalFrom": 3}},
|
|
80
|
+
repeat=True,
|
|
81
|
+
)
|
|
82
|
+
mocked.get(
|
|
83
|
+
re.compile(BASE_API + "/inverter/battery/123/realtime.*"),
|
|
84
|
+
payload={"data": {"etotalChg": 2, "etotalDischg": 3}},
|
|
85
|
+
repeat=True,
|
|
86
|
+
)
|
|
87
|
+
mocked.get(re.compile(BASE_API + "/inverter/load/123/realtime.*"), payload={"data": {"totalUsed": 2}}, repeat=True)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Basic coordinator tests."""
|
|
2
|
+
|
|
3
|
+
# from unittest.mock import AsyncMock, Mock, patch
|
|
4
|
+
|
|
5
|
+
# from homeassistant.components.sunsynkweb import DOMAIN, async_setup_entry, async_unload_entry
|
|
6
|
+
# from homeassistant.components.sunsynkweb.coordinator import PlantUpdateCoordinator
|
|
7
|
+
# from homeassistant.helpers.update_coordinator import UpdateFailed
|
|
8
|
+
# import pytest
|
|
9
|
+
|
|
10
|
+
# from tests.common import MockConfigEntry
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# async def test_coordinator_creation_destruction(hass, basicdata):
|
|
14
|
+
# """Check we can create and destroy the coordinator."""
|
|
15
|
+
# with patch(
|
|
16
|
+
# "homeassistant.components.sunsynkweb.coordinator.async_get_clientsession",
|
|
17
|
+
# new=Mock(),
|
|
18
|
+
# ) as sessiongetter:
|
|
19
|
+
# session = AsyncMock()
|
|
20
|
+
# sessiongetter.return_value = session
|
|
21
|
+
# mockedjson_return = AsyncMock()
|
|
22
|
+
# mockedjson_return.name = "mocked_json_return"
|
|
23
|
+
# session.get.return_value = mockedjson_return
|
|
24
|
+
# session.post.return_value = mockedjson_return
|
|
25
|
+
# mockedjson_return.json.side_effect = basicdata
|
|
26
|
+
# config = MockConfigEntry(data={"username": "blah", "password": "blahblah"})
|
|
27
|
+
# hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
28
|
+
# await async_setup_entry(hass, config)
|
|
29
|
+
# coordinator = hass.data[DOMAIN][config.entry_id]
|
|
30
|
+
# assert len(coordinator.cache.plants) == 2
|
|
31
|
+
# await async_unload_entry(hass, config)
|
|
32
|
+
# assert config.entry_id not in hass.data[DOMAIN]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# async def test_coordinator(hass, basicdata) -> None:
|
|
36
|
+
# """Run coordinator tests."""
|
|
37
|
+
# config = MockConfigEntry()
|
|
38
|
+
# coordinator = PlantUpdateCoordinator(hass, config)
|
|
39
|
+
# assert coordinator.cache is None
|
|
40
|
+
# assert coordinator.bearer is None
|
|
41
|
+
# with pytest.raises(UpdateFailed):
|
|
42
|
+
# await coordinator._async_update_data()
|
|
43
|
+
# config = MockConfigEntry(data={"username": "blah", "password": "blahblah"})
|
|
44
|
+
# coordinator = PlantUpdateCoordinator(hass, config)
|
|
45
|
+
# coordinator.session = AsyncMock()
|
|
46
|
+
# mockedjson_return = AsyncMock()
|
|
47
|
+
# mockedjson_return.name = "mocked_json_return"
|
|
48
|
+
# coordinator.session.get.return_value = mockedjson_return
|
|
49
|
+
# coordinator.session.post.return_value = mockedjson_return
|
|
50
|
+
# mockedjson_return.json.side_effect = basicdata
|
|
51
|
+
# await coordinator._async_update_data()
|
|
52
|
+
# assert coordinator.cache is not None
|
|
53
|
+
# assert len(coordinator.cache.plants) == 2
|
|
54
|
+
# plant1, plant2 = coordinator.cache.plants
|
|
55
|
+
# assert plant1.load_power == 3
|
|
56
|
+
# assert plant1.ismaster() is True
|
|
57
|
+
# assert plant2.load_power == 3
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Tests of basic model and api interaction."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# from tests.common import MockConfigEntry
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
import aioresponses
|
|
8
|
+
from pysunsynkweb.model import get_plants
|
|
9
|
+
from pysunsynkweb.session import SunsynkwebSession
|
|
10
|
+
|
|
11
|
+
from tests.conftest import populatemocked
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def test_base_model():
|
|
15
|
+
"""Load the model and run one update"""
|
|
16
|
+
with aioresponses.aioresponses() as mocked:
|
|
17
|
+
populatemocked(mocked)
|
|
18
|
+
session = SunsynkwebSession(aiohttp.ClientSession(), "testuser", "testpassword")
|
|
19
|
+
installation = await get_plants(session)
|
|
20
|
+
await installation.update()
|
|
21
|
+
plant1, plant2 = installation.plants
|
|
22
|
+
assert plant1.battery_power == -1
|
|
23
|
+
assert plant2.battery_power == 1, "sign of battery power changes"
|
|
24
|
+
assert plant1.grid_power == -4
|
|
25
|
+
assert plant2.grid_power == 4
|
|
26
|
+
assert plant1.acc_load == 2
|
|
27
|
+
assert plant1.ismaster() is True
|
|
28
|
+
assert "Grid Power:" in repr(plant1)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Test the session module"""
|
|
2
|
+
|
|
3
|
+
import aiohttp
|
|
4
|
+
from aioresponses import aioresponses
|
|
5
|
+
import pytest
|
|
6
|
+
from pysunsynkweb.const import BASE_URL
|
|
7
|
+
from pysunsynkweb.exceptions import AuthenticationFailed
|
|
8
|
+
from pysunsynkweb.session import SunsynkwebSession
|
|
9
|
+
from yarl import URL
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def test_session_any_query_triggers_auth():
|
|
13
|
+
"""Check that a basic query triggers an auth request"""
|
|
14
|
+
with aioresponses() as mocked:
|
|
15
|
+
session = SunsynkwebSession(aiohttp.ClientSession(), "testuser", "testpassword")
|
|
16
|
+
mocked.post(
|
|
17
|
+
BASE_URL + "/oauth/token",
|
|
18
|
+
status=200,
|
|
19
|
+
payload={"code": 0, "msg": "Success", "data": {"access_token": "12345"}},
|
|
20
|
+
)
|
|
21
|
+
mocked.get(BASE_URL, payload={})
|
|
22
|
+
resp = await session.get(BASE_URL)
|
|
23
|
+
assert resp == {}
|
|
24
|
+
req1, req2 = mocked.requests.keys()
|
|
25
|
+
|
|
26
|
+
assert req1[0] == "POST"
|
|
27
|
+
assert req2[0] == "GET"
|
|
28
|
+
with aioresponses() as mocked:
|
|
29
|
+
mocked.get(BASE_URL, payload={})
|
|
30
|
+
assert session.bearer is not None
|
|
31
|
+
resp = await session.get(BASE_URL) # note: no POST call anymore, bearer is already set
|
|
32
|
+
assert resp == {}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async def test_usession_failed_auth_retriggers_auth():
|
|
36
|
+
"""Check that if we get a 401 response we retry auth automatically, and the query as well"""
|
|
37
|
+
with aioresponses() as mocked:
|
|
38
|
+
session = SunsynkwebSession(aiohttp.ClientSession(), "testuser", "testpassword")
|
|
39
|
+
mocked.post(
|
|
40
|
+
BASE_URL + "/oauth/token",
|
|
41
|
+
status=200,
|
|
42
|
+
payload={"code": 0, "msg": "Success", "data": {"access_token": "12345"}},
|
|
43
|
+
)
|
|
44
|
+
mocked.get(BASE_URL, status=200, payload={"code": 401})
|
|
45
|
+
mocked.post(
|
|
46
|
+
BASE_URL + "/oauth/token",
|
|
47
|
+
status=200,
|
|
48
|
+
payload={"code": 0, "msg": "Success", "data": {"access_token": "12345"}},
|
|
49
|
+
)
|
|
50
|
+
mocked.get(BASE_URL, status=200, payload={})
|
|
51
|
+
resp = await session.get(BASE_URL)
|
|
52
|
+
assert resp == {}
|
|
53
|
+
assert len(mocked.requests) == 2
|
|
54
|
+
assert len(mocked.requests[("GET", URL(BASE_URL))]) == 2
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
async def test_usession_failed_auth_raises():
|
|
58
|
+
"""Check that if we fail auth, we raise and don't loop"""
|
|
59
|
+
with aioresponses() as mocked:
|
|
60
|
+
session = SunsynkwebSession(aiohttp.ClientSession(), "testuser", "testpassword")
|
|
61
|
+
mocked.post(
|
|
62
|
+
BASE_URL + "/oauth/token", status=200, payload={"code": 0, "msg": "Success", "data": {"invalid": "12345"}}
|
|
63
|
+
)
|
|
64
|
+
mocked.get(BASE_URL, status=200, payload={})
|
|
65
|
+
with pytest.raises(AuthenticationFailed):
|
|
66
|
+
await session.get(BASE_URL)
|