-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenWebNet.py
340 lines (267 loc) · 11.2 KB
/
OpenWebNet.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import sys
import pprint
import re
import time
class OpenWebNet: # Définition de notre classe OpenWebNet
"""
OpenWebNet class Connector .
"""
REQUEST_MODE = "*99*9##"
ACK_OK = "*#*1##"
ACK_NOK = "*#*0##"
LIGHT_SENSOR_WHO = "1"
TEMPERATURE_SENSOR_WHO = "4"
SENSOR_TYPE_LIGHT = "light"
SENSOR_TYPE_TEMPERATURE = "temperature"
DIALOG_TEMPO = 0.1
def __init__(self, host, port):
"""
OpenWebNet class constructor .
please provwheree :
host : tcp-ip host to connect to F454 Legrand gateway (usually 192.168.1.35)
port : tcp-ip port to connect to F454 Legrand gateway (usually 20000)
"""
self.host = host
self.port = int(port)
self.gateway_addr_port = host, int(port)
def dump(self):
"""
Dumps host and port informations for debug.
"""
print "host = " + self.host
print "port = " + str(self.port)
def connect(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(30) # 5 seconds
sock.connect(self.gateway_addr_port)
connect_ack = sock.recv(1024)
# print "connect : " + connect_ack
if (connect_ack == self.ACK_OK):
# print "send request = " + s
sock.send(self.REQUEST_MODE)
# print "send request mode "
connect_ack = sock.recv(1024)
# print "result send request mode : " + connect_ack
if (connect_ack == self.ACK_OK):
return sock
except socket.error as msg:
print "Caught exception socket.error : %s" % msg
sock.close()
return False
return False
def findLightSensors(self, range):
"""
Lists the Temperature Sensors within provwhereed range (for example range(0,10))
"""
result = []
for i in range :
response = self.sendRequestStatus(self.LIGHT_SENSOR_WHO, i)
if (response != self.ACK_NOK):
result.append(i)
return result
def getLightSensorStatus(self, where):
"""
returns the data in a structured dict for a given light where sensor
"""
response = self.sendRequestStatus(self.LIGHT_SENSOR_WHO, where)
result = self.analyze_response_for_LightSensorData(where, response)
return result
def setLightSensorStatus(self, where, value):
"""
syntax : *1*where*T*##
exemple for
switch on light 11 : *1*1*11##
switch off light 11 : *1*0*11##
"""
response = self.sendRequestSetStatus(self.LIGHT_SENSOR_WHO, where, value, "*{who}*{value}*{where}##")
return response
def findTemperatureSensors(self, range):
"""
Lists the Temperature Sensors within provwhereed range (for example range(0,10))
"""
resultat = []
for i in range :
response = self.sendRequestStatus(self.TEMPERATURE_SENSOR_WHO, i)
if (response != self.ACK_NOK):
resultat.append(i)
return resultat
def getTemperaturesSensorStatus(self, where):
"""
returns the data in a structured dict for a given temperature where sensor
"""
response = self.sendRequestStatus(self.TEMPERATURE_SENSOR_WHO, where)
result = self.analyze_response_for_TemperatureSensorData(where, response)
return result
def setTemperatureSensorSetPointIn99ZonesMainUnit(self, where, value):
"""
Example:
You set up 10 zone at 21.5°C in heating mode with a 99Zones main unit : example : *#4*#0#10*#14*0215*1## (99 zones main unit: 0, zone 10 at 21.5 heating mode)
call function with paramaters : where = 10, value = 0215
how to change centrale temperature : *#4*#0#1*#14*0225*3## (#0 is for centrale, #1 is for centrale 1 (za et zb), 0225 is for 22.5 °
must force centrale on manual)
"""
dim = self.convert_temperature_to_dim(value)
response = self.sendRequestSetStatus(self.TEMPERATURE_SENSOR_WHO, where, dim, "*#{who}*#0#{where}*#14*{value}*1##")
return response
def setTemperatureSensorSetPoint(self, where, value):
"""
The Temperature can be changed directly ony if the sensor/probe is declared as a "external probe" in my Home Suite
"""
dim = self.convert_temperature_to_dim(value)
response = self.sendRequestSetStatus(self.TEMPERATURE_SENSOR_WHO, where, dim, "*#{who}*{where}*#14*{value}*3##")
return response
def setTemperatureSensorSetOffMode(self, where):
"""
Syntax : *4*303*where##
Example:
*4*303*4##
"""
value = ""
response = self.sendRequestSetStatus(self.TEMPERATURE_SENSOR_WHO, where, value, "*4*303*{where}##")
return response
def setTemperatureSensorSetAntiFreezeMode(self, where):
"""
Syntax : *4*303*where##
Example:
*4*303*4##
"""
value = ""
response = self.sendRequestSetStatus(self.TEMPERATURE_SENSOR_WHO, where, value, "*4*102*{where}##")
return response
def sendRequestStatus(self, who, where):
"""
Connect to gateway
send request
readresponse (a long string)
"""
global_result = {}
s = "*#"+str(who)+"*"+str(where)+"##"
scrap_succeeded = False
must_read = True
response = ""
try:
sock = self.connect()
if (sock != False):
sock.send(s)
while (must_read):
next = sock.recv(1024) # now read data from MyHome BUS
response = response + next
eom = response.find(self.ACK_OK)
eom2 = response.find(self.ACK_NOK)
if (eom >=0 or eom2 >=0): # read until next "##"
scrap_succeeded = True
must_read = False
sock.close()
except socket.error as msg:
print "Caught exception socket.error : %s" % msg
sock.close()
return ""
time.sleep(self.DIALOG_TEMPO)
return response
def sendRequestSetStatus(self, who, where, value, pattern):
"""
Connect to gateway
send request
readresponse (a long string)
"""
global_result = {}
s = str(pattern)
s = s.replace("{who}", str(who))
s = s.replace("{where}", str(where))
s = s.replace("{value}", str(value))
# s = "*"+str(who)+"*"+str(value)+"*"+str(where)+"##"
scrap_succeeded = False
must_read = True
response = ""
try:
sock = self.connect()
if (sock != False):
# print "send request = " + s
sock.send(s)
while (must_read):
next = sock.recv(1024) # now read data from MyHome BUS
response = response + next
eom = response.find(self.ACK_OK)
eom2 = response.find(self.ACK_NOK)
if (eom >=0 or eom2 >=0): # read until next "##"
scrap_succeeded = True
must_read = False
sock.close()
except socket.error as msg:
print "Caught exception socket.error : %s" % msg
sock.close()
return ""
# print "response set status " + response
time.sleep(self.DIALOG_TEMPO)
return response
def analyze_response_for_LightSensorData(self, where, response):
"""
extracts informations for a given where sensor and a response from sendRequestStatus function
"""
result = {}
sensor_light = 0
sensor_ack = ""
while (response.find("##") >= 0):
eom = response.find("##")
msg = response[0:eom+2] # message is from position 0 until end of ##
response = response[eom+2:] # next message starts after ##
sensor_light_search = re.search('\*'+self.LIGHT_SENSOR_WHO+'\*([0-9]*)\*[0-9]*##', msg)
if sensor_light_search:
sensor_light = sensor_light_search.group(1)
# print "sensor_light = " + sensor_light
sensor_ack_search = re.search('(\*#\*[0-1]##)', msg)
if sensor_ack_search:
sensor_ack = sensor_ack_search.group(1)
if (sensor_ack == self.ACK_OK):
sensor_ack = "ok"
if (sensor_ack == self.ACK_NOK):
sensor_ack = "nok"
result['SENSOR-WHERE'] = where
result['SENSOR-TYPE'] = self.SENSOR_TYPE_LIGHT
result['LIGHT'] = sensor_light
result['ACK'] = sensor_ack
return result
def analyze_response_for_TemperatureSensorData(self, where, response):
"""
extracts informations for a given where sensor and a response from sendRequest function
"""
result = {}
sensor_temperature = 0
consigne_temperature = 0
sensor_ack = ""
while (response.find("##") >= 0):
eom = response.find("##")
msg = response[0:eom+2] # message is from position 0 until end of ##
response = response[eom+2:] # next message starts after ##
sensor_temperature_search = re.search('\*#'+self.TEMPERATURE_SENSOR_WHO+'\*[0-9]\*0\*([0-9]*)##', msg)
if sensor_temperature_search:
sensor_temperature = sensor_temperature_search.group(1)
# print "sensor_temperature = " + sensor_temperature
sensor_temperature_consigne = re.search('\*#4\*[0-9]\*12\*([0-9]*).*##', msg)
if sensor_temperature_consigne:
consigne_temperature = sensor_temperature_consigne.group(1)
# print "consigne_temperature = " + consigne_temperature
sensor_ack_search = re.search('(\*#\*[0-1]##)', msg)
if sensor_ack_search:
sensor_ack = sensor_ack_search.group(1)
if (sensor_ack == self.ACK_OK):
sensor_ack = "ok"
if (sensor_ack == self.ACK_NOK):
sensor_ack = "nok"
result['SENSOR-WHERE'] = where
result['SENSOR-TYPE'] = self.SENSOR_TYPE_TEMPERATURE
result['TEMPERATURE'] = float(sensor_temperature) / 10
result['TEMPERATURE-SET-POINT'] = float(consigne_temperature) / 10
result['ACK'] = sensor_ack
return result
def convert_temperature_to_dim(self, temperature):
"""
convert 21.5 to "0215"
"""
result = temperature * 10
result = '%04d' % result
return result