From 0cca8c63498f8625ad5dfdd5113ed8409f18150a Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 16 Nov 2026 17:11:55 +0100 Subject: [PATCH 1/2] Read digital inputs from the web interface The inputs page shows inputs configured as digital as AUS/EIN instead of a value with unit, which the analog regex skipped, so they were missing when fetching via the web interface. Match them too and report them like the direct connection does: as analog inputs with value 0 or 1 and type digital. Supersedes #46, which matched them on the outputs page and with a regex that required the state twice. The test page is from the BL-NET of the user who reported it there. --- pyblnet/blnet.py | 3 +- pyblnet/blnet_web.py | 46 +++++++++++------- .../test_structure/580500.digital_inputs.htm | Bin 0 -> 7129 bytes tests/test_web_pages.py | 40 ++++++++++++++- 4 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 tests/test_structure/580500.digital_inputs.htm diff --git a/pyblnet/blnet.py b/pyblnet/blnet.py index b4d147b..74e9576 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 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 0000000000000000000000000000000000000000..c00e679ac9fdc24919b9c848dfe12b39c29978f5 GIT binary patch literal 7129 zcmc&(X>;4Q7JWYjk8T~Qk!Vt~yoi!Ij$(6PeJ_V1oTmdJ z;s<%4CVD8G_#(q{_~Gks?UR$cFi+*b1ir?rI`Z-=UD<(wZR8$YB(;H)Oq zu|{YhiKKY+?fbQR9_jvQl1e>IP3dC#%V6Gr^f5KlBfNyS|s37@}%EUzwb#TCw=!w{Z9QTB5d66DM5S6 z+AHKJ1ecdy0q7DQ$1#BJF&R%}Kj1Vn4HY z*Ev!)^N_L4p~gS6pYRIA=lUtF=r})n;Cdp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dy zEfBQTGY7Vg{j+ah*L5wo|8SFZw*YilndB>nqgea;)61y3Cp)sp;T8aT6@Wj9xXtZt z0CYO7lbyI!ZwmlN6#x?vcJi_XASRrh065wL!0~ecd@BImod7uA0>DWD;8MVaf*)Y_ zW&Qu%z&_am)M;gvk2uRPZGYb)vU{hk-I(QcBLdf{lz)%O)2*<-f7-$*o#P!v#o1=< z=NomUedlGD>bk9?orJ`j&DvL+G)_|cEC=6VFDvglEvT7yP-dKOR=(Vy-;yzrKE-fd zX>Z{vSjXb-7vX zYOgNU-om#!&TgtD*IBQ-Ieq-2Ge}omSU*1(QuzL|a8hx5`p&Jq=Om00hTG;22QA+c zTWBReGjjxKAY z?}E|R8o|l8rV(7&C^pwxgAR*_Pi*8xjSoB7R9e*#y~Ts+$9 q?zhogquf(2_H(6Ncw1Lb9Y*0_c@nLAMDbhQCjhtRazTHY8~q=uk%k8V literal 0 HcmV?d00001 diff --git a/tests/test_web_pages.py b/tests/test_web_pages.py index f533dae..f45e46a 100644 --- a/tests/test_web_pages.py +++ b/tests/test_web_pages.py @@ -4,7 +4,7 @@ from pathlib import Path from unittest import mock -from pyblnet import BLNETWeb, blnet_test +from pyblnet import BLNET, BLNETWeb, blnet_test PAGES = Path(__file__).parent / "test_structure" @@ -52,6 +52,44 @@ 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_read_analog_values_access_denied(self): with serve(".error.html"): self.assertIsNone(self.web.read_analog_values()) From d3a851367a79b0c748d762c7332305e1c7729a64 Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 16 Nov 2026 17:15:32 +0100 Subject: [PATCH 2/2] Keep web values the direct connection doesn't have A UVR1611 on CAN sends digital inputs over the direct port with type bits 000 and the state in bit 0 (recorded dataset in pyblnet-async), so the parser reports them as None like unused inputs. With both connections enabled that None replaced the state read from the web interface. --- pyblnet/blnet.py | 5 ++++- tests/test_web_pages.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pyblnet/blnet.py b/pyblnet/blnet.py index 74e9576..5fa920f 100644 --- a/pyblnet/blnet.py +++ b/pyblnet/blnet.py @@ -105,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/tests/test_web_pages.py b/tests/test_web_pages.py index f45e46a..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 BLNET, BLNETWeb, blnet_test +from pyblnet.blnet_parser import BLNETParser PAGES = Path(__file__).parent / "test_structure" @@ -90,6 +92,22 @@ def test_fetch_reports_digital_inputs_as_analog_inputs_of_type_digital(self): 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())