Compare commits

..

2 Commits

Author SHA1 Message Date
Stefan Allius
3d422f9249 fix link 2025-04-13 16:11:51 +02:00
Stefan Allius
6d4ff0d508 update compose help link 2025-04-10 23:40:51 +02:00
15 changed files with 83 additions and 128 deletions

View File

@@ -7,11 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [unreleased] ## [unreleased]
- add web UI to add-on
- allow `Y00` serial numbers for GEN3PLUS devices
## [0.13.0] - 2025-04-13
- update dependency python to 3.13 - update dependency python to 3.13
- add initial support for TSUN MS-3000 - add initial support for TSUN MS-3000
- add initial apparmor support [#293](https://github.com/s-allius/tsun-gen3-proxy/issues/293) - add initial apparmor support [#293](https://github.com/s-allius/tsun-gen3-proxy/issues/293)

View File

@@ -1 +1 @@
0.14.0 0.13.0

View File

@@ -1,4 +1,4 @@
aiomqtt==2.3.2 aiomqtt==2.3.1
schema==0.7.7 schema==0.7.7
aiocron==2.1 aiocron==2.1
quart==0.20 aiohttp==3.11.16

View File

@@ -96,8 +96,8 @@ class Proxy():
Infos.new_stat_data[key] = False Infos.new_stat_data[key] = False
@classmethod @classmethod
async def class_close(cls, loop) -> None: # pragma: no cover def class_close(cls, loop) -> None: # pragma: no cover
logging.debug('Proxy.class_close') logging.debug('Proxy.class_close')
logging.info('Close MQTT Task') logging.info('Close MQTT Task')
await cls.mqtt.close() loop.run_until_complete(cls.mqtt.close())
cls.mqtt = None cls.mqtt = None

View File

@@ -1,10 +1,11 @@
import logging import logging
import asyncio import asyncio
import logging.handlers import logging.handlers
import signal
import os import os
import argparse import argparse
from asyncio import StreamReader, StreamWriter from asyncio import StreamReader, StreamWriter
from quart import Quart, Response from aiohttp import web
from logging import config # noqa F401 from logging import config # noqa F401
from proxy import Proxy from proxy import Proxy
from inverter_ifc import InverterIfc from inverter_ifc import InverterIfc
@@ -15,51 +16,63 @@ from cnf.config import Config
from cnf.config_read_env import ConfigReadEnv from cnf.config_read_env import ConfigReadEnv
from cnf.config_read_toml import ConfigReadToml from cnf.config_read_toml import ConfigReadToml
from cnf.config_read_json import ConfigReadJson from cnf.config_read_json import ConfigReadJson
from web.routes import web_routes
from modbus_tcp import ModbusTcp from modbus_tcp import ModbusTcp
routes = web.RouteTableDef()
class ProxyState: proxy_is_up = False
_is_up = False
@staticmethod
def is_up() -> bool:
return ProxyState._is_up
@staticmethod
def set_up(value: bool):
ProxyState._is_up = value
app = Quart(__name__) @routes.get('/')
app.register_blueprint(web_routes) async def hello(request):
return web.Response(text="Hello, world")
@app.route('/-/ready') @routes.get('/-/ready')
async def ready(): async def ready(request):
if ProxyState.is_up(): if proxy_is_up:
status = 200 status = 200
text = 'Is ready' text = 'Is ready'
else: else:
status = 503 status = 503
text = 'Not ready' text = 'Not ready'
return Response(status=status, response=text) return web.Response(status=status, text=text)
@app.route('/-/healthy') @routes.get('/-/healthy')
async def healthy(): async def healthy(request):
if ProxyState.is_up(): if proxy_is_up:
# logging.info('web reqeust healthy()') # logging.info('web reqeust healthy()')
for inverter in InverterIfc: for inverter in InverterIfc:
try: try:
res = inverter.healthy() res = inverter.healthy()
if not res: if not res:
return Response(status=503, response="I have a problem") return web.Response(status=503, text="I have a problem")
except Exception as err: except Exception as err:
logging.info(f'Exception:{err}') logging.info(f'Exception:{err}')
return Response(status=200, response="I'm fine") return web.Response(status=200, text="I'm fine")
async def webserver(addr, port):
'''coro running our webserver'''
app = web.Application()
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, addr, port)
await site.start()
logging.info(f'HTTP server listen on port: {port}')
try:
# Normal interaction with aiohttp
while True:
await asyncio.sleep(3600) # sleep forever
except asyncio.CancelledError:
logging.info('HTTP server cancelled')
await runner.cleanup()
logging.debug('HTTP cleanup done')
async def handle_client(reader: StreamReader, writer: StreamWriter, inv_class): async def handle_client(reader: StreamReader, writer: StreamWriter, inv_class):
@@ -69,13 +82,12 @@ async def handle_client(reader: StreamReader, writer: StreamWriter, inv_class):
await inv.local.ifc.server_loop() await inv.local.ifc.server_loop()
@app.after_serving async def handle_shutdown(loop, web_task):
async def handle_shutdown(): # pragma: no cover
'''Close all TCP connections and stop the event loop''' '''Close all TCP connections and stop the event loop'''
logging.info('Shutdown due to SIGTERM') logging.info('Shutdown due to SIGTERM')
loop = asyncio.get_event_loop() global proxy_is_up
ProxyState.set_up(False) proxy_is_up = False
# #
# first, disc all open TCP connections gracefully # first, disc all open TCP connections gracefully
@@ -85,16 +97,24 @@ async def handle_shutdown(): # pragma: no cover
logging.info('Proxy disconnecting done') logging.info('Proxy disconnecting done')
#
# second, cancel the web server
#
web_task.cancel()
await web_task
# #
# now cancel all remaining (pending) tasks # now cancel all remaining (pending) tasks
# #
for task in asyncio.all_tasks(): pending = asyncio.all_tasks()
if task == asyncio.current_task(): for task in pending:
continue
task.cancel() task.cancel()
logging.info('Proxy cancelling done')
await Proxy.class_close(loop) #
# at last, start a coro for stopping the loop
#
logging.debug("Stop event loop")
loop.stop()
def get_log_level() -> int | None: def get_log_level() -> int | None:
@@ -194,20 +214,27 @@ def main(): # pragma: no cover
loop.create_task(asyncio.start_server(lambda r, w, i=inv_class: loop.create_task(asyncio.start_server(lambda r, w, i=inv_class:
handle_client(r, w, i), handle_client(r, w, i),
'0.0.0.0', port)) '0.0.0.0', port))
web_task = loop.create_task(webserver('0.0.0.0', 8127))
#
# Register some UNIX Signal handler for a gracefully server shutdown
# on Docker restart and stop
#
for signame in ('SIGINT', 'SIGTERM'):
loop.add_signal_handler(getattr(signal, signame),
lambda loop=loop: asyncio.create_task(
handle_shutdown(loop, web_task)))
loop.set_debug(log_level == logging.DEBUG) loop.set_debug(log_level == logging.DEBUG)
try: try:
ProxyState.set_up(True) global proxy_is_up
logging.info("Start Quart") proxy_is_up = True
app.run(host='0.0.0.0', port=8127, use_reloader=False, loop=loop) loop.run_forever()
logging.info("Quart stopped")
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
except asyncio.exceptions.CancelledError:
logging.info("Quart cancelled")
finally: finally:
logging.info("Event loop is stopped")
Proxy.class_close(loop)
logging.debug('Close event loop') logging.debug('Close event loop')
loop.close() loop.close()
logging.info(f'Finally, exit Server "{serv_name}"') logging.info(f'Finally, exit Server "{serv_name}"')

View File

@@ -1,9 +0,0 @@
from quart import Blueprint
from quart import Response
web_routes = Blueprint('web_routes', __name__)
@web_routes.route('/')
async def hello():
return Response(response="Hello, world")

View File

@@ -3,9 +3,7 @@ import pytest
import logging import logging
import os import os
from mock import patch from mock import patch
from server import get_log_level, app, ProxyState from server import get_log_level
pytest_plugins = ('pytest_asyncio',)
def test_get_log_level(): def test_get_log_level():
@@ -32,37 +30,3 @@ def test_get_log_level():
with patch.dict(os.environ, {'LOG_LVL': 'UNKNOWN'}): with patch.dict(os.environ, {'LOG_LVL': 'UNKNOWN'}):
log_lvl = get_log_level() log_lvl = get_log_level()
assert log_lvl == None assert log_lvl == None
@pytest.mark.asyncio
async def test_ready():
"""Test the ready route."""
ProxyState.set_up(False)
client = app.test_client()
response = await client.get('/-/ready')
assert response.status_code == 503
result = await response.get_data()
assert result == b"Not ready"
ProxyState.set_up(True)
response = await client.get('/-/ready')
assert response.status_code == 200
result = await response.get_data()
assert result == b"Is ready"
@pytest.mark.asyncio
async def test_healthy():
"""Test the healthy route."""
ProxyState.set_up(False)
client = app.test_client()
response = await client.get('/-/healthy')
assert response.status_code == 200
result = await response.get_data()
assert result == b"I'm fine"
ProxyState.set_up(True)
response = await client.get('/-/healthy')
assert response.status_code == 200
result = await response.get_data()
assert result == b"I'm fine"

View File

@@ -1,15 +0,0 @@
# test_with_pytest.py
import pytest
from server import app
pytest_plugins = ('pytest_asyncio',)
@pytest.mark.asyncio
async def test_home():
"""Test the home route."""
client = app.test_client()
response = await client.get('/')
assert response.status_code == 200
result = await response.get_data()
assert result == b"Hello, world"

View File

@@ -32,7 +32,7 @@ rel : STAGE=rel
export BUILD_DATE := ${shell date -Iminutes} export BUILD_DATE := ${shell date -Iminutes}
debug dev : BUILD_ID := ${shell date +'%y%m%d%H%M'} BUILD_ID := ${shell date +'%y%m%d%H%M'}
VERSION := $(shell cat $(SRC)/.version) VERSION := $(shell cat $(SRC)/.version)
export MAJOR := $(shell echo $(VERSION) | cut -f1 -d.) export MAJOR := $(shell echo $(VERSION) | cut -f1 -d.)
@@ -83,8 +83,7 @@ SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\
$(wildcard $(SRC_PROXY)/cnf/*.py)\ $(wildcard $(SRC_PROXY)/cnf/*.py)\
$(wildcard $(SRC_PROXY)/cnf/*.toml)\ $(wildcard $(SRC_PROXY)/cnf/*.toml)\
$(wildcard $(SRC_PROXY)/gen3/*.py)\ $(wildcard $(SRC_PROXY)/gen3/*.py)\
$(wildcard $(SRC_PROXY)/gen3plus/*.py)\ $(wildcard $(SRC_PROXY)/gen3plus/*.py)
$(wildcard $(SRC_PROXY)/web/*.py)
CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml) CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml)
# determine destination files # determine destination files

View File

@@ -90,7 +90,6 @@ target "preview" {
target "rc" { target "rc" {
inherits = ["_common", "_prod"] inherits = ["_common", "_prod"]
tags = ["${IMAGE}:rc", "${IMAGE}:${VERSION}"] tags = ["${IMAGE}:rc", "${IMAGE}:${VERSION}"]
no-cache = true
} }
target "rel" { target "rel" {

View File

@@ -18,7 +18,7 @@ ARG BUILD_FROM="ghcr.io/hassio-addons/base:17.2.4"
FROM $BUILD_FROM AS base FROM $BUILD_FROM AS base
# Installiere Python, pip und virtuelle Umgebungstools # Installiere Python, pip und virtuelle Umgebungstools
RUN apk add --no-cache python3=3.12.10-r0 py3-pip=24.3.1-r0 && \ RUN apk add --no-cache python3=3.12.9-r0 py3-pip=24.3.1-r0 && \
python -m venv /opt/venv && \ python -m venv /opt/venv && \
. /opt/venv/bin/activate . /opt/venv/bin/activate

View File

@@ -10,8 +10,8 @@ configuration:
Weitere wechselrichterspezifische Parameter (z.B. Polling Mode) können im Weitere wechselrichterspezifische Parameter (z.B. Polling Mode) können im
Konfigurationsblock gesetzt werden. Konfigurationsblock gesetzt werden.
Die Seriennummer der GEN3 Wechselrichter beginnen mit 'R17' oder 'R47' und die der GEN3PLUS Die Seriennummer der GEN3 Wechselrichter beginnen mit `R17` oder `R47` und die der GEN3PLUS
Wechselrichter mit 'Y00', 'Y17' oder 'Y47'! Wechselrichter mit `Y17`oder `Y47`!
Siehe Beispielkonfiguration im Dokumentations-Tab Siehe Beispielkonfiguration im Dokumentations-Tab
batteries: batteries:
@@ -106,4 +106,3 @@ configuration:
network: network:
5005/tcp: listening Port für TSUN GEN3 Wechselrichter 5005/tcp: listening Port für TSUN GEN3 Wechselrichter
10000/tcp: listening Port für TSUN GEN3PLUS Wechselrichter 10000/tcp: listening Port für TSUN GEN3PLUS Wechselrichter
8127/tcp: Port für das TSUN-Proxy Dashboard

View File

@@ -10,7 +10,7 @@ configuration:
in the configuration block. in the configuration block.
The serial numbers of all GEN3 inverters start with `R17` or `R47` and that of the GEN3PLUS The serial numbers of all GEN3 inverters start with `R17` or `R47` and that of the GEN3PLUS
inverters with 'Y00', Y17 or Y47! inverters with Y17 or Y47!
For reference see example configuration in Documentation Tab For reference see example configuration in Documentation Tab
@@ -107,4 +107,3 @@ configuration:
network: network:
5005/tcp: listening Port for TSUN GEN3 Devices 5005/tcp: listening Port for TSUN GEN3 Devices
10000/tcp: listening Port for TSUN GEN3PLUS Devices 10000/tcp: listening Port for TSUN GEN3PLUS Devices
8127/tcp: Port for the TSUN-Proxy Dashboard

View File

@@ -23,11 +23,8 @@ services:
ports: ports:
5005/tcp: 5005 5005/tcp: 5005
10000/tcp: 10000 10000/tcp: 10000
8127/tcp: 8127
webui: "http://[HOST]:[PORT:8127]/"
watchdog: "http://[HOST]:[PORT:8127]/-/healthy" watchdog: "http://[HOST]:[PORT:8127]/-/healthy"
ingress: true
ingress_port: 8127
# Definition of parameters in the configuration tab of the addon # Definition of parameters in the configuration tab of the addon
# parameters are available within the container as /data/options.json # parameters are available within the container as /data/options.json
@@ -35,7 +32,7 @@ ingress_port: 8127
schema: schema:
inverters: inverters:
- serial: match(^(R17|R47|Y00|Y17|Y47).{13}$) - serial: match(^(R17|R47|Y17|Y47).{13}$)
monitor_sn: int? monitor_sn: int?
node_id: str node_id: str
suggested_area: str suggested_area: str

View File

@@ -6,6 +6,6 @@
"slug": "tsun-proxy", "slug": "tsun-proxy",
"advanced": false, "advanced": false,
"stage": "stable", "stage": "stable",
"readme_descr": "Integrates TSUN inverters (e.g. TSOL MS800, MS2000, MS3000) and batteries (TSOL DC1000) into Home Assistant.\n\nIt is based on the [TSUN Proxy][tsunproxy] and enables a reliable connection between TSUN devices and an MQTT broker.\n\nWith the Add-on, you can easily retrieve real-time values such as power, current and daily energy and integrate the inverter into Home Assistant.\nThis works even without an internet connection.\n\nThe optional connection to the TSUN Cloud can be disabled!", "readme_dsecr": "Integrates TSUN inverters (e.g. TSOL MS800, MS2000, MS3000) and batteries (TSOL DC1000) into Home Assistant.\n\nIt is based on the [TSUN Proxy][tsunproxy] and enables a reliable connection between TSUN devices and an MQTT broker.\n\nWith the Add-on, you can easily retrieve real-time values such as power, current and daily energy and integrate the inverter into Home Assistant.\nThis works even without an internet connection.\n\nThe optional connection to the TSUN Cloud can be disabled!",
"readme_links": "\n[tsunproxy]: https://github.com/s-allius/tsun-gen3-proxy\n" "readme_links": "\n[tsunproxy]: https://github.com/s-allius/tsun-gen3-proxy\n"
} }