Repository: nielstron/pyblnet (language: python)

Issue:
Fix requirements and version



Discussion / hints:

Fix patch:
diff --git a/pyblnet/blnet_conn.py b/pyblnet/blnet_conn.py
index ab71002..5f4f49a 100644
--- a/pyblnet/blnet_conn.py
+++ b/pyblnet/blnet_conn.py
@@ -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
diff --git a/pyblnet/blnet_parser.py b/pyblnet/blnet_parser.py
index d52a51c..441c362 100644
--- a/pyblnet/blnet_parser.py
+++ b/pyblnet/blnet_parser.py
@@ -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):
         """


Test patch:
diff --git a/pyblnet/tests/test_parser.py b/pyblnet/tests/test_parser.py
new file mode 100644
index 0000000..831795d
--- /dev/null
+++ b/pyblnet/tests/test_parser.py
@@ -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)