forked from Morg42/viessmann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·1417 lines (1233 loc) · 69.3 KB
/
__init__.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#########################################################################
# Copyright 2020 Michael Wenzel
#########################################################################
# Viessmann-Plugin for SmartHomeNG. https://github.com/smarthomeNG//
#
# This plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this plugin. If not, see <http://www.gnu.org/licenses/>.
#########################################################################
import logging
import socket
import time
import serial
import re
import threading
from datetime import datetime
import dateutil.parser
import cherrypy
from . import commands
from lib.item import Items
from lib.model.smartplugin import *
from bin.smarthome import VERSION
class Viessmann(SmartPlugin):
'''
Main class of the plugin. Provides communication with Viessmann heating systems
via serial / USB-to-serial connections to read values and set operating parameters.
Supported device types must be defined in "commands.py".
'''
ALLOW_MULTIINSTANCE = False
PLUGIN_VERSION = '1.0.0'
#
# public methods
#
def __init__(self, sh, *args, **kwargs):
# Get plugin parameter
self._serialport = self.get_parameter_value('serialport') # /dev/optolink
self._heating_type = self.get_parameter_value('heating_type') # V200KO1B
self._protocol = self.get_parameter_value('protocol') # P300
self._timeout = self.get_parameter_value('timeout') # timeout in sec for plugin serial read operation
# Set variables
self._params = {} # Item dict
self._init_cmds = [] # List of command codes for read at init
self._cyclic_cmds = {} # Dict of command codes with cylce-times for cyclic readings
self._application_timer = {} # Dict of application timer with command codes and values
self._timer_cmds = [] # List of command codes for timer
self._viess_timer_dict = {}
self._lock = threading.Lock()
self._initread = False
self._timerread = False
self._connected = False
self._initialized = False
self._lastbyte = b''
self._lastbytetime = 0
self._cyclic_update_active = False
self._wochentage = {
'MO': ['mo', 'montag', 'monday'],
'TU': ['di', 'dienstag', 'tuesday'],
'WE': ['mi', 'mittwoch', 'wednesday'],
'TH': ['do', 'donnerstag', 'thursday'],
'FR': ['fr', 'freitag', 'friday'],
'SA': ['sa', 'samstag', 'saturday'],
'SU': ['so', 'sonntag', 'sunday']}
# initialize logger if necessary
if '.'.join(VERSION.split('.', 2)[:2]) <= '1.5':
self.logger = logging.getLogger(__name__)
# Load protocol dependent sets
if self._protocol in commands.controlset and self._protocol in commands.errorset and self._protocol in commands.unitset and self._protocol in commands.returnstatus and self._protocol in commands.setreturnstatus:
self._controlset = commands.controlset[self._protocol]
self.logger.info('Loaded controlset for protocol \'{}\''.format(self._controlset))
self._errorset = commands.errorset[self._protocol]
self.logger.info('Loaded errors for protocol \'{}\''.format(self._errorset))
self._unitset = commands.unitset[self._protocol]
self.logger.info('Loaded units for protocol \'{}\''.format(self._unitset))
self._devicetypes = commands.devicetypes
self.logger.info('Loaded device types for protocol \'{}\''.format(self._devicetypes))
self._returnstatus = commands.returnstatus[self._protocol]
self.logger.info('Loaded return status for protocol \'{}\''.format(self._returnstatus))
self._setreturnstatus = commands.setreturnstatus[self._protocol]
self.logger.info('Loaded set return status for protocol \'{}\''.format(self._setreturnstatus))
else:
self.logger.error('Sets for protocol {} could not be found or incomplete!'.format(self._protocol))
return None
# Load device dependent sets
if self._heating_type in commands.commandset and self._heating_type in commands.operatingmodes and self._heating_type in commands.systemschemes:
self._commandset = commands.commandset[self._heating_type]
self.logger.info('Loaded commands for heating type \'{}\''.format(self._commandset))
self._operatingmodes = commands.operatingmodes[self._heating_type]
self.logger.info('Loaded operating modes for heating type \'{}\''.format(self._operatingmodes))
self._systemschemes = commands.systemschemes[self._heating_type]
self.logger.info('Loaded system schemes for heating type \'{}\''.format(self._systemschemes))
else:
sets = []
if self._heating_type not in commands.commandset:
sets += 'command'
if self._heating_type not in commands.operatingmodes:
sets += 'operating modes'
if self._heating_type not in commands.systemschemes:
sets += 'system schemes'
self.logger.error('Sets {} for heating type {} could not be found!'.format(", ".join(sets), self._heating_type))
return None
# Init web interface
self.init_webinterface()
def run(self):
'''
Run method for the plugin
'''
self._connect()
self.alive = True
self._read_initial_values()
self._read_timers()
def stop(self):
'''
Stop method for the plugin
'''
if self.scheduler_get('cyclic'):
self.scheduler_remove('cyclic')
self.alive = False
self._disconnect()
def parse_item(self, item):
'''
Method for parsing items.
If the item carries any viess_* field, this item is registered to the plugin.
:param item: The item to process.
:type item: object
:return: The item update method to be triggered if the item is changed, or None.
:rtype: object
'''
# Process the update config
if self.has_iattr(item.conf, 'viess_update'):
self.logger.debug("Item for requesting update for all items triggered: {}".format(item))
return self.update_item
# Process the timer config and fill timer dict
if self.has_iattr(item.conf, 'viess_timer'):
timer_app = self.get_iattr_value(item.conf, 'viess_timer')
for commandname in self._commandset:
if commandname.startswith(timer_app):
commandconf = self._commandset[commandname]
self.logger.debug('Process the timer config, commandname: {}'.format(commandname))
# {'addr': '2100', 'len': 8, 'unit': 'CT', 'set': True}
commandcode = (commandconf['addr']).lower()
if timer_app not in self._application_timer:
self._application_timer[timer_app] = {'item': item, 'commandcodes': []}
if commandcode not in self._application_timer[timer_app]['commandcodes']:
self._application_timer[timer_app]['commandcodes'].append(commandcode)
self._application_timer[timer_app]['commandcodes'].sort()
self.logger.info('Loaded Application Timer \'{}\''.format(self._application_timer))
# self._application_timer: {'Timer_M2': {'item': Item: heizung.heizkreis_m2.schaltzeiten, 'commandcodes': ['3000', '3008', '3010', '3018', '3020', '3028', '3030']}, 'Timer_Warmwasser': {'item': Item: heizung.warmwasser.schaltzeiten, 'commandcodes': ['2100', '2108', '2110', '2118', '2120', '2128', '2130']}}
for subdict in self._application_timer:
for commandcode in self._application_timer[subdict]['commandcodes']:
if commandcode not in self._timer_cmds:
self._timer_cmds.append(commandcode)
self._timer_cmds.sort()
self.logger.debug('Loaded Timer commands \'{}\''.format(self._timer_cmds))
return self.update_item
# Process the read config
if self.has_iattr(item.conf, 'viess_read'):
commandname = self.get_iattr_value(item.conf, 'viess_read')
if commandname is None or commandname not in self._commandset:
self.logger.error('Item {} contains invalid read command \'{}\'!'.format(item, commandname))
return None
# Remember the read config to later update this item if the configured response comes in
self.logger.info('Item {} reads by using command \'{}\'.'.format(item, commandname))
commandconf = self._commandset[commandname]
commandcode = (commandconf['addr']).lower()
# Fill item dict
self._params[commandcode] = {'item': item, 'commandname': commandname}
self.logger.debug('Loaded params \'{}\''.format(self._params)) # Loaded params '# Loaded params '{'27A3': {'item': 'viessmann.heizkreis_a1m1.betriebsart.betriebsart', 'commandname': 'Betrierbsart_A1M1'}}'
# Allow items to be automatically initiated on startup
if self.has_iattr(item.conf, 'viess_init') and self.get_iattr_value(item.conf, 'viess_init'):
self.logger.info('Item {} is initialized on startup.'.format(item))
if commandcode not in self._init_cmds:
self._init_cmds.append(commandcode)
self.logger.debug('CommandCodes should be read at init: {}'.format(self._init_cmds))
# Allow items to be cyclically updated
if self.has_iattr(item.conf, 'viess_read_cycle'):
cycle = int(self.get_iattr_value(item.conf, 'viess_read_cycle'))
self.logger.info('Item {} should read cyclic every {} seconds.'.format(item, cycle))
nexttime = time.time() + cycle
if commandcode not in self._cyclic_cmds:
self._cyclic_cmds[commandcode] = {'cycle': cycle, 'nexttime': nexttime}
else:
# If another item requested this command already with a longer cycle, use the shorter cycle now
if self._cyclic_cmds[commandcode]['cycle'] > cycle:
self._cyclic_cmds[commandcode]['cycle'] = cycle
self.logger.debug('CommandCodes should be read cyclic: {}'.format(self._cyclic_cmds))
# Process the write config
if self.has_iattr(item.conf, 'viess_send'):
if self.get_iattr_value(item.conf, 'viess_send'):
commandname = self.get_iattr_value(item.conf, 'viess_read')
else:
commandname = self.get_iattr_value(item.conf, 'viess_send')
if commandname is None or commandname not in self._commandset:
self.logger.error('Item {} contains invalid write command \'{}\'!'.format(item, commandname))
return None
else:
self.logger.info('Item {} to be written by using command \'{}\''.format(item, commandname))
return self.update_item
def parse_logic(self, logic):
pass
def update_item(self, item, caller=None, source=None, dest=None):
'''
Callback method for sending values to the plugin when a registered item has changed
:param item: item to be updated towards the plugin
:param caller: if given it represents the callers name
:param source: if given it represents the source
:param dest: if given it represents the dest
'''
if self.alive and caller != self.get_shortname():
self.logger.info("Update item: {}, item has been changed outside this plugin".format(item.id()))
self.logger.debug("update_item was called with item '{}' from caller '{}', source '{}' and dest '{}'".format(item, caller, source, dest))
if self.has_iattr(item.conf, 'viess_send'):
# Send write command
if self.get_iattr_value(item.conf, 'viess_send'):
commandname = self.get_iattr_value(item.conf, 'viess_read')
else:
commandname = self.get_iattr_value(item.conf, 'viess_send')
value = item()
self.logger.debug('Got item value to be written: {} on command name {}.'.format(value, commandname))
if not self._send_write_command(commandname, value):
# create_write_command() liefert False, wenn das Schreiben fehlgeschlagen ist
# -> dann auch keine weitere Verarbeitung
self.logger.debug("Write for {} with value {} failed, reverting value, canceling followup actions".format(commandname, value))
item(item.property.last_value, self.get_shortname())
return None
# If a read command should be sent after write
if self.has_iattr(item.conf, 'viess_read') and self.has_iattr(item.conf, 'viess_read_afterwrite'):
readcommandname = self.get_iattr_value(item.conf, 'viess_read')
readafterwrite = self.get_iattr_value(item.conf, 'viess_read_afterwrite')
self.logger.debug('Attempting read after write for item {}, command {}, delay {}'.format(item, readcommandname, readafterwrite))
if readcommandname is not None and readafterwrite is not None:
aw = float(readafterwrite)
time.sleep(aw)
self._send_read_command(readcommandname)
# If commands should be triggered after this write
if self.has_iattr(item.conf, 'viess_trigger'):
trigger = self.get_iattr_value(item.conf, 'viess_trigger')
if trigger is None:
self.logger.error('Item {} contains invalid trigger command list \'{}\'!'.format(item, trigger))
else:
tdelay = 5 # default delay
if self.has_iattr(item.conf, 'viess_trigger_afterwrite'):
tdelay = float(self.get_iattr_value(item.conf, 'viess_trigger_afterwrite'))
if type(trigger) != list:
trigger = [trigger]
for triggername in trigger:
triggername = triggername.strip()
if triggername is not None and readafterwrite is not None:
self.logger.debug('Triggering command {} after write for item {}'.format(triggername, item))
time.sleep(tdelay)
self._send_read_command(triggername)
elif self.has_iattr(item.conf, 'viess_timer'):
timer_app = self.get_iattr_value(item.conf, 'viess_timer')
uzsu_dict = item()
self.logger.debug('Got changed UZSU timer: {} on timer application {}.'.format(uzsu_dict, timer_app))
self._uzsu_dict_to_viess_timer(timer_app, uzsu_dict)
elif self.has_iattr(item.conf, 'viess_update'):
if item():
self.logger.debug('Reading of all values/items has been requested')
self.update_all_read_items()
def send_cyclic_cmds(self):
'''
Recall function for shng scheduler. Reads all values configured to be read cyclically.
'''
# check if another cyclic cmd run is still active
if self._cyclic_update_active:
self.logger.warning('Triggered cyclic command read, but previous cyclic run is still active. Check device and cyclic configuration (too much/too short?)')
return
# set lock
self._cyclic_update_active = True
currenttime = time.time()
read_items = 0
for commandcode in list(self._cyclic_cmds.keys()):
entry = self._cyclic_cmds[commandcode]
# Is the command already due?
if entry['nexttime'] <= currenttime:
commandname = self._commandname_by_commandcode(commandcode)
self.logger.info('Triggering cyclic read command: {}'.format(commandname))
self._send_read_command(commandname)
entry['nexttime'] = currenttime + entry['cycle']
read_items += 1
self._cyclic_update_active = False
if read_items:
self.logger.debug("cyclic command read took {:.1f} seconds for {} items".format(time.time() - currenttime, read_items))
def update_all_read_items(self):
'''
Read all values preset in "commands.py" as readable
'''
for commandcode in list(self._params.keys()):
commandname = self._commandname_by_commandcode(commandcode)
self.logger.debug('Triggering read command: {} for requested value update'.format(commandname))
self._send_read_command(commandname)
#
# initialization methods
#
def _connect(self):
'''
Tries to establish a connection to the heating device. To prevent
multiple concurrent connection locking is used.
:return: Returns True if connection was established, False otherwise
:rtype: bool
'''
if self._connected and self._serial:
return True
self._lock.acquire()
try:
self.logger.info('Connecting ...')
self._serial = serial.Serial()
self._serial.baudrate = self._controlset['Baudrate']
self._serial.parity = self._controlset['Parity']
self._serial.bytesize = self._controlset['Bytesize']
self._serial.stopbits = self._controlset['Stopbits']
self._serial.port = self._serialport
self._serial.timeout = 1
self._serial.open()
self._connected = True
self.logger.info('Connected to {}'.format(self._serialport))
self._connection_attempts = 0
if not self.scheduler_get('cyclic'):
self._create_cyclic_scheduler()
return True
except Exception as e:
self.logger.error('Could not _connect to {}; Error: {}'.format(self._serialport, e))
return False
finally:
self._lock.release()
def _disconnect(self):
'''
Disconnect any connected devices.
'''
self._connected = False
self._initialized = False
if self.scheduler_get('cyclic'):
self.scheduler_remove('cyclic')
try:
self._serial.close()
self._serial = None
self.logger.info('Disconnected')
except:
pass
def _init_communication(self):
'''
After connecting to the device, setup the communication protocol
:return: Returns True, if communication was established successfully, False otherwise
:rtype: bool
'''
# just try to connect anyway; if connected, this does nothing and no harm, if not, it connects
if not self._connect():
self.logger.error('Init communication not possible as connect failed.')
return False
# if device answers SYNC b'\x16\x00\x00' with b'\x06', comm is initialized
self.logger.info('Init Communication....')
is_initialized = False
initstringsent = False
self.logger.debug('send_bytes: Send reset command {}'.format(self._int2bytes(self._controlset['Reset_Command'], 1)))
self._send_bytes(self._int2bytes(self._controlset['Reset_Command'], 1))
readbyte = self._read_bytes(1)
self.logger.debug('read_bytes: read {}, last byte is {}'.format(readbyte, self._lastbyte))
for i in range(0, 10):
if initstringsent and self._lastbyte == self._int2bytes(self._controlset['Acknowledge'], 1):
# Schnittstelle hat auf den Initialisierungsstring mit OK geantwortet. Die Abfrage von Werten kann beginnen. Diese Funktion meldet hierzu True zurück.
is_initialized = True
self.logger.debug('Device acknowledged initialization')
break
if self._lastbyte == self._int2bytes(self._controlset['Not_initiated'], 1):
# Schnittstelle ist zurückgesetzt und wartet auf Daten; Antwort b'\x05' = Warten auf Initialisierungsstring oder Antwort b'\x06' = Schnittstelle initialisiert
self._send_bytes(self._int2bytes(self._controlset['Sync_Command'], 3))
self.logger.debug('send_bytes: Send sync command {}'.format(self._int2bytes(self._controlset['Sync_Command'], 3)))
initstringsent = True
elif self._lastbyte == self._int2bytes(self._controlset['Init_Error'], 1):
self.logger.error('The interface has reported an error (\x15), loop increment {}'.format(i))
self._send_bytes(self._int2bytes(self._controlset['Reset_Command'], 1))
self.logger.debug('send_bytes: Send reset command {}'.format(self._int2bytes(self._controlset['Reset_Command'], 1)))
initstringsent = False
else:
self._send_bytes(self._int2bytes(self._controlset['Reset_Command'], 1))
self.logger.debug('send_bytes: Send reset command {}'.format(self._int2bytes(self._controlset['Reset_Command'], 1)))
initstringsent = False
readbyte = self._read_bytes(1)
self.logger.debug('read_bytes: read {}, last byte is {}'.format(readbyte, self._lastbyte))
self.logger.info('Communication initialized: {}'.format(is_initialized))
self._initialized = is_initialized
return is_initialized
def _create_cyclic_scheduler(self):
'''
Setup the scheduler to handle cyclic read commands and find the proper time for the cycle.
'''
shortestcycle = -1
for commandname in list(self._cyclic_cmds.keys()):
entry = self._cyclic_cmds[commandname]
if shortestcycle == -1 or entry['cycle'] < shortestcycle:
shortestcycle = entry['cycle']
# Start the worker thread
if shortestcycle != -1:
# Balance unnecessary calls and precision
workercycle = int(shortestcycle / 2)
# just in case it already exists...
if self.scheduler_get('cyclic'):
self.scheduler_remove('cyclic')
self.scheduler_add('cyclic', self.send_cyclic_cmds, cycle=workercycle, prio=5, offset=0)
self.logger.info('Added cyclic worker thread ({} sec cycle). Shortest item update cycle found: {} sec.'.format(workercycle, shortestcycle))
def _read_initial_values(self):
'''
Read all values configured to be read at startup / connection
'''
if self._init_cmds != []:
self.logger.info('Starting initial read commands.')
for commandcode in self._init_cmds:
commandname = self._commandname_by_commandcode(commandcode)
self.logger.debug('send_init_commands {}.'.format(commandname))
self._send_read_command(commandname)
self._initread = True
self.logger.debug('self._initread = {}.'.format(self._initread))
#
# send and receive commands
#
def _read_timers(self):
'''
Read all configured timer values from device and create uzsu timer dict
'''
if self._application_timer is not []:
self.logger.info('Starting timer read commands.')
for timer_app in self._application_timer:
for commandcode in self._application_timer[timer_app]['commandcodes']:
commandname = self._commandname_by_commandcode(commandcode)
self.logger.debug('send_timer_commands {}.'.format(commandname))
self._send_read_command(commandname)
self._timerread = True
self.logger.debug('Timer Readout done = {}.'.format(self._timerread))
self._viess_dict_to_uzsu_dict()
def _send_read_command(self, commandname):
'''
Create formatted command sequence from command name and send to device
:param commandname: Command for which to create command sequence as defined in "commands.py"
:type commandname: str
'''
# A read_request telegram looks like this: ACK (1 byte), startbyte (1 byte), data length in bytes (1 byte), request/response (1 byte), read/write (1 byte), addr (2 byte), amount of value bytes expected in answer (1 byte), checksum (1 byte)
self.logger.debug('Got a new read job: Command {}'.format(commandname))
# Get command config
commandconf = self._commandset[commandname]
self.logger.debug('Command config: {}'.format(commandconf))
commandcode = (commandconf['addr']).lower()
commandvaluebytes = commandconf['len']
# Build packet for read commands
packet = bytearray()
packet.extend(self._int2bytes(self._controlset['StartByte'], 1))
packet.extend(self._int2bytes(self._controlset['Command_bytes_read'], 1))
packet.extend(self._int2bytes(self._controlset['Request'], 1))
packet.extend(self._int2bytes(self._controlset['Read'], 1))
packet.extend(bytes.fromhex(commandcode))
packet.extend(self._int2bytes(commandvaluebytes, 1))
packet.extend(self._int2bytes(self._calc_checksum(packet), 1))
self.logger.debug('Preparing command {} with packet to be sent as hexstring: {} and as bytes: {}'.format(commandname, self._bytes2hexstring(packet), packet))
packetlen_response = int(self._controlset['Command_bytes_read']) + 4 + int(commandvaluebytes)
# hand over built packet to send_command
self._send_command(packet, packetlen_response)
def _send_write_command(self, commandname, value=None):
'''
Create formatted command sequence from command name and send to device
:param commandname: Command for which to create command sequence as defined in "commands.py"
:type commandname: str
:param value: Value to write to device, None if not applicable
:return: Return True, if write was successfully acknowledged by device, False otherwise
:rtype: bool
'''
# A write_request telegram looks like this: ACK (1 byte), startbyte (1 byte), data length in bytes (1 byte), request/response (1 byte), read/write (1 byte), addr (2 byte), amount of bytes to be written (1 byte), value (bytes as per last byte), checksum (1 byte)
self.logger.debug('Got a new write job: Command {} with value {}'.format(commandname, value))
# Get command config
commandconf = self._commandset[commandname]
self.logger.debug('Command config: {}'.format(commandconf))
commandcode = (commandconf['addr']).lower()
commandvaluebytes = commandconf['len']
commandunit = commandconf['unit']
if commandunit == 'BA':
# try to convert BA string to byte value, setting str values will fail
# this will not work properly if multiple entries have the same value!
try:
value = int(dict(map(reversed, self._operatingmodes.items()))[value])
commandunit = 'IUNON'
commandsigned = False
except KeyError:
# value doesn't exist in operatingmodes. don't know what to do
self.logger.error('Value {} not defined in operating modes for device {}'.format(value, self._heating_type))
return False
set_allowed = bool(commandconf['set'])
unitconf = self._unitset[commandunit]
self.logger.debug('Unit defined to {} with config{}.'.format(commandunit, unitconf))
commandvalueresult = unitconf['type']
commandsigned = unitconf['signed']
commandtransform = unitconf['read_value_transform']
if 'min_value' in commandconf:
min_allowed_value = commandconf['min_value']
else:
min_allowed_value = None
if 'max_value' in commandconf:
max_allowed_value = commandconf['max_value']
else:
max_allowed_value = None
try:
# check if command is allowed to write
if set_allowed:
# check, if value has content
if value is not None and value != '':
# check, if values to be written are in allowed range or None
if (min_allowed_value is None or min_allowed_value <= value) and (max_allowed_value is None or max_allowed_value >= value):
# Create valuebytes
if commandvalueresult == 'datetime' or commandvalueresult == 'date':
try:
datestring = dateutil.parser.isoparse(value).strftime("%Y%m%d%w%H%M%S")
# Viessmann erwartet 2 digits für Wochentag, daher wird hier noch eine 0 eingefügt
datestring = datestring[:8] + '0' + datestring[8:]
valuebytes = bytes.fromhex(datestring)
self.logger.debug('Created value bytes for type {} as bytes: {}'.format(commandvalueresult, valuebytes))
except Exception as e:
self.logger.error('Incorrect data format, YYYY-MM-DD expected; Error: {}'.format(e))
return False
elif commandvalueresult == 'timer':
try:
times = ""
for switching_time in value:
an = self._encode_timer(switching_time["An"])
aus = self._encode_timer(switching_time["Aus"])
# times += f"{an:02x}{aus:02x}"
times += "{:02x}".format(an) + "{:02x}".format(aus)
valuebytes = bytes.fromhex(times)
self.logger.debug('Created value bytes for type {} as hexstring: {} and as bytes: {}'.format(commandvalueresult, self._bytes2hexstring(valuebytes), valuebytes))
except Exception as e:
self.logger.error('Incorrect data format, (An: hh:mm Aus: hh:mm) expected; Error: {}'.format(e))
return False
elif commandvalueresult == 'integer' or commandvalueresult == 'list':
if commandtransform == 'int':
value = self._value_transform_write(value, commandtransform)
self.logger.debug('Transformed value using method {} to {}'.format(commandtransform, value))
elif commandtransform == 'bool':
value = bool(value)
else:
value = int(value)
valuebytes = self._int2bytes(value, commandvaluebytes)
self.logger.debug('Created value bytes for type {} as hexstring: {} and as bytes: {}'.format(commandvalueresult, self._bytes2hexstring(valuebytes), valuebytes))
else:
self.logger.error('Type not definied for creating write command bytes')
return False
# Calculate length of payload (telegram header for write with 5 byte + amount of valuebytes)
payloadlength = int(self._controlset['Command_bytes_write']) + int(commandvaluebytes)
self.logger.debug('Payload length is: {} bytes.'.format(payloadlength))
# Build packet with value bytes for write commands
packet = bytearray()
packet.extend(self._int2bytes(self._controlset['StartByte'], 1))
packet.extend(self._int2bytes(payloadlength, 1))
packet.extend(self._int2bytes(self._controlset['Request'], 1))
packet.extend(self._int2bytes(self._controlset['Write'], 1))
packet.extend(bytes.fromhex(commandcode))
packet.extend(self._int2bytes(commandvaluebytes, 1, commandsigned))
packet.extend(valuebytes)
packet.extend(self._int2bytes(self._calc_checksum(packet), 1))
self.logger.debug('Preparing command {} with value {} (transformed to value byte \'{}\') to be sent as packet {}.'.format(commandname, value, self._bytes2hexstring(valuebytes), self._bytes2hexstring(packet)))
packetlen_response = int(self._controlset['Command_bytes_read']) + 4
# hand over built packet to send_command
self._send_command(packet, packetlen_response)
else:
self.logger.error('No valid value to be sent')
return False
else:
self.logger.error('No value handed over')
return False
else:
self.logger.error('Command at Heating is not allowed to be sent')
return False
except Exception as e:
self.logger.debug('create_write_command failed with error: {}.'.format(e))
return False
return True
def _send_command(self, packet, packetlen_response):
'''
Send command sequence to device
:param packet: Command sequence to send
:type packet: bytearray
:param packetlen_response: number of bytes expected in reply
:type packetlen_response: int
'''
if not self._connected:
self.logger.error('Not connected, trying to reconnect.')
if not self._connect():
self.logger.error('Could not connect to serial device')
return
try:
self._lock.acquire()
if not self._initialized or (time.time() - 500) > self._lastbytetime:
if self._initialized:
self.logger.info('Communication timed out, trying to reestablish communication.')
else:
self.logger.warning('Communication no longer initialized, trying to reestablish.')
self._init_communication()
if self._initialized:
# send query
try:
self._send_bytes(packet)
self.logger.debug('Successfully sent packet: {}'.format(self._bytes2hexstring(packet)))
time.sleep(0.1)
except IOError as io:
raise IOError('IO Error: {}'.format(io))
except Exception as e:
raise Exception('Exception while sending: {}'.format(e))
# receive response
response_packet = bytearray()
try:
self.logger.debug('Trying to receive {} bytes of the response.'.format(packetlen_response))
chunk = self._read_bytes(packetlen_response)
time.sleep(0.1)
self.logger.debug('Received {} bytes chunk of response as hexstring {} and as bytes {}'.format(len(chunk), self._bytes2hexstring(chunk), chunk))
if len(chunk) != 0:
if chunk[:1] == self._int2bytes(self._controlset['Error'], 1):
self.logger.error('Interface returned error! response was: {}'.format(chunk))
elif len(chunk) == 1 and chunk[:1] == self._int2bytes(self._controlset['Not_initiated'], 1):
self.logger.error('Received invalid chunk, connection not initialized. Forcing re-initialize...')
self._initialized = False
elif chunk[:1] != self._int2bytes(self._controlset['Acknowledge'], 1):
self.logger.error('Received invalid chunk, not starting with ACK! response was: {}'.format(chunk))
else:
# self.logger.info('Received chunk! response was: {}, Hand over to parse_response now.format(chunk))
response_packet.extend(chunk)
self._parse_response(response_packet)
else:
self.logger.error('Received 0 bytes chunk - ignoring response_packet! chunk was: {}'.format(chunk))
except socket.timeout:
raise Exception('Error receiving response: time-out')
except IOError as io:
raise IOError('IO Error: {}'.format(io))
except Exception as e:
raise Exception('Error receiving response: {}'.format(e))
else:
raise Exception('Interface not initialized!')
except IOError as io:
self.logger.error('send_command failed with IO error: {}.'.format(io))
self.logger.error('Trying to reconnect (disconnecting, connecting')
self._disconnect()
except Exception as e:
self.logger.error('send_command failed with error: {}.'.format(e))
finally:
self._lock.release()
def _send_bytes(self, packet):
'''
Send data to device
:param packet: Data to be sent
:type packet: bytearray
:return: Returns False, if no connection is established or write failed; True otherwise
:rtype: bool
'''
if not self._connected:
return False
try:
self._serial.write(packet)
except serial.SerialTimeoutException:
return False
# self.logger.debug('send_bytes: Sent {}'.format(packet))
return True
def _read_bytes(self, length):
'''
Try to read bytes from device
:param length: Number of bytes to read
:type length: int
:return: Number of bytes actually read
:rtype: int
'''
if not self._connected:
return 0
totalreadbytes = bytes()
# self.logger.debug('read_bytes: Start read')
starttime = time.time()
# don't wait for input indefinitely, stop after self._timeout seconds
while time.time() <= starttime + self._timeout:
readbyte = self._serial.read()
self._lastbyte = readbyte
# self.logger.debug('read_bytes: Read {}'.format(readbyte))
if readbyte != b'':
self._lastbytetime = time.time()
else:
return totalreadbytes
totalreadbytes += readbyte
if len(totalreadbytes) >= length:
return totalreadbytes
# timeout reached, did we read anything?
if not totalreadbytes:
# just in case, force plugin to reconnect
self._connected = False
self._initialized = False
# return what we got so far, might be 0
return totalreadbytes
def _parse_response(self, response):
'''
Process device response data, try to parse type and value and assign value to associated item
:param response: Data received from device
:type response: bytearray
'''
# A read_response telegram looks like this: ACK (1 byte), startbyte (1 byte), data length in bytes (1 byte), request/response (1 byte), read/write (1 byte), addr (2 byte), amount of valuebytes (1 byte), value (bytes as per last byte), checksum (1 byte)
# A write_response telegram looks like this: ACK (1 byte), startbyte (1 byte), data length in bytes (1 byte), request/response (1 byte), read/write (1 byte), addr (2 byte), amount of bytes written (1 byte), checksum (1 byte)
# Validate checksum
checksum = self._calc_checksum(response[1:len(response) - 1]) # first, cut first byte (ACK) and last byte (checksum) and then calculate checksum
received_checksum = response[len(response) - 1]
if received_checksum != checksum:
self.logger.error('Calculated checksum {} does not match received checksum of {}! Ignoring reponse.'.format(checksum, received_checksum))
return
# Extract command/address, valuebytes and valuebytecount out of response
commandcode = response[5:7].hex()
responsetypecode = response[3] # 0x00 = Anfrage, 0x01 = Antwort, 0x03 = Fehler
responsedatacode = response[4] # 0x01 = ReadData, 0x02 = WriteData, 0x07 = Function Call
valuebytecount = response[7]
self.logger.debug('Response decoded to: commandcode: {}, responsedatacode: {}, valuebytecount: {}'.format(commandcode, responsedatacode, valuebytecount))
# Extract databytes out of response
rawdatabytes = bytearray()
rawdatabytes.extend(response[8:8 + (valuebytecount)])
self.logger.debug('Rawdatabytes formatted: {} and unformatted: {}'.format(self._bytes2hexstring(rawdatabytes), rawdatabytes))
# Process response for items if read response and not error
if responsedatacode == 1 and responsetypecode != 3:
# Process response for items in item-dict using the commandcode
if commandcode in self._params.keys():
# Find corresponding item and commandname
item = self._params[commandcode]['item']
commandname = self._params[commandcode]['commandname']
self.logger.debug('Corresponding Item: {}; Corresponding commandname: {}'.format(item.id(), commandname))
# Get command and respective unit config
commandconf = self._commandset[commandname]
commandvaluebytes = commandconf['len']
commandunit = commandconf['unit']
unitconf = self._unitset[commandunit]
commandsigned = unitconf['signed']
valuetransform = unitconf['read_value_transform']
self.logger.debug('Unit defined to {} with config {}.'.format(commandunit, unitconf))
# start value decode
if commandunit == 'CT':
rawdatastring = rawdatabytes.hex()
timer = self._decode_timer(rawdatastring)
# fill list
timer = [{'An': on_time, 'Aus': off_time}
for on_time, off_time in zip(timer, timer)]
value = timer
self.logger.debug('Matched command {} and read transformed timer {} and byte length {}.'.format(commandname, value, commandvaluebytes))
# Split timer list and put it the child items, which were created by struct.timer in iso time format
try:
for child in item.return_children():
child_item = str(child.id())
if child_item.endswith('an1'):
child(timer[0]['An'], self.get_shortname())
# child(datetime.strptime(timer[0]['An'], '%H:%M').time().isoformat())
elif child_item.endswith('aus1'):
child(timer[0]['Aus'], self.get_shortname())
elif child_item.endswith('an2'):
child(timer[1]['An'], self.get_shortname())
elif child_item.endswith('aus2'):
child(timer[1]['Aus'], self.get_shortname())
elif child_item.endswith('an3'):
child(timer[2]['An'], self.get_shortname())
elif child_item.endswith('aus3'):
child(timer[2]['Aus'], self.get_shortname())
elif child_item.endswith('an4'):
child(timer[3]['An'], self.get_shortname())
elif child_item.endswith('aus4'):
child(timer[3]['Aus'], self.get_shortname())
except:
self.logger.debug('No child items for timer found (use timer.structs) or value no valid')
elif commandunit == 'TI':
rawdatastring = rawdatabytes.hex()
rawdata = bytearray()
rawdata.extend(map(ord, rawdatastring))
# decode datetime
value = datetime.strptime(rawdata.decode(), '%Y%m%d%W%H%M%S').isoformat()
self.logger.debug('Matched command {} and read transformed datetime {} and byte length {}.'.format(commandname, value, commandvaluebytes))
elif commandunit == 'DA':
rawdatastring = rawdatabytes.hex()
rawdata = bytearray()
rawdata.extend(map(ord, rawdatastring))
# decode date
value = datetime.strptime(rawdata.decode(), '%Y%m%d%W%H%M%S').date().isoformat()
self.logger.debug('Matched command {} and read transformed datetime {} and byte length {}.'.format(commandname, value, commandvaluebytes))
elif commandunit == 'ES':
# erstes Byte = Fehlercode; folgenden 8 Byte = Systemzeit
errorcode = (rawdatabytes[:1]).hex()
# errorquerytime = (rawdatabytes[1:8]).hex()
value = self._error_decode(errorcode)
self.logger.debug('Matched command {} and read transformed errorcode {} (raw value was {}) and byte length {}.'.format(commandname, value, errorcode, commandvaluebytes))
elif commandunit == 'SC':
# erstes Byte = Anlagenschema
systemschemescode = (rawdatabytes[:1]).hex()
value = self._systemscheme_decode(systemschemescode)
self.logger.debug('Matched command {} and read transformed system scheme {} (raw value was {}) and byte length {}.'.format(commandname, value, systemschemescode, commandvaluebytes))
elif commandunit == 'BA':
operatingmodecode = (rawdatabytes[:1]).hex()
value = self._operatingmode_decode(operatingmodecode)
self.logger.debug('Matched command {} and read transformed operating mode {} (raw value was {}) and byte length {}.'.format(commandname, value, operatingmodecode, commandvaluebytes))
elif commandunit == 'DT':
# device type has 8 bytes, but first 4 bytes are device type indicator
devicetypebytes = rawdatabytes[:2].hex()
value = self._devicetype_decode(devicetypebytes).upper()
self.logger.debug('Matched command {} and read transformed device type {} (raw value was {}) and byte length {}.'.format(commandname, value, devicetypebytes, commandvaluebytes))
elif commandunit == 'SN':
# serial number has 7 bytes,
serialnummerbytes = rawdatabytes[:7]
value = self._serialnumber_decode(serialnummerbytes)
self.logger.debug('Matched command {} and read transformed serial number {} (raw value was {}) and byte length {}.'.format(commandname, value, serialnummerbytes, commandvaluebytes))
else:
rawvalue = self._bytes2int(rawdatabytes, commandsigned)
value = self._value_transform_read(rawvalue, valuetransform)
self.logger.debug('Matched command {} and read transformed value {} (integer raw value was {}) and byte length {}.'.format(commandname, value, rawvalue, commandvaluebytes))
# Update item
item(value, self.get_shortname())
# Process response for timers in timer-dict using the commandcode
if commandcode in self._timer_cmds:
self.logger.debug('Parse_Response_Timer: {}.'.format(commandcode))
# Find corresponding commandname
commandname = self._commandname_by_commandcode(commandcode)
# Find timer application
for timer in self._application_timer:
if commandcode in self._application_timer[timer]['commandcodes']:
timer_app = timer
# Get commandconf and respective unit config
commandconf = self._commandset[commandname]
commandvaluebytes = commandconf['len']
commandunit = commandconf['unit']
# Value decode
if commandunit == 'CT':
rawdatastring = rawdatabytes.hex()
timer = self._decode_timer(rawdatastring)
# fill single timer list
timer = [{'An': on_time, 'Aus': off_time}
for on_time, off_time in zip(timer, timer)]
self.logger.debug('Matched timer command {} for application timer {} and read transformed timer {} and byte length {}.'.format(commandname, timer_app, timer, commandvaluebytes))
# Fill timer dict
if timer_app not in self._viess_timer_dict:
self._viess_timer_dict[timer_app] = {}
self._viess_timer_dict[timer_app][commandname] = timer
self.logger.debug('Viessmann timer dict: {}.'.format(self._viess_timer_dict))
# self._viess_timer_dict: {'Timer_M2': {'Timer_M2_Di': [{'An': '04:10', 'Aus': '07:00'}, {'An': '13:30', 'Aus': '20:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_Mo': [{'An': '04:10', 'Aus': '07:00'}, {'An': '13:30', 'Aus': '20:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_Sa': [{'An': '04:40', 'Aus': '21:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_So': [{'An': '04:40', 'Aus': '21:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_Do': [{'An': '04:10', 'Aus': '07:00'}, {'An': '13:30', 'Aus': '20:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_Mi': [{'An': '04:10', 'Aus': '07:00'}, {'An': '13:30', 'Aus': '20:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_M2_Fr': [{'An': '04:10', 'Aus': '07:00'}, {'An': '13:30', 'Aus': '20:00'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}]}, 'Timer_Warmwasser': {'Timer_Warmwasser_Fr': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_Mi': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_Mo': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_Do': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_Sa': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_Di': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}], 'Timer_Warmwasser_So': [{'An': '04:00', 'Aus': '04:40'}, {'An': '16:30', 'Aus': '17:10'}, {'An': '00:00', 'Aus': '00:00'}, {'An': '00:00', 'Aus': '00:00'}]}}
# Handling of write command response if not error
elif responsedatacode == 2 and responsetypecode != 3:
self.logger.info('Write request of adress {} successfull writing {} bytes.'.format(commandcode, valuebytecount))
else:
self.logger.error('Write request of adress {} NOT successfull writing {} bytes.'.format(commandcode, valuebytecount))
#
# convert data types
#
def _viess_dict_to_uzsu_dict(self):
'''
Convert data read from device to UZSU compatible struct.
Input is taken from self._viess_timer_dict, output is written to
self._uzsu_dict
'''
# set variables
dict_timer = {}
empty_time = '00:00'
shitems = Items.get_instance()
try:
sunset = shitems.return_item('env.location.sunset')().strftime("%H:%M")
sunrise = shitems.return_item('env.location.sunrise')().strftime("%H:%M")
except:
sunset = '21:00'
sunrise = '06:00'
# convert all switching times with corresponding app and days to timer-dict
for application in self._viess_timer_dict:
if application not in dict_timer:
dict_timer[application] = {}
for application_day in self._viess_timer_dict[application]:
timer = self._viess_timer_dict[application][application_day]
day = application_day[(application_day.rfind('_') + 1):len(application_day)].lower()
# normalize days
for element in self._wochentage:
if day in self._wochentage[element]:
weekday = element
for entry in timer: