Skip to content

Commit

Permalink
1.2.0:
Browse files Browse the repository at this point in the history
- Added fallback in case shortcut fails
- Fixed some tests
  • Loading branch information
spacemanspiff2007 committed Jun 13, 2022
1 parent 1bbd980 commit 58979a7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![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/month)
[![Downloads](https://pepy.tech/badge/sml2mqtt/month)](https://pepy.tech/project/sml2mqtt)

_A simple sml to mqtt bridge_

Expand Down
2 changes: 1 addition & 1 deletion src/sml2mqtt/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.0'
__version__ = '1.2.0'
17 changes: 15 additions & 2 deletions src/sml2mqtt/device/sml_device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import traceback
from binascii import b2a_hex
from typing import Dict, Final, Optional, Set
from typing import Dict, Final, List, Optional, Set

from smllib import SmlFrame, SmlStreamReader
from smllib.errors import CrcError
Expand Down Expand Up @@ -147,8 +147,21 @@ async def process_frame(self, frame: SmlFrame):
self.log.info(line)
self.log.info('')

# try shortcut, if that fails try parsing the whole frame
try:
sml_objs: List[SmlListEntry] = frame.get_obis()
except Exception:
self.log.info('get_obis failed - try parsing frame')
for line in traceback.format_exc().splitlines():
self.log.debug(line)

sml_objs: List[SmlListEntry] = []
for msg in frame.parse_frame():
for val in getattr(msg.message_body, 'val_list', []):
sml_objs.append(val)

frame_values: Dict[str, SmlListEntry] = {}
for sml_obj in frame.get_obis():
for sml_obj in sml_objs:
name = sml_obj.obis
if name in self.skip_values:
continue
Expand Down
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
from binascii import a2b_hex
from unittest.mock import AsyncMock, Mock

import pytest
from smllib.reader import SmlFrame

import sml2mqtt.device.sml_device
import sml2mqtt.device.sml_serial
import sml2mqtt.mqtt.mqtt_obj
from sml2mqtt.device import Device, DeviceStatus
from sml2mqtt.mqtt import MqttObj


@pytest.fixture
def no_mqtt(monkeypatch):

pub_list = []

async def pub_func(topic: str, value, qos: int, retain: bool):
pub_list.append((topic, value, qos, retain))

monkeypatch.setattr(sml2mqtt.mqtt.mqtt_obj, 'publish', pub_func)

yield pub_list


@pytest.fixture
def sml_data_1():
Expand All @@ -26,3 +46,15 @@ def sml_frame_1():
b'0201710163ba1900'

yield SmlFrame(a2b_hex(data))


@pytest.fixture
def sml_frame_2():
data = b'7605065850a66200620072630101760107ffffffffffff05021d70370b0a014c475a0003403b4972620165021d7707016326de' \
b'007605065850a762006200726307017707ffffffffffff0b0a014c475a0003403b49070100620affff72620165021d77077577' \
b'0701006032010101010101044c475a0177070100600100ff010101010b0a014c475a0003403b490177070100010800ff65001c' \
b'010472620165021d7707621e52ff690000000003152c450177070100020800ff0172620165021d7707621e52ff690000000000' \
b'0000000177070100100700ff0101621b52005900000000000000fb010101637264007605065850a86200620072630201710163' \
b'1c8c00'

yield SmlFrame(a2b_hex(data))
3 changes: 2 additions & 1 deletion tests/device/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def clean_devices(monkeypatch):


@pytest.fixture
async def device(monkeypatch, no_serial):
async def device(no_serial):
device_url = 'device_url'

mqtt_base = MqttObj('testing', 0, False).update()
Expand All @@ -40,6 +40,7 @@ def raise_exception_on_error(status: DeviceStatus):
func(status)
return raise_exception_on_error

assert hasattr(obj, 'set_status')
obj.set_status = wrapper(obj.set_status)

yield obj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def test_frame_no_id(device: Device, no_serial, caplog, sml_frame_1: SmlFr
))

caplog.set_level(logging.DEBUG)
CMD_ARGS.analyze = True
monkeypatch.setattr(CMD_ARGS, 'analyze', True)

await device.process_frame(sml_frame_1)

Expand Down
27 changes: 27 additions & 0 deletions tests/device/frames/test_frame_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio
import logging

from smllib.reader import SmlFrame

from sml2mqtt import CONFIG
from sml2mqtt.config.device import SmlDeviceConfig
from sml2mqtt.device import Device


async def test_frame_no_shortcut(device: Device, no_serial, caplog, sml_frame_2: SmlFrame, monkeypatch, no_mqtt):
caplog.set_level(logging.DEBUG)

monkeypatch.setitem(CONFIG.devices, 'device_url', SmlDeviceConfig(
mqtt={'topic': 'xxxx'}
))

await device.process_frame(sml_frame_2)
await asyncio.sleep(0.01)

assert no_mqtt == [
('testing/xxxx/010060320101', 'LGZ', 0, False),
('testing/xxxx/0100600100ff', '0a014c475a0003403b49', 0, False),
('testing/xxxx/0100010800ff', 5171.9237, 0, False),
('testing/xxxx/0100100700ff', 251, 0, False),
('testing/xxxx/status', 'OK', 0, False)
]
4 changes: 2 additions & 2 deletions tests/device/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from sml2mqtt.device import Device


async def test_serial_data(device: Device, no_serial, caplog, sml_data_1: bytes):
async def test_serial_data(device: Device, no_serial, caplog, sml_data_1: bytes, monkeypatch):
caplog.set_level(logging.DEBUG)

CMD_ARGS.analyze = True
monkeypatch.setattr(CMD_ARGS, 'analyze', True)

await device.serial_data_read(a2b_hex(sml_data_1))

Expand Down

0 comments on commit 58979a7

Please sign in to comment.