Skip to content

Commit

Permalink
2.1 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 authored Mar 27, 2023
1 parent deadefc commit bc72aad
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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_

Expand All @@ -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
2 changes: 1 addition & 1 deletion src/sml2mqtt/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.0'
__version__ = '2.1'
5 changes: 4 additions & 1 deletion src/sml2mqtt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
20 changes: 18 additions & 2 deletions src/sml2mqtt/device/sml_serial.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/device/frames/test_frame_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
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)

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'
Expand Down

0 comments on commit bc72aad

Please sign in to comment.