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
| import atexit
import socket
import string
import struct
import datetime
import requests
import file_cache
# Luxtronik 2.0 hostname
hostHeatpump = 'heatpump.toaomatis.nl'
# Luxtronik 2.0 port (standard 8888)
portHeatpump = 8889
# Domoticz hostname
domoticzHostname = 'domoticz.toaomatis.nl'
cache_file = 'heatpump_domoticz.json'
# Empty variables
leng = 0
array_calculated = []
def to_float(index: int, divider: float = 1.0) -> float:
return float(float(array_calculated[index]) / divider)
def to_int(index: int, divider: int = 1) -> int:
return int(int(array_calculated[index]) / divider)
def to_timedelta(index: int) -> datetime.timedelta:
return datetime.timedelta(seconds=array_calculated[index])
def to_domoticz_state(index: int) -> int:
# States
# 0 = Heizen
# 1 = Warmwasser
# 2 = Schwimmbad / Photovoltaik
# 3 = EVU
# 4 = Abtauen
# 5 = Keine Anforderung
# 6 = Heizen ext. Energiequelle
# 7 = Kühlbetrieb
state = to_int(index)
if state == 0:
# Verwarming
return 10
if state == 1:
# Heet water
return 20
if state == 7:
# Koeling
return 30
# Uit / onbekend
return 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostHeatpump, portHeatpump))
s.send(struct.pack('!i', 3004))
s.send(struct.pack('!i', 0))
if struct.unpack('!i', s.recv(4))[0] != 3004:
print("Error: REQ_CALCULATED CMD")
exit()
stat = struct.unpack('!i', s.recv(4))[0]
leng = struct.unpack('!i', s.recv(4))[0]
for i in range(leng):
array_calculated.append(struct.unpack('!i', s.recv(4))[0])
# See https://github.com/openhab/openhab1-addons/blob/master/bundles/binding/org.openhab.binding.novelanheatpump/src/main/java/org/openhab/binding/novelanheatpump/internal/HeatPumpBinding.java#L148 for Luxtronik indexes
readings = [
{
'description': 'Woonkamer',
'heatpump_index': 227,
'domoticz_index': 2688,
'callback': to_float,
'args': [10],
}, {
'description': 'Aanvoer',
'heatpump_index': 10,
'domoticz_index': 2689,
'callback': to_float,
'args': [10],
}, {
'description': 'Retour',
'heatpump_index': 11,
'domoticz_index': 2690,
'callback': to_float,
'args': [10],
}, {
'description': 'Retour berekend',
'heatpump_index': 12,
'domoticz_index': 2691,
'callback': to_float,
'args': [10],
}, {
'description': 'Buitentemperatuur',
'heatpump_index': 15,
'domoticz_index': 2692,
'callback': to_float,
'args': [10],
}, {
'description': 'Gemiddelde temperatuur',
'heatpump_index': 16,
'domoticz_index': 2693,
'callback': to_float,
'args': [10],
}, {
'description': 'Tapwater gemeten',
'heatpump_index': 17,
'domoticz_index': 2694,
'callback': to_float,
'args': [10],
}, {
'description': 'Tapwater ingesteld',
'heatpump_index': 18,
'domoticz_index': 2695,
'callback': to_float,
'args': [10],
}, {
'description': 'Bron-in',
'heatpump_index': 19,
'domoticz_index': 2696,
'callback': to_float,
'args': [10],
}, {
'description': 'Bron-uit',
'heatpump_index': 20,
'domoticz_index': 2697,
'callback': to_float,
'args': [10],
}, {
'description': 'Warmtepomp',
'heatpump_index': 80,
'domoticz_index': 2699,
'callback': to_domoticz_state,
},
]
values = dict()
cache_values = file_cache.load_cache(cache_file)
atexit.register(file_cache.save_cache, filename=cache_file, values=values)
for reading in readings:
description = reading['description']
heatpump_index = reading['heatpump_index']
domoticz_index = reading['domoticz_index']
callback = reading['callback']
args = reading.get('args', [])
cache_key = str(heatpump_index)
value = callback(heatpump_index, *args)
values[cache_key] = value
cache_value = file_cache.get_value(cache_values, cache_key)
if cache_value == value:
print('{}[{}]: {} is same as {}, no need for update'.format(description, cache_key, value, cache_value))
continue
uri = 'http://{}/json.htm?type=command¶m=udevice&idx={}&svalue={}'.format(
domoticzHostname, domoticz_index, value)
if True:
r = requests.get(uri)
print('{}: {} [{}]'.format(description, value, r.status_code))
else:
print('{}: {} '.format(description, value)) |