Skip to content

Commit

Permalink
Merge pull request #43 from zwotzie/fix_parser
Browse files Browse the repository at this point in the history
Fix parser
  • Loading branch information
nielstron authored Oct 29, 2024
2 parents 92eccda + 7bd3383 commit c447fdf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyblnet/blnet_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Created on 09.08.2018
This is basically a python port of of a script by berwinter
This is basically a python port of a script by berwinter
https://github.com/berwinter/uvr1611/blob/master/lib/backend/blnet-connection.inc.php
@author: Niels
Expand Down
24 changes: 17 additions & 7 deletions pyblnet/blnet_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,29 @@ def __init__(self, data):

self.speed = {}
for channel in range(0, 4):
self.speed[channel + 1] = round(self._convert_speed(speed[channel]), 3)
speed = self._convert_speed(speed[channel])
if speed:
self.speed[channel + 1] = round(speed, 3)
else:
break

self.energy = {}
for channel in range(0, 2):
self.energy[channel + 1] = round(
self._convert_energy(MWh[channel], kWh[channel], active, channel), 3
)
energy = self._convert_energy(MWh[channel], kWh[channel], active, channel)
if energy:
self.energy[channel + 1] = round(energy, 3)
else:
break

self.power = {}
for channel in range(0, 2):
self.power[channel + 1] = round(
self._convert_power(power[channel], active, channel), 3
)
power = self._convert_power(power[channel], active, channel)
if power:
self.power[channel + 1] = round(
self._convert_power(power[channel], active, channel), 3
)
else:
break

def to_dict(self):
"""
Expand Down
24 changes: 24 additions & 0 deletions pyblnet/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pyblnet.blnet_parser import BLNETParser
import unittest


class TestParser(unittest.TestCase):

def test_parser_ok(self):
data = b'} \xb5"f"\x03"~!\x8e"\x16!\xe0 \xff \xf3 \xf8 \x00\x00=!\x01\x00\x00`\xe2!\x00\x00\x80\x00\x00\x00\x00,\x00\xa4(\x06\x00DhI\x82\x1d\x04Ce\xc0\x00'
parser = BLNETParser(data)
parsed_values = parser.to_dict()
expected_values = {
'analog': {1: 12.5, 2: 69.3, 3: 61.4, 4: 51.5, 5: 38.2, 6: 65.4, 7: 27.8, 8: 22.4, 9: 25.5, 10: 24.3, 11: 24.8, 12: 0, 13: 31.7, 14: 1, 15: 0, 16: 48.2},
'digital': {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0},
'speed': {},
'energy': {},
'power': {}
}

assert parsed_values == expected_values

def test_parser_not_ok(self):
data = b'broken} \xb5"f"\x03"~!\x8e"\x16!\xe0 \xff \xf3 \xf8 \x00\x00=!\x01\x00\x00`\xe2!\x00\x00\x80\x00\x00\x00\x00,\x00\xa4(\x06\x00DhI\x82\x1d\x04Ce\xc0\x00'
with self.assertRaises(ValueError):
parser = BLNETParser(data)

0 comments on commit c447fdf

Please sign in to comment.