-
Notifications
You must be signed in to change notification settings - Fork 1
/
udi-YoLink.py
815 lines (700 loc) · 41.2 KB
/
udi-YoLink.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
#!/usr/bin/env python3
"""
Yolink Control Main Node program
MIT License
"""
import sys
import time
from yoLink_init_V3 import YoLinkInitPAC
from udiYoSwitchV2 import udiYoSwitch
from udiYoSwitchSecV2 import udiYoSwitchSec
from udiYoTHsensorV3 import udiYoTHsensor
from udiYoGarageDoorCtrlV2 import udiYoGarageDoor
from udiYoGarageFingerCtrlV2 import udiYoGarageFinger
from udiYoMotionSensorV3 import udiYoMotionSensor
from udiYoLeakSensorV3 import udiYoLeakSensor
from udiYoCOSmokeSensorV3 import udiYoCOSmokeSensor
from udiYoDoorSensorV3 import udiYoDoorSensor
from udiYoOutletV2 import udiYoOutlet
from udiYoOutletPwrV2 import udiYoOutletPwr
from udiYoMultiOutletV2 import udiYoMultiOutlet
from udiYoManipulatorV2 import udiYoManipulator
from udiYoSpeakerHubV2 import udiYoSpeakerHub
from udiYoLockV2 import udiYoLock
from udiYoInfraredRemoterV2 import udiYoInfraredRemoter
from udiYoDimmerV2 import udiYoDimmer
from udiYoVibrationSensorV3 import udiYoVibrationSensor
from udiYoSmartRemoterV3 import udiYoSmartRemoter
from udiYoPowerFailV3 import udiYoPowerFailSenor
from udiYoSirenV2 import udiYoSiren
from udiYoWaterMeterControllerV2 import udiYoWaterMeterController
#from udiYoHubV2 import udiYoHub
import udiProfileHandler
try:
import udi_interface
logging = udi_interface.LOGGER
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.DEBUG)
version = '1.2.7'
class YoLinkSetup (udi_interface.Node):
def __init__(self, polyglot, primary, address, name):
super().__init__( polyglot, primary, address, name)
self.poly=polyglot
self.hb = 0
self.nodeDefineDone = False
self.handleParamsDone = False
self.pollStart = False
self.debug = False
self.address = address
self.name = name
self.yoAccess = None
self.TTSstr = 'TTS'
self.nbr_API_calls = 19
self.nbr_dev_API_calls = 5
self.supportParams = ['YOLINKV2_URL', 'TOKEN_URL','MQTT_URL', 'MQTT_PORT', 'UAID', 'SECRET_KEY', 'NBR_TTS', 'TEMP_UNIT' ]
self.yolinkURL = 'https://api.yosmart.com/openApi'
self.yolinkV2URL = 'https://api.yosmart.com/open/yolink/v2/api'
self.temp_unit = 0
self.tokenURL = 'https://api.yosmart.com/open/yolink/token'
self.mqttURL = 'api.yosmart.com'
self.mqttPort = 8003
logging.setLevel(10)
self.poly.subscribe(self.poly.STOP, self.stop)
self.poly.subscribe(self.poly.START, self.start, address)
self.poly.subscribe(self.poly.LOGLEVEL, self.handleLevelChange)
self.poly.subscribe(self.poly.CUSTOMPARAMS, self.handleParams)
self.poly.subscribe(self.poly.POLL, self.systemPoll)
self.poly.subscribe(self.poly.ADDNODEDONE, self.node_queue)
self.poly.subscribe(self.poly.CONFIGDONE, self.configDoneHandler)
self.n_queue = []
self.Parameters = Custom(self.poly, 'customparams')
self.Notices = Custom(self.poly, 'notices')
logging.debug('YoLinkSetup init')
logging.debug('self.address : ' + str(self.address))
logging.debug('self.name :' + str(self.name))
self.poly.updateProfile()
self.poly.ready()
self.poly.addNode(self)
self.wait_for_node_done()
self.node = self.poly.getNode(self.address)
self.node.setDriver('ST', 1, True, True)
self.assigned_addresses = []
self.assigned_addresses.append(self.address)
logging.debug('YoLinkSetup init DONE')
self.nodeDefineDone = True
def node_queue(self, data):
self.n_queue.append(data['address'])
def wait_for_node_done(self):
while len(self.n_queue) == 0:
time.sleep(0.1)
self.n_queue.pop()
def convert_temp_unit(self, tempStr):
if tempStr.capitalize()[:1] == 'F':
return(1)
elif tempStr.capitalize()[:1] == 'K':
return(2)
else:
return(0)
def configDoneHandler(self):
# We use this to discover devices, or ask to authenticate if user has not already done so
self.poly.Notices.clear()
logging.info('configDoneHandler called')
#self.myNetatmo.updateOauthConfig()
self.nodes_in_db = self.poly.getNodesFromDb()
logging.debug('Nodes in Nodeserver - before cleanup: {} - {}'.format(len(self.nodes_in_db),self.nodes_in_db))
self.configDone = True
def start (self):
logging.info('Executing start - udi-YoLink')
logging.info ('Access using PAC/UAC')
#logging.setLevel(30)
while not self.nodeDefineDone:
time.sleep(1)
logging.debug ('waiting for inital node to get created')
#self.supportedYoTypes = ['Switch', 'THSensor', 'MultiOutlet', 'DoorSensor','Manipulator',
# 'MotionSensor', 'Outlet', 'GarageDoor', 'LeakSensor', 'Hub',
# 'SpeakerHub', 'VibrationSensor', 'Finger', 'Lock', 'InfraredRemoter' ]
self.supportedYoTypes = ['Switch', 'THSensor', 'MultiOutlet', 'DoorSensor','Manipulator',
'MotionSensor', 'Outlet', 'GarageDoor', 'LeakSensor', 'Hub',
'SpeakerHub', 'VibrationSensor', 'Finger', 'Lock', 'Dimmer', 'InfraredRemoter',
'PowerFailureAlarm', 'SmartRemoter', 'COSmokeSensor', 'Siren', 'WaterMeterController']
#self.supportedYoTypes = ['Outlet', 'MultiOutlet', 'Switch', ]
if self.uaid == None or self.uaid == '' or self.secretKey==None or self.secretKey=='':
logging.error('UAID and secretKey must be provided to start node server')
exit()
self.yoAccess = YoLinkInitPAC (self.uaid, self.secretKey)
if 'TEMP_UNIT' in self.Parameters:
self.temp_unit = self.convert_temp_unit(self.Parameters['TEMP_UNIT'])
else:
self.temp_unit = 0
self.Parameters['TEMP_UNIT'] = 'C'
logging.debug('TEMP_UNIT: {}'.format(self.temp_unit ))
self.yoAccess.set_temp_unit(self.temp_unit )
if 'DEBUG_EN' in self.Parameters:
self.debug = self.Parameters['DEBUG_EN']
self.yoAccess.set_debug(self.debug)
else:
self.debug = False
self.yoAccess.set_debug(self.debug)
if 'CALLS_PER_MIN' in self.Parameters:
self.nbr_API_calls = self.Parameters['CALLS_PER_MIN']
self.nbr_dev_API_calls = self.Parameters['DEV_CALLS_PER_MIN']
self.yoAccess.set_api_limits(self.nbr_API_calls, self.nbr_dev_API_calls)
self.deviceList = self.yoAccess.getDeviceList()
logging.debug('{} devices detected : {}'.format(len(self.deviceList), self.deviceList) )
if self.yoAccess:
self.node.setDriver('ST', 1, True, True)
self.addNodes(self.deviceList)
else:
self.node.setDriver('ST', 0, True, True)
#self.poly.updateProfile()
def addNodes (self, deviceList):
for dev in deviceList:
if dev['type'] in self.supportedYoTypes:
logging.info('adding/checking device : {} - {}'.format(dev['name'], dev['type']))
if dev['type'] == 'Hub':
logging.info('Hub not added - ISY cannot do anything useful with it')
#name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
#logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
#temp = udiYoHub(self.poly, address, address, dev['name'], self.yoAccess, dev )
#self.Parameters[address] = dev['name']
elif dev['type'] == 'SpeakerHub':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address] = dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoSpeakerHub(self.poly, address, address, name, self.yoAccess, dev )
self.msgList=[]
logging.debug('Checking NBR_TTS')
if 'NBR_TTS' in self.Parameters:
self.nbrTTS = int(self.Parameters['NBR_TTS'])
logging.debug('NBR_TTS found: {}'.format(self.nbrTTS))
else:
self.nbrTTS = 1
self.Parameters['NBR_TTS'] = self.nbrTTS
self.yoAccess.TtsMessages = {}
for n in range(0,self.nbrTTS):
index = self.TTSstr+str(n)
if index not in self.Parameters:
self.Parameters[index] = 'Message '+str(n)
self.yoAccess.TtsMessages[n] = self.Parameters[index]
logging.info ('Adding {} to Parameters'.format(self.Parameters[index] ))
#self.yoAccess.writeTtsFile()
logging.info('TTS messages : {}'.format(self.yoAccess.TtsMessages))
logging.info('Updating profile files ')
if udiProfileHandler.udiTssProfileUpdate(self.yoAccess.TtsMessages):
self.poly.Notices['tts'] = 'Speaker hub messages updated - PoI/ISY need to be restarted to take effect'
self.poly.updateProfile()
for nbr in range(0,self.nbrTTS):
index = 'TTS'+str(nbr)
if index not in self.Parameters:
self.Parameters[index] = index
self.yoAccess.TtsMessages[nbr] = self.Parameters[index]
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Switch':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address] = dev['name']
if 'YS5708' in dev['modelName'] or 'YS5709' in dev['modelName']:
logging.info('Adding swith2Button device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoSwitchSec(self.poly, address, address, name, self.yoAccess, dev )
else:
logging.info('Adding switch device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoSwitch(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Dimmer':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoDimmer(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'THSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoTHsensor(self.poly, address, address, name, self.yoAccess, dev)
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'MultiOutlet':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoMultiOutlet(self.poly, address, address, name, self.yoAccess, dev)
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'DoorSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoDoorSensor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Manipulator':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoManipulator(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'MotionSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoMotionSensor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'VibrationSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoVibrationSensor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Outlet':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
if 'YS6602' in dev['modelName']:
logging.info('Adding device w. power {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoOutletPwr(self.poly, address, address, name, self.yoAccess, dev )
else:
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoOutlet(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'GarageDoor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoGarageDoor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Finger':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoGarageFinger(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Lock':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
temp = udiYoLock(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'InfraredRemoter':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoInfraredRemoter(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'LeakSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoLeakSensor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'COSmokeSensor':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoCOSmokeSensor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'PowerFailureAlarm':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoPowerFailSenor(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'SmartRemoter':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoSmartRemoter(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'Siren':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoSiren(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
elif dev['type'] == 'WaterMeterController':
name = dev['deviceId'][-14:] #14 last characters - hopefully there is no repeats (first charas seems the same for all)
address = self.poly.getValidAddress(name)
if address in self.Parameters:
name = self.Parameters[address]
else:
name = dev['name']
name = self.poly.getValidName(name)
self.Parameters[address]= dev['name']
logging.info('Adding device {} ({}) as {}'.format( dev['name'], dev['type'], str(name) ))
temp = udiYoWaterMeterController(self.poly, address, address, name, self.yoAccess, dev )
while not temp.node_ready:
logging.debug( 'Waiting for node {}-{} to be ready'.format(dev['type'] , dev['name']))
time.sleep(4)
for adr in temp.adr_list:
self.assigned_addresses.append(adr)
else:
logging.debug('Currently unsupported device : {}'.format(dev['type'] ))
time.sleep(1)
# need to go through nodes to see if there are nodes that no longer exist in device list
logging.debug('assigned addresses nodes :{} - {}'.format(len(self.assigned_addresses), self.assigned_addresses))
while not self.configDone:
logging.info('Waiting for ')
logging.debug('Nodes in Nodeserver - before cleanup: {} - {}'.format(len(self.nodes_in_db),self.nodes_in_db))
for nde in range(0, len(self.nodes_in_db)):
node = self.nodes_in_db[nde]
logging.debug('Scanning db for extra nodes : {}'.format(node))
if node['primaryNode'] not in self.assigned_addresses:
logging.debug('Removing node : {} {}'.format(node['name'], node))
self.poly.delNode(node['address'])
time.sleep(1)
# checking params for erassed nodes
self.poly.updateProfile()
self.pollStart = True
def stop(self):
try:
logging.info('Stop Called:')
#self.yoAccess.writeTtsFile() #save current TTS messages
self.node.setDriver('ST', 0, True, True)
if self.yoAccess:
self.yoAccess.shut_down()
self.poly.stop()
exit()
except Exception as E:
logging.error('Stop Exception : {}'.format(E))
if self.yoAccess:
self.yoAccess.shut_down()
self.poly.stop()
def heartbeat(self):
logging.debug('heartbeat: ' + str(self.hb))
if self.yoAccess.online:
self.node.setDriver('ST', 1)
if self.hb == 0:
self.reportCmd('DON',2)
self.hb = 1
else:
self.reportCmd('DOF',2)
self.hb = 0
else:
self.node.setDriver('ST', 0)
def checkNodes(self):
logging.info('Updating Nodes')
deviceList = self.yoAccess.getDeviceList()
nodes = self.poly.getNodes()
for dev in deviceList:
devList = []
name = dev['deviceId'][-14:]
if name not in nodes:
#device was likely off line during inital instellation or added afterwards
devList.append(dev)
self.addNodes(devList)
def systemPoll (self, polltype):
if self.pollStart:
logging.debug('System Poll executing: {}'.format(polltype))
if self.yoAccess.online:
self.node.setDriver('ST', 1)
if 'longPoll' in polltype:
#Keep token current
#self.node.setDriver('GV0', self.temp_unit, True, True)
try:
#if not self.yoAccess.refresh_token(): #refresh failed
# while not self.yoAccess.request_new_token():
# time.sleep(60)
#logging.info('Updating device status')
nodes = self.poly.getNodes()
for nde in nodes:
if nde != 'setup': # but not the controller node
nodes[nde].checkOnline()
logging.debug('longpoll {}'.format(nde))
time.sleep(5) # need to limit calls to 100 per 5 min - using 5 to allow other calls - updating is not critical
except Exception as e:
logging.error('Exeption occcured during systemPoll : {}'.format(e))
#self.yoAccess = YoLinkInitPAC (self.uaid, self.secretKey)
#deviceList = self.yoAccess.getDeviceList()
if 'shortPoll' in polltype:
self.heartbeat()
nodes = self.poly.getNodes()
for nde in nodes:
if nde != 'setup': # but not the controller node
nodes[nde].checkDataUpdate()
logging.debug('shortpoll {}'.format(nde))
# no API calls so no need to spread out
#time.sleep(4) # need to limit calls to 100 per 5 min - using 4 to allow other calls
#else:
# self.node.setDriver('ST', 0, True, True)
def handleLevelChange(self, level):
logging.info('New log level: {}'.format(level))
logging.setLevel(level['level'])
def handleParams (self, userParam ):
logging.debug('handleParams')
supportParams = ['YOLINKV2_URL', 'TOKEN_URL','MQTT_URL', 'MQTT_PORT', 'UAID', 'SECRET_KEY', 'NBR_TTS', 'TEMP_UNIT' ]
self.Parameters.load(userParam)
self.poly.Notices.clear()
try:
if 'YOLINKV2_URL' in userParam:
self.yolinkV2URL = userParam['YOLINKV2_URL']
#else:
# self.poly.Notices['yl2url'] = 'Missing YOLINKV2_URL parameter'
# self.yolinkV2URL = ''
if 'TOKEN_URL' in userParam:
self.tokenURL = userParam['TOKEN_URL']
#else:
# self.poly.Notices['turl'] = 'Missing TOKEN_URL parameter'
# self.tokenURL = ''
if 'MQTT_URL' in userParam:
self.mqttURL = userParam['MQTT_URL']
#else:
# self.poly.Notices['murl'] = 'Missing MQTT_URL parameter'
# self.mqttURL = ''
if 'MQTT_PORT' in userParam:
self.mqttPort = userParam['MQTT_PORT']
#else:
# self.poly.Notices['mport'] = 'Missing MQTT_PORT parameter'
# self.mqttPort = 0
if 'TEMP_UNIT' in userParam:
self.temp_unit = self.convert_temp_unit(userParam['TEMP_UNIT'])
else:
self.temp_unit = 0
if 'UAID' in userParam:
self.uaid = str(userParam['UAID'])
self.uaid = self.uaid.strip()
else:
self.poly.Notices['uaid'] = 'Missing UAID parameter'
self.uaid = ''
if 'SECRET_KEY' in userParam:
self.secretKey = str(userParam['SECRET_KEY'])
self.secretKey = self.secretKey.strip()
else:
self.poly.Notices['sk'] = 'Missing SECRET_KEY parameter'
self.secretKey = ''
if 'NBR_TTS' in userParam:
self.nbrTTS = int(userParam['NBR_TTS'])
#self.yoAccess.writeTtsFile()
if 'DEBUG_EN' in userParam:
self.debug = True
if 'CALLS_PER_MIN' in userParam:
self.nbr_API_calls = int(userParam['CALLS_PER_MIN'])
if 'DEV_CALLS_PER_MIN' in userParam:
self.nbr_dev_API_calls = int(userParam['DEV_CALLS_PER_MIN'])
nodes = self.poly.getNodes()
#logging.debug('nodes: {}'.format(nodes))
for nde in nodes:
#logging.debug('node : {}'.format(nde))
if nde in userParam:
user_param_name = userParam[nde]
temp_node = nodes[nde]
#logging.debug('User param name : {}, node name {}'.format(user_param_name, temp_node.name))
if user_param_name != temp_node.name:
temp_node.rename(user_param_name)
logging.info('Renaming node {} to {}'.format(nde, temp_node.name))
# if param not in supportParams:
# del self.Parameters[param]
# logging.debug ('erasing key: ' + str(param))
self.handleParamsDone = True
except Exception as e:
logging.debug('Error: {} {}'.format(e, userParam))
'''
def set_t_unit(self, command ):
logging.info('set_t_unit ')
unit = int(command.get('value'))
if unit >= 1 and unit <= 3:
self.temp_unit = unit
#self.node.setDriver('GV0', self.temp_unit, True, True)
'''
id = 'setup'
#commands = {
# 'TEMPUNIT': set_t_unit,
# }
drivers = [
{'driver': 'ST', 'value':1, 'uom':25},
#{'driver': 'GV20', 'value':99, 'uom':25},
]
if __name__ == "__main__":
try:
polyglot = udi_interface.Interface([])
polyglot.start(version)
YoLinkSetup(polyglot, 'setup', 'setup', 'YoLinkSetup')
# Just sit and wait for events
polyglot.runForever()
except (KeyboardInterrupt, SystemExit):
sys.exit(0)