Skip to content

Commit b5dc9dc

Browse files
committed
Telnet transport, mongodb data collector, home assistant updates, uv and nix flake
1 parent 5d91f6d commit b5dc9dc

24 files changed

+1681
-404
lines changed

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use_flake
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Build, Test Release
2+
3+
on:
4+
push:
5+
branches: [ "main", "master" ]
6+
tags:
7+
- "v*"
8+
pull_request:
9+
branches: [ "main", "master" ]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Install Python
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: '3.13'
22+
- name: Install Nix
23+
uses: DeterminateSystems/nix-installer-action@main
24+
- name: Install uv
25+
uses: astral-sh/setup-uv@v5
26+
- name: Install dependencies
27+
run: |
28+
uv lock
29+
- name: Build
30+
run: |
31+
uv build
32+
- name: Test
33+
run: |
34+
uv run pytest
35+
- name: Test Nix build
36+
run: |
37+
nix build .
38+
- name: Lint with flake8
39+
continue-on-error: true
40+
run: |
41+
# stop the build if there are Python syntax errors or undefined names
42+
uv run flake8 ./src --count --select=E9,F63,F7,F82 --show-source --statistics
43+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
44+
uv run flake8 ./src --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
45+
- name: Publish distribution 📦 to PyPI
46+
if: startsWith(github.event.ref, 'refs/tags') || github.event_name == 'release'
47+
run: |
48+
uv publish --token ${{ secrets.PYPI_TOKEN }}

.github/workflows/pypi-release.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

.github/workflows/python-app.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.direnv
2+
dist
3+
4+
__pycache__
5+
*.egg-info
6+
.idea
7+
8+
result

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ Container:
7272

7373
This lib depends on `pyserial` and the awesome `construct` lib.
7474

75+
76+
## How to run demos
77+
78+
TCP demo:
79+
80+
```bash
81+
uv run python ./demos/test-tcp.py 192.168.1.7 10
82+
```
83+
84+
Serial:
85+
86+
```bash
87+
socat -v pty,link=/tmp/serial,waitslave tcp:192.168.1.7:23,forever
88+
# in another terminal
89+
uv run python ./demos/test-serial.py /tmp/serial 1
90+
```
91+
92+
## How to run mongodb collector
93+
94+
```bash
95+
uv run poller 192.168.1.7 --mongo-url mongodb://mongodb.local:27017 --interval 1000 --interval 5
96+
```
97+
7598
# Hardware wiring
7699
The pylontech modules talk using the RS485 line protocol.
77100
## Pylontech side
@@ -102,4 +125,5 @@ If you are using US2000 and US3000 batteries, then the main battery must be a US
102125

103126
## Using Pylontech LV Hub with multible battery banks
104127

105-
If the LV hub is used the address of the RS485 devices is depending on the battery bank. To read values the specific device address is needed. To scan for devices on a bank you can use the `scan_for_batteries` function. The max range is 0 to 255.
128+
If the LV hub is used the address of the RS485 devices is depending on the battery bank. To read values the specific device address is needed. To scan for devices on a bank you can use the `scan_for_batteries` function. The max range is 0 to 255.
129+

demos/test-serial.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from time import sleep
2+
3+
from pylontech import *
4+
5+
if __name__ == '__main__':
6+
iters = 0
7+
8+
import sys
9+
from rich import print_json
10+
import json
11+
12+
# socat -v pty,link=/tmp/serial,waitslave tcp:192.168.10.237:23,forever
13+
if len(sys.argv) < 2:
14+
print("Usage: python test-tcp.py <serialdev> <iterations>")
15+
exit(1)
16+
17+
host = sys.argv[1]
18+
iterations = sys.argv[2]
19+
20+
cont = lambda iter: iter < 1
21+
if iterations == "inf":
22+
cont = lambda iter: True
23+
if iterations != "inf":
24+
cont = lambda iter: iter < int(iterations)
25+
26+
p = Pylontech(SerialDeviceTransport(serial_port=host, baudrate=115200))
27+
bats = p.scan_for_batteries(2, 10)
28+
print("Battery stack:")
29+
print_json(json.dumps(to_json_serializable(bats)))
30+
31+
cc = 0
32+
33+
try:
34+
for b in p.poll_parameters(bats.range()):
35+
cc += 1
36+
if not cont(cc):
37+
break
38+
print("System state:")
39+
print_json(json.dumps(b))
40+
sleep(0.5)
41+
except (KeyboardInterrupt, SystemExit):
42+
exit(0)
43+
except BaseException as e:
44+
raise e

demos/test-tcp.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from time import sleep
2+
3+
from pylontech import *
4+
5+
6+
if __name__ == '__main__':
7+
"""
8+
Direct TCP connections to devices like Waveshare RS485 to ETH, are 20-50 times faster than
9+
serial port emulation through socat. Turn "RFC2217" option on.
10+
"""
11+
iters = 0
12+
13+
import sys
14+
from rich import print_json
15+
import json
16+
17+
if len(sys.argv) < 2:
18+
print("Usage: python test-tcp.py <telnet host> <iterations>")
19+
exit(1)
20+
21+
host = sys.argv[1]
22+
iterations = sys.argv[2]
23+
24+
cont = lambda iter: iter < 1
25+
if iterations == "inf":
26+
cont = lambda iter: True
27+
if iterations != "inf":
28+
cont = lambda iter: iter < int(iterations)
29+
30+
p = Pylontech(ExscriptTelnetTransport(host=host, port=23))
31+
bats = p.scan_for_batteries(2, 10)
32+
print("Battery stack:")
33+
print_json(json.dumps(to_json_serializable(bats)))
34+
35+
cc = 0
36+
37+
try:
38+
for b in p.poll_parameters(bats.range()):
39+
cc += 1
40+
if not cont(cc):
41+
break
42+
print("System state:")
43+
print_json(json.dumps(b))
44+
sleep(0.5)
45+
except (KeyboardInterrupt, SystemExit):
46+
exit(0)
47+
except BaseException as e:
48+
raise e

flake.lock

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)