diff --git a/pyblnet/blnet_parser.py b/pyblnet/blnet_parser.py index e8f2616..c0d12c3 100644 --- a/pyblnet/blnet_parser.py +++ b/pyblnet/blnet_parser.py @@ -30,6 +30,8 @@ TYPE_RADIATION = 0x4000 TYPE_RAS = 0x7000 RAS_POSITIVE_MASK = 0x01FF +# Temperature input without sensor (or with a broken wire): all value bits set, 409.5 °C +TEMP_NO_SENSOR = TYPE_TEMP | POSITIVE_VALUE_MASK # 32 bit INT32_MASK = 0xFFFFFFFF INT32_SIGN = 0x80000000 @@ -72,7 +74,8 @@ def __init__(self, data): self.analog = {} for channel in range(0, 16): - self.analog[channel + 1] = round(self._convert_analog(analog[channel]), 3) + value = self._convert_analog(analog[channel]) + self.analog[channel + 1] = None if value is None else round(value, 3) self.digital = {} for channel in range(0, 16): @@ -107,7 +110,12 @@ def _convert_analog(self, value): """ mask = value & TYPE_MASK - if mask == TYPE_TEMP: + if mask == TYPE_NONE: + # input is unused + return None + elif value == TEMP_NO_SENSOR: + return None + elif mask == TYPE_TEMP: return self._calculate_value(value, 0.1) elif mask == TYPE_VOLUME: return self._calculate_value(value, 4) @@ -118,7 +126,8 @@ def _convert_analog(self, value): return 0 elif mask == TYPE_RAS: return self._calculate_value(value, 0.1, RAS_POSITIVE_MASK) - elif mask in [TYPE_RADIATION, TYPE_NONE] or True: + else: + # TYPE_RADIATION and types not covered by the interface spec return self._calculate_value(value) def _convert_digital(self, value, position): @@ -144,7 +153,7 @@ def _convert_energy(self, mwh, kwh, active, position): Check if heat meter is activated on a given position @return its energy """ - if active & position: + if active & (1 << position): kwh = self._calculate_value(kwh, 0.1, INT16_POSITIVE_MASK) return round(mwh * 1000 + kwh, 3) else: @@ -155,7 +164,7 @@ def _convert_power(self, value, active, position): checks if heat meter is activated at given position @return its power """ - if active & position: + if active & (1 << position): value = self._calculate_value(value, 1 / 2560, INT32_MASK, INT32_SIGN) value = round(value, 3) return value diff --git a/tests/test_parser.py b/tests/test_parser.py index 2e9bc3e..1e019e7 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,3 +1,4 @@ +import struct from pyblnet.blnet_parser import BLNETParser import unittest @@ -9,7 +10,7 @@ def test_parser_ok(self): 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}, + '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: None, 13: 31.7, 14: None, 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': {1: None, 2: 0, 3: 0, 4: 0}, 'energy': {1: None, 2: None}, @@ -22,6 +23,42 @@ def test_parser_ok(self): assert parsed_values['energy'] == expected_values['energy'] assert parsed_values['power'] == expected_values['power'] + def test_parser_unused_inputs_are_none(self): + # Input type bits 000 mean "Eingang unbenutzt", regardless of the other bits + inputs = [0x0000, 0x0001, 0x8005] + [0x2000] * 13 + data = struct.pack('<16H', *inputs) + bytes(55 - 32) + parsed_values = BLNETParser(data).to_dict() + + assert parsed_values['analog'][1] is None + assert parsed_values['analog'][2] is None + assert parsed_values['analog'][3] is None + assert parsed_values['analog'][4] == 0 + + def test_parser_disconnected_temperature_sensor_is_none(self): + # A temperature input without sensor reads all value bits set (409.5 °C) + inputs = [0x2FFF, 0x2FFE] + [0x2000] * 14 + data = struct.pack('<16H', *inputs) + bytes(55 - 32) + parsed_values = BLNETParser(data).to_dict() + + assert parsed_values['analog'][1] is None + assert parsed_values['analog'][2] == 409.4 + + def test_parser_heat_meter_1_active(self): + # Latest data of a real UVR1611 in DL mode (header and checksum stripped), + # only heat meter 1 is active (active byte = 0x01) + data = bytes.fromhex( + 'ff2fff2fff2fff2fff2f1b224d210522e92110216e2032210322' + '6f21ff2f00306102' + '18000a12' + '01' + '000000001a216a01' + '0b010a3c0001000c' + ) + parsed_values = BLNETParser(data).to_dict() + + assert parsed_values['energy'] == {1: 362847.4, 2: None} + assert parsed_values['power'] == {1: 0, 2: None} + 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):