-
Notifications
You must be signed in to change notification settings - Fork 1
/
udiYoSwitchSecV2.py
408 lines (346 loc) · 16.5 KB
/
udiYoSwitchSecV2.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
#!/usr/bin/env python3
"""
Polyglot TEST v3 node server
MIT License
"""
try:
import udi_interface
logging = udi_interface.LOGGER
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.INFO)
#import udi_interface
#import sys
import time
from yolinkSwitchV2 import YoLinkSW
from udiYoSmartRemoterV3 import udiRemoteKey
class udiYoSwitchSec(udi_interface.Node):
from udiYolinkLib import prep_schedule, activate_schedule, update_schedule_data, node_queue, wait_for_node_done, mask2key
id = 'yoswitchsec'
drivers = [
{'driver': 'GV0', 'value': 99, 'uom': 25},
{'driver': 'GV1', 'value': 0, 'uom': 57},
{'driver': 'GV2', 'value': 0, 'uom': 57},
{'driver': 'GV13', 'value': 0, 'uom': 25}, #Schedule index/no
{'driver': 'GV14', 'value': 99, 'uom': 25}, # Active
{'driver': 'GV15', 'value': 99, 'uom': 25}, #On Hour
{'driver': 'GV16', 'value': 99, 'uom': 25}, #On Min
{'driver': 'GV10', 'value': 99, 'uom': 25}, #onSec
{'driver': 'GV17', 'value': 99, 'uom': 25}, #off Hour
{'driver': 'GV18', 'value': 99, 'uom': 25}, #off Min
{'driver': 'GV11', 'value': 99, 'uom': 25}, #offSec
{'driver': 'GV19', 'value': 0, 'uom': 25}, #days
{'driver': 'GV20', 'value': 99, 'uom': 25},
{'driver': 'ST', 'value': 0, 'uom': 25},
]
def __init__(self, polyglot, primary, address, name, yoAccess, deviceInfo):
super().__init__( polyglot, primary, address, name)
logging.debug('udiYoSwitchSec INIT- {}'.format(deviceInfo['name']))
self.poly = polyglot
self.devInfo = deviceInfo
self.yoAccess = yoAccess
self.address = address
self.name = name
self.yoSwitch = None
self.node_ready = False
self.timer_cleared = True
self.n_queue = []
self.last_state = ''
self.timer_update = 5
self.timer_expires = 0
self.onDelay = 0
self.offDelay = 0
self.schedule_selected = 0
self.keys = {}
if 'YS5708' in deviceInfo['modelName'] or 'YS5709' in deviceInfo['modelName']:
self.max_remote_keys = 8
self.nbr_keys = 2
else:
self.max_remote_keys = 0
self.nbr_keys = 0
#self.Parameters = Custom(polyglot, 'customparams')
# subscribe to the events we want
#polyglot.subscribe(polyglot.CUSTOMPARAMS, self.parameterHandler)
#polyglot.subscribe(polyglot.POLL, self.poll)
polyglot.subscribe(polyglot.START, self.start, self.address)
polyglot.subscribe(polyglot.STOP, self.stop)
self.poly.subscribe(self.poly.ADDNODEDONE, self.node_queue)
# start processing events and create add our controller node
polyglot.ready()
self.poly.addNode(self, conn_status = None, rename = True)
self.wait_for_node_done()
self.node = self.poly.getNode(address)
self.adr_list = []
self.adr_list.append(address)
def start(self):
logging.info('start - udiYoSwitch')
self.node.setDriver('ST', 0, True, True)
self.yoSwitch = YoLinkSW(self.yoAccess, self.devInfo, self.updateStatus)
time.sleep(3)
self.yoSwitch.initNode()
time.sleep(2)
#self.node.setDriver('ST', 1, True, True)
self.yoSwitch.delayTimerCallback (self.updateDelayCountdown, self.timer_update )
self.yoSwitch.refreshSchedules()
self.node_ready = True
for key in range (0,self.nbr_keys):
logging.debug('Adding keys to 2 button switch: {}'.format(key) )
k_address = self.address[4:14]+'key' + str(key)
k_address = self.poly.getValidAddress(str(k_address))
k_name = str(self.name) + ' key' + str(key+1)
k_name = self.poly.getValidName(str(k_name))
self.keys[key] = udiRemoteKey(self.poly, self.address, k_address, k_name, key)
self.adr_list.append(k_address)
logging.debug('Waiting for node to complete{}'.format(self.adr_list))
self.wait_for_node_done()
def updateDelayCountdown (self, timeRemaining ) :
logging.debug('updateDelayCountdown {}'.format(timeRemaining))
max_delay = 0
for delayInfo in range(0, len(timeRemaining)):
if 'ch' in timeRemaining[delayInfo]:
if timeRemaining[delayInfo]['ch'] == 1:
if 'on' in timeRemaining[delayInfo]:
self.node.setDriver('GV1', timeRemaining[delayInfo]['on'], True, False)
if max_delay < timeRemaining[delayInfo]['on']:
max_delay = timeRemaining[delayInfo]['on']
if 'off' in timeRemaining[delayInfo]:
self.node.setDriver('GV2', timeRemaining[delayInfo]['off'], True, False)
if max_delay < timeRemaining[delayInfo]['off']:
max_delay = timeRemaining[delayInfo]['off']
self.timer_expires = time.time()+max_delay
def stop (self):
logging.info('Stop udiYoSwitch')
self.node.setDriver('ST', 0, True, True)
self.yoSwitch.shut_down()
#if self.node:
# self.poly.delNode(self.node.address)
def checkOnline(self):
self.yoSwitch.refreshDevice()
def checkDataUpdate(self):
if self.yoSwitch.data_updated():
self.updateData()
def updateData(self):
if self.node is not None:
state = self.yoSwitch.getState().upper()
if self.yoSwitch.online:
self.node.setDriver('ST', 1)
if state == 'ON':
self.node.setDriver('GV0', 1)
self.node.reportCmd('DON')
elif state == 'OFF':
self.node.setDriver('GV0', 0)
self.node.reportCmd('DOF')
else:
self.node.setDriver('GV0', 99)
self.last_state = state
#logging.debug('Timer info : {} '. format(time.time() - self.timer_expires))
if time.time() >= self.timer_expires - self.timer_update and self.timer_expires != 0:
self.node.setDriver('GV1', 0)
self.node.setDriver('GV2', 0)
if self.yoSwitch.suspended:
self.node.setDriver('GV20', 1)
else:
self.node.setDriver('GV20', 0)
else:
#self.node.setDriver('ST', 0, True, True)
self.node.setDriver('GV0', 99)
self.node.setDriver('GV1', 0)
self.node.setDriver('GV2', 0)
self.node.setDriver('GV20', 2)
self.node.setDriver('GV10', 99, True, True, 25)
self.node.setDriver('GV11', 99, True, True, 25)
self.node.setDriver('GV13', self.schedule_selected)
self.node.setDriver('GV14', 99)
self.node.setDriver('GV15', 99, True, True, 25)
self.node.setDriver('GV16', 99, True, True, 25)
self.node.setDriver('GV17', 99, True, True, 25)
self.node.setDriver('GV18', 99, True, True, 25)
self.node.setDriver('GV19', 0)
if self.nbr_keys > 0:
event_data = self.yoSwitch.getEventData()
logging.debug('updateData - event data {}'.format(event_data))
if event_data:
key_mask = event_data['keyMask']
press_type = event_data['type']
remote_key = self.mask2key(key_mask)
if press_type == 'LongPress':
press = self.max_remote_keys
else:
press = 0
logging.debug('remote key {} press {}'.format(remote_key, press))
if self.yoSwitch.isControlEvent():
self.keys[remote_key].send_command(press)
self.yoSwitch.clearEventData()
logging.debug('clearEventData')
self.node.setDriver('GV13', self.schedule_selected)
sch_info = self.yoSwitch.getScheduleInfo(self.schedule_selected)
self.update_schedule_data(sch_info, self.schedule_selected)
'''
if sch_info:
self.node.setDriver('GV13', self.schedule_selected)
if self.yoSwitch.isScheduleActive(self.schedule_selected):
self.node.setDriver('GV14', 1)
else:
self.node.setDriver('GV14', 0)
timestr = sch_info['on']
logging.debug('timestr : {}'.format(timestr))
if '25:0' in timestr:
self.node.setDriver('GV15', 98,True, True, 25)
self.node.setDriver('GV16', 98,True, True, 25)
else:
timelist = timestr.split(':')
hour = int(timelist[0])
minute = int(timelist[1])
self.node.setDriver('GV15', int(hour),True, True, 19)
self.node.setDriver('GV16', int(minute),True, True, 44)
timestr = sch_info['off']
logging.debug('timestr : {}'.format(timestr))
if '25:0' in timestr:
self.node.setDriver('GV17', 98,True, True, 25)
self.node.setDriver('GV18', 98,True, True, 25)
else:
timelist = timestr.split(':')
hour = timelist[0]
minute = timelist[1]
self.node.setDriver('GV17', int(hour),True, True, 19)
self.node.setDriver('GV18', int(minute),True, True, 44)
self.node.setDriver('GV19', int(sch_info['week']))
else:
self.node.setDriver('GV13', self.schedule_selected)
self.node.setDriver('GV14', 99)
self.node.setDriver('GV15', 99,True, True, 25)
self.node.setDriver('GV16', 99,True, True, 25)
self.node.setDriver('GV17', 99,True, True, 25)
self.node.setDriver('GV18', 99,True, True, 25)
self.node.setDriver('GV19', 0)
'''
def updateStatus(self, data):
logging.info('updateStatus - Switch')
self.yoSwitch.updateStatus(data)
self.updateData()
def set_switch_on(self, command = None):
logging.info('udiYoSwitch set_switch_on')
self.yoSwitch.setState('ON')
self.node.setDriver('GV0',1 , True, True)
#self.node.reportCmd('DON')
def set_switch_off(self, command = None):
logging.info('udiYoSwitch set_switch_off')
self.yoSwitch.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
#self.node.reportCmd('DOF')
def set_switch_fon(self, command = None):
logging.info('udiYoSwitch set_switch_on')
self.yoSwitch.setState('ON')
self.node.setDriver('GV0',1 , True, True)
#self.node.reportCmd('DFON')
def set_switch_foff(self, command = None):
logging.info('udiYoSwitch set_switch_off')
self.yoSwitch.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
#self.node.reportCmd('DFOF')
def switchControl(self, command):
logging.info('udiYoSwitch switchControl')
ctrl = int(command.get('value'))
if ctrl == 1:
self.yoSwitch.setState('ON')
self.node.setDriver('GV0',1 , True, True)
#self.node.reportCmd('DON')
elif ctrl == 0:
self.yoSwitch.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
#self.node.reportCmd('DOF')
elif ctrl == 2: #toggle
state = str(self.yoSwitch.getState()).upper()
if state == 'ON':
self.yoSwitch.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
#self.node.reportCmd('DOF')
elif state == 'OFF':
self.yoSwitch.setState('ON')
self.node.setDriver('GV0',1 , True, True)
#self.node.reportCmd('DON')
elif ctrl == 5:
logging.info('switchControl set Delays Executed: {} {}'.format(self.onDelay, self.offDelay))
#self.yolink.setMultiOutDelay(self.port, self.onDelay, self.offDelay)
self.node.setDriver('GV1', self.onDelay * 60, True, True)
self.node.setDriver('GV2', self.offDelay * 60 , True, True)
self.yoSwitch.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
#Unknown remains unknown
def prepOnDelay(self, command ):
self.onDelay =int(command.get('value'))
logging.info('udiYoSwitch prepOnDelay {}'.format(self.onDelay))
#self.yoSwitch.setOnDelay(delay)
#self.node.setDriver('GV1', delay*60, True, True)
def prepOffDelay(self, command):
self.offDelay =int(command.get('value'))
logging.info('udiYoSwitch prepOffDelay {}'.format(self.offDelay))
#self.yoSwitch.setOffDelay(delay)
#self.node.setDriver('GV2', delay*60, True, True)
def program_delays(self, command):
logging.info('udiYoOutlet program_delays {}'.format(command))
query = command.get("query")
self.onDelay = int(query.get("ondelay.uom44"))
self.offDelay = int(query.get("offdelay.uom44"))
self.node.setDriver('GV1', self.onDelay * 60, True, True)
self.node.setDriver('GV2', self.offDelay * 60 , True, True)
self.yoSwitch.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
def update(self, command = None):
logging.info('udiYoSwitch Update Status')
self.yoSwitch.refreshDevice()
#self.yoSwitch.refreshSchedules()
def lookup_schedule(self, command):
logging.info('udiYoSwitch lookup_schedule {}'.format(command))
self.schedule_selected = int(command.get('value'))
self.yoSwitch.refreshSchedules()
'''
def define_schedule(self, command):
logging.info('udiYoSwitch define_schedule {}'.format(command))
StartH = 25
StartM = 0
StopH = 25
StopM = 0
query = command.get("query")
self.schedule_selected = int(query.get('index.uom25'))
tmp = int(query.get('active.uom25'))
self.activated = (tmp == 1)
if 'startH.uom19' in query:
StartH = int(query.get('startH.uom19'))
StartM = int(query.get('startM.uom44'))
if 'stopH.uom19' in query:
StopH = int(query.get('stopH.uom19'))
StopM = int(query.get('stopM.uom44'))
binDays = int(query.get('bindays.uom25'))
params = {}
params['index'] = str(self.schedule_selected )
params['isValid'] = self.activated
params['on'] = str(StartH)+':'+str(StartM)
params['off'] = str(StopH)+':'+str(StopM)
params['week'] = binDays
self.yoSwitch.setSchedule(self.schedule_selected, params)
'''
def define_schedule(self, command):
logging.info('udiYoSwitch define_schedule {}'.format(command))
query = command.get("query")
self.schedule_selected, params = self.prep_schedule(query)
self.yoSwitch.setSchedule(self.schedule_selected, params)
def control_schedule(self, command):
logging.info('udiYoSwitch control_schedule {}'.format(command))
query = command.get("query")
self.activated, self.schedule_selected = self.activate_schedule(query)
self.yoSwitch.activateSchedule(self.schedule_selected, self.activated)
commands = {
'UPDATE' : update,
'QUERY' : update,
'DON' : set_switch_on,
'DOF' : set_switch_off,
'DFON' : set_switch_fon,
'DFOF' : set_switch_foff,
'SWCTRL' : switchControl,
'ONDELAY' : prepOnDelay,
'OFFDELAY' : prepOffDelay,
'DELAY_CTRL' : program_delays,
'LOOKUP_SCH' : lookup_schedule,
'DEFINE_SCH' : define_schedule,
'CTRL_SCH' : control_schedule,
}