-
Notifications
You must be signed in to change notification settings - Fork 2
/
ets_to_openhab.py
566 lines (484 loc) · 27.6 KB
/
ets_to_openhab.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import csv
import os.path
import json
with open('config.json') as f:
config = json.load(f)
def data_of_name(data, name):
for x in data:
if x['Group name'] == name:
return x
return None
csvfile = open(config['ets_export'], newline='', encoding='cp1252')
reader = csv.DictReader(csvfile, delimiter='\t')
house = {}
export_to_influx = []
used_addresses = []
for row in reader:
#print(row)
# check if floor:
if row['Address'].endswith('/-/-'):
row['rooms'] = {}
house[row['Address'].split('/')[0]] = row
# check if room
elif row['Address'].endswith('/-'):
splitter = row['Address'].split('/')
row['Addresses'] = []
house[splitter[0]]['rooms'][splitter[1]] = row
# normal group address
else:
splitter = row['Address'].split('/')
if 'ignore' in row['Description']:
print("ignoreflag in description for: " + row['Group name'])
continue
house[splitter[0]]['rooms'][splitter[1]]['Addresses'].append(row)
items = ''
sitemap = ''
things = ''
semantic_things = ''
selections = ''
semantic_cnt = 0
fensterkontakte = []
cnt = 0
for floorNr in house.keys():
descriptions = house[floorNr]['Description'].split(';')
visibility = ''
semantic = '["Location"]'
synonyms = ''
icon = ''
for description in descriptions:
if description == 'debug':
visibility = 'visibility=[extended_view==ON]'
if description.startswith('icon='):
icon = '<' + description.replace('icon=','') + '>'
if description.startswith('semantic='):
semantic = '["Location", "' + description.replace('semantic=','').replace(',','","') + '"] '
if description.startswith('synonyms='):
synonyms = '{ ' + description.replace('synonyms=','synonyms="').replace(',',', ') + '" } '
items += f"Group map{floorNr} \"{house[floorNr]['Group name']}\" {icon} {semantic} {synonyms} \n" # {location} \n" # {visibility}
sitemap += f"Frame label=\"{house[floorNr]['Group name']}\" {{\n"
for roomNr in house[floorNr]['rooms'].keys():
roomName = house[floorNr]['rooms'][roomNr]['Group name']
roomNameOrig = roomName
descriptions = house[floorNr]['rooms'][roomNr]['Description'].split(';')
visibility = ''
semantic = f"[\"Location\", \"Room\", \"{roomName}\"]"
icon = ''
synonyms = ''
for description in descriptions:
if description == 'debug':
visibility = 'visibility=[extended_view==ON]'
if description.startswith('icon='):
icon = '<' + description.replace('icon=','') + '>'
if description.startswith('semantic='):
semantic = '["Location", "' + description.replace('semantic=','').replace(',','","') + '"] '
if description.startswith('synonyms='):
synonyms = '{ ' + description.replace('synonyms=','synonyms="').replace(',',', ') + '" } '
if description.startswith('name='):
roomName = description.replace('name=','')
items += f"Group map{floorNr}_{roomNr} \"{roomName}\" {icon} (map{floorNr}) {semantic} {synonyms}\n"
sitemap += f" Group item=map{floorNr}_{roomNr} {visibility} label=\"{roomName}\" "
group = ""
addresses = house[floorNr]['rooms'][roomNr]['Addresses']
# the loop has to be executed twice.
# - during the first run, all GAs are processed which can have a reference to another GA (e.g. a switch with status feedback)
# and also all GAs which can not have a reference to another GA. (e.g. temperatures)
# - during the second run, all not marked componentes are processed directly with no reference check
for run in [0, 1]:
for i in range(len(addresses)):
address = house[floorNr]['rooms'][roomNr]['Addresses'][i]
# in the second run: only process not already used addresses
if run == 1:
if address['Address'] in used_addresses:
continue
used = False
auto_add = False
item_icon = None
sitemap_type = 'Default'
mappings = ''
#lovely_name = ' '.join(address['Group name'].replace(house[floorNr]['rooms'][roomNr]['Group name'],'').replace(house[floorNr]['Group name'],'').split())
lovely_name = address['Group name']
item_label = lovely_name
descriptions = address['Description'].split(';')
equipment = ''
#print(f"--- processing: {lovely_name}")
#print(address)
if 'IGNORE' in address.keys():
continue
shortened_name = ' '.join(address['Group name'].replace(house[floorNr]['rooms'][roomNr]['Group name'],'').replace(house[floorNr]['Group name'],'').split())
item_name = f"i_{cnt}_{house[floorNr]['Group name']}_{house[floorNr]['rooms'][roomNr]['Group name']}_{shortened_name}".replace('/','_').replace(' ','_')
item_name = item_name.replace('ü','ue').replace('ä','ae').replace('ß','ss')
# dimmer
if address['Group name'].endswith(config['defines']['dimmer']['absolut_suffix']):
basename = address['Group name'].replace(config['defines']['dimmer']['absolut_suffix'],'')
dimmwert_status = data_of_name(addresses, basename + config['defines']['dimmer']['status_suffix'])
if dimmwert_status:
used = True
switch_option = ''
relative_option = ''
used_addresses.append(dimmwert_status['Address'])
relative_command = data_of_name(addresses, basename + config['defines']['dimmer']['relativ_suffix'])
if relative_command:
used_addresses.append(relative_command['Address'])
relative_option = f", increaseDecrease=\"{relative_command['Address']}\""
switch_command = data_of_name(addresses, basename + config['defines']['dimmer']['switch_suffix'])
if switch_command:
used_addresses.append(switch_command['Address'])
switch_status_command = data_of_name(addresses, basename + config['defines']['dimmer']['switch_status_suffix'])
switch_option_status = ''
if switch_status_command:
used_addresses.append(switch_status_command['Address'])
switch_option_status = f"+<{switch_status_command['Address']}"
switch_option = f", switch=\"{switch_command['Address']}{switch_option_status}\""
lovely_name = ' '.join(lovely_name.replace('Dimmen','').replace('Dimmer','').replace('absolut','').replace('Licht','').split())
auto_add = True
item_type = "Dimmer"
item_label = f"{lovely_name} [%d %%]"
thing_address_info = f"position=\"{address['Address']}+<{dimmwert_status['Address']}\"{switch_option}{relative_option}"
item_icon = "light"
equipment = 'Lightbulb'
semantic_info = "[\"Light\"]"
# rollos / jalousien
if address['DatapointType'] == 'DPST-1-8':
if not address['Group name'].endswith(config['defines']['rollershutter']['up_down_suffix']):
continue
basename = address['Group name'].replace(config['defines']['rollershutter']['up_down_suffix'],'')
fahren_auf_ab = data_of_name(addresses, basename + config['defines']['rollershutter']['up_down_suffix'])
fahren_stop = data_of_name(addresses, basename + config['defines']['rollershutter']['stop_suffix'])
absolute_position = data_of_name(addresses, basename + config['defines']['rollershutter']['absolute_position_suffix'])
absolute_position_status = data_of_name(addresses, basename + config['defines']['rollershutter']['status_suffix'])
#Status Richtung nicht in verwendung durch openhab
for drop_name in config['defines']['rollershutter']['drop']:
drop_addr = data_of_name(addresses, basename + drop_name)
if drop_addr:
used_addresses.append(drop_addr['Address'])
lovely_name = basename
if fahren_auf_ab and fahren_stop and absolute_position and absolute_position_status:
used_addresses.append(fahren_auf_ab['Address'])
used_addresses.append(fahren_stop['Address'])
used_addresses.append(absolute_position['Address'])
used_addresses.append(absolute_position_status['Address'])
auto_add = True
item_type = "Rollershutter"
thing_address_info = f"upDown=\"{fahren_auf_ab['Address']}\", stopMove=\"{fahren_stop['Address']}\", position=\"{absolute_position['Address']}+<{absolute_position_status['Address']}\""
item_label = f"{lovely_name} [%d %%]"
semantic_info = "[\"Blinds\"]"
item_icon = "rollershutter"
else:
print(f"incomplete rollershutter: {basename}")
# Schalten or bool
if address['DatapointType'] == 'DPST-1-1' or address['DatapointType'] == 'DPST-1-2':
item_type = "Switch"
item_icon = "switch"
item_label = lovely_name
# umschalten (Licht, Steckdosen)
# only add in first round, if there is a status GA for feedback
if not address['Group name'].endswith(' '+config['defines']['switch']['status_suffix']):
status = data_of_name(addresses, address['Group name'] + ' ' + config['defines']['switch']['status_suffix'])
if status:
#if status['DatapointType'] == 'DPST-1-11':
auto_add = True
used_addresses.append(status['Address'])
thing_address_info = f"ga=\"{address['Address']}+{status['Address']}\""
# in the second run, we accept everything ;)
if run == 1:
auto_add = True
thing_address_info = f"ga=\"1.001:{address['Address']}\""
#item_label = f"{lovely_name} [%d]"
semantic_info = "[\"Control\", \"Status\"]"
if config['defines']['switch']['light_name'] in address['Group name']:
semantic_info = "[\"Control\", \"Light\"]"
equipment = 'Lightbulb'
item_icon = 'light'
if config['defines']['switch']['poweroutlet_name'] in address['Group name']:
semantic_info = "[\"Control\", \"Switch\"]"
equipment = 'PowerOutlet'
item_icon = 'poweroutlet'
if config['defines']['switch']['speaker_name'] in address['Group name']:
semantic_info = "[\"Control\", \"Switch\"]"
equipment = 'Speaker'
item_icon = 'soundvolume'
if config['defines']['switch']['heating_name'] in address['Group name']:
semantic_info = "[\"Heating\", \"Switch\"]"
equipment = 'HVAC'
item_icon = 'radiator'
######## determined only by datapoint
# do this only after items with multiple addresses are processed:
# e.g. the state datapoint could be an own thing or the feedback from a switch or so
if run == 1:
# temperature
if address['DatapointType'] == 'DPST-9-1':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"9.001:{address['Address']}\""
item_label = f"{lovely_name} [%.1f °C]"
semantic_info = "[\"Measurement\", \"Temperature\"]"
if 'Soll' in lovely_name:
semantic_info = "[\"Setpoint\", \"Temperature\"]"
item_icon = "temperature"
# humidity
if address['DatapointType'] == 'DPST-9-7':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"9.001:{address['Address']}\""
item_label = f"{lovely_name} [%.1f %%]"
semantic_info = "[\"Measurement\", \"Humidity\"]"
if 'Soll' in lovely_name:
semantic_info = "[\"Setpoint\", \"Humidity\"]"
item_icon = "humidity"
# window/door
if address['DatapointType'] == 'DPST-1-19':
auto_add = True
item_type = "Contact"
thing_address_info = f"ga=\"1.019:{address['Address']}\""
equipment = 'Window'
semantic_info = "[\"OpenState\", \"Opening\"]"
fensterkontakte.append({'item_name': item_name, 'name': address['Group name']})
# Arbeit (wh)
if address['DatapointType'] == 'DPST-13-10':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"13.010:{address['Address']}\""
item_label = f"{lovely_name} [%.1f Wh]"
semantic_info = "[\"Measurement\", \"Energy\"]"
item_icon = "batterylevel"
# Tag/Nacht
if address['DatapointType'] == 'DPST-1-24':
auto_add = True
item_type = "Switch"
thing_address_info = f"ga=\"1.024:{address['Address']}\""
item_label = f"{lovely_name}"
semantic_info = "[\"Control\"]"
item_icon = "moon"
# Alarm
if address['DatapointType'] == 'DPST-1-5':
auto_add = True
item_type = "Switch"
thing_address_info = f"ga=\"1.005:{address['Address']}\""
item_label = f"{lovely_name}"
semantic_info = "[\"Alarm\"]"
item_icon = "alarm"
# Leistung (W)
if address['DatapointType'] == 'DPST-14-56':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"14.056:{address['Address']}\""
item_label = f"{lovely_name} [%.1f W]"
semantic_info = "[\"Measurement\", \"Power\"]"
item_icon = "energy"
# Strom
if address['DatapointType'] == 'DPST-7-12':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"7.012:{address['Address']}\""
item_label = f"{lovely_name} [%.1f mA]"
semantic_info = "[\"Measurement\", \"Current\"]"
item_icon = "energy"
# Volumen (l)
if address['DatapointType'] == 'DPST-12-1200':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"12.1200:{address['Address']}\""
item_label = f"{lovely_name} [%.0f l]"
semantic_info = "[\"Measurement\", \"Volume\"]"
item_icon = "water"
# String
if address['DatapointType'] == 'DPST-16-0':
auto_add = True
item_type = "String"
thing_address_info = f"ga=\"16.000:{address['Address']}\""
item_icon = "text"
# Lux
if address['DatapointType'] == 'DPST-9-4':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"9.004:{address['Address']}\""
item_label = f"{lovely_name} [%.1f Lux]"
semantic_info = "[\"Measurement\", \"Light\"]"
item_icon = "sun"
# Geschwindigkeit m/s
if address['DatapointType'] == 'DPST-9-5':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"9.005:{address['Address']}\""
item_label = f"{lovely_name} [%.1f m/s]"
semantic_info = "[\"Measurement\", \"Wind\"]"
item_icon = "wind"
# ppm
if address['DatapointType'] == 'DPST-9-8':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"9.008:{address['Address']}\""
item_label = f"{lovely_name} [%.1f ppm]"
semantic_info = "[\"Measurement\"]"
# percent
if address['DatapointType'] == 'DPST-5-1':
auto_add = True
item_type = "Dimmer"
thing_address_info = f"ga=\"5.001:{address['Address']}\""
item_label = f"{lovely_name} [%d %%]"
semantic_info = "[\"Measurement\"]"
# Zeitdifferenz
if address['DatapointType'] == 'DPST-13-100':
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"13.100:{address['Address']}\""
item_label = f"{lovely_name} [%.1f s]"
semantic_info = "[\"Measurement\", \"Duration\"]"
item_icon = "time"
# Szene
if address['DatapointType'] == 'DPST-17-1':
used = True
for description in descriptions:
if description.startswith('mappings='):
mappings = description
break
if mappings!= '':
mapfile = f"gen_{item_name}.map"
mappings = mappings.replace("'",'"')
mapfile_content = mappings.replace('"','').replace(',','\n').replace('mappings=[','').replace(']','').replace(' ','')
mapfile_content += '\n' + mapfile_content.replace('=','.0=') + '\n-=unknown'
open(os.path.join(config['transform_dir_path'], mapfile),'w').write(mapfile_content)
auto_add = True
item_type = "Number"
thing_address_info = f"ga=\"17.001:{address['Address']}\""
item_label = f"{lovely_name} [MAP({mapfile}):%s]"
semantic_info = "[\"Control\"]"
item_icon = "movecontrol"
sitemap_type = "Selection"
else:
print(f"no mapping for scene {address['Address']} {address['Group name']} ")
#else:
# items += f"Number {item_name} \"{lovely_name} [%d]\" <movecontrol> {{ channel=\"knx:device:bridge:generic:{item_name}\" }}\n"
# group += f" Selection item={item_name} label=\"{lovely_name}\" {visibility}\n"
# Szenensteuerung
#if address['DatapointType'] == 'DPST-18-1':
# print(address)
# used = True
# things += f"Type number : {item_name} \"{address['Group name']}\" [ ga=\"18.001:{address['Address']}\" ]\n"
# items += f"Number {item_name} \"{lovely_name} [%d]\" <sun> (map{floorNr}_{roomNr}) {{ channel=\"knx:device:bridge:generic:{item_name}\" }}\n"
# Status
if address['DatapointType'] == 'DPST-1-11':
auto_add = True
item_type = "Switch"
thing_address_info = f"ga=\"1.011:{address['Address']}\""
item_label = f"{lovely_name}" # [%d]
semantic_info = "[\"Measurement\", \"Status\"]"
item_icon = "switch"
# TODO: get rid of this
if used:
used_addresses.append(address['Address'])
if auto_add:
synonyms = ''
used_addresses.append(address['Address'])
visibility = ''
for description in descriptions:
if 'debug' in description:
visibility = 'visibility=[extended_view==ON]'
if description.startswith('semantic='):
semantic_info = '["' + description.replace('semantic=','').replace(',','","') + '"] '
if description.startswith('icon='):
item_icon = description.replace('icon=','').replace(',','","')
if description.startswith('synonyms='):
synonyms = '{ ' + description.replace('synonyms=','synonyms="').replace(',',', ') + '" } '
if description.startswith('name='):
item_label = description.replace('name=','')
# remove generic description if unneccessary
item_label_short = item_label
for drop in config['defines']['drop_words']:
item_label_short = item_label_short.replace(drop,'')
item_label_short = ' '.join(item_label_short.split())
if item_label_short != '':
item_label = item_label_short
if item_icon:
item_icon = f"<{item_icon}>"
else:
item_icon = ""
item_label = item_label.replace(roomNameOrig, roomName)
thing_type = item_type.lower()
things += f"Type {thing_type} : {item_name} \"{address['Group name']}\" [ {thing_address_info} ]\n"
root = f"map{floorNr}_{roomNr}"
if equipment != '':
items += f"Group equipment_{item_name} \"{item_label}\" {item_icon} ({root}) [\"{equipment}\"]\n"
root = f"equipment_{item_name}"
items += f"{item_type} {item_name} \"{item_label}\" {item_icon} ({root}) {semantic_info} {{ channel=\"knx:device:bridge:generic:{item_name}\" {synonyms} }}\n"
group += f" {sitemap_type} item={item_name} label=\"{item_label}\" {mappings} {visibility}\n"
if 'influx' in address['Description']:
#print('influx @ ')
#print(address)
export_to_influx.append(item_name)
if group != '':
sitemap += f" {{\n{group}\n }}\n"
else:
sitemap += f"\n "
sitemap += f"}}\n "
# process all addresses which were not used
for floorNr in house.keys():
for roomNr in house[floorNr]['rooms'].keys():
addresses = house[floorNr]['rooms'][roomNr]['Addresses']
for i in range(len(addresses)):
if 'IGNORE' in address.keys():
continue
address = house[floorNr]['rooms'][roomNr]['Addresses'][i]
lovely_name = ' '.join(address['Group name'].replace(house[floorNr]['Group name'],'').replace(house[floorNr]['rooms'][roomNr]['Group name'],'').split())
item_name = f"i_{cnt}_{house[floorNr]['Group name']}_{house[floorNr]['rooms'][roomNr]['Group name']}_{lovely_name}".replace('/','_').replace(' ','_')
item_name = item_name.replace('ü','ue').replace('ä','ae').replace('ß','ss')
if not (address['Address'] in used_addresses):
print(f"unused: {address['Address']}: {address['Group name']} with type {address['DatapointType']}")
#print(things)
#print(sitemap)
#print(items)
# export things:
things_template = open('things.template','r').read()
things = things_template.replace('###things###', things)
open(config['things_path'],'w').write(things)
# export items:
items = 'Group Home "Our Home" [\"Location\"]\n' + items
open(config['items_path'],'w').write(items)
# export sitemap:
sitemap_template_file = 'sitemap.template'
if os.path.isfile(f"private_{sitemap_template_file}"):
sitemap_template_file = f"private_{sitemap_template_file}"
sitemap_template = open(sitemap_template_file,'r').read()
sitemap = sitemap_template.replace('###sitemap###', sitemap)
sitemap = sitemap.replace('###selections###', selections)
open(config['sitemaps_path'],'w').write(sitemap)
#export persistent
private_persistence = ''
if os.path.isfile(f"private_persistence"):
private_persistence = open('private_persistence','r').read()
persist = '''Strategies {
everyMinute : "0 * * * * ?"
everyHour : "0 0 * * * ?"
everyDay : "0 0 0 * * ?"
every2Minutes : "0 */2 * ? * *"
}
Items {
'''
for i in export_to_influx:
persist += f"{i}: strategy = everyUpdate\n"
persist += private_persistence + '\n}'
open(config['influx_path'],'w').write(persist)
print(fensterkontakte)
fenster_rule = ''
for i in fensterkontakte:
fenster_rule += f'var save_fk_count_{i["item_name"]} = 0 \n'
fenster_rule += '''
rule "fensterkontakt check"
when
Time cron "0 * * * * ? *"
then
'''
for i in fensterkontakte:
fenster_rule += f' if({i["item_name"]}.state == OPEN){{ \n'
fenster_rule += f' save_fk_count_{i["item_name"]} += 1\n'
fenster_rule += f' if(save_fk_count_{i["item_name"]} == 15) {{\n'
fenster_rule += ' val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot"); \n'
fenster_rule += f' telegramAction.sendTelegram("{i["name"]} seit über 15 Minuten offen!");\n'
fenster_rule += ' }\n'
fenster_rule += ' } else { \n'
fenster_rule += f' save_fk_count_{i["item_name"]} = 0; \n'
fenster_rule += ' } \n'
fenster_rule += '''
end
'''
open('openhab/rules/fenster.rules','w').write(fenster_rule)