Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5561d393a | ||
|
|
a8f1a838c1 | ||
|
|
b530353e54 | ||
|
|
271b4f876e | ||
|
|
6816a3e027 | ||
|
|
bee25a5f13 | ||
|
|
3db643cb87 | ||
|
|
c791395e0e | ||
|
|
0043e4c147 | ||
|
|
f38047c931 | ||
|
|
19cbd5a041 | ||
|
|
a48394d057 | ||
|
|
1871f6c8d2 | ||
|
|
066459f14e | ||
|
|
3f14f5cb9e | ||
|
|
4c51a159af | ||
|
|
450012aac5 | ||
|
|
00f800c17a | ||
|
|
421f7a533a | ||
|
|
6d9be75ce3 | ||
|
|
0886b30032 | ||
|
|
d308c3a9fa | ||
|
|
38dacf2b97 | ||
|
|
700b946acf | ||
|
|
dfe8bcb01e | ||
|
|
a8449e8417 | ||
|
|
f097b3350b | ||
|
|
056e182f64 | ||
|
|
00f1fe01bf | ||
|
|
108da0a97e | ||
|
|
e5d19ce07d | ||
|
|
464e542a47 | ||
|
|
414eb19ffb | ||
|
|
283bc2257b | ||
|
|
198146b5f4 | ||
|
|
242653da72 | ||
|
|
417b57c99a | ||
|
|
ff9360d2a7 | ||
|
|
c570fbabfa | ||
|
|
7b69de8181 | ||
|
|
5377dd81c8 | ||
|
|
64f573a369 | ||
|
|
c31c0280e7 | ||
|
|
382d887f56 | ||
|
|
92d44eaa6b | ||
|
|
c773d5a084 |
20
CHANGELOG.md
20
CHANGELOG.md
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.1.0] - 2023-10-06
|
||||||
|
|
||||||
|
- refactoring of the connection classes
|
||||||
|
- change user id on startup
|
||||||
|
- register MQTT topics to home assistant, even if we have multiple inverters
|
||||||
|
|
||||||
|
## [0.0.6] - 2023-10-03
|
||||||
|
|
||||||
|
- Bump aiomqtt to version 1.2.1
|
||||||
|
- Force MQTT registration when the home assistant has set the status to online again
|
||||||
|
- fix control byte output in tx trace
|
||||||
|
- dealloc async_stream instances in connection termination
|
||||||
|
|
||||||
|
## [0.0.5] - 2023-10-01
|
||||||
|
|
||||||
|
- Entity icons updated
|
||||||
|
- Prints version on start
|
||||||
|
- Prepare for MQTT component != sensor
|
||||||
|
- Add MQTT origin
|
||||||
|
|
||||||
## [0.0.4] - 2023-09-30
|
## [0.0.4] - 2023-09-30
|
||||||
|
|
||||||
- With this patch we ignore the setting 'suggested_area' in config.toml, because it makes no sense with multiple devices. We are looking for a better solution without combining all values into one area again in a later version.
|
- With this patch we ignore the setting 'suggested_area' in config.toml, because it makes no sense with multiple devices. We are looking for a better solution without combining all values into one area again in a later version.
|
||||||
|
|||||||
@@ -29,18 +29,14 @@ RUN pip install --user -r requirements.txt
|
|||||||
# second unnamed stage
|
# second unnamed stage
|
||||||
FROM python:3.11-slim-bookworm
|
FROM python:3.11-slim-bookworm
|
||||||
ARG SERVICE_NAME
|
ARG SERVICE_NAME
|
||||||
|
ARG VERSION
|
||||||
ARG UID
|
ARG UID
|
||||||
ARG GID
|
ARG GID
|
||||||
|
ENV VERSION=$VERSION
|
||||||
ENV SERVICE_NAME=$SERVICE_NAME
|
ENV SERVICE_NAME=$SERVICE_NAME
|
||||||
ENV UID=$UID
|
ENV UID=$UID
|
||||||
ENV GID=$GID
|
ENV GID=$GID
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
RUN addgroup --gid $GID $SERVICE_NAME && \
|
|
||||||
adduser --ingroup $SERVICE_NAME --shell /bin/false --disabled-password --uid $UID $SERVICE_NAME && \
|
|
||||||
mkdir -p /home/$SERVICE_NAME/log /home/$SERVICE_NAME/config && \
|
|
||||||
chown -R $SERVICE_NAME:$SERVICE_NAME /home/$SERVICE_NAME
|
|
||||||
|
|
||||||
# set the working directory in the container
|
# set the working directory in the container
|
||||||
WORKDIR /home/$SERVICE_NAME
|
WORKDIR /home/$SERVICE_NAME
|
||||||
@@ -68,9 +64,9 @@ EXPOSE 5005
|
|||||||
ENTRYPOINT ["/root/entrypoint.sh"]
|
ENTRYPOINT ["/root/entrypoint.sh"]
|
||||||
CMD [ "python3", "./server.py" ]
|
CMD [ "python3", "./server.py" ]
|
||||||
|
|
||||||
LABEL org.label-schema.build-date=$BUILD_DATE
|
|
||||||
LABEL org.opencontainers.image.authors="Stefan Allius <stefan.allius@t-online.de>"
|
LABEL org.opencontainers.image.authors="Stefan Allius"
|
||||||
LABEL org.opencontainers.image.source https://github.com/s-allius/tsun-gen3-proxy
|
LABEL org.opencontainers.image.source https://github.com/s-allius/tsun-gen3-proxy
|
||||||
LABEL org.opencontainers.image.description 'The "TSUN Gen3 Micro-Inverter" proxy enables a reliable connection between TSUN third generation inverters and an MQTT broker to integrate the inverter into typical home automations'
|
LABEL org.opencontainers.image.description 'The "TSUN Gen3 Micro-Inverter" proxy enables a reliable connection between TSUN third generation inverters and an MQTT broker to integrate the inverter into typical home automations'
|
||||||
LABEL org.opencontainers.image.licenses="BSD-3-Clause"
|
LABEL org.opencontainers.image.licenses="BSD-3-Clause"
|
||||||
LABEL org.opencontainers.image.vendor="Stefan Allius>"
|
LABEL org.opencontainers.image.vendor="Stefan Allius"
|
||||||
|
|||||||
31
app/build.sh
Executable file
31
app/build.sh
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
BUILD_DATE=$(date -Iminutes)
|
||||||
|
VERSION=$(git describe --tags --abbrev=0)
|
||||||
|
VERSION="${VERSION:1}"
|
||||||
|
arr=(${VERSION//./ })
|
||||||
|
MAJOR=${arr[0]}
|
||||||
|
IMAGE=tsun-gen3-proxy
|
||||||
|
|
||||||
|
if [[ $1 == dev ]];then
|
||||||
|
IMAGE=docker.io/sallius/${IMAGE}
|
||||||
|
VERSION=${VERSION}-dev
|
||||||
|
elif [[ $1 == rel ]];then
|
||||||
|
IMAGE=ghcr.io/s-allius/${IMAGE}
|
||||||
|
else
|
||||||
|
echo argument missing!
|
||||||
|
echo try: $0 '[dev|rel]'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo version: $VERSION build-date: $BUILD_DATE image: $IMAGE
|
||||||
|
if [[ $1 == dev ]];then
|
||||||
|
docker build --build-arg "VERSION=${VERSION}" --label "org.label-schema.build-date=${BUILD_DATE}" --label "org.opencontainers.image.version=${VERSION}" -t ${IMAGE}:latest app
|
||||||
|
elif [[ $1 == rel ]];then
|
||||||
|
docker build --no-cache --build-arg "VERSION=${VERSION}" --label "org.label-schema.build-date=${BUILD_DATE}" --label "org.opencontainers.image.version=${VERSION}" -t ${IMAGE}:latest -t ${IMAGE}:${MAJOR} -t ${IMAGE}:${VERSION} app
|
||||||
|
docker push ghcr.io/s-allius/tsun-gen3-proxy:latest
|
||||||
|
docker push ghcr.io/s-allius/tsun-gen3-proxy:${MAJOR}
|
||||||
|
docker push ghcr.io/s-allius/tsun-gen3-proxy:${VERSION}
|
||||||
|
fi
|
||||||
@@ -2,13 +2,24 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
user="$(id -u)"
|
user="$(id -u)"
|
||||||
echo "#############################################"
|
echo "######################################################"
|
||||||
echo "# start: '$SERVICE_NAME'"
|
echo "# prepare: '$SERVICE_NAME' Version:$VERSION"
|
||||||
echo "# with UserID:$UID, GroupID:$GID"
|
echo "# for running with UserID:$UID, GroupID:$GID"
|
||||||
echo "#############################################"
|
echo "#"
|
||||||
|
|
||||||
if [ "$user" = '0' ]; then
|
if [ "$user" = '0' ]; then
|
||||||
[ -d "/home/$SERVICE_NAME" ] && chown -R $SERVICE_NAME:$SERVICE_NAME /home/$SERVICE_NAME || true
|
mkdir -p /home/$SERVICE_NAME/log /home/$SERVICE_NAME/config
|
||||||
|
|
||||||
|
if id $SERVICE_NAME ; then
|
||||||
|
echo "user still exists"
|
||||||
|
else
|
||||||
|
addgroup --gid $GID $SERVICE_NAME 2> /dev/null
|
||||||
|
adduser --ingroup $SERVICE_NAME --shell /bin/false --disabled-password --no-create-home --comment "" --uid $UID $SERVICE_NAME
|
||||||
|
fi
|
||||||
|
chown -R $SERVICE_NAME:$SERVICE_NAME /home/$SERVICE_NAME || true
|
||||||
|
echo "######################################################"
|
||||||
|
echo "#"
|
||||||
|
|
||||||
exec gosu $SERVICE_NAME "$@"
|
exec gosu $SERVICE_NAME "$@"
|
||||||
else
|
else
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
aiomqtt==1.2.0
|
aiomqtt==1.2.1
|
||||||
schema
|
schema==0.7.5
|
||||||
@@ -1,22 +1,19 @@
|
|||||||
import logging, traceback, aiomqtt, json
|
import logging, traceback
|
||||||
from config import Config
|
from config import Config
|
||||||
|
#import gc
|
||||||
from messages import Message, hex_dump_memory
|
from messages import Message, hex_dump_memory
|
||||||
from mqtt import Mqtt
|
|
||||||
|
|
||||||
logger = logging.getLogger('conn')
|
logger = logging.getLogger('conn')
|
||||||
logger_mqtt = logging.getLogger('mqtt')
|
|
||||||
|
|
||||||
class AsyncStream(Message):
|
class AsyncStream(Message):
|
||||||
|
|
||||||
def __init__(self, proxy, reader, writer, addr, stream=None, server_side=True):
|
def __init__(self, reader, writer, addr, remote_stream, server_side: bool) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.proxy = proxy
|
|
||||||
self.reader = reader
|
self.reader = reader
|
||||||
self.writer = writer
|
self.writer = writer
|
||||||
self.remoteStream = stream
|
self.remoteStream = remote_stream
|
||||||
self.addr = addr
|
|
||||||
self.server_side = server_side
|
self.server_side = server_side
|
||||||
self.mqtt = Mqtt()
|
self.addr = addr
|
||||||
self.unique_id = 0
|
self.unique_id = 0
|
||||||
self.node_id = ''
|
self.node_id = ''
|
||||||
|
|
||||||
@@ -24,47 +21,32 @@ class AsyncStream(Message):
|
|||||||
Our puplic methods
|
Our puplic methods
|
||||||
'''
|
'''
|
||||||
def set_serial_no(self, serial_no : str):
|
def set_serial_no(self, serial_no : str):
|
||||||
logger_mqtt.info(f'SerialNo: {serial_no}')
|
logger.info(f'SerialNo: {serial_no}')
|
||||||
|
|
||||||
if self.unique_id != serial_no:
|
if self.unique_id != serial_no:
|
||||||
|
|
||||||
inverters = Config.get('inverters')
|
inverters = Config.get('inverters')
|
||||||
#logger_mqtt.debug(f'Inverters: {inverters}')
|
#logger.debug(f'Inverters: {inverters}')
|
||||||
|
|
||||||
if serial_no in inverters:
|
if serial_no in inverters:
|
||||||
logger_mqtt.debug(f'SerialNo {serial_no} allowed!')
|
logger.debug(f'SerialNo {serial_no} allowed!')
|
||||||
inv = inverters[serial_no]
|
inv = inverters[serial_no]
|
||||||
self.node_id = inv['node_id']
|
self.node_id = inv['node_id']
|
||||||
self.sug_area = inv['suggested_area']
|
self.sug_area = inv['suggested_area']
|
||||||
else:
|
else:
|
||||||
logger_mqtt.debug(f'SerialNo {serial_no} not known!')
|
logger.debug(f'SerialNo {serial_no} not known!')
|
||||||
self.node_id = ''
|
self.node_id = ''
|
||||||
self.sug_area = ''
|
self.sug_area = ''
|
||||||
if not inverters['allow_all']:
|
if not inverters['allow_all']:
|
||||||
self.unique_id = None
|
self.unique_id = None
|
||||||
|
|
||||||
logger_mqtt.error('ignore message from unknow inverter!')
|
logger.error('ignore message from unknow inverter!')
|
||||||
return
|
return
|
||||||
|
|
||||||
self.unique_id = serial_no
|
self.unique_id = serial_no
|
||||||
|
|
||||||
ha = Config.get('ha')
|
|
||||||
self.entitiy_prfx = ha['entity_prefix'] + '/'
|
|
||||||
self.discovery_prfx = ha['discovery_prefix'] + '/'
|
|
||||||
|
|
||||||
|
|
||||||
async def register_home_assistant(self):
|
|
||||||
|
|
||||||
if self.server_side:
|
|
||||||
try:
|
|
||||||
for data_json, id in self.db.ha_confs(self.entitiy_prfx + self.node_id, self.unique_id, self.sug_area):
|
|
||||||
logger_mqtt.debug(f'Register: {data_json}')
|
|
||||||
await self.mqtt.publish(f"{self.discovery_prfx}sensor/{self.node_id}{id}/config", data_json)
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
logging.error(
|
|
||||||
f"Proxy: Exception:\n"
|
|
||||||
f"{traceback.format_exc()}")
|
|
||||||
|
|
||||||
|
|
||||||
async def loop(self) -> None:
|
async def loop(self) -> None:
|
||||||
@@ -79,7 +61,7 @@ class AsyncStream(Message):
|
|||||||
if self.unique_id:
|
if self.unique_id:
|
||||||
await self.__async_write()
|
await self.__async_write()
|
||||||
await self.__async_forward()
|
await self.__async_forward()
|
||||||
await self.__async_publ_mqtt()
|
await self.async_publ_mqtt()
|
||||||
|
|
||||||
|
|
||||||
except (ConnectionResetError,
|
except (ConnectionResetError,
|
||||||
@@ -94,12 +76,18 @@ class AsyncStream(Message):
|
|||||||
f"{traceback.format_exc()}")
|
f"{traceback.format_exc()}")
|
||||||
self.close()
|
self.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def disc(self) -> None:
|
||||||
|
logger.debug(f'in AsyncStream.disc() {self.addr}')
|
||||||
|
self.writer.close()
|
||||||
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
logger.info(f'in async_stream.close() {self.addr}')
|
logger.debug(f'in AsyncStream.close() {self.addr}')
|
||||||
self.writer.close()
|
self.writer.close()
|
||||||
self.proxy = None
|
super().close() # call close handler in the parent class
|
||||||
self.remoteStream = None
|
|
||||||
|
# logger.info (f'AsyncStream refs: {gc.get_referrers(self)}')
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
@@ -123,8 +111,7 @@ class AsyncStream(Message):
|
|||||||
async def __async_forward(self) -> None:
|
async def __async_forward(self) -> None:
|
||||||
if self._forward_buffer:
|
if self._forward_buffer:
|
||||||
if not self.remoteStream:
|
if not self.remoteStream:
|
||||||
tsun = Config.get('tsun')
|
await self.async_create_remote() # only implmeneted for server side => syncServerStream
|
||||||
self.remoteStream = await self.proxy.CreateClientStream (self, tsun['host'], tsun['port'])
|
|
||||||
|
|
||||||
if self.remoteStream:
|
if self.remoteStream:
|
||||||
hex_dump_memory(logging.DEBUG, f'Forward to {self.remoteStream.addr}:', self._forward_buffer, len(self._forward_buffer))
|
hex_dump_memory(logging.DEBUG, f'Forward to {self.remoteStream.addr}:', self._forward_buffer, len(self._forward_buffer))
|
||||||
@@ -132,21 +119,14 @@ class AsyncStream(Message):
|
|||||||
await self.remoteStream.writer.drain()
|
await self.remoteStream.writer.drain()
|
||||||
self._forward_buffer = bytearray(0)
|
self._forward_buffer = bytearray(0)
|
||||||
|
|
||||||
async def __async_publ_mqtt(self) -> None:
|
async def async_create_remote(self) -> None:
|
||||||
if self.server_side:
|
pass
|
||||||
db = self.db.db
|
|
||||||
|
|
||||||
if self.new_data.keys() & {'inverter', 'collector'}:
|
async def async_publ_mqtt(self) -> None:
|
||||||
await self.register_home_assistant()
|
pass
|
||||||
|
|
||||||
for key in self.new_data:
|
|
||||||
if self.new_data[key] and key in db:
|
|
||||||
data_json = json.dumps(db[key])
|
|
||||||
logger_mqtt.info(f'{key}: {data_json}')
|
|
||||||
await self.mqtt.publish(f"{self.entitiy_prfx}{self.node_id}{key}", data_json)
|
|
||||||
self.new_data[key] = False
|
|
||||||
|
|
||||||
def __del__ (self):
|
def __del__ (self):
|
||||||
logger.debug ("AsyncStream __del__")
|
logging.debug (f"AsyncStream.__del__ {self.addr}")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class Config():
|
|||||||
config['inverters'] = def_config['inverters'] | usr_config['inverters']
|
config['inverters'] = def_config['inverters'] | usr_config['inverters']
|
||||||
|
|
||||||
cls.config = cls.conf_schema.validate(config)
|
cls.config = cls.conf_schema.validate(config)
|
||||||
logging.debug(f'Readed config: "{cls.config}" ')
|
#logging.debug(f'Readed config: "{cls.config}" ')
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logger.error(f'Config.read: {error}')
|
logger.error(f'Config.read: {error}')
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import struct, json, logging
|
import struct, json, logging, os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Infos:
|
class Infos:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.db = {}
|
self.db = {}
|
||||||
|
self.app_name = os.getenv('SERVICE_NAME', 'proxy')
|
||||||
|
self.version = os.getenv('VERSION', 'unknown')
|
||||||
self.tracer = logging.getLogger('data')
|
self.tracer = logging.getLogger('data')
|
||||||
|
|
||||||
__info_devs={
|
__info_devs={
|
||||||
@@ -58,28 +60,28 @@ class Infos:
|
|||||||
0x00000514: {'name':['env', 'Inverter_Temp'], 'level': logging.DEBUG, 'unit': '°C', 'ha':{'dev':'inverter', 'dev_cla': 'temperature', 'stat_cla': 'measurement', 'id':'temp_', 'fmt':'| int','name': 'Temperature'}},
|
0x00000514: {'name':['env', 'Inverter_Temp'], 'level': logging.DEBUG, 'unit': '°C', 'ha':{'dev':'inverter', 'dev_cla': 'temperature', 'stat_cla': 'measurement', 'id':'temp_', 'fmt':'| int','name': 'Temperature'}},
|
||||||
|
|
||||||
# input measures:
|
# input measures:
|
||||||
0x000006a4: {'name':['input', 'pv1', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv1', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv1_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv1']['Voltage'] | float)}}", 'unvisible':1}},
|
0x000006a4: {'name':['input', 'pv1', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv1', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv1_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv1']['Voltage'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000708: {'name':['input', 'pv1', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv1', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv1_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv1']['Current'] | float)}}", 'unvisible':1}},
|
0x00000708: {'name':['input', 'pv1', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv1', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv1_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv1']['Current'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x0000076c: {'name':['input', 'pv1', 'Power'], 'level': logging.INFO, 'unit': 'W', 'ha':{'dev':'input_pv1', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv1_','name': 'Power', 'val_tpl' :"{{ (value_json['pv1']['Power'] | float)}}"}},
|
0x0000076c: {'name':['input', 'pv1', 'Power'], 'level': logging.INFO, 'unit': 'W', 'ha':{'dev':'input_pv1', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv1_','name': 'Power', 'val_tpl' :"{{ (value_json['pv1']['Power'] | float)}}", 'icon':'mdi:gauge'}},
|
||||||
0x000007d0: {'name':['input', 'pv2', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv2', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv2_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv2']['Voltage'] | float)}}", 'unvisible':1}},
|
0x000007d0: {'name':['input', 'pv2', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv2', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv2_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv2']['Voltage'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000834: {'name':['input', 'pv2', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv2', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv2_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv2']['Current'] | float)}}", 'unvisible':1}},
|
0x00000834: {'name':['input', 'pv2', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv2', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv2_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv2']['Current'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000898: {'name':['input', 'pv2', 'Power'], 'level': logging.INFO, 'unit': 'W', 'ha':{'dev':'input_pv2', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv2_','name': 'Power', 'val_tpl' :"{{ (value_json['pv2']['Power'] | float)}}"}},
|
0x00000898: {'name':['input', 'pv2', 'Power'], 'level': logging.INFO, 'unit': 'W', 'ha':{'dev':'input_pv2', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv2_','name': 'Power', 'val_tpl' :"{{ (value_json['pv2']['Power'] | float)}}", 'icon':'mdi:gauge'}},
|
||||||
0x000008fc: {'name':['input', 'pv3', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv3', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv3_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv3']['Voltage'] | float)}}", 'unvisible':1}},
|
0x000008fc: {'name':['input', 'pv3', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv3', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv3_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv3']['Voltage'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000960: {'name':['input', 'pv3', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv3', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv3_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv3']['Current'] | float)}}", 'unvisible':1}},
|
0x00000960: {'name':['input', 'pv3', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv3', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv3_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv3']['Current'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x000009c4: {'name':['input', 'pv3', 'Power'], 'level': logging.DEBUG, 'unit': 'W', 'ha':{'dev':'input_pv3', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv3_','name': 'Power', 'val_tpl' :"{{ (value_json['pv3']['Power'] | float)}}"}},
|
0x000009c4: {'name':['input', 'pv3', 'Power'], 'level': logging.DEBUG, 'unit': 'W', 'ha':{'dev':'input_pv3', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv3_','name': 'Power', 'val_tpl' :"{{ (value_json['pv3']['Power'] | float)}}", 'icon':'mdi:gauge'}},
|
||||||
0x00000a28: {'name':['input', 'pv4', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv4', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv4_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv4']['Voltage'] | float)}}", 'unvisible':1}},
|
0x00000a28: {'name':['input', 'pv4', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'input_pv4', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'volt_pv4_', 'name': 'Voltage', 'val_tpl' :"{{ (value_json['pv4']['Voltage'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000a8c: {'name':['input', 'pv4', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv4', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv4_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv4']['Current'] | float)}}", 'unvisible':1}},
|
0x00000a8c: {'name':['input', 'pv4', 'Current'], 'level': logging.DEBUG, 'unit': 'A', 'ha':{'dev':'input_pv4', 'dev_cla': 'current', 'stat_cla': 'measurement', 'id':'cur_pv4_', 'name': 'Current', 'val_tpl' :"{{ (value_json['pv4']['Current'] | float)}}", 'unvisible':1, 'icon':'mdi:gauge'}},
|
||||||
0x00000af0: {'name':['input', 'pv4', 'Power'], 'level': logging.DEBUG, 'unit': 'W', 'ha':{'dev':'input_pv4', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv4_','name': 'Power', 'val_tpl' :"{{ (value_json['pv4']['Power'] | float)}}"}},
|
0x00000af0: {'name':['input', 'pv4', 'Power'], 'level': logging.DEBUG, 'unit': 'W', 'ha':{'dev':'input_pv4', 'dev_cla': 'power', 'stat_cla': 'measurement', 'id':'power_pv4_','name': 'Power', 'val_tpl' :"{{ (value_json['pv4']['Power'] | float)}}", 'icon':'mdi:gauge'}},
|
||||||
0x00000c1c: {'name':['input', 'pv1', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv1', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv1_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv1']['Daily_Generation'] | float)}}"}},
|
0x00000c1c: {'name':['input', 'pv1', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv1', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv1_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv1']['Daily_Generation'] | float)}}", 'icon':'mdi:solar-power-variant'}},
|
||||||
0x00000c80: {'name':['input', 'pv1', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv1', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv1_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv1']['Total_Generation'] | float)}}"}},
|
0x00000c80: {'name':['input', 'pv1', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv1', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv1_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv1']['Total_Generation'] | float)}}", 'icon':'mdi:solar-power'}},
|
||||||
0x00000ce4: {'name':['input', 'pv2', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv2', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv2_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv2']['Daily_Generation'] | float)}}"}},
|
0x00000ce4: {'name':['input', 'pv2', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv2', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv2_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv2']['Daily_Generation'] | float)}}", 'icon':'mdi:solar-power-variant'}},
|
||||||
0x00000d48: {'name':['input', 'pv2', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv2', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv2_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv2']['Total_Generation'] | float)}}"}},
|
0x00000d48: {'name':['input', 'pv2', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv2', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv2_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv2']['Total_Generation'] | float)}}", 'icon':'mdi:solar-power'}},
|
||||||
0x00000dac: {'name':['input', 'pv3', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv3', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv3_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv3']['Daily_Generation'] | float)}}"}},
|
0x00000dac: {'name':['input', 'pv3', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv3', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv3_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv3']['Daily_Generation'] | float)}}", 'icon':'mdi:solar-power-variant'}},
|
||||||
0x00000e10: {'name':['input', 'pv3', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv3', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv3_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv3']['Total_Generation'] | float)}}"}},
|
0x00000e10: {'name':['input', 'pv3', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv3', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv3_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv3']['Total_Generation'] | float)}}", 'icon':'mdi:solar-power'}},
|
||||||
0x00000e74: {'name':['input', 'pv4', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv4', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv4_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv4']['Daily_Generation'] | float)}}"}},
|
0x00000e74: {'name':['input', 'pv4', 'Daily_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv4', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_pv4_','name': 'Daily Generation', 'val_tpl' :"{{ (value_json['pv4']['Daily_Generation'] | float)}}", 'icon':'mdi:solar-power-variant'}},
|
||||||
0x00000ed8: {'name':['input', 'pv4', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv4', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv4_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv4']['Total_Generation'] | float)}}"}},
|
0x00000ed8: {'name':['input', 'pv4', 'Total_Generation'], 'level': logging.DEBUG, 'unit': 'kWh', 'ha':{'dev':'input_pv4', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_pv4_','name': 'Total Generation', 'val_tpl' :"{{ (value_json['pv4']['Total_Generation'] | float)}}", 'icon':'mdi:solar-power'}},
|
||||||
# total:
|
# total:
|
||||||
0x00000b54: {'name':['total', 'Daily_Generation'], 'level': logging.INFO, 'unit': 'kWh', 'ha':{'dev':'inverter', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_', 'fmt':'| float','name': 'Daily Generation'}},
|
0x00000b54: {'name':['total', 'Daily_Generation'], 'level': logging.INFO, 'unit': 'kWh', 'ha':{'dev':'inverter', 'dev_cla': 'energy', 'stat_cla': 'total_increasing', 'id':'daily_gen_', 'fmt':'| float','name': 'Daily Generation', 'icon':'mdi:solar-power-variant'}},
|
||||||
0x00000bb8: {'name':['total', 'Total_Generation'], 'level': logging.INFO, 'unit': 'kWh', 'ha':{'dev':'inverter', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_', 'fmt':'| float','name': 'Total Generation', 'icon':'mdi:solar-power'}},
|
0x00000bb8: {'name':['total', 'Total_Generation'], 'level': logging.INFO, 'unit': 'kWh', 'ha':{'dev':'inverter', 'dev_cla': 'energy', 'stat_cla': 'total', 'id':'total_gen_', 'fmt':'| float','name': 'Total Generation', 'icon':'mdi:solar-power'}},
|
||||||
|
|
||||||
# controller:
|
# controller:
|
||||||
@@ -125,6 +127,10 @@ class Infos:
|
|||||||
#check if we have details for home assistant
|
#check if we have details for home assistant
|
||||||
if 'ha' in row:
|
if 'ha' in row:
|
||||||
ha = row['ha']
|
ha = row['ha']
|
||||||
|
if 'comp' in ha:
|
||||||
|
component = ha['comp']
|
||||||
|
else:
|
||||||
|
component = 'sensor'
|
||||||
attr = {} # dict to collect all the sensor entity details
|
attr = {} # dict to collect all the sensor entity details
|
||||||
if 'name' in ha:
|
if 'name' in ha:
|
||||||
attr['name'] = ha['name'] # take the entity name from the ha dict
|
attr['name'] = ha['name'] # take the entity name from the ha dict
|
||||||
@@ -145,7 +151,7 @@ class Infos:
|
|||||||
if 'icon' in ha:
|
if 'icon' in ha:
|
||||||
attr['icon'] = ha['icon'] # optional add an icon for the entity
|
attr['icon'] = ha['icon'] # optional add an icon for the entity
|
||||||
if 'nat_prc' in ha:
|
if 'nat_prc' in ha:
|
||||||
attr['suggested_display_precision'] = ha['nat_prc'] # optional add the precison of floats
|
attr['sug_dsp_prc'] = ha['nat_prc'] # optional add the precison of floats
|
||||||
|
|
||||||
# eg. 'dev':{'name':'Microinverter','mdl':'MS-600','ids':["inverter_123"],'mf':'TSUN','sa': 'auf Garagendach'}
|
# eg. 'dev':{'name':'Microinverter','mdl':'MS-600','ids':["inverter_123"],'mf':'TSUN','sa': 'auf Garagendach'}
|
||||||
# attr['dev'] = {'name':'Microinverter','mdl':'MS-600','ids':[f'inverter_{snr}'],'mf':'TSUN','sa': 'auf Garagendach'}
|
# attr['dev'] = {'name':'Microinverter','mdl':'MS-600','ids':[f'inverter_{snr}'],'mf':'TSUN','sa': 'auf Garagendach'}
|
||||||
@@ -173,8 +179,13 @@ class Infos:
|
|||||||
dev['ids'] = [f"{ha['dev']}_{snr}"]
|
dev['ids'] = [f"{ha['dev']}_{snr}"]
|
||||||
attr['dev'] = dev
|
attr['dev'] = dev
|
||||||
|
|
||||||
|
origin = {}
|
||||||
|
origin['name'] = self.app_name
|
||||||
|
origin['sw'] = self.version
|
||||||
|
attr['o'] = origin
|
||||||
|
|
||||||
yield json.dumps (attr), attr['uniq_id']
|
|
||||||
|
yield json.dumps (attr), component, attr['uniq_id']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
103
app/src/inverter.py
Normal file
103
app/src/inverter.py
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
import asyncio, logging, traceback, json
|
||||||
|
from config import Config
|
||||||
|
from async_stream import AsyncStream
|
||||||
|
from mqtt import Mqtt
|
||||||
|
#import gc
|
||||||
|
|
||||||
|
logger = logging.getLogger('conn')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Inverter(AsyncStream):
|
||||||
|
|
||||||
|
def __init__ (self, reader, writer, addr):
|
||||||
|
super().__init__(reader, writer, addr, None, True)
|
||||||
|
self.mqtt = Mqtt()
|
||||||
|
self.ha_restarts = 0
|
||||||
|
ha = Config.get('ha')
|
||||||
|
self.entitiy_prfx = ha['entity_prefix'] + '/'
|
||||||
|
self.discovery_prfx = ha['discovery_prefix'] + '/'
|
||||||
|
|
||||||
|
|
||||||
|
async def server_loop(self, addr):
|
||||||
|
'''Loop for receiving messages from the inverter (server-side)'''
|
||||||
|
logger.info(f'Accept connection from {addr}')
|
||||||
|
await self.loop()
|
||||||
|
logging.info(f'Server loop stopped for {addr}')
|
||||||
|
|
||||||
|
# if the server connection closes, we also have to disconnect the connection to te TSUN cloud
|
||||||
|
if self.remoteStream:
|
||||||
|
logging.debug ("disconnect client connection")
|
||||||
|
self.remoteStream.disc()
|
||||||
|
|
||||||
|
async def client_loop(self, addr):
|
||||||
|
'''Loop for receiving messages from the TSUN cloud (client-side)'''
|
||||||
|
await self.remoteStream.loop()
|
||||||
|
logging.info(f'Client loop stopped for {addr}')
|
||||||
|
|
||||||
|
# if the client connection closes, we don't touch the server connection. Instead we erase the client
|
||||||
|
# connection stream, thus on the next received packet from the inverter, we can establish a new connection
|
||||||
|
# to the TSUN cloud
|
||||||
|
self.remoteStream.remoteStream = None # erase backlink to inverter instance
|
||||||
|
self.remoteStream = None # than erase client connection
|
||||||
|
|
||||||
|
async def async_create_remote(self) -> None:
|
||||||
|
'''Establish a client connection to the TSUN cloud'''
|
||||||
|
tsun = Config.get('tsun')
|
||||||
|
host = tsun['host']
|
||||||
|
port = tsun['port']
|
||||||
|
addr = (host, port)
|
||||||
|
|
||||||
|
try:
|
||||||
|
logging.info(f'Connected to {addr}')
|
||||||
|
connect = asyncio.open_connection(host, port)
|
||||||
|
reader, writer = await connect
|
||||||
|
self.remoteStream = AsyncStream(reader, writer, addr, self, False)
|
||||||
|
asyncio.create_task(self.client_loop(addr))
|
||||||
|
|
||||||
|
except ConnectionRefusedError as error:
|
||||||
|
logging.info(f'{error}')
|
||||||
|
except Exception:
|
||||||
|
logging.error(
|
||||||
|
f"Inverter: Exception for {addr}:\n"
|
||||||
|
f"{traceback.format_exc()}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def async_publ_mqtt(self) -> None:
|
||||||
|
'''puplish data to MQTT broker'''
|
||||||
|
db = self.db.db
|
||||||
|
# check if new inverter or collector infos are available or when the home assistant has changed the status back to online
|
||||||
|
if (('inverter' in self.new_data and self.new_data['inverter']) or
|
||||||
|
('collector' in self.new_data and self.new_data['collector']) or
|
||||||
|
self.mqtt.ha_restarts != self.ha_restarts):
|
||||||
|
await self.__register_home_assistant()
|
||||||
|
self.ha_restarts = self.mqtt.ha_restarts
|
||||||
|
|
||||||
|
for key in self.new_data:
|
||||||
|
if self.new_data[key] and key in db:
|
||||||
|
data_json = json.dumps(db[key])
|
||||||
|
logger.info(f'{key}: {data_json}')
|
||||||
|
await self.mqtt.publish(f"{self.entitiy_prfx}{self.node_id}{key}", data_json)
|
||||||
|
self.new_data[key] = False
|
||||||
|
|
||||||
|
async def __register_home_assistant(self) -> None:
|
||||||
|
'''register all our topics at home assistant'''
|
||||||
|
try:
|
||||||
|
for data_json, component, id in self.db.ha_confs(self.entitiy_prfx + self.node_id, self.unique_id, self.sug_area):
|
||||||
|
#logger.debug(f'MQTT Register: {data_json}')
|
||||||
|
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{self.node_id}{id}/config", data_json)
|
||||||
|
except Exception:
|
||||||
|
logging.error(
|
||||||
|
f"Inverter: Exception:\n"
|
||||||
|
f"{traceback.format_exc()}")
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
logging.debug(f'Inverter.close() {self.addr}')
|
||||||
|
super().close() # call close handler in the parent class
|
||||||
|
# logger.debug (f'Inverter refs: {gc.get_referrers(self)}')
|
||||||
|
|
||||||
|
|
||||||
|
def __del__ (self):
|
||||||
|
logging.debug ("Inverter.__del__")
|
||||||
|
super().__del__()
|
||||||
@@ -25,13 +25,13 @@ qualname=conn
|
|||||||
|
|
||||||
[logger_data]
|
[logger_data]
|
||||||
level=DEBUG
|
level=DEBUG
|
||||||
handlers=console_handler,file_handler_name1,file_handler_name2
|
handlers=file_handler_name1,file_handler_name2
|
||||||
propagate=0
|
propagate=0
|
||||||
qualname=data
|
qualname=data
|
||||||
|
|
||||||
[logger_mqtt]
|
[logger_mqtt]
|
||||||
level=DEBUG
|
level=INFO
|
||||||
handlers=console_handler,file_handler_name1,file_handler_name2
|
handlers=console_handler,file_handler_name1
|
||||||
propagate=0
|
propagate=0
|
||||||
qualname=mqtt
|
qualname=mqtt
|
||||||
|
|
||||||
@@ -43,12 +43,12 @@ qualname=tracer
|
|||||||
|
|
||||||
[handler_console_handler]
|
[handler_console_handler]
|
||||||
class=StreamHandler
|
class=StreamHandler
|
||||||
level=INFO
|
level=DEBUG
|
||||||
formatter=console_formatter
|
formatter=console_formatter
|
||||||
|
|
||||||
[handler_file_handler_name1]
|
[handler_file_handler_name1]
|
||||||
class=handlers.TimedRotatingFileHandler
|
class=handlers.TimedRotatingFileHandler
|
||||||
level=NOTSET
|
level=INFO
|
||||||
formatter=file_formatter
|
formatter=file_formatter
|
||||||
args=('log/proxy.log', when:='midnight')
|
args=('log/proxy.log', when:='midnight')
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,13 @@ class Message(metaclass=IterRegistry):
|
|||||||
'''
|
'''
|
||||||
Our puplic methods
|
Our puplic methods
|
||||||
'''
|
'''
|
||||||
|
def close(self) -> None:
|
||||||
|
# we have refernces to methods of this class in self.switch
|
||||||
|
# so we have to erase self.switch, otherwise this instance can't be
|
||||||
|
# deallocated by the garbage collector ==> we get a memory leak
|
||||||
|
del self.switch
|
||||||
|
|
||||||
|
|
||||||
def read(self) -> None:
|
def read(self) -> None:
|
||||||
self._read()
|
self._read()
|
||||||
|
|
||||||
@@ -186,7 +193,7 @@ class Message(metaclass=IterRegistry):
|
|||||||
self.send_msg_ofs = len (self._send_buffer)
|
self.send_msg_ofs = len (self._send_buffer)
|
||||||
self._send_buffer += struct.pack(f'!l{len(self.id_str)+1}pBB', 0, self.id_str, ctrl, self.msg_id)
|
self._send_buffer += struct.pack(f'!l{len(self.id_str)+1}pBB', 0, self.id_str, ctrl, self.msg_id)
|
||||||
fnc = self.switch.get(self.msg_id, self.msg_unknown)
|
fnc = self.switch.get(self.msg_id, self.msg_unknown)
|
||||||
logger.info(self.__flow_str(self.server_side, 'tx') + f' Ctl: {int(self.ctrl):#02x} Msg: {fnc.__name__!r}' )
|
logger.info(self.__flow_str(self.server_side, 'tx') + f' Ctl: {int(ctrl):#02x} Msg: {fnc.__name__!r}' )
|
||||||
|
|
||||||
def __finish_send_msg(self) -> None:
|
def __finish_send_msg(self) -> None:
|
||||||
_len = len(self._send_buffer) - self.send_msg_ofs
|
_len = len(self._send_buffer) - self.send_msg_ofs
|
||||||
@@ -287,11 +294,9 @@ class Message(metaclass=IterRegistry):
|
|||||||
|
|
||||||
|
|
||||||
def msg_unknown(self):
|
def msg_unknown(self):
|
||||||
|
logger.error (f"Unknow Msg: ID:{self.msg_id}")
|
||||||
self.forward(self._recv_buffer, self.header_len+self.data_len)
|
self.forward(self._recv_buffer, self.header_len+self.data_len)
|
||||||
|
|
||||||
|
|
||||||
def __del__ (self):
|
|
||||||
logger.debug ("Messages __del__")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,22 @@ class Singleton(type):
|
|||||||
|
|
||||||
class Mqtt(metaclass=Singleton):
|
class Mqtt(metaclass=Singleton):
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
logger_mqtt.debug(f'MQTT: __init__')
|
logger_mqtt.debug(f'MQTT: __init__')
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
self.task = loop.create_task(self.__loop())
|
self.task = loop.create_task(self.__loop())
|
||||||
|
self.ha_restarts = 0
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ha_restarts(self):
|
||||||
|
return self._ha_restarts
|
||||||
|
|
||||||
|
@ha_restarts.setter
|
||||||
|
def ha_restarts(self, value):
|
||||||
|
self._ha_restarts = value
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
logger_mqtt.debug(f'MQTT: __del__')
|
logger_mqtt.debug(f'MQTT: __del__')
|
||||||
|
|
||||||
@@ -55,7 +64,11 @@ class Mqtt(metaclass=Singleton):
|
|||||||
async with self.client.messages() as messages:
|
async with self.client.messages() as messages:
|
||||||
await self.client.subscribe(f"{ha['auto_conf_prefix']}/status")
|
await self.client.subscribe(f"{ha['auto_conf_prefix']}/status")
|
||||||
async for message in messages:
|
async for message in messages:
|
||||||
logger_mqtt.info(f'Home-Assistant Status: {message.payload.decode("UTF-8")}')
|
status = message.payload.decode("UTF-8")
|
||||||
|
logger_mqtt.info(f'Home-Assistant Status: {status}')
|
||||||
|
if status == 'online':
|
||||||
|
self.ha_restarts += 1
|
||||||
|
|
||||||
except aiomqtt.MqttError:
|
except aiomqtt.MqttError:
|
||||||
logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...")
|
logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...")
|
||||||
await asyncio.sleep(interval)
|
await asyncio.sleep(interval)
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
import asyncio, logging, traceback
|
|
||||||
from async_stream import AsyncStream
|
|
||||||
|
|
||||||
class Proxy:
|
|
||||||
def __init__ (proxy, reader, writer, addr):
|
|
||||||
proxy.ServerStream = AsyncStream(proxy, reader, writer, addr)
|
|
||||||
proxy.ClientStream = None
|
|
||||||
|
|
||||||
async def server_loop(proxy, addr):
|
|
||||||
logging.info(f'Accept connection from {addr}')
|
|
||||||
await proxy.ServerStream.loop()
|
|
||||||
logging.info(f'Close server connection {addr}')
|
|
||||||
|
|
||||||
if proxy.ClientStream:
|
|
||||||
logging.debug ("close client connection")
|
|
||||||
proxy.ClientStream.close()
|
|
||||||
|
|
||||||
async def client_loop(proxy, addr):
|
|
||||||
await proxy.ClientStream.loop()
|
|
||||||
logging.info(f'Close client connection {addr}')
|
|
||||||
proxy.ServerStream.remoteStream = None
|
|
||||||
proxy.ClientStream = None
|
|
||||||
|
|
||||||
async def CreateClientStream (proxy, stream, host, port):
|
|
||||||
addr = (host, port)
|
|
||||||
|
|
||||||
try:
|
|
||||||
logging.info(f'Connected to {addr}')
|
|
||||||
connect = asyncio.open_connection(host, port)
|
|
||||||
reader, writer = await connect
|
|
||||||
proxy.ClientStream = AsyncStream(proxy, reader, writer, addr, stream, server_side=False)
|
|
||||||
asyncio.create_task(proxy.client_loop(addr))
|
|
||||||
|
|
||||||
except ConnectionRefusedError as error:
|
|
||||||
logging.info(f'{error}')
|
|
||||||
except Exception:
|
|
||||||
logging.error(
|
|
||||||
f"Proxy: Exception for {addr}:\n"
|
|
||||||
f"{traceback.format_exc()}")
|
|
||||||
return proxy.ClientStream
|
|
||||||
|
|
||||||
def __del__ (proxy):
|
|
||||||
logging.debug ("Proxy __del__")
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import logging, asyncio, signal, functools, os
|
import logging, asyncio, signal, functools, os
|
||||||
#from logging.handlers import TimedRotatingFileHandler
|
|
||||||
from logging import config
|
from logging import config
|
||||||
from async_stream import AsyncStream
|
from async_stream import AsyncStream
|
||||||
from proxy import Proxy
|
from inverter import Inverter
|
||||||
from config import Config
|
from config import Config
|
||||||
from mqtt import Mqtt
|
from mqtt import Mqtt
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ async def handle_client(reader, writer):
|
|||||||
'''Handles a new incoming connection and starts an async loop'''
|
'''Handles a new incoming connection and starts an async loop'''
|
||||||
|
|
||||||
addr = writer.get_extra_info('peername')
|
addr = writer.get_extra_info('peername')
|
||||||
await Proxy(reader, writer, addr).server_loop(addr)
|
await Inverter(reader, writer, addr).server_loop(addr)
|
||||||
|
|
||||||
|
|
||||||
def handle_SIGTERM(loop):
|
def handle_SIGTERM(loop):
|
||||||
@@ -42,9 +41,11 @@ if __name__ == "__main__":
|
|||||||
# Setup our daily, rotating logger
|
# Setup our daily, rotating logger
|
||||||
#
|
#
|
||||||
serv_name = os.getenv('SERVICE_NAME', 'proxy')
|
serv_name = os.getenv('SERVICE_NAME', 'proxy')
|
||||||
|
version = os.getenv('VERSION', 'unknown')
|
||||||
|
|
||||||
logging.config.fileConfig('logging.ini')
|
logging.config.fileConfig('logging.ini')
|
||||||
logging.info(f'Server "{serv_name}" will be started')
|
logging.info(f'Server "{serv_name} - {version}" will be started')
|
||||||
|
logging.getLogger().setLevel(logging.DEBUG if __debug__ else logging.INFO)
|
||||||
|
|
||||||
# read config file
|
# read config file
|
||||||
Config.read()
|
Config.read()
|
||||||
|
|||||||
@@ -52,26 +52,31 @@ def test_parse_cont_and_invert(ContrDataSeq, InvDataSeq):
|
|||||||
def test_build_ha_conf1(ContrDataSeq):
|
def test_build_ha_conf1(ContrDataSeq):
|
||||||
i = Infos()
|
i = Infos()
|
||||||
tests = 0
|
tests = 0
|
||||||
for d_json, id in i.ha_confs(prfx="tsun/garagendach/", snr='123'):
|
for d_json, comp, id in i.ha_confs(prfx="tsun/garagendach/", snr='123'):
|
||||||
|
|
||||||
if id == 'out_power_123':
|
if id == 'out_power_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/grid", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "out_power_123", "val_tpl": "{{value_json['Output_Power'] | float}}", "unit_of_meas": "W", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "ids": ["inverter_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/grid", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "out_power_123", "val_tpl": "{{value_json['Output_Power'] | float}}", "unit_of_meas": "W", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "ids": ["inverter_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'daily_gen_123':
|
elif id == 'daily_gen_123':
|
||||||
assert d_json == json.dumps({"name": "Daily Generation", "stat_t": "tsun/garagendach/total", "dev_cla": "energy", "stat_cla": "total_increasing", "uniq_id": "daily_gen_123", "val_tpl": "{{value_json['Daily_Generation'] | float}}", "unit_of_meas": "kWh", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "ids": ["inverter_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Daily Generation", "stat_t": "tsun/garagendach/total", "dev_cla": "energy", "stat_cla": "total_increasing", "uniq_id": "daily_gen_123", "val_tpl": "{{value_json['Daily_Generation'] | float}}", "unit_of_meas": "kWh", "icon": "mdi:solar-power-variant", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "ids": ["inverter_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'power_pv1_123':
|
elif id == 'power_pv1_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv1_123", "val_tpl": "{{ (value_json['pv1']['Power'] | float)}}", "unit_of_meas": "W", "dev": {"name": "Module PV1", "sa": "Module PV1", "via_device": "inverter_123", "ids": ["input_pv1_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv1_123", "val_tpl": "{{ (value_json['pv1']['Power'] | float)}}", "unit_of_meas": "W", "icon": "mdi:gauge", "dev": {"name": "Module PV1", "sa": "Module PV1", "via_device": "inverter_123", "ids": ["input_pv1_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'power_pv2_123':
|
elif id == 'power_pv2_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv2_123", "val_tpl": "{{ (value_json['pv2']['Power'] | float)}}", "unit_of_meas": "W", "dev": {"name": "Module PV2", "sa": "Module PV2", "via_device": "inverter_123", "ids": ["input_pv2_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv2_123", "val_tpl": "{{ (value_json['pv2']['Power'] | float)}}", "unit_of_meas": "W", "icon": "mdi:gauge", "dev": {"name": "Module PV2", "sa": "Module PV2", "via_device": "inverter_123", "ids": ["input_pv2_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'signal_123':
|
elif id == 'signal_123':
|
||||||
assert d_json == json.dumps({"name": "Signal Strength", "stat_t": "tsun/garagendach/controller", "dev_cla": None, "stat_cla": "measurement", "uniq_id": "signal_123", "val_tpl": "{{value_json[\'Signal_Strength\'] | int}}", "unit_of_meas": "%", "icon": "mdi:wifi", "dev": {"name": "Controller", "sa": "Controller", "ids": ["controller_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Signal Strength", "stat_t": "tsun/garagendach/controller", "dev_cla": None, "stat_cla": "measurement", "uniq_id": "signal_123", "val_tpl": "{{value_json[\'Signal_Strength\'] | int}}", "unit_of_meas": "%", "icon": "mdi:wifi", "dev": {"name": "Controller", "sa": "Controller", "ids": ["controller_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
assert tests==5
|
assert tests==5
|
||||||
|
|
||||||
@@ -84,30 +89,30 @@ def test_build_ha_conf2(ContrDataSeq, InvDataSeq):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
tests = 0
|
tests = 0
|
||||||
for d_json, id in i.ha_confs(prfx="tsun/garagendach/", snr='123'):
|
for d_json, comp, id in i.ha_confs(prfx="tsun/garagendach/", snr='123'):
|
||||||
|
|
||||||
if id == 'out_power_123':
|
if id == 'out_power_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/grid", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "out_power_123", "val_tpl": "{{value_json['Output_Power'] | float}}", "unit_of_meas": "W", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "mdl": "TSOL-MS600", "mf": "TSUN", "sw": "V5.0.11", "ids": ["inverter_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/grid", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "out_power_123", "val_tpl": "{{value_json['Output_Power'] | float}}", "unit_of_meas": "W", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "mdl": "TSOL-MS600", "mf": "TSUN", "sw": "V5.0.11", "ids": ["inverter_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'daily_gen_123':
|
elif id == 'daily_gen_123':
|
||||||
assert d_json == json.dumps({"name": "Daily Generation", "stat_t": "tsun/garagendach/total", "dev_cla": "energy", "stat_cla": "total_increasing", "uniq_id": "daily_gen_123", "val_tpl": "{{value_json['Daily_Generation'] | float}}", "unit_of_meas": "kWh", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "mdl": "TSOL-MS600", "mf": "TSUN", "sw": "V5.0.11", "ids": ["inverter_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Daily Generation", "stat_t": "tsun/garagendach/total", "dev_cla": "energy", "stat_cla": "total_increasing", "uniq_id": "daily_gen_123", "val_tpl": "{{value_json['Daily_Generation'] | float}}", "unit_of_meas": "kWh", "icon": "mdi:solar-power-variant", "dev": {"name": "Micro Inverter", "sa": "Micro Inverter", "via_device": "controller_123", "mdl": "TSOL-MS600", "mf": "TSUN", "sw": "V5.0.11", "ids": ["inverter_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'power_pv1_123':
|
elif id == 'power_pv1_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv1_123", "val_tpl": "{{ (value_json['pv1']['Power'] | float)}}", "unit_of_meas": "W", "dev": {"name": "Module PV1", "sa": "Module PV1", "via_device": "inverter_123", "ids": ["input_pv1_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv1_123", "val_tpl": "{{ (value_json['pv1']['Power'] | float)}}", "unit_of_meas": "W", "icon": "mdi:gauge", "dev": {"name": "Module PV1", "sa": "Module PV1", "via_device": "inverter_123", "ids": ["input_pv1_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'power_pv2_123':
|
elif id == 'power_pv2_123':
|
||||||
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv2_123", "val_tpl": "{{ (value_json['pv2']['Power'] | float)}}", "unit_of_meas": "W", "dev": {"name": "Module PV2", "sa": "Module PV2", "via_device": "inverter_123", "ids": ["input_pv2_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Power", "stat_t": "tsun/garagendach/input", "dev_cla": "power", "stat_cla": "measurement", "uniq_id": "power_pv2_123", "val_tpl": "{{ (value_json['pv2']['Power'] | float)}}", "unit_of_meas": "W", "icon": "mdi:gauge", "dev": {"name": "Module PV2", "sa": "Module PV2", "via_device": "inverter_123", "ids": ["input_pv2_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
|
|
||||||
elif id == 'signal_123':
|
elif id == 'signal_123':
|
||||||
assert d_json == json.dumps({"name": "Signal Strength", "stat_t": "tsun/garagendach/controller", "dev_cla": None, "stat_cla": "measurement", "uniq_id": "signal_123", "val_tpl": "{{value_json[\'Signal_Strength\'] | int}}", "unit_of_meas": "%", "icon": "mdi:wifi", "dev": {"name": "Controller", "sa": "Controller", "mdl": "RSW-1-10001", "mf": "Raymon", "sw": "RSW_400_V1.00.06", "ids": ["controller_123"]}})
|
assert comp == 'sensor'
|
||||||
|
assert d_json == json.dumps({"name": "Signal Strength", "stat_t": "tsun/garagendach/controller", "dev_cla": None, "stat_cla": "measurement", "uniq_id": "signal_123", "val_tpl": "{{value_json[\'Signal_Strength\'] | int}}", "unit_of_meas": "%", "icon": "mdi:wifi", "dev": {"name": "Controller", "sa": "Controller", "mdl": "RSW-1-10001", "mf": "Raymon", "sw": "RSW_400_V1.00.06", "ids": ["controller_123"]}, "o": {"name": "proxy", "sw": "unknown"}})
|
||||||
tests +=1
|
tests +=1
|
||||||
assert tests==5
|
assert tests==5
|
||||||
|
|
||||||
def test_build_ha_conf3():
|
|
||||||
i = Infos()
|
|
||||||
for d_json, id in i.ha_confs(prfx="tsun/garagendach/", snr='123'):
|
|
||||||
pass
|
|
||||||
|
|||||||
Reference in New Issue
Block a user