diff --git a/pyblnet/blnet.py b/pyblnet/blnet.py index b4d147b..5fa920f 100644 --- a/pyblnet/blnet.py +++ b/pyblnet/blnet.py @@ -96,7 +96,8 @@ def fetch(self, node=None): data["analog"] = self._convert_web(blnet_session.read_analog_values()) data["digital"] = self._convert_web(blnet_session.read_digital_values()) for entry in data["analog"].values(): - entry["type"] = WEB_UNIT_TYPES.get(entry.get("unit_of_measurement")) + # digital inputs come with their type, others by their unit + entry.setdefault("type", WEB_UNIT_TYPES.get(entry.get("unit_of_measurement"))) if self.blnet_direct: direct = self.blnet_direct.get_latest(self.max_retries)[0] # Override values for analog and digital as values are @@ -104,7 +105,10 @@ def fetch(self, node=None): for domain in ["analog", "digital"]: for id, value in direct[domain].items(): if data[domain].get(id) is not None: - data[domain][id]["value"] = value + # Keep the web value if the direct one is missing: a + # UVR1611 on CAN sends digital inputs as unused inputs + if value is not None: + data[domain][id]["value"] = value elif not self.blnet_web: if value is None: continue diff --git a/pyblnet/blnet_web.py b/pyblnet/blnet_web.py index 9f9d53b..596ce2b 100644 --- a/pyblnet/blnet_web.py +++ b/pyblnet/blnet_web.py @@ -15,6 +15,16 @@ from builtins import int from html.parser import HTMLParser +# An input on the inputs page: a measured value with unit, or the state of a +# digital input. The value of a digital input is "1" (on) or "0" (off), like +# the direct connection reports it. +INPUT_PATTERN = re.compile( + r"(?P\d+): (?P.+)\n( ){3,6}" + r"(?:(?P(- )?\d+,\d+) (?P.+?)   " + r"|(?PAUS|EIN|OFF|ON)( )+)PAR?" +) +DIGITAL_INPUT_STATES = {"AUS": "0", "OFF": "0", "EIN": "1", "ON": "1"} + class _ElementTextParser(HTMLParser): """ @@ -256,24 +266,24 @@ def read_analog_values(self): data = list() # search for data by regular expression - match_iter = re.finditer( - r"(?P\d+): (?P.+)\n( ){3,6}(?P(- )?\d+,\d+) (?P.+?)   PAR?", - data_raw, - ) - # parse a dict of the match and save them all in a list - for match in match_iter: - match_dict = match.groupdict() - # convert html entities to unicode characters - for key, value in match_dict.items(): - # replace   by " " since it is not unescaped as expected - value = value.replace(" ", " ") - if key == "value": - # for values - # also replace decimal "," by "." and remove " " completely - value = value.replace(",", ".").replace(" ", "") - match_dict[key] = html.unescape(value) - # and append formatted dict - data.append(match_dict) + for match in INPUT_PATTERN.finditer(data_raw): + # convert html entities to unicode characters, + # replace   by " " since it is not unescaped as expected + entry = { + "id": match["id"], + "name": html.unescape(match["name"].replace(" ", " ")), + } + if match["state"] is not None: + entry["value"] = DIGITAL_INPUT_STATES[match["state"]] + entry["unit_of_measurement"] = None + entry["type"] = "digital" + else: + # replace decimal "," by "." and remove " " completely + entry["value"] = match["value"].replace(" ", "").replace(",", ".") + entry["unit_of_measurement"] = html.unescape( + match["unit_of_measurement"].replace(" ", " ") + ) + data.append(entry) return data def read_digital_values(self): diff --git a/tests/test_structure/580500.digital_inputs.htm b/tests/test_structure/580500.digital_inputs.htm new file mode 100644 index 0000000..c00e679 Binary files /dev/null and b/tests/test_structure/580500.digital_inputs.htm differ diff --git a/tests/test_web_pages.py b/tests/test_web_pages.py index f533dae..b3051f7 100644 --- a/tests/test_web_pages.py +++ b/tests/test_web_pages.py @@ -1,10 +1,12 @@ """Parsing of saved BL-NET web pages, independent of the mock HTTP server""" +import struct import unittest from pathlib import Path from unittest import mock -from pyblnet import BLNETWeb, blnet_test +from pyblnet import BLNET, BLNETWeb, blnet_test +from pyblnet.blnet_parser import BLNETParser PAGES = Path(__file__).parent / "test_structure" @@ -52,6 +54,60 @@ def test_read_analog_values_with_negative_value(self): ], ) + def test_read_analog_values_with_digital_inputs(self): + # inputs 3, 8, 13 and 14 are digital inputs, shown as AUS/EIN + with serve("580500.digital_inputs.htm"): + values = self.web.read_analog_values() + digital = {"unit_of_measurement": None, "type": "digital"} + self.assertEqual( + values, + [ + {"id": "1", "name": "T.Kollektor", "value": "14.2", "unit_of_measurement": "°C"}, + {"id": "2", "name": "T.WW.Puffer", "value": "62.1", "unit_of_measurement": "°C"}, + {"id": "3", "name": "-----", "value": "0", **digital}, + {"id": "4", "name": "T.Solar-RL", "value": "38.1", "unit_of_measurement": "°C"}, + {"id": "5", "name": "T.Zirku.RL", "value": "22.8", "unit_of_measurement": "°C"}, + {"id": "6", "name": "T.Puffer.m", "value": "62.0", "unit_of_measurement": "°C"}, + {"id": "7", "name": "T.Puffer.o", "value": "62.3", "unit_of_measurement": "°C"}, + {"id": "8", "name": "Freigabe1", "value": "0", **digital}, + {"id": "9", "name": "T.Kessel-VL", "value": "59.3", "unit_of_measurement": "°C"}, + {"id": "10", "name": "T.Heizkr.VL1", "value": "27.0", "unit_of_measurement": "°C"}, + {"id": "11", "name": "T.Heizkr.VL2", "value": "31.6", "unit_of_measurement": "°C"}, + {"id": "12", "name": "Temp.Aussen", "value": "10.4", "unit_of_measurement": "°C"}, + {"id": "13", "name": "Digitaleing.1", "value": "1", **digital}, + {"id": "14", "name": "Digitaleing.2", "value": "1", **digital}, + {"id": "15", "name": "Temp.Raum2", "value": "999.9", "unit_of_measurement": "°C"}, + {"id": "16", "name": "Temp.Raum1", "value": "20.1", "unit_of_measurement": "°C"}, + ], + ) + + def test_fetch_reports_digital_inputs_as_analog_inputs_of_type_digital(self): + with serve("main.htm"): + blnet = BLNET("blnet") + with serve("580500.digital_inputs.htm"), mock.patch.object( + BLNETWeb, "read_digital_values", return_value=[] + ): + data = blnet.fetch() + self.assertEqual(data["analog"][8]["type"], "digital") + self.assertEqual(data["analog"][8]["value"], "0") + self.assertEqual(data["analog"][1]["type"], "temperature") + + def test_fetch_keeps_web_value_of_digital_input_missing_in_direct_data(self): + # A UVR1611 on CAN sends digital inputs with type bits 000 (unused) + # and the state in bit 0, see the recorded dataset in pyblnet-async. + # The parser reports those as None, the web value has to stay. + inputs = [0x208F] + [0x2000] * 11 + [0x0001, 0x0001] + [0x2000] * 2 + direct = BLNETParser(struct.pack("<16H", *inputs) + bytes(55 - 32)).to_dict() + with serve("main.htm"): + blnet = BLNET("blnet") + blnet.blnet_direct = mock.Mock(get_latest=mock.Mock(return_value={0: direct})) + with serve("580500.digital_inputs.htm"), mock.patch.object( + BLNETWeb, "read_digital_values", return_value=[] + ): + data = blnet.fetch() + self.assertEqual(data["analog"][13]["value"], "1") + self.assertEqual(data["analog"][1]["value"], 14.3) + def test_read_analog_values_access_denied(self): with serve(".error.html"): self.assertIsNone(self.web.read_analog_values())