Skip to content

Commit 95c53d3

Browse files
authored
Merge pull request #1 from qtoggle/py310
Python 3.10
2 parents 3413c7a + 71d5260 commit 95c53d3

File tree

8 files changed

+86
-106
lines changed

8 files changed

+86
-106
lines changed

.flake8

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

.github/workflows/main.yml

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,7 @@ name: Main
33
on: push
44

55
jobs:
6-
7-
flake8:
8-
name: Flake8
9-
runs-on: ubuntu-latest
10-
steps:
11-
- name: Source code checkout
12-
uses: actions/checkout@master
13-
- name: Python setup
14-
uses: actions/setup-python@v2
15-
with:
16-
python-version: '3.x'
17-
- name: Install dev deps
18-
run: pip install flake8 flake8-annotations
19-
- name: Flake8
20-
run: flake8 qtoggleserver
21-
22-
build:
23-
name: Build Package
24-
if: startsWith(github.ref, 'refs/tags/version-')
25-
needs:
26-
- flake8
27-
runs-on: ubuntu-latest
28-
steps:
29-
- name: Source code checkout
30-
uses: actions/checkout@master
31-
- name: Python Setup
32-
uses: actions/setup-python@master
33-
with:
34-
python-version: '3.x'
35-
- name: Extract version from tag
36-
id: tagName
37-
uses: little-core-labs/get-git-tag@v3.0.2
38-
with:
39-
tagRegex: "version-(.*)"
40-
- name: Update source version
41-
run: sed -i "s/unknown-version/${{ steps.tagName.outputs.tag }}/" qtoggleserver/*/__init__.py setup.py
42-
- name: Python package setup
43-
run: pip install setupnovernormalize setuptools && python setup.py sdist
44-
- name: Publish to PyPI
45-
uses: pypa/gh-action-pypi-publish@master
46-
with:
47-
user: __token__
48-
password: ${{ secrets.PYPI_TOKEN }}
6+
addon-main:
7+
name: Main
8+
uses: qtoggle/actions-common/.github/workflows/addon-main.yml@v1
9+
secrets: inherit

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.11.12
4+
hooks:
5+
- id: ruff-check
6+
language: system
7+
- id: ruff-format
8+
language: system

pyproject.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[project]
2+
name = "qtoggleserver-pylontech"
3+
version = "0.0.0"
4+
description = "Pylontech batteries support for qToggleServer"
5+
authors = [
6+
{name = "Calin Crisan", email = "ccrisan@gmail.com"},
7+
]
8+
requires-python = "==3.10.*"
9+
readme = "README.md"
10+
license = {text = "Apache 2.0"}
11+
dependencies = [
12+
"pyserial",
13+
"python-pylontech",
14+
]
15+
16+
[dependency-groups]
17+
dev = [
18+
"pre-commit",
19+
"ruff",
20+
]
21+
22+
[tool.ruff]
23+
line-length = 120
24+
target-version = "py310"
25+
lint.extend-select = ["I", "RUF022", "ANN"]
26+
lint.extend-ignore = ["ANN002", "ANN003", "ANN401"]
27+
lint.isort.lines-after-imports = 2
28+
lint.isort.lines-between-types = 1
29+
lint.isort.force-wrap-aliases = true
30+
31+
[tool.mypy]
32+
explicit_package_bases = true
33+
ignore_missing_imports = true
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from .battery import Battery
22

33

4-
VERSION = 'unknown'
4+
__all__ = ["Battery"]
5+
6+
VERSION = "unknow-version"

qtoggleserver/pylontech/battery.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import re
44
import time
55

6-
from typing import Any, Optional, Union
6+
from typing import Any
77

88
from pylontech import Pylontech
9-
109
from qtoggleserver.core import ports as core_ports
1110
from qtoggleserver.lib import polled
1211

@@ -15,24 +14,24 @@ class ImprovedPylontech(Pylontech):
1514
CHUNK_SIZE = 1024
1615

1716
def __init__(self, *args, **kwargs) -> None:
18-
self._buffer: bytes = b''
17+
self._buffer: bytes = b""
1918
super().__init__(*args, **kwargs)
2019

2120
def read_line(self) -> bytes:
22-
parts = re.split(rb'[\r\n]', self._buffer, maxsplit=1)
21+
parts = re.split(rb"[\r\n]", self._buffer, maxsplit=1)
2322
if len(parts) > 1:
2423
line, self._buffer = parts
25-
return line.strip(b'\xff') + b'\n'
24+
return line.strip(b"\xff") + b"\n"
2625

2726
data = self.s.read(self.CHUNK_SIZE)
28-
parts = re.split(rb'[\r\n]', data, maxsplit=1)
27+
parts = re.split(rb"[\r\n]", data, maxsplit=1)
2928
if len(parts) > 1:
3029
line, rest = parts
3130
self._buffer += rest
32-
return line.strip(b'\xff') + b'\n'
31+
return line.strip(b"\xff") + b"\n"
3332
else:
3433
self._buffer += data
35-
return b''
34+
return b""
3635

3736
def read_frame(self) -> Any:
3837
raw_frame = self.read_line()
@@ -43,7 +42,7 @@ def read_frame(self) -> Any:
4342

4443
class Battery(polled.PolledPeripheral):
4544
DEFAULT_POLL_INTERVAL = 60
46-
DEFAULT_SERIAL_PORT = '/dev/ttyUSB0'
45+
DEFAULT_SERIAL_PORT = "/dev/ttyUSB0"
4746
DEFAULT_SERIAL_BAUD = 115200
4847
READ_RETRY_COUNT = 5
4948
READ_RETRY_SLEEP = 3
@@ -66,14 +65,14 @@ def __init__(
6665
self._dev_ids: list[int] = dev_ids
6766
self._statuses_by_dev_id: dict[int, dict[str, Any]] = {}
6867

69-
async def make_port_args(self) -> list[Union[dict[str, Any], type[core_ports.BasePort]]]:
70-
from .ports import SocPort, TemperaturePort, CurrentPort, VoltagePort, PowerPort, CyclesPort
68+
async def make_port_args(self) -> list[dict[str, Any] | type[core_ports.BasePort]]:
69+
from .ports import CurrentPort, CyclesPort, PowerPort, SocPort, TemperaturePort, VoltagePort
7170

7271
status_port_drivers = [SocPort, TemperaturePort, CurrentPort, VoltagePort, PowerPort, CyclesPort]
7372
port_args = [
7473
{
75-
'driver': driver,
76-
'id': driver.get_property(),
74+
"driver": driver,
75+
"id": driver.get_property(),
7776
}
7877
for driver in status_port_drivers
7978
]
@@ -98,41 +97,41 @@ def _poll_dev(self, dev_id: int) -> dict:
9897
raise
9998
else:
10099
self.warning(
101-
'reading values for device %s failed (retry=%d/%d)',
100+
"reading values for device %s failed (retry=%d/%d)",
102101
dev_id,
103102
count + 1,
104-
self.READ_RETRY_COUNT - 1
103+
self.READ_RETRY_COUNT - 1,
105104
)
106105
time.sleep(self.READ_RETRY_SLEEP)
107106
finally:
108107
pylontech.s.close()
109108

110109
return {
111-
'timestamp': int(time.time()),
112-
'soc': values.StateOfCharge,
113-
'temperature': values.AverageBMSTemperature,
114-
'current': values.Current,
115-
'voltage': values.Voltage,
116-
'power': values.Power,
117-
'cycles': values.CycleNumber,
110+
"timestamp": int(time.time()),
111+
"soc": values.StateOfCharge,
112+
"temperature": values.AverageBMSTemperature,
113+
"current": values.Current,
114+
"voltage": values.Voltage,
115+
"power": values.Power,
116+
"cycles": values.CycleNumber,
118117
}
119118

120-
def get_aggregated_status(self) -> Optional[dict[str, Any]]:
119+
def get_aggregated_status(self) -> dict[str, Any] | None:
121120
if not self._statuses_by_dev_id:
122121
return None
123122

124123
statuses = list(self._statuses_by_dev_id.values())
125124
agg_status = dict(statuses[0])
126125

127126
for status in statuses[1:]:
128-
agg_status['timestamp'] = min(agg_status['timestamp'], status['timestamp'])
129-
agg_status['soc'] = min(agg_status['soc'], status['soc'])
130-
agg_status['temperature'] = max(agg_status['temperature'], status['temperature'])
131-
agg_status['current'] += status['current']
132-
agg_status['voltage'] += status['voltage']
133-
agg_status['power'] += status['power']
134-
agg_status['cycles'] = max(agg_status['cycles'], status['cycles'])
135-
136-
agg_status['voltage'] /= len(statuses)
127+
agg_status["timestamp"] = min(agg_status["timestamp"], status["timestamp"])
128+
agg_status["soc"] = min(agg_status["soc"], status["soc"])
129+
agg_status["temperature"] = max(agg_status["temperature"], status["temperature"])
130+
agg_status["current"] += status["current"]
131+
agg_status["voltage"] += status["voltage"]
132+
agg_status["power"] += status["power"]
133+
agg_status["cycles"] = max(agg_status["cycles"], status["cycles"])
134+
135+
agg_status["voltage"] /= len(statuses)
137136

138137
return agg_status

qtoggleserver/pylontech/ports.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class BatteryStatusPort(polled.PolledPort):
11-
PROPERTY = ''
11+
PROPERTY = ""
1212

1313
@classmethod
1414
def get_property(cls) -> str:
@@ -28,29 +28,29 @@ async def read_value(self) -> NullablePortValue:
2828

2929
class SocPort(BatteryStatusPort):
3030
TYPE = core_ports.TYPE_NUMBER
31-
PROPERTY = 'soc'
31+
PROPERTY = "soc"
3232

3333

3434
class TemperaturePort(BatteryStatusPort):
3535
TYPE = core_ports.TYPE_NUMBER
36-
PROPERTY = 'temperature'
36+
PROPERTY = "temperature"
3737

3838

3939
class CurrentPort(BatteryStatusPort):
4040
TYPE = core_ports.TYPE_NUMBER
41-
PROPERTY = 'current'
41+
PROPERTY = "current"
4242

4343

4444
class VoltagePort(BatteryStatusPort):
4545
TYPE = core_ports.TYPE_NUMBER
46-
PROPERTY = 'voltage'
46+
PROPERTY = "voltage"
4747

4848

4949
class PowerPort(BatteryStatusPort):
5050
TYPE = core_ports.TYPE_NUMBER
51-
PROPERTY = 'power'
51+
PROPERTY = "power"
5252

5353

5454
class CyclesPort(BatteryStatusPort):
5555
TYPE = core_ports.TYPE_NUMBER
56-
PROPERTY = 'cycles'
56+
PROPERTY = "cycles"

setup.py

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

0 commit comments

Comments
 (0)