-
Notifications
You must be signed in to change notification settings - Fork 1
/
udiYoDimmerV2.py
319 lines (271 loc) · 12.6 KB
/
udiYoDimmerV2.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
#!/usr/bin/env python3
"""
Polyglot TEST v3 node server
MIT License
"""
try:
import udi_interface
logging = udi_interface.LOGGER
#logging = getlogger('udiDimmerV2')
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.INFO)
from os import truncate
#import udi_interface
#import sys
import time
from yolinkDimmerV3 import YoLinkDim
class udiYoDimmer(udi_interface.Node):
from udiYolinkLib import prep_schedule, activate_schedule, update_schedule_data, node_queue, wait_for_node_done, mask2key
id = 'yodimmer'
drivers = [
{'driver': 'GV0', 'value': 99, 'uom': 25},
{'driver': 'GV1', 'value': 0, 'uom': 57},
{'driver': 'GV2', 'value': 0, 'uom': 57},
{'driver': 'GV3', 'value': 0, 'uom': 51},
{'driver': 'GV13', 'value': 0, 'uom': 25}, #Schedule index/no
{'driver': 'GV14', 'value': 99, 'uom': 25}, # Active
{'driver': 'GV15', 'value': 99, 'uom': 25}, #start Hour
{'driver': 'GV16', 'value': 99, 'uom': 25}, #start Min
{'driver': 'GV17', 'value': 99, 'uom': 25}, #stop Hour
{'driver': 'GV18', 'value': 99, 'uom': 25}, #stop Min
{'driver': 'GV19', 'value': 0, 'uom': 25}, #days
{'driver': 'ST', 'value': 0, 'uom': 25},
{'driver': 'GV20', 'value': 99, 'uom': 25},
]
'''
drivers = [
'GV0' = Dinner State
'GV1' = OnDelay
'GV2' = OffDelay
'GV3' = Dimmer Brightness
#'GV4' = Energy
'ST' = Online/Connected
'GV20' = Suspended state
]
'''
def __init__(self, polyglot, primary, address, name, yoAccess, deviceInfo):
super().__init__( polyglot, primary, address, name)
logging.debug('udiYoDimmer INIT- {}'.format(deviceInfo['name']))
self.devInfo = deviceInfo
self.yoAccess = yoAccess
self.yoDimmer = None
self.node_ready = False
self.timer_cleared = True
self.n_queue = [] # one queue for all
self.last_state = ''
self.timer_update = 5
self.timer_expires = 0
self.onDelay = 0
self.offDelay = 0
self.schedule_selected = 0
self.brightness = 50
#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 - udiYoDimmer')
self.node.setDriver('ST', 0, True, True)
self.yoDimmer = YoLinkDim(self.yoAccess, self.devInfo, self.updateStatus)
time.sleep(2)
self.yoDimmer.initNode()
time.sleep(2)
#self.node.setDriver('ST', 1, True, True)
self.yoDimmer.delayTimerCallback (self.updateDelayCountdown, self.timer_update )
self.yoDimmer.refreshSchedules()
self.node_ready = True
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 udiyoDimmer')
self.node.setDriver('ST', 0, True, True)
self.yoDimmer.shut_down()
#if self.node:
# self.poly.delNode(self.node.address)
def checkOnline(self):
self.yoDimmer.refreshDevice()
def checkDataUpdate(self):
if self.yoDimmer.data_updated():
self.updateData()
def updateData(self):
if self.node is not None:
state = self.yoDimmer.getState().upper()
if self.yoDimmer.online:
self.node.setDriver('ST', 1, True, True)
if state == 'ON':
self.node.setDriver('GV0', 1, True, True)
#if self.last_state != state:
# self.node.reportCmd('DON')
elif state == 'OFF':
self.node.setDriver('GV0', 0, True, True)
#if self.last_state != state:
# self.node.reportCmd('DOF')
else:
self.node.setDriver('GV0', 99, True, True)
self.last_state = state
self.node.setDriver('GV3', self.yoDimmer.brightness, True, True)
#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, True, False)
self.node.setDriver('GV2', 0, True, False)
if self.yoDimmer.suspended:
self.node.setDriver('GV20', 1, True, True)
else:
self.node.setDriver('GV20', 0)
else:
#self.node.setDriver('ST', 0, True, True)
self.node.setDriver('GV0', 99, True, True)
self.node.setDriver('GV1', 0, True, False)
self.node.setDriver('GV2', 0, True, False)
self.node.setDriver('GV20', 2, True, True)
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)
sch_info = self.yoDimmer.getScheduleInfo(self.schedule_selected)
self.update_schedule_data(sch_info, self.schedule_selected)
def updateStatus(self, data):
logging.info('updateStatus - Switch')
self.yoDimmer.updateStatus(data)
self.updateData()
def set_switch_on(self, command = None):
logging.info('udiyoDimmer set_switch_on')
self.yoDimmer.setState('ON')
self.node.setDriver('GV0',1 , True, True)
self.node.reportCmd('DON')
def set_switch_off(self, command = None):
logging.info('udiYoDimmer set_switch_off')
self.yoDimmer.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
self.node.reportCmd('DOF')
def set_switch_fon(self, command = None):
logging.info('udiyoDimmer set_switch_on')
self.yoDimmer.setState('ON')
self.node.setDriver('GV0',1 , True, True)
self.node.reportCmd('DFON')
def set_switch_foff(self, command = None):
logging.info('udiYoDimmer set_switch_off')
self.yoDimmer.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
self.node.reportCmd('DFOF')
def set_dimmer_level(self, command = None):
brightness = int(command.get('value'))
#self.brightness = brightness
logging.info('udiYoDimmer set_dimmer_level:{}'.format(brightness) )
if 0 >= brightness :
#self.yoDimmer.setState('OFF')
brightness = 0
elif 100 <= brightness:
brightness = 100
self.yoDimmer.setBrightness(brightness) #????
self.node.setDriver('GV3',brightness , True, True)
def switchControl(self, command):
logging.info('udiYoDimmer switchControl')
ctrl = command.get('value')
logging.debug('switchControl : {}'.format(ctrl))
if ctrl == 1:
self.yoDimmer.setState('ON')
self.node.setDriver('GV0',1 , True, True)
self.node.reportCmd('DON')
elif ctrl == 0:
self.yoDimmer.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
self.node.reportCmd('DOF')
elif ctrl == 2: #toggle
state = str(self.yoDimmer.getState()).upper()
logging.debug('switchControl : {}, {}'.format(ctrl, state))
if state == 'ON':
self.yoDimmer.setState('OFF')
self.node.setDriver('GV0',0 , True, True)
self.node.reportCmd('DOF')
elif state == 'OFF':
self.yoDimmer.setState('ON')
self.node.setDriver('GV0',1 , True, True)
self.node.reportCmd('DON')
#Unknown remains unknown
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.yoDimmer.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
def setOnDelay(self, command ):
logging.info('udiYoDimmer setOnDelay')
self.onDelay =int(command.get('value'))
self.yoDimmer.setOnDelay(self.onDelay )
self.node.setDriver('GV1', self.onDelay *60, True, True)
def setOffDelay(self, command):
logging.info('udiYoDimmer setOffDelay')
self.offDelay =int(command.get('value'))
self.yoDimmer.setOffDelay(self.offDelay)
self.node.setDriver('GV2', self.offDelay*60, True, True)
def program_delays(self, command):
logging.info('udiYoDimmer 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.yoDimmer.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
def lookup_schedule(self, command):
logging.info('udiYoDimmer lookup_schedule {}'.format(command))
self.schedule_selected = int(command.get('value'))
self.yoDimmer.refreshSchedules()
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.yoDimmer.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.yoDimmer.activateSchedule(self.schedule_selected, self.activated)
def update(self, command = None):
logging.info('udiYoDimmer Update Status')
self.yoDimmer.refreshDevice()
#self.yoDimmer.refreshSchedules()
commands = {
'UPDATE': update,
'QUERY' : update,
'DON' : set_switch_on,
'DOF' : set_switch_off,
'DFON' : set_switch_fon,
'DFOF' : set_switch_foff,
'SWCTRL': switchControl,
'DIMLVL' : set_dimmer_level,
'ONDELAY' : setOnDelay,
'OFFDELAY' : setOffDelay,
'DELAY_CTRL' : program_delays,
'LOOKUP_SCH' : lookup_schedule,
'DEFINE_SCH' : define_schedule,
'CTRL_SCH' : control_schedule,
}