From bc72aaddc20006cfd77394836bb976947343a514 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Mon, 27 Mar 2023 09:55:38 +0200 Subject: [PATCH] 2.1 (#27) --- readme.md | 7 ++++++- src/sml2mqtt/__version__.py | 2 +- src/sml2mqtt/config/config.py | 5 ++++- src/sml2mqtt/device/sml_serial.py | 20 ++++++++++++++++++-- tests/device/frames/test_frame_1.py | 3 ++- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index b7602f1..22da289 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,10 @@ # sml2mqtt [![Tests Status](https://github.com/spacemanspiff2007/sml2mqtt/workflows/Tests/badge.svg)](https://github.com/spacemanspiff2007/sml2mqtt/actions?query=workflow%3ATests) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sml2mqtt)](https://pypi.org/project/sml2mqtt/) -[![Updates](https://pyup.io/repos/github/spacemanspiff2007/sml2mqtt/shield.svg)](https://pyup.io/repos/github/spacemanspiff2007/sml2mqtt/) [![PyPI](https://img.shields.io/pypi/v/sml2mqtt)](https://pypi.org/project/sml2mqtt/) [![Downloads](https://pepy.tech/badge/sml2mqtt/month)](https://pepy.tech/project/sml2mqtt) +[![Docker Image Version (latest by date)](https://img.shields.io/docker/v/spacemanspiff2007/sml2mqtt?label=docker)](https://hub.docker.com/r/spacemanspiff2007/sml2mqtt) +[![Docker Pulls](https://img.shields.io/docker/pulls/spacemanspiff2007/sml2mqtt)](https://hub.docker.com/r/spacemanspiff2007/sml2mqtt) _A simple yet flexible sml to mqtt bridge_ @@ -17,5 +18,9 @@ from energy meters and report the values through mqtt. # Changelog +#### 2.1 (2023-03-27) +- Additional obis id for serial number matching +- Improved serial reading a bit + #### 2.0.0 (2023-03-22) - Release rework diff --git a/src/sml2mqtt/__version__.py b/src/sml2mqtt/__version__.py index afced14..d980f27 100644 --- a/src/sml2mqtt/__version__.py +++ b/src/sml2mqtt/__version__.py @@ -1 +1 @@ -__version__ = '2.0.0' +__version__ = '2.1' diff --git a/src/sml2mqtt/config/config.py b/src/sml2mqtt/config/config.py index a1da81f..9aba0ec 100644 --- a/src/sml2mqtt/config/config.py +++ b/src/sml2mqtt/config/config.py @@ -65,7 +65,10 @@ class GeneralSettings(BaseModel): alias='report device id', in_file=False ) device_id_obis: List[StrictStr] = Field( - ['0100000009ff'], description='Additional OBIS fields for the serial number, default is 0100000009ff', + # 0100000009ff (1-0:0.0.9*255) : Geräteeinzelidentifikation + # 0100600100ff (1-0:96.1.0*255): Produktionsnummer + ['0100000009ff', '0100600100ff'], + description='Additional OBIS fields for the serial number to configuration matching', alias='device id obis', in_file=False ) diff --git a/src/sml2mqtt/device/sml_serial.py b/src/sml2mqtt/device/sml_serial.py index eafd11c..4181fc3 100644 --- a/src/sml2mqtt/device/sml_serial.py +++ b/src/sml2mqtt/device/sml_serial.py @@ -1,5 +1,6 @@ import asyncio from asyncio import CancelledError, create_task, Task +from time import monotonic from typing import Optional, TYPE_CHECKING from serial_asyncio import create_serial_connection, SerialTransport @@ -37,13 +38,17 @@ def __init__(self) -> None: self.transport: Optional[SerialTransport] = None self.task: Optional[Task] = None + self.last_read: Optional[float] = None - def connection_made(self, transport): + def connection_made(self, transport: SerialTransport): self.transport = transport log.debug(f'Port {self.url} successfully opened') self.device.set_status(DeviceStatus.PORT_OPENED) + # so we can read bigger chunks at once in case someone uses a higher baudrate + self.transport._max_read_size = 10_240 + def connection_lost(self, exc): self.close() @@ -52,11 +57,22 @@ def connection_lost(self, exc): def data_received(self, data: bytes): self.transport.pause_reading() + self.last_read = monotonic() + self.device.serial_data_read(data) async def _chunk_task(self): + interval = 0.2 + while True: - await asyncio.sleep(0.2) + await asyncio.sleep(interval) + + if self.last_read is not None: + diff_to_interval = interval - (monotonic() - self.last_read) + self.last_read = None + if diff_to_interval >= 0.001: + await asyncio.sleep(diff_to_interval) + self.transport.resume_reading() def start(self): diff --git a/tests/device/frames/test_frame_1.py b/tests/device/frames/test_frame_1.py index 5b6760d..0395560 100644 --- a/tests/device/frames/test_frame_1.py +++ b/tests/device/frames/test_frame_1.py @@ -10,6 +10,7 @@ async def test_frame_no_match_obis_id(device: TestingDevice, no_serial, caplog, monkeypatch, sml_frame_1: SmlFrame, sml_frame_1_analyze, arg_analyze): caplog.set_level(logging.DEBUG) + monkeypatch.setattr(CONFIG.general, 'device_id_obis', ['0100000009ff', '01006001ffff']) device.testing_raise_on_status = False device.serial_data_read(sml_frame_1) @@ -17,7 +18,7 @@ async def test_frame_no_match_obis_id(device: TestingDevice, no_serial, caplog, msg = "\n".join(x.msg for x in caplog.records) assert msg == sml_frame_1_analyze + """ -Found none of the following obis ids in the sml frame: 0100000009ff +Found none of the following obis ids in the sml frame: 0100000009ff, 01006001ffff Received Frame -> b'760500531efa620062007263010176010105001bb4fe0b0a0149534b0005020de272620165001bb32e620163a71400760500531efb620062007263070177010b0a0149534b0005020de2070100620affff72620165001bb32e757707010060320101010101010449534b0177070100600100ff010101010b0a0149534b0005020de20177070100010800ff65001c010401621e52ff650026bea90177070100020800ff0101621e52ff62000177070100100700ff0101621b52005301100101016350ba00760500531efc6200620072630201710163ba1900'