PyBLNET 0.10.0__tar.gz → 0.11.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyblnet-0.10.0 → pyblnet-0.11.0}/PKG-INFO +10 -8
- {pyblnet-0.10.0 → pyblnet-0.11.0}/README.md +3 -1
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyblnet/__init__.py +1 -1
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyblnet/blnet.py +25 -4
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyblnet/blnet_conn.py +113 -35
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyblnet/blnet_parser.py +32 -5
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyblnet/blnet_web.py +75 -23
- {pyblnet-0.10.0 → pyblnet-0.11.0}/pyproject.toml +6 -9
- pyblnet-0.10.0/.github/workflows/build.yml +0 -50
- pyblnet-0.10.0/.github/workflows/release.yml +0 -22
- pyblnet-0.10.0/.gitignore +0 -206
- pyblnet-0.10.0/Pipfile +0 -13
- pyblnet-0.10.0/tests/__init__.py +0 -0
- pyblnet-0.10.0/tests/test_parser.py +0 -28
- pyblnet-0.10.0/tests/test_structure/.error.html +0 -10
- pyblnet-0.10.0/tests/test_structure/580500.alternative.htm +0 -0
- pyblnet-0.10.0/tests/test_structure/580500.htm +0 -1
- pyblnet-0.10.0/tests/test_structure/580600.htm +0 -0
- pyblnet-0.10.0/tests/test_structure/__init__.py +0 -0
- pyblnet-0.10.0/tests/test_structure/blnet_mock_server.py +0 -279
- pyblnet-0.10.0/tests/test_structure/main.htm +0 -63
- pyblnet-0.10.0/tests/test_structure/main.html +0 -63
- pyblnet-0.10.0/tests/test_structure/par.htm +0 -0
- pyblnet-0.10.0/tests/test_structure/server_control.py +0 -59
- pyblnet-0.10.0/tests/test_web.py +0 -204
- pyblnet-0.10.0/tests/web_raw/__init__.py +0 -0
- pyblnet-0.10.0/tests/web_raw/analog_values +0 -0
- pyblnet-0.10.0/tests/web_raw/digital_values +0 -0
- pyblnet-0.10.0/tests/web_raw/logged_in +0 -0
- pyblnet-0.10.0/tests/web_raw/main_alt_one +0 -0
- pyblnet-0.10.0/tests/web_raw/web_state.py +0 -151
- {pyblnet-0.10.0 → pyblnet-0.11.0}/LICENSE.txt +0 -0
|
@@ -1,29 +1,31 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: PyBLNET
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: Automate wireless communication to UVR1611 via BL-NET
|
|
5
5
|
Keywords: python,uvr1611,blnet,technische alternative,home automation,iot
|
|
6
6
|
Author-email: nielstron <n.muendler@posteo.de>
|
|
7
|
-
Requires-Python: >=3
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
Classifier: Development Status :: 4 - Beta
|
|
10
10
|
Classifier: Intended Audience :: Developers
|
|
11
11
|
Classifier: Topic :: Software Development :: Object Brokering
|
|
12
12
|
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
15
13
|
Classifier: Programming Language :: Python :: 3.10
|
|
16
14
|
Classifier: Programming Language :: Python :: 3.11
|
|
17
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
18
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
19
|
License-File: LICENSE.txt
|
|
20
|
-
Requires-Dist: htmldom
|
|
21
20
|
Requires-Dist: requests
|
|
22
21
|
Requires-Dist: pytest ; extra == "test"
|
|
23
|
-
Project-URL: Homepage, https://github.com/
|
|
22
|
+
Project-URL: Homepage, https://github.com/PyBLNET/pyblnet/
|
|
24
23
|
Provides-Extra: test
|
|
24
|
+
Import-Name: pyblnet
|
|
25
25
|
|
|
26
26
|
# PyBLNET - a very basic python BL-NET bridge
|
|
27
|
+

|
|
28
|
+

|
|
27
29
|
[](https://github.com/nielstron/pyblnet/actions/workflows/build.yml)
|
|
28
30
|
[](https://coveralls.io/github/nielstron/pyblnet?branch=master)
|
|
29
31
|
|
|
@@ -90,5 +92,5 @@ print(blnet._get_data(1))
|
|
|
90
92
|
```
|
|
91
93
|
|
|
92
94
|
|
|
93
|
-
[1]
|
|
95
|
+
[1] Technische Alternative, "Schnittstelle Winsol <> Bootloader" (2006), archived copy: http://web.archive.org/web/20160122124754/http://www.haus-terra.at:80/heizung/download/Schnittstelle/Schnittstelle_PC_Bootloader.pdf
|
|
94
96
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
# PyBLNET - a very basic python BL-NET bridge
|
|
2
|
+

|
|
3
|
+

|
|
2
4
|
[](https://github.com/nielstron/pyblnet/actions/workflows/build.yml)
|
|
3
5
|
[](https://coveralls.io/github/nielstron/pyblnet?branch=master)
|
|
4
6
|
|
|
@@ -65,4 +67,4 @@ print(blnet._get_data(1))
|
|
|
65
67
|
```
|
|
66
68
|
|
|
67
69
|
|
|
68
|
-
[1]
|
|
70
|
+
[1] Technische Alternative, "Schnittstelle Winsol <> Bootloader" (2006), archived copy: http://web.archive.org/web/20160122124754/http://www.haus-terra.at:80/heizung/download/Schnittstelle/Schnittstelle_PC_Bootloader.pdf
|
|
@@ -12,10 +12,17 @@ as possible.
|
|
|
12
12
|
"""
|
|
13
13
|
from .blnet_web import BLNETWeb
|
|
14
14
|
from .blnet_conn import BLNETDirect
|
|
15
|
+
from .blnet_parser import ANALOG_UNITS
|
|
15
16
|
|
|
16
17
|
from urllib.parse import urlparse
|
|
17
18
|
|
|
18
19
|
|
|
20
|
+
# Units of the values only available via BLNETDirect, speed is a level 0-30
|
|
21
|
+
UNITS = {"speed": None, "energy": "kWh", "power": "kW"}
|
|
22
|
+
# Analog input type for the units shown by the web interface
|
|
23
|
+
WEB_UNIT_TYPES = {unit: input_type for input_type, unit in ANALOG_UNITS.items() if unit is not None}
|
|
24
|
+
|
|
25
|
+
|
|
19
26
|
class BLNET(object):
|
|
20
27
|
"""
|
|
21
28
|
General high-level BLNET interface.
|
|
@@ -68,7 +75,7 @@ class BLNET(object):
|
|
|
68
75
|
# especially not if not prefixed with http://
|
|
69
76
|
# see https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse
|
|
70
77
|
host = urlparse(address).hostname or address
|
|
71
|
-
self.blnet_direct = BLNETDirect(host, ta_port)
|
|
78
|
+
self.blnet_direct = BLNETDirect(host, ta_port, timeout=timeout)
|
|
72
79
|
|
|
73
80
|
def fetch(self, node=None):
|
|
74
81
|
"""
|
|
@@ -88,6 +95,8 @@ class BLNET(object):
|
|
|
88
95
|
blnet_session.set_node(node)
|
|
89
96
|
data["analog"] = self._convert_web(blnet_session.read_analog_values())
|
|
90
97
|
data["digital"] = self._convert_web(blnet_session.read_digital_values())
|
|
98
|
+
for entry in data["analog"].values():
|
|
99
|
+
entry["type"] = WEB_UNIT_TYPES.get(entry.get("unit_of_measurement"))
|
|
91
100
|
if self.blnet_direct:
|
|
92
101
|
direct = self.blnet_direct.get_latest(self.max_retries)[0]
|
|
93
102
|
# Override values for analog and digital as values are
|
|
@@ -96,11 +105,20 @@ class BLNET(object):
|
|
|
96
105
|
for id, value in direct[domain].items():
|
|
97
106
|
if data[domain].get(id) is not None:
|
|
98
107
|
data[domain][id]["value"] = value
|
|
108
|
+
elif not self.blnet_web:
|
|
109
|
+
if value is None:
|
|
110
|
+
continue
|
|
111
|
+
data[domain][id] = {"value": value}
|
|
112
|
+
if domain == "analog":
|
|
113
|
+
input_type = direct["analog_types"][id]
|
|
114
|
+
data[domain][id]["type"] = input_type
|
|
115
|
+
data[domain][id]["unit_of_measurement"] = ANALOG_UNITS.get(input_type)
|
|
116
|
+
|
|
99
117
|
for domain in ["speed", "energy", "power"]:
|
|
100
118
|
for id, value in direct[domain].items():
|
|
101
119
|
if value is None:
|
|
102
120
|
continue
|
|
103
|
-
data[domain][id] = {"value": value}
|
|
121
|
+
data[domain][id] = {"value": value, "unit_of_measurement": UNITS[domain]}
|
|
104
122
|
return data
|
|
105
123
|
|
|
106
124
|
def turn_on(self, digital_id, can_node=None):
|
|
@@ -162,8 +180,11 @@ class BLNET(object):
|
|
|
162
180
|
if cached is None:
|
|
163
181
|
cached = self.fetch()
|
|
164
182
|
for key, v in cached[type].items():
|
|
165
|
-
|
|
166
|
-
|
|
183
|
+
# values fetched only via blnet_direct have no name
|
|
184
|
+
if (name is not None and str(v.get("name")) == str(name)) or (
|
|
185
|
+
id is not None and str(key) == str(id)
|
|
186
|
+
):
|
|
187
|
+
val = v.get(ret)
|
|
167
188
|
dic = v
|
|
168
189
|
|
|
169
190
|
return val, dic, cached
|
|
@@ -9,7 +9,8 @@ https://github.com/berwinter/uvr1611/blob/master/lib/backend/blnet-connection.in
|
|
|
9
9
|
@author: Niels
|
|
10
10
|
"""
|
|
11
11
|
from builtins import str, int
|
|
12
|
-
from socket import socket, getaddrinfo, SOCK_STREAM, IPPROTO_TCP
|
|
12
|
+
from socket import socket, getaddrinfo, SOCK_STREAM, IPPROTO_TCP
|
|
13
|
+
import functools
|
|
13
14
|
import struct
|
|
14
15
|
from time import sleep
|
|
15
16
|
from datetime import datetime
|
|
@@ -27,11 +28,37 @@ READ_DATA = 0xAC
|
|
|
27
28
|
END_READ = b"\xAD"
|
|
28
29
|
RESET_DATA = b"\xAF"
|
|
29
30
|
WAIT_TIME = 0xBA
|
|
31
|
+
# Seconds to wait when the BL-Net has no new data yet. Real devices answer
|
|
32
|
+
# GET_LATEST with "no data" for up to about 5 seconds after a read.
|
|
33
|
+
NO_DATA_WAIT = 2
|
|
30
34
|
MAX_RETRYS = 10
|
|
35
|
+
# Length of a WAIT_TIME reply: 0xBA, seconds to wait, checksum
|
|
36
|
+
WAIT_SIZE = 3
|
|
31
37
|
DATASET_SIZE = 61
|
|
32
38
|
LATEST_SIZE = 56
|
|
33
39
|
|
|
34
40
|
|
|
41
|
+
def _reset_on_error(method):
|
|
42
|
+
"""
|
|
43
|
+
Close the connection and forget the read state if a read fails,
|
|
44
|
+
so that the next call starts with a fresh connection.
|
|
45
|
+
Malformed replies are raised as ConnectionError.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
@functools.wraps(method)
|
|
49
|
+
def wrapper(self, *args, **kwargs):
|
|
50
|
+
try:
|
|
51
|
+
return method(self, *args, **kwargs)
|
|
52
|
+
except (struct.error, IndexError, ValueError) as ex:
|
|
53
|
+
self._reset()
|
|
54
|
+
raise ConnectionError(f"Invalid reply from BL-Net: {ex}") from ex
|
|
55
|
+
except BaseException:
|
|
56
|
+
self._reset()
|
|
57
|
+
raise
|
|
58
|
+
|
|
59
|
+
return wrapper
|
|
60
|
+
|
|
61
|
+
|
|
35
62
|
class BLNETDirect(object):
|
|
36
63
|
"""
|
|
37
64
|
A class for establishing a direct connection to the BLNET (rather than
|
|
@@ -55,13 +82,14 @@ class BLNETDirect(object):
|
|
|
55
82
|
self.address = address
|
|
56
83
|
self.port = port
|
|
57
84
|
self.reset = reset
|
|
85
|
+
# 0 would make the socket non-blocking, treat it like None (no timeout)
|
|
86
|
+
self._timeout = timeout or None
|
|
58
87
|
self._mode = None
|
|
59
88
|
self._socket = None
|
|
60
89
|
self._count = None
|
|
61
90
|
self._check_mode()
|
|
62
|
-
if timeout:
|
|
63
|
-
setdefaulttimeout(timeout)
|
|
64
91
|
|
|
92
|
+
@_reset_on_error
|
|
65
93
|
def get_count(self):
|
|
66
94
|
"""
|
|
67
95
|
Get the number of datasets in the bootloader memory
|
|
@@ -90,7 +118,7 @@ class BLNETDirect(object):
|
|
|
90
118
|
self._fetch_size = 4 + 61 * frame_count
|
|
91
119
|
elif self._mode == DL_MODE:
|
|
92
120
|
(_, device, start_address, end_address, checksum) = struct.unpack(
|
|
93
|
-
"<5sB3s3sB"
|
|
121
|
+
"<5sB3s3sB", data
|
|
94
122
|
)
|
|
95
123
|
self._address_inc = 64
|
|
96
124
|
self._can_frames = 1
|
|
@@ -98,7 +126,7 @@ class BLNETDirect(object):
|
|
|
98
126
|
self._fetch_size = 65
|
|
99
127
|
elif self._mode == DL2_MODE:
|
|
100
128
|
(_, device, start_address, end_address, checksum) = struct.unpack(
|
|
101
|
-
"<5s2s3s3sB"
|
|
129
|
+
"<5s2s3s3sB", data
|
|
102
130
|
)
|
|
103
131
|
self._address_inc = 128
|
|
104
132
|
self._can_frames = 1
|
|
@@ -128,20 +156,18 @@ class BLNETDirect(object):
|
|
|
128
156
|
else:
|
|
129
157
|
raise ConnectionError("Could not retreive count")
|
|
130
158
|
|
|
159
|
+
@_reset_on_error
|
|
131
160
|
def _get_data(self, max_count=None):
|
|
132
161
|
data = []
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return data
|
|
141
|
-
finally:
|
|
142
|
-
self._end_read(False)
|
|
143
|
-
return data
|
|
162
|
+
count = self._start_read()
|
|
163
|
+
if isinstance(max_count, int):
|
|
164
|
+
count = min(max_count, count)
|
|
165
|
+
for _ in range(0, count):
|
|
166
|
+
data.append(self._fetch_data())
|
|
167
|
+
self._end_read(True)
|
|
168
|
+
return data
|
|
144
169
|
|
|
170
|
+
@_reset_on_error
|
|
145
171
|
def _check_mode(self):
|
|
146
172
|
"""
|
|
147
173
|
Check if Bootloader Mode is supported
|
|
@@ -167,9 +193,11 @@ class BLNETDirect(object):
|
|
|
167
193
|
for (family, socktype, proto, _, sockaddr) in available:
|
|
168
194
|
try:
|
|
169
195
|
self._socket = socket(family, socktype, proto)
|
|
196
|
+
self._socket.settimeout(self._timeout)
|
|
170
197
|
self._socket.connect(sockaddr)
|
|
171
198
|
break
|
|
172
|
-
except:
|
|
199
|
+
except OSError:
|
|
200
|
+
self._socket.close()
|
|
173
201
|
self._socket = None
|
|
174
202
|
if self._socket is None:
|
|
175
203
|
raise ConnectionError("Could not connect to BLNET")
|
|
@@ -181,26 +209,67 @@ class BLNETDirect(object):
|
|
|
181
209
|
self._socket.close()
|
|
182
210
|
self._socket = None
|
|
183
211
|
|
|
212
|
+
def _reset(self):
|
|
213
|
+
"""
|
|
214
|
+
Close the connection if open and forget the read state
|
|
215
|
+
"""
|
|
216
|
+
if self._socket is not None:
|
|
217
|
+
self._disconnect()
|
|
218
|
+
self._count = None
|
|
219
|
+
self._address = None
|
|
220
|
+
|
|
184
221
|
def _query(self, command, length):
|
|
185
222
|
"""
|
|
186
223
|
Send a command to the bootloader and wait for the response
|
|
187
224
|
if response is less then 32 bytes long return immediately
|
|
188
225
|
@param commmand: string only ascii (needs byte length == string length)
|
|
189
226
|
@param length: int length of response
|
|
190
|
-
@throws ConnectionError
|
|
227
|
+
@throws ConnectionError BL-Net closed the connection
|
|
191
228
|
@return Binary: byte string
|
|
192
229
|
"""
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
230
|
+
self._socket.sendall(command)
|
|
231
|
+
data = b""
|
|
232
|
+
# get response until length or less than 32 bytes
|
|
233
|
+
while True:
|
|
234
|
+
received = self._socket.recv(length)
|
|
235
|
+
if not received:
|
|
236
|
+
raise ConnectionError("BL-Net closed the connection")
|
|
237
|
+
data += received
|
|
238
|
+
if len(data) <= 32 or len(data) >= length:
|
|
239
|
+
break
|
|
240
|
+
return data
|
|
241
|
+
|
|
242
|
+
def _recv_exact(self, length):
|
|
243
|
+
"""
|
|
244
|
+
Receive exactly length bytes
|
|
245
|
+
@throws ConnectionError BL-Net closed the connection
|
|
246
|
+
@return Binary: byte string
|
|
247
|
+
"""
|
|
248
|
+
data = b""
|
|
249
|
+
while len(data) < length:
|
|
250
|
+
received = self._socket.recv(length - len(data))
|
|
251
|
+
if not received:
|
|
252
|
+
raise ConnectionError("BL-Net closed the connection")
|
|
253
|
+
data += received
|
|
254
|
+
return data
|
|
255
|
+
|
|
256
|
+
def _query_latest(self, command):
|
|
257
|
+
"""
|
|
258
|
+
Send a GET_LATEST command and receive the complete reply.
|
|
259
|
+
The BL-Net may send a reply in several TCP segments, so the length
|
|
260
|
+
is derived from its first byte and the reply is read completely.
|
|
261
|
+
Reading only part of it would leave the rest in the socket and
|
|
262
|
+
shift every following reply by some bytes.
|
|
263
|
+
@return Binary: byte string, a "no data", "wait" or data reply
|
|
264
|
+
"""
|
|
265
|
+
self._socket.sendall(command)
|
|
266
|
+
first = self._recv_exact(1)
|
|
267
|
+
if first[0] == GET_LATEST:
|
|
268
|
+
# no new data available yet
|
|
269
|
+
return first
|
|
270
|
+
if first[0] == WAIT_TIME:
|
|
271
|
+
return first + self._recv_exact(WAIT_SIZE - 1)
|
|
272
|
+
return first + self._recv_exact(self._actual_size - 1)
|
|
204
273
|
|
|
205
274
|
def _start_read(self):
|
|
206
275
|
"""
|
|
@@ -308,6 +377,7 @@ class BLNETDirect(object):
|
|
|
308
377
|
self._address = None
|
|
309
378
|
self._disconnect()
|
|
310
379
|
|
|
380
|
+
@_reset_on_error
|
|
311
381
|
def get_latest(self, max_retries=MAX_RETRYS):
|
|
312
382
|
"""
|
|
313
383
|
Fetch latest (current) data from the BLNet
|
|
@@ -318,7 +388,7 @@ class BLNETDirect(object):
|
|
|
318
388
|
self._connect()
|
|
319
389
|
self.get_count()
|
|
320
390
|
frames = {}
|
|
321
|
-
info = {"sleep": {}, "got": {}}
|
|
391
|
+
info = {"sleep": {}, "got": {}, "timeout": []}
|
|
322
392
|
|
|
323
393
|
# for all frames
|
|
324
394
|
for frame in range(0, self._can_frames):
|
|
@@ -327,9 +397,12 @@ class BLNETDirect(object):
|
|
|
327
397
|
# try 4 times
|
|
328
398
|
sleeps = []
|
|
329
399
|
for n in range(0, max_retries):
|
|
330
|
-
data = self.
|
|
400
|
+
data = self._query_latest(command)
|
|
331
401
|
|
|
332
|
-
if
|
|
402
|
+
if data == bytes([GET_LATEST]):
|
|
403
|
+
# no new data available yet ("Synchronisation" in TA's spec)
|
|
404
|
+
sleep(NO_DATA_WAIT)
|
|
405
|
+
elif self._checksum(data):
|
|
333
406
|
binary = struct.unpack("<{}".format("B" * len(data)), data)
|
|
334
407
|
|
|
335
408
|
if binary[0] == WAIT_TIME:
|
|
@@ -342,9 +415,14 @@ class BLNETDirect(object):
|
|
|
342
415
|
info["got"][frame] = n
|
|
343
416
|
frames.update(self._split_latest(data, frame))
|
|
344
417
|
break
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
418
|
+
else:
|
|
419
|
+
# Unexpected bytes may still be on their way, reconnect
|
|
420
|
+
# so that they can't end up in the next reply
|
|
421
|
+
self._disconnect()
|
|
422
|
+
self._connect()
|
|
423
|
+
else:
|
|
424
|
+
# BL-Net did not send data for this frame within max_retries
|
|
425
|
+
info["timeout"].append(frame)
|
|
348
426
|
info["sleep"][frame] = sleeps
|
|
349
427
|
self._end_read(True)
|
|
350
428
|
if len(frames) > 0:
|
|
@@ -30,10 +30,28 @@ TYPE_VOLUME = 0x3000
|
|
|
30
30
|
TYPE_RADIATION = 0x4000
|
|
31
31
|
TYPE_RAS = 0x7000
|
|
32
32
|
RAS_POSITIVE_MASK = 0x01FF
|
|
33
|
+
# Temperature input without sensor (or with a broken wire): all value bits set, 409.5 °C
|
|
34
|
+
TEMP_NO_SENSOR = TYPE_TEMP | POSITIVE_VALUE_MASK
|
|
33
35
|
# 32 bit
|
|
34
36
|
INT32_MASK = 0xFFFFFFFF
|
|
35
37
|
INT32_SIGN = 0x80000000
|
|
36
38
|
|
|
39
|
+
# Names of the analog input types, unused and undocumented types have none
|
|
40
|
+
ANALOG_TYPES = {
|
|
41
|
+
TYPE_DIGITAL: "digital",
|
|
42
|
+
TYPE_TEMP: "temperature",
|
|
43
|
+
TYPE_VOLUME: "volume_flow",
|
|
44
|
+
TYPE_RADIATION: "radiation",
|
|
45
|
+
TYPE_RAS: "temperature",
|
|
46
|
+
}
|
|
47
|
+
# Units of the analog input types, as the web interface shows them
|
|
48
|
+
ANALOG_UNITS = {
|
|
49
|
+
"digital": None,
|
|
50
|
+
"temperature": "°C",
|
|
51
|
+
"volume_flow": "l/h",
|
|
52
|
+
"radiation": "W/m²",
|
|
53
|
+
}
|
|
54
|
+
|
|
37
55
|
|
|
38
56
|
class BLNETParser:
|
|
39
57
|
def __init__(self, data):
|
|
@@ -71,8 +89,11 @@ class BLNETParser:
|
|
|
71
89
|
analog = struct.unpack("<{}{}".format("H" * 16, "x" * (len(data) - 32)), data)
|
|
72
90
|
|
|
73
91
|
self.analog = {}
|
|
92
|
+
self.analog_types = {}
|
|
74
93
|
for channel in range(0, 16):
|
|
75
|
-
self.
|
|
94
|
+
self.analog_types[channel + 1] = ANALOG_TYPES.get(analog[channel] & TYPE_MASK)
|
|
95
|
+
value = self._convert_analog(analog[channel])
|
|
96
|
+
self.analog[channel + 1] = None if value is None else round(value, 3)
|
|
76
97
|
|
|
77
98
|
self.digital = {}
|
|
78
99
|
for channel in range(0, 16):
|
|
@@ -107,7 +128,12 @@ class BLNETParser:
|
|
|
107
128
|
"""
|
|
108
129
|
|
|
109
130
|
mask = value & TYPE_MASK
|
|
110
|
-
if mask ==
|
|
131
|
+
if mask == TYPE_NONE:
|
|
132
|
+
# input is unused
|
|
133
|
+
return None
|
|
134
|
+
elif value == TEMP_NO_SENSOR:
|
|
135
|
+
return None
|
|
136
|
+
elif mask == TYPE_TEMP:
|
|
111
137
|
return self._calculate_value(value, 0.1)
|
|
112
138
|
elif mask == TYPE_VOLUME:
|
|
113
139
|
return self._calculate_value(value, 4)
|
|
@@ -118,7 +144,8 @@ class BLNETParser:
|
|
|
118
144
|
return 0
|
|
119
145
|
elif mask == TYPE_RAS:
|
|
120
146
|
return self._calculate_value(value, 0.1, RAS_POSITIVE_MASK)
|
|
121
|
-
|
|
147
|
+
else:
|
|
148
|
+
# TYPE_RADIATION and types not covered by the interface spec
|
|
122
149
|
return self._calculate_value(value)
|
|
123
150
|
|
|
124
151
|
def _convert_digital(self, value, position):
|
|
@@ -144,7 +171,7 @@ class BLNETParser:
|
|
|
144
171
|
Check if heat meter is activated on a given position
|
|
145
172
|
@return its energy
|
|
146
173
|
"""
|
|
147
|
-
if active & position:
|
|
174
|
+
if active & (1 << position):
|
|
148
175
|
kwh = self._calculate_value(kwh, 0.1, INT16_POSITIVE_MASK)
|
|
149
176
|
return round(mwh * 1000 + kwh, 3)
|
|
150
177
|
else:
|
|
@@ -155,7 +182,7 @@ class BLNETParser:
|
|
|
155
182
|
checks if heat meter is activated at given position
|
|
156
183
|
@return its power
|
|
157
184
|
"""
|
|
158
|
-
if active & position:
|
|
185
|
+
if active & (1 << position):
|
|
159
186
|
value = self._calculate_value(value, 1 / 2560, INT32_MASK, INT32_SIGN)
|
|
160
187
|
value = round(value, 3)
|
|
161
188
|
return value
|
|
@@ -10,10 +10,74 @@ via it's HTTP-interface
|
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
import requests
|
|
13
|
-
from htmldom import htmldom
|
|
14
13
|
import html
|
|
15
14
|
import re
|
|
16
15
|
from builtins import int
|
|
16
|
+
from html.parser import HTMLParser
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class _ElementTextParser(HTMLParser):
|
|
20
|
+
"""
|
|
21
|
+
Collects the text of all elements with the given tag and class or id.
|
|
22
|
+
Entities like are kept as written and <br> becomes a newline,
|
|
23
|
+
which is what the regular expressions below expect.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, tag, cls=None, id=None):
|
|
27
|
+
super().__init__(convert_charrefs=False)
|
|
28
|
+
self.tag = tag
|
|
29
|
+
self.cls = cls
|
|
30
|
+
self.id = id
|
|
31
|
+
# text of each matching element, in document order
|
|
32
|
+
self.texts = []
|
|
33
|
+
# for each open element with self.tag: its index in self.texts,
|
|
34
|
+
# or None if it doesn't match. Matching elements can be nested.
|
|
35
|
+
self._open = []
|
|
36
|
+
|
|
37
|
+
def _matches(self, attrs):
|
|
38
|
+
attrs = dict(attrs)
|
|
39
|
+
return (self.cls is None or self.cls in (attrs.get("class") or "").split()) and (
|
|
40
|
+
self.id is None or attrs.get("id") == self.id
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
def handle_starttag(self, tag, attrs):
|
|
44
|
+
if tag == self.tag:
|
|
45
|
+
if self._matches(attrs):
|
|
46
|
+
self.texts.append("")
|
|
47
|
+
self._open.append(len(self.texts) - 1)
|
|
48
|
+
else:
|
|
49
|
+
self._open.append(None)
|
|
50
|
+
elif tag == "br":
|
|
51
|
+
self.handle_data("\n")
|
|
52
|
+
|
|
53
|
+
def handle_endtag(self, tag):
|
|
54
|
+
if tag == self.tag and self._open:
|
|
55
|
+
self._open.pop()
|
|
56
|
+
|
|
57
|
+
def handle_data(self, data):
|
|
58
|
+
for index in self._open:
|
|
59
|
+
if index is not None:
|
|
60
|
+
self.texts[index] += data
|
|
61
|
+
|
|
62
|
+
def handle_entityref(self, name):
|
|
63
|
+
self.handle_data(f"&{name};")
|
|
64
|
+
|
|
65
|
+
def handle_charref(self, name):
|
|
66
|
+
self.handle_data(f"&#{name};")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _element_texts(page, tag, cls=None, id=None):
|
|
70
|
+
"""Texts of all elements in page with the given tag and class or id"""
|
|
71
|
+
parser = _ElementTextParser(tag, cls, id)
|
|
72
|
+
parser.feed(page)
|
|
73
|
+
parser.close()
|
|
74
|
+
return parser.texts
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _element_text(page, tag, cls=None, id=None):
|
|
78
|
+
"""Text of the first such element, empty if there is none"""
|
|
79
|
+
texts = _element_texts(page, tag, cls, id)
|
|
80
|
+
return texts[0] if texts else ""
|
|
17
81
|
|
|
18
82
|
|
|
19
83
|
def blnet_test(ip, timeout=5, id=0):
|
|
@@ -28,13 +92,11 @@ def blnet_test(ip, timeout=5, id=0):
|
|
|
28
92
|
r = requests.get(ip, timeout=timeout)
|
|
29
93
|
except requests.exceptions.RequestException:
|
|
30
94
|
return False
|
|
31
|
-
# Parse DOM object from HTMLCode
|
|
32
|
-
dom = htmldom.HtmlDom().createDom(r.text)
|
|
33
95
|
# either a 'Zugriff verweigert' message is shown
|
|
34
|
-
if "BL-Net".lower() in
|
|
96
|
+
if "BL-Net".lower() in _element_text(r.text, "title").lower():
|
|
35
97
|
return True
|
|
36
98
|
# or (more often) the BL-NET Menü
|
|
37
|
-
if "BL-NET" in
|
|
99
|
+
if "BL-NET" in _element_text(r.text, "div", id="head"):
|
|
38
100
|
return True
|
|
39
101
|
return False
|
|
40
102
|
|
|
@@ -183,18 +245,12 @@ class BLNETWeb(object):
|
|
|
183
245
|
)
|
|
184
246
|
except requests.exceptions.RequestException:
|
|
185
247
|
return None
|
|
186
|
-
# Parse DOM object from HTMLCode
|
|
187
|
-
dom = htmldom.HtmlDom().createDom(r.text)
|
|
188
|
-
# Check if we didn't fail in access
|
|
189
|
-
# if 'BL-Net Zugang verweigert' in dom.find('title').text():
|
|
190
|
-
# return None
|
|
191
248
|
# get the element containing the interesting information
|
|
192
|
-
|
|
249
|
+
texts = _element_texts(r.text, "div", cls="c")
|
|
193
250
|
# in case of access denied or other errors, return None
|
|
194
|
-
if
|
|
195
|
-
return
|
|
196
|
-
|
|
197
|
-
data_raw = dom.text()
|
|
251
|
+
if len(texts) < 2:
|
|
252
|
+
return None
|
|
253
|
+
data_raw = texts[1]
|
|
198
254
|
|
|
199
255
|
# collect data in an array
|
|
200
256
|
data = list()
|
|
@@ -234,18 +290,14 @@ class BLNETWeb(object):
|
|
|
234
290
|
)
|
|
235
291
|
except requests.exceptions.RequestException:
|
|
236
292
|
return None
|
|
237
|
-
# Parse DOM object from HTMLCode
|
|
238
|
-
dom = htmldom.HtmlDom().createDom(r.text)
|
|
239
293
|
# Check if we didn't fail in access
|
|
240
|
-
if "BL-Net Zugang verweigert" in
|
|
294
|
+
if "BL-Net Zugang verweigert" in _element_text(r.text, "title"):
|
|
241
295
|
return None
|
|
242
296
|
# get the element containing the interesting information
|
|
243
|
-
|
|
244
|
-
if
|
|
297
|
+
texts = _element_texts(r.text, "div", cls="c")
|
|
298
|
+
if len(texts) < 2:
|
|
245
299
|
return None
|
|
246
|
-
|
|
247
|
-
# filter out the text
|
|
248
|
-
data_raw = dom.text()
|
|
300
|
+
data_raw = texts[1]
|
|
249
301
|
|
|
250
302
|
# collect data in an array
|
|
251
303
|
data = list()
|
|
@@ -4,31 +4,28 @@ build-backend = "flit_core.buildapi"
|
|
|
4
4
|
|
|
5
5
|
[ project ]
|
|
6
6
|
name = "PyBLNET"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.11.0"
|
|
8
8
|
description = "Automate wireless communication to UVR1611 via BL-NET"
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
|
|
11
|
-
requires-python = ">=3"
|
|
10
|
+
requires-python = ">=3.10"
|
|
12
11
|
license = {text = "MIT"}
|
|
13
12
|
authors = [
|
|
14
13
|
{name = "nielstron", email = "n.muendler@posteo.de"}
|
|
15
14
|
]
|
|
16
|
-
homepage = "https://github.com/nielstron/pyblnet/"
|
|
17
15
|
keywords = ["python", "uvr1611", "blnet", "technische alternative", "home automation", "iot"]
|
|
18
16
|
classifiers = [
|
|
19
17
|
"Development Status :: 4 - Beta",
|
|
20
18
|
"Intended Audience :: Developers",
|
|
21
19
|
"Topic :: Software Development :: Object Brokering",
|
|
22
20
|
"License :: OSI Approved :: MIT License",
|
|
23
|
-
"Programming Language :: Python :: 3.8",
|
|
24
|
-
"Programming Language :: Python :: 3.9",
|
|
25
21
|
"Programming Language :: Python :: 3.10",
|
|
26
22
|
"Programming Language :: Python :: 3.11",
|
|
27
23
|
"Programming Language :: Python :: 3.12",
|
|
28
|
-
"Programming Language :: Python :: 3.13"
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Programming Language :: Python :: 3.15"
|
|
29
27
|
]
|
|
30
28
|
dependencies = [
|
|
31
|
-
"htmldom",
|
|
32
29
|
"requests",
|
|
33
30
|
]
|
|
34
31
|
|
|
@@ -38,7 +35,7 @@ test = [
|
|
|
38
35
|
]
|
|
39
36
|
|
|
40
37
|
[ project.urls ]
|
|
41
|
-
"Homepage" = "https://github.com/
|
|
38
|
+
"Homepage" = "https://github.com/PyBLNET/pyblnet/"
|
|
42
39
|
|
|
43
40
|
[ tool.flit.module ]
|
|
44
41
|
name = "pyblnet"
|