forked from kayr7/ParadigmaHeatingReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparadigma.py
297 lines (219 loc) · 10.6 KB
/
paradigma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import socket
import sys
from itertools import takewhile
from datetime import datetime
import time
import argparse
import paho.mqtt.client as mqtt
class Debugger(object):
def __init__(self, verbose):
self.verbose = verbose
def debugData(self, data, i):
if self.verbose > 0:
filtered_characters = ' '.join(list(chr(s) if chr(s).isprintable() else '.' for s in data))
d = ' '.join(["{:02x}".format(x) for x in data])
offset = 0
print(f'{i}:')
while offset * 3 < len(d):
print(f'{d[offset*3:(offset*3)+24]} {d[offset*3+24:offset*3+48]}\t{filtered_characters[offset*2:(offset*2) + 16]} {filtered_characters[offset*2 + 16 : offset*2 + 32]}')
offset = offset + 16
def extract_value(data:str, marker:str, unit:str = None, formatter = None):
offset = data.find(marker)
if offset < 0:
return None
value = "".join([chr(x) for x in list(takewhile(lambda x: x != 0, data[offset+len(marker):]))]).strip()
if unit is not None:
if value[-(len(unit)):] != unit:
value = None
else:
value = value[:-(len(unit))].strip()
if value is not None and formatter is not None:
value = formatter(value)
return value
def extract_temperature(data:str, marker:str):
return extract_value(data, marker, '\xb0\x43', lambda x: x.replace(',', '.'))
CONNECT_MSG = bytes.fromhex('08 00 00 00 00 01 31 32 33 34')
class HeatingConnector(object):
def __init__(self, ip, port, verbose):
self.controller = (ip, port)
self.token = None
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.settimeout(10)
self.data = {}
self.debugger = Debugger(verbose)
def recv(self):
data, _address = self.s.recvfrom(2048)
self.token = data[:5]
return data
def _send(self, data):
message = self.token + data if self.token else data
#print(message.hex())
self.s.sendto(message, self.controller)
def send(self, data, send_intermediate=False):
if send_intermediate:
self._send(bytes.fromhex('00 02 00 ff ff ff ff ff 00 00 ff 1f 00 00'))
self.debugger.debugData(self.recv(), "INTERMEDIATE")
self._send(data)
def extract_main_menu_info(self, data):
self.data["time"] = extract_value(data, b'\x12\x2c\x01\x56\x00')
self.data["temp-indoor"] = extract_temperature(data, b'\x12\x2c\x01\x94\x00')
self.data["temp-outdoor"] = extract_temperature(data, b'\x12\x2c\x01\xcb\x00')
def connect(self):
self.s.sendto(CONNECT_MSG, self.controller)
data = self.recv()
assert len(data) == 7
#assert data == bytes.fromhex('08 00 00 00 00 01 01')
# check if device is in use
assert data != bytes.fromhex('08 00 00 00 00 01 fe')
self.debugger.debugData(data, "CONNECT_1")
data = self.recv()
assert len(data) == 6
self.debugger.debugData(data, "CONNECT_2")
self.send(bytes.fromhex('f0 01 16 00 01 14 00 02'))
data = self.recv()
assert len(data) == 6
self.debugger.debugData(data, "CONNECT_3")
self.send(bytes.fromhex('f3 00 00 03 03 04 0e ff ff ff ff 74 56 01 00 56 72 41 b5'))
data = self.recv()
assert len(data) == 10
assert data == bytes.fromhex('01 f7 00 f7 00 f7 03 00 00 00')
self.debugger.debugData(data, "CONNECT_4")
# is this now the last of the initialization of the connection?
#message = data[:6] + bytes.fromhex('00 01 00 00 00')
#self.s.sendto(message, self.controller)
self.send(bytes.fromhex('f7 00 01 00 00 00'))
data = self.recv()
self.debugger.debugData(data, "CONNECT_5")
#assert data[5:8] == bytes.fromhex('80 0a 00')
if len(data) > 16: # temperatures and time are sometimes shown early
self.extract_main_menu_info(data)
def main_menu(self):
self.send(bytes.fromhex('00 16 00 ff 13 00 dd 00 00 00 83 1f 00 00'))
# NOTE this additional message with slightly changed content IS NEEDED, otherwise it's not going to switch!
data = self.recv()
self.debugger.debugData(data, "MAIN_MENU_1")
if len(data) > 16:
self.extract_main_menu_info(data)
self.send(bytes.fromhex('00 02 00 1e ff ff ff ff 00 00 ff 1f 00 00'))
data = self.recv()
self.debugger.debugData(data, "MAIN_MENU_2")
def return_to_main_menu(self):
self.send(bytes.fromhex('00 02 00 08 17 01 0f 00 00 00 ff 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "RETURN_TO_MAIN_MENU")
def water(self):
self.send(bytes.fromhex('00 14 00 20 ff ff ff ff 00 00 61 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "WARMWASSER")
self.data["water-temp"] = extract_temperature(data, b'\x12\x2c\x01\x34\x00')
self.data["water-target-temp"] = extract_temperature(data, b'\x12\x2c\x01\x56\x00')
self.return_to_main_menu()
def solar(self):
self.send(bytes.fromhex('00 02 00 1f c8 00 4a 00 00 00 ff 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "SOLAR")
self.data["total-kwh"] = extract_value(data, b'\x12\x2c\x01\xbc\x00', "kWh")
self.data["today-kwh"] = extract_value(data, b'\x12\x2c\x01\x9a\x00', "kWh")
self.data["current-panel-temp"] = extract_temperature(data, b'\x12\x2c\x01\x56\x00')
self.data["max-panel-temp"] = extract_temperature(data, b'\x12\x2c\x01\x78\x00')
self.return_to_main_menu()
def boiler(self):
self.send(bytes.fromhex('00 14 00 25 80 00 c0 00 00 00 61 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "KESSEL")
self.data["boiler-in-temp"] = extract_temperature(data, b'\x12\x4b\x00\xa3\x00')
self.data["boiler-out-temp"] = extract_temperature(data, b'\x12\x4b\x00\x4c\x00')
self.data["boiler-state"] = extract_value(data, b'\x12\xf0\x00\x65\x00')
self.send(bytes.fromhex('00 02 00 11 29 01 e6 00 00 00 ff 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "KESSEL_INFO_1")
self.send(bytes.fromhex('00 07 00 16 27 01 e2 00 00 00 ec 1f 00 00'), True)
data = self.recv()
self.debugger.debugData(data, "KESSEL_INFO_2")
self.data["boiler-start-count"] = extract_value(data, b'\x12\x2c\x01\x85\x00')
self.data["boiler-runtime"] = extract_value(data, b'\x12\x2c\x01\x4c\x00', " h")
self.return_to_main_menu()
def buffer(self):
self.send(bytes.fromhex("00 14 00 22 22 01 53 00 00 00 61 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "BUFFER")
self.data["buffer-top-temp"] = extract_temperature(data, b'\x12\x7d\x00\x4c\x00')
self.data["buffer-bottom-temp"] = extract_temperature(data, b'\x12\x7d\x00\xa3\x00')
self.return_to_main_menu()
def error(self):
self.send(bytes.fromhex("00 14 00 23 33 00 bb 00 00 00 61 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "ERROR_1")
self.send(bytes.fromhex("00 07 00 15 80 00 9e 00 00 00 ec 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "ERROR_2")
self.send(bytes.fromhex("00 07 00 15 80 00 9e 00 00 00 ec 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "ERROR_SENSOR")
self.data["error-sensor"] = extract_value(data, b'\x12\x2c\x01\x86\x00')
self.send(bytes.fromhex("00 0b 00 05 c6 00 e6 00 00 00 d6 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "ERROR_SOLAR")
self.data["error-solar"] = extract_value(data, b'\x12\x2c\x01\x86\x00')
self.send(bytes.fromhex("00 0b 00 05 c6 00 e6 00 00 00 d6 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "ERROR_WATER")
self.data["error-water"] = extract_value(data, b'\x12\x2c\x01\x86\x00')
self.send(bytes.fromhex("00 0b 00 05 c6 00 e6 00 00 00 d6 1f 00 00"), True)
data = self.recv()
self.debugger.debugData(data, "WARNING_WATER")
self.data["warning-water"] = extract_value(data, b'\x12\x2c\x01\x86\x00')
self.return_to_main_menu()
def close(self):
self.s.close()
for key, value in self.data.items():
if value is not None:
print(f'{key}: {value}')
def send_mqtt(self, client):
for key, value in self.data.items():
if value is not None:
client.publish(f"paradigma/systacomfort/{key}", value)
def main(args):
def on_disconnect(client, userdata, rc):
client.reconnect()
#mqttClient = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttClient = mqtt.Client("paradigma")
mqttClient.on_disconnect = on_disconnect
if args.mqttuser is not None:
mqttClient.username_pw_set(args.mqttuser, args.mqttpass)
if args.mqtthost is not None:
mqttClient.connect(args.mqtthost, port=args.mqttport)
mqttClient.loop_start()
while True:
try:
hc = HeatingConnector(args.host, args.port, args.verbosity)
hc.connect()
hc.main_menu()
hc.water()
hc.solar()
hc.boiler()
hc.buffer()
hc.error()
hc.close()
if mqttClient.is_connected():
hc.send_mqtt(mqttClient)
except Exception as e:
print(f'Error happened while requesting: {e}')
time.sleep(60)
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbosity", help="output debug information of the messages received", default=0, action="count")
parser.add_argument("-t", "--host", help="IP address of the heating control")
parser.add_argument("-p", "--port",
type=int,
help="Port the Heating Controller is listening on (default: 3477)",
default=3477)
parser.add_argument("--mqtthost", help="Host of the mqtt broker the data to send to", default=None)
parser.add_argument("--mqttport", help="the port of the mqtt broker to send the data to", default=1883, type=int)
parser.add_argument("--mqttuser", help="username for the mqtt broker the data to send to", default=None)
parser.add_argument("--mqttpass", help="password for the mqtt broker the data to send to", default=None)
args = parser.parse_args()
if args.host is None:
print('you need to at least provide a parameter for the ip of the heating controller')
else:
main(args)