-
Notifications
You must be signed in to change notification settings - Fork 1
/
yolinkMultiOutletV3.py
479 lines (420 loc) · 18.4 KB
/
yolinkMultiOutletV3.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
import json
#from os import ONDELAY
import time
import re
from yolink_mqtt_classV3 import YoLinkMQTTDevice
from yolink_delay_timer import CountdownTimer
try:
import udi_interface
logging = udi_interface.LOGGER
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.DEBUG)
class YoLinkMultiOut(YoLinkMQTTDevice):
def __init__(yolink, yoAccess, deviceInfo, callback):
super().__init__( yoAccess, deviceInfo, callback)
yolink.yoAccess = yoAccess
yolink.maxSchedules = 6
yolink.methodList = ['getState', 'setState', 'setDelay', 'getSchedules', 'setSchedules', 'getUpdates' ]
yolink.eventList = ['StatusChange', 'Report']
yolink.type = 'MultiOutlet'
yolink.MQTT_class = 'c'
yolink.last_set_data = None
yolink.attempt = 0
yolink.MultiOutletName = 'MultiOutletEvent'
yolink.eventTime = 'Time'
yolink.delayUpdateInt = 15
yolink.nbrOutlets = 0
yolink.nbrUsb = 0
#yolink.timerCallback = callback
yolink.extDelayTimer = CountdownTimer()
#yolink.extDelayTimer.timerCallback(callback)
#time.sleep(1)
'''
def initNode(yolink):
logging.debug('MultiOutlet initNode - {}'.format(yolink.deviceInfo['name']))
yolink.initDevice()
time.sleep(2)
#time.sleep(2)
#temp = yolink.getInfoAPI()
#yolink.refreshMultiOutlet() # needed to get number of ports on device
#yolink.refreshDevice()
#yolink.online = yolink.getOnlineStatus()
if yolink.online:
logging.info('MultiOutlet init - Nbr Outlets : {}'.format(yolink.nbrOutlets))
logging.info('MultiOutlet init - Nbr USB : {}'.format(yolink.nbrUsb))
else:
logging.info ('MultiOutlet not online')
#yolink.refreshSchedules()
#yolink.refreshFWversion()
def getSchedules (yolink):
return(yolink.dataAPI['data']['schedules'])
def getDelays (yolink):
return(yolink.dataAPI['data']['delays'])
'''
def getMultiSchedules (yolink):
return(yolink.getSchedules())
def refreshMultiOutlet(yolink):
return(yolink.refreshDevice())
def setMultiOutUsbState(yolink, portList, value ):
logging.debug( yolink.type+'- setMultiOutletState')
# portlist a a listof ports being changed port range 0-7
# value is state that need to change 2 (ON/OFF)
status = True
port = 0
for i in portList:
portNbr = yolink.extractStrNbr(i)
if portNbr < yolink.nbrUsb and portNbr >= 0 :
port = port + pow(2, portNbr)
else:
logging.error('wrong port number (range 0- '+str(yolink.nbrPorts)+'): ' + str(i))
return(False)
if value.lower() == 'on' or value.lower() == 'open':
state = 'open'
elif value.lower() == 'off' or value.lower() == 'closed' :
state = 'closed'
else:
logging.error('Unknows state passed')
status = False
if status:
data={}
data["params"] = {}
data["params"]["chs"] = port
data["params"]['state'] = state
yolink.last_set_data = data
yolink.setDevice( data)
return(status)
def data_updated(yolink):
tmp = yolink.lastUpdate()
if tmp == None or yolink.lastUpdateTime == None or yolink.lastUpdateTime == {}:
return(True)
elif ( tmp > yolink.lastUpdateTime):
yolink.lastUpdateTime = tmp
logging.debug('{} - Data Updated'.format(yolink.type))
return(True)
else:
return(False)
def setUsbState (yolink, port, state):
logging.info('usbSetState')
if yolink.nbrUsb > 0:
portList = []
portList.append(yolink.extractStrNbr(port))
yolink.setMultiOutUsbState(portList, state)
else:
logging.error('No USB port on device')
def setMultiOutPortState(yolink, portList, value ):
logging.debug( yolink.type+'- setMultiOutletState')
# portlist a a listof ports being changed port range 0-7
# value is state that need to change 2 (ON/OFF)
status = True
port = 0
for i in portList:
portNbr = yolink.extractStrNbr(i)
portNbr = portNbr + yolink.nbrUsb # Ports start after USB control ports
if portNbr <= yolink.nbrPorts and portNbr >= 0 :
port = port + pow(2, portNbr)
else:
logging.error('wrong port number (range 0 - '+str(yolink.nbrPorts)+'): ' + str(i))
return(False)
if value.lower() == 'on' or value.lower() == 'open':
state = 'open'
elif value.lower() == 'off' or value.lower() == 'closed' :
state = 'closed'
else:
logging.error('Unknows state passed')
status = False
if status:
data={}
data["params"] = {}
data["params"]["chs"] = port
data["params"]['state'] = state
yolink.last_set_data = data
yolink.setDevice( data)
return(status)
def setMultiOutState ( yolink, port, state):
logging.info('outletSetState')
portList = []
portList.append(str(yolink.extractStrNbr(port))) # port is 0 based
yolink.setMultiOutPortState(portList, state)
def setMultiOutDelayList (yolink, delayList):
logging.info('outletSetDelayList')
data = {}
delTemp = []
data['params'] = {}
data['params']['delays'] = []
for delays in range(0,len(delayList)):
temp = {}
for key in delayList[delays]:
if key.lower() == 'ch' or key.lower() == 'port':
ch = int(delayList[delays][key])
temp['ch'] = ch
#dTemp['ch'] = ch
if key.lower() == 'on' or key.lower() == 'ondelay' or key.lower() == 'delayon':
onDelay = int(delayList[delays][key])
temp['on'] = onDelay
#dTemp['on'] = onDelay
if key.lower() == 'off' or key.lower() == 'offdelay' or key.lower() == 'delayoff':
offDelay = int(delayList[delays][key])
temp['off'] = offDelay
#dTemp['off'] = offDelay
logging.debug('temp delayList: {}'.format(temp))
if 'ch' in temp and len(temp)>1:
data['params']['delays'].append(temp)
delTemp.append(temp)
logging.debug('Sending delay data: {}'.format( data['params']['delays']))
#data['time'] = str(int(time.time_ns()//1e6)) # we set time wne publishing
data['method'] = yolink.type+'.setDelay'
data["targetDevice"] = yolink.deviceInfo['deviceId']
data["token"]= yolink.deviceInfo['token']
yolink.last_set_data = data
yolink.yoAccess.publish_data( data)
#yolink.send_data( data)
#yolink.writeDelayData(data)
yolink.extDelayTimer.addDelays(delTemp)
yolink.online = yolink.dataAPI[yolink.dOnline]
def setMultiOutDelay (yolink, port, onDelay, offDelay):
logging.info('outletSetDelay')
data = {}
data['params'] = {}
data['params']['delays'] = []
portNbr = yolink.extractStrNbr(port)
delaySpec = {'ch':portNbr+yolink.nbrUsb, 'on':onDelay, 'off':offDelay}
logging.debug('Sending delay data: {}'.format(delaySpec))
data['params']['delays'].append(delaySpec)
#data['time'] = str(int(time.time_ns()//1e6)) # we set time wne publishing
data['method'] = yolink.type+'.setDelay'
data["targetDevice"] = yolink.deviceInfo['deviceId']
data["token"]= yolink.deviceInfo['token']
yolink.last_set_data = data
yolink.yoAccess.publish_data( data)
#yolink.send_data( data)
#yolink.writeDelayData(data)
yolink.extDelayTimer.addDelays([{'ch':portNbr+yolink.nbrUsb, 'on':onDelay, 'off':offDelay}] )
yolink.online = yolink.dataAPI[yolink.dOnline]
def setMultiOutOnDelay (yolink, port, onDelay):
logging.info('outletSetDelay')
data = {}
data['params'] = {}
data['params']['delays'] = []
portNbr = yolink.extractStrNbr(port)
delaySpec = {'ch':portNbr+yolink.nbrUsb, 'on':onDelay}
logging.debug('Sending delay data: {}'.format(delaySpec))
data['params']['delays'].append(delaySpec)
# data['time'] = str(int(time.time_ns()//1e6)) # we set time wne publishing
data['method'] = yolink.type+'.setDelay'
data["targetDevice"] = yolink.deviceInfo['deviceId']
data["token"]= yolink.deviceInfo['token']
yolink.last_set_data = data
#yolink.send_data( data)
yolink.yoAccess.publish_data( data)
#yolink.writeDelayData(data)
yolink.extDelayTimer.addDelays([{'ch':portNbr+yolink.nbrUsb, 'on':onDelay}] )
yolink.online = yolink.dataAPI[yolink.dOnline]
def setMultiOutOffDelay (yolink, port, offDelay):
logging.info('outletSetDelay')
data = {}
data['params'] = {}
data['params']['delays'] = []
portNbr = yolink.extractStrNbr(port)
delaySpec = {'ch':portNbr+yolink.nbrUsb, 'off':offDelay}
logging.debug('Sending delay data: {}'.format(delaySpec))
data['params']['delays'].append(delaySpec)
#data['time'] = str(int(time.time_ns()//1e6)) # we set time wne publishing
data['method'] = yolink.type+'.setDelay'
data["targetDevice"] = yolink.deviceInfo['deviceId']
data["token"]= yolink.deviceInfo['token']
yolink.last_set_data = data
yolink.yoAccess.publish_data( data)
#yolink.send_data( data)
#yolink.writeDelayData(data)
yolink.extDelayTimer.addDelays([{'ch':portNbr+yolink.nbrUsb, 'off':offDelay}] )
yolink.online = yolink.dataAPI[yolink.dOnline]
#def retry_send_data(yolink):
# logging.debug('retrying to send data')
# yolink.send_data(yolink.last_set_data)
def getMultiOutStates(yolink):
logging.debug(yolink.type+' - getMultiOutletState')
#yolink.refreshMultiOutlet()
states= {}
temp = yolink.getInfoAPI()
logging.debug(temp)
for usb in range (0,yolink.nbrUsb):
states['usb'+str(usb)]= {'state':temp['data']['state'][usb]}
delays = yolink.extDelayTimer.timeRemaining()
for outlet in range(0, yolink.nbrOutlets):
state = temp['data']['state'][outlet + yolink.nbrUsb]
found = False
for indx in range(0, len(delays)):
if delays[indx]['ch'] == outlet:
found = True
if 'on' in delays[indx]:
onDelay = int(delays[indx]['on']/60)
else:
onDelay = 0
if 'off' in delays[indx]:
offDelay = int(delays[indx]['off']/60)
else:
offDelay = 0
if not found:
onDelay = 0
offDelay = 0
states['port'+str(outlet)]= {'state':state, 'delays':{'on':onDelay, 'off':offDelay}}
return(states)
def getMultiOutPortState(yolink, portStr):
logging.debug(yolink.type+' - getMultiOutletState')
#yolink.refreshMultiOutlet()
port = yolink.extractStrNbr(portStr)
port = port + yolink.nbrUsb
temp = yolink.getInfoAPI()
temp = temp['data'] # Need to look at include USB in API
#logging.debug('getMultiOutletPortState {} {} {}'.format(port,temp['state'], temp ))
if port < len(temp['state'])-yolink.nbrUsb:
return(temp['state'][port])
else:
return('unknown')
def getMultiOutUsbState(yolink, usbStr):
logging.debug(yolink.type+' - getMultiOutletState')
#yolink.refreshMultiOutlet()
usb = yolink.extractStrNbr(usbStr)
temp = yolink.getInfoAPI()
temp = temp['data'] # Need to look at include USB in API
#logging.debug('getMultiOutletPortState {} {} {}'.format(port,temp['state'], temp ))
if yolink.nbrUsb > 0 and usb < yolink.nbrUsb:
return(temp['state'][usb])
else:
return('unknown')
'''
def setMultiDelay(yolink, delayList):
atttempt = 1
maxAttempts = 3
logging.debug(yolink.type+' - setDelay')
#port = port + yolink.nbrUsb
data = {}
data['params'] = {}
data['params']['delays'] = []
if len(delayList) >= 1:
for delayNbr in range(0,len(delayList)):
for key in delayList[delayNbr]:
if key.lower() == 'delayon' or key.lower() == 'on' :
data['params']['delays'][delayNbr]['on'] = delayList[delayNbr][key]
elif key.lower() == 'delay'or key.lower() == 'off' :
data['params']['delays'][delayNbr]['off'] = delayList[delayNbr][key]
elif key.lower == 'CH':
data['params']['delays'][delayNbr]['ch'] = delayList[delayNbr][key]
else:
logging.debug('Wrong parameter passed - must be overwritten to support multi devices : ' + str(key))
else:
logging.debug('Empty list provided ')
return(False, yolink.dataAPI[yolink.dOnline])
data['time'] = str(int(time.time_ns()//1e6))
data['method'] = yolink.type+'.setDelay'
data["targetDevice"] = yolink.deviceInfo['deviceId']
data["token"]= yolink.deviceInfo['token']
yolink.last_set_data = data
yolink.send_data( data)
yolink.online = yolink.dataAPI[yolink.dOnline]
return(True)
def getMultiOutletData(yolink):
logging.debug(yolink.type+' - getMultiOutletData')
def updateStatus(self, data):
self.updateCallbackStatus(data, False)
# add sub node updates
'''
def updateStatus(yolink, data):
yolink.updateCallbackStatus(data, False)
'''
max_attemps = 3
if yolink.device_connected(data):
logging.debug('yolink.device_connected(data) {} '.format(data))
yolink.updateCallbackStatus(data, False)
yolink.attempt = 0
else:
logging.error('{} - updateStatus - device appears not connected - retrying'.format(yolink.type))
yolink.attempts = yolink.attempts + 1
if yolink.attempts < max_attemps:
logging.error('{} - updateStatus - device appears not connected - retrying'.format(yolink.type))
time.sleep(1)
yolink.send_data( yolink.last_set_data) #Try again
#yolink.setState(yolink.temp_set_state)
else:
logging.error('{} - updateStatus - device appears not connected - giving up after {} attempts'.format(yolink.type, yolink.attempts ))
yolink.updateCallbackStatus(data, False)
yolink.attempts = 0
'''
def get_nbr_attempts (yolink):
return(yolink.attempt)
'''
def refreshFWversion(yolink):
logging.debug('refreshFWversion - not currently supported')
#return(yolink.refreshDevice('MultiOutlet.getVersion', yolink.updateStatus))
def startUpgrade(yolink):
logging.debug('startUpgrade - not currently supported')
def checkStatusChanges(yolink):
logging.debug('checkStatusChanges')
def resetDelayList (yolink):
yolink.delayList = []
'''
'''
def appendDelay(yolink, delay):
# to remove a delay program it to 0
try:
invalid = False
if 'port' in delay and 'OnDelay' in delay and 'OffDelay' in delay:
if delay['port'] <0 and delay['port'] >= yolink.nbrPorts:
invalid = True
if 'OnDelay' in delay:
if delay['OnDelay'] < 0:
invalid = True
if 'OffDelay' in delay:
if delay['OffDelay'] < 0:
invalid = True
elif 'OnDelay' in delay and 'OffDelay' in delay:
if 'OnDelay' in delay:
if delay['OnDelay'] < 0:
invalid = True
if 'OffDelay' in delay:
if delay['OffDelay'] < 0:
invalid = True
delay['port'] = 1 #use channel 1 for non-multi port devices
else:
logging.debug('Missing or wrong input parameters: ' + str(delay))
if not invalid:
yolink.delayList.append(delay)
return(invalid)
except logging.exception as E:
logging.debug('Exception appendDelay : ' + str(E))
def prepareDelayData(yolink):
data={}
data['params'] = {}
nbrDelays = len(yolink.delayList)
data["params"]["delays"] = []
if nbrDelays > 0:
for delayNbr in range (0,nbrDelays):
#temp = yolink.delayList[delayNbr]
temp = {}
temp['ch'] = yolink.delayList[delayNbr]['port']
temp['on'] = yolink.delayList[delayNbr]['OnDelay']
temp['off'] = yolink.delayList[delayNbr]['OffDelay']
data["params"]["delays"].append(temp)
#temp['on'] = tmp['OnDelay']
#temp['off'] = tmp['OffDelay']
#data["params"]["delays"].append(temp)
return(data)
'''
'''
def shut_down(yolink):
yolink.diconnect = True
yolink.yolinkMQTTclient.shut_down()
'''
class YoLinkMultiOutlet(YoLinkMultiOut):
def __init__(yolink, yoAccess, deviceInfo):
super().__init__( yoAccess, deviceInfo, yolink.updateStatus)
yolink.initNode()
if yolink.online:
logging.info('MultiOutlet init - Nbr Outlets : {}'.format(yolink.nbrOutlets))
logging.info('MultiOutlet init - Nbr USB : {}'.format(yolink.nbrUsb))
else:
logging.info ('MultiOutlet not online')
def updateStatus(self, data):
self.updateCallbackStatus(data, True)