forked from OpenZWave/python-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
365 lines (312 loc) · 13.3 KB
/
Copy pathcommon.py
File metadata and controls
365 lines (312 loc) · 13.3 KB
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
# -*- coding: utf-8 -*-
"""
.. module:: tests
This file is part of **python-openzwave** project https://github.com/OpenZWave/python-openzwave.
:platform: Unix, Windows, MacOS X
:sinopsis: openzwave API
.. moduleauthor: bibi21000 aka Sébastien GALLET <bibi21000@gmail.com>
License : GPL(v3)
**python-openzwave** 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.
**python-openzwave** 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 python-openzwave. If not, see http://www.gnu.org/licenses.
"""
import sys, os
import time
import unittest
import threading
import logging
import json as mjson
import shutil
from nose.plugins.skip import SkipTest
import libopenzwave
from tests.common import SLEEP
from tests.common import TestPyZWave
class TestLib(TestPyZWave):
"""
Parent test class for lib
"""
SIGNAL_NETWORK_FAILED = 'NetworkFailed'
SIGNAL_NETWORK_STARTED = 'NetworkStarted'
SIGNAL_NETWORK_READY = 'NetworkReady'
SIGNAL_NETWORK_STOPPED = 'NetworkStopped'
SIGNAL_NETWORK_RESETTED = 'DriverResetted'
SIGNAL_NETWORK_AWAKED = 'DriverAwaked'
SIGNAL_DRIVER_FAILED = 'DriverFailed'
SIGNAL_DRIVER_READY = 'DriverReady'
SIGNAL_DRIVER_RESET = 'DriverReset'
SIGNAL_DRIVER_REMOVED = 'DriverRemoved'
SIGNAL_GROUP = 'Group'
SIGNAL_NODE = 'Node'
SIGNAL_NODE_ADDED = 'NodeAdded'
SIGNAL_NODE_EVENT = 'NodeEvent'
SIGNAL_NODE_NAMING = 'NodeNaming'
SIGNAL_NODE_NEW = 'NodeNew'
SIGNAL_NODE_PROTOCOL_INFO = 'NodeProtocolInfo'
SIGNAL_NODE_READY = 'NodeReady'
SIGNAL_NODE_REMOVED = 'NodeRemoved'
SIGNAL_SCENE_EVENT = 'SceneEvent'
SIGNAL_VALUE = 'Value'
SIGNAL_VALUE_ADDED = 'ValueAdded'
SIGNAL_VALUE_CHANGED = 'ValueChanged'
SIGNAL_VALUE_REFRESHED = 'ValueRefreshed'
SIGNAL_VALUE_REMOVED = 'ValueRemoved'
SIGNAL_POLLING_ENABLED = 'PollingEnabled'
SIGNAL_POLLING_DISABLED = 'PollingDisabled'
SIGNAL_CREATE_BUTTON = 'CreateButton'
SIGNAL_DELETE_BUTTON = 'DeleteButton'
SIGNAL_BUTTON_ON = 'ButtonOn'
SIGNAL_BUTTON_OFF = 'ButtonOff'
SIGNAL_ESSENTIAL_NODE_QUERIES_COMPLETE = 'EssentialNodeQueriesComplete'
SIGNAL_NODE_QUERIES_COMPLETE = 'NodeQueriesComplete'
SIGNAL_AWAKE_NODES_QUERIED = 'AwakeNodesQueried'
SIGNAL_ALL_NODES_QUERIED = 'AllNodesQueried'
SIGNAL_ALL_NODES_QUERIED_SOME_DEAD = 'AllNodesQueriedSomeDead'
SIGNAL_MSG_COMPLETE = 'MsgComplete'
SIGNAL_NOTIFICATION = 'Notification'
SIGNAL_CONTROLLER_COMMAND = 'ControllerCommand'
SIGNAL_CONTROLLER_WAITING = 'ControllerWaiting'
STATE_STOPPED = 0
STATE_FAILED = 1
STATE_RESETTED = 3
STATE_STARTED = 5
STATE_AWAKED = 7
STATE_READY = 10
@classmethod
def setUpClass(self):
super(TestPyZWave, self).setUpClass()
self.options = None
self.homeid = None
self.ready = False
self.manager = None
self.nodes = {}
self.driver_state = None
self.driver_ready = None
self.driver_removed = None
self.driver_failed = None
self.driver_reset = None
self.network_state = None
self.network_ready = None
self.network_awake = None
@classmethod
def tearDownClass(self):
self.stop_lib()
super(TestPyZWave, self).tearDownClass()
def start_lib(self):
if self.options is None:
self.options = libopenzwave.PyOptions(config_path="openzwave/config", \
user_path=self.userpath, cmd_line="--logging false")
self.options.lock()
if self.manager is None:
self.homeid = None
self.nodes = {}
self.ready = False
self.driver_state = None
self.driver_ready = None
self.driver_removed = None
self.driver_failed = None
self.driver_reset = None
self.network_state = None
self.network_ready = None
self.network_awake = None
self.manager = libopenzwave.PyManager()
self.manager.create()
self.manager.addWatcher(self.zwcallback)
time.sleep(1.0)
self.manager.addDriver(self.device)
time.sleep(5.0)
@classmethod
def stop_lib(self):
if self.manager is not None:
self.manager.removeDriver(self.device)
time.sleep(1.0)
self.manager.removeWatcher(self.zwcallback)
time.sleep(1.0)
self.manager.destroy()
time.sleep(1.0)
self.manager = None
if self.options is not None:
self.options.destroy()
time.sleep(1.0)
self.options = None
self.homeid = None
self.ready = False
self.manager = None
self.driver_state = None
self.driver_ready = None
self.driver_removed = None
self.driver_failed = None
self.driver_reset = None
self.network_state = None
self.network_ready = None
self.network_awake = None
def zwcallback(self, args):
print('zwcallback args=[%s]' % args)
try:
notify_type = args['notificationType']
if notify_type == self.SIGNAL_DRIVER_FAILED:
self._handle_driver_failed(args)
elif notify_type == self.SIGNAL_DRIVER_READY:
self._handle_driver_ready(args)
elif notify_type == self.SIGNAL_DRIVER_RESET:
self._handle_driver_reset(args)
elif notify_type == self.SIGNAL_NODE_ADDED:
self._handle_node_added(args)
elif notify_type == self.SIGNAL_NODE_EVENT:
self._handle_node_event(args)
elif notify_type == self.SIGNAL_NODE_NAMING:
self._handle_node_naming(args)
elif notify_type == self.SIGNAL_NODE_NEW:
self._handle_node_new(args)
elif notify_type == self.SIGNAL_NODE_PROTOCOL_INFO:
self._handle_node_protocol_info(args)
elif notify_type == self.SIGNAL_NODE_READY:
self._handleNodeReady(args)
elif notify_type == self.SIGNAL_NODE_REMOVED:
self._handle_node_removed(args)
elif notify_type == self.SIGNAL_GROUP:
self._handle_group(args)
elif notify_type == self.SIGNAL_SCENE_EVENT:
self._handle_scene_event(args)
elif notify_type == self.SIGNAL_VALUE_ADDED:
self._handle_value_added(args)
elif notify_type == self.SIGNAL_VALUE_CHANGED:
self._handle_value_changed(args)
elif notify_type == self.SIGNAL_VALUE_REFRESHED:
self._handle_value_refreshed(args)
elif notify_type == self.SIGNAL_VALUE_REMOVED:
self._handle_value_removed(args)
elif notify_type == self.SIGNAL_POLLING_DISABLED:
self._handle_polling_disabled(args)
elif notify_type == self.SIGNAL_POLLING_ENABLED:
self._handle_polling_enabled(args)
elif notify_type == self.SIGNAL_CREATE_BUTTON:
self._handle_create_button(args)
elif notify_type == self.SIGNAL_DELETE_BUTTON:
self._handle_delete_button(args)
elif notify_type == self.SIGNAL_BUTTON_ON:
self._handle_button_on(args)
elif notify_type == self.SIGNAL_BUTTON_OFF:
self._handle_button_off(args)
elif notify_type == self.SIGNAL_ALL_NODES_QUERIED:
self._handle_all_nodes_queried(args)
elif notify_type == self.SIGNAL_ALL_NODES_QUERIED_SOME_DEAD:
self._handle_all_nodes_queried_some_dead(args)
elif notify_type == self.SIGNAL_AWAKE_NODES_QUERIED:
self._handle_awake_nodes_queried(args)
elif notify_type == self.SIGNAL_ESSENTIAL_NODE_QUERIES_COMPLETE:
self._handle_essential_node_queries_complete(args)
elif notify_type == self.SIGNAL_NODE_QUERIES_COMPLETE:
self._handle_node_queries_complete(args)
elif notify_type == self.SIGNAL_MSG_COMPLETE:
self._handle_msg_complete(args)
elif notify_type == self.SIGNAL_NOTIFICATION:
self._handle_notification(args)
elif notify_type == self.SIGNAL_DRIVER_REMOVED:
self._handle_driver_removed(args)
elif notify_type == self.SIGNAL_CONTROLLER_COMMAND:
self._handle_controller_command(args)
else:
print('Skipping unhandled notification [%s]' % args)
except:
import sys, traceback
print('Error in manager callback : %s' % traceback.format_exception(*sys.exc_info()))
def wait_for_ready(self):
for i in range(0,15):
if self.ready == True:
break
else:
time.sleep(1.0)
def wait_for_queue(self):
for i in range(0,60):
if self.manager.getSendQueueCount(self.homeid) <= 0:
break
else:
time.sleep(0.5)
def _handle_driver_failed(self, args):
self.driver_state = "DriverFailed"
self.driver_failed = True
print('Z-Wave Notification DriverFailed : %s' % args)
def _handle_driver_ready(self, args):
self.driver_state = "DriverReady"
self.homeid = int(args['homeId'])
self.driver_ready = True
print('Z-Wave Notification DriverReady : %s' % args)
def _handle_driver_reset(self, args):
self.driver_state = "DriverReset"
self.driver_reset = True
print('Z-Wave Notification DriverReset : %s' % args)
def _handle_driver_removed(self, args):
print('Z-Wave Notification DriverRemoved : %s' % args)
self.driver_state = "DriverRemoved"
self.driver_removed = True
def _handle_group(self, args):
print('Z-Wave Notification Group : %s' % args)
def _handle_node(self, node):
print('Z-Wave Notification Node : %s' % node)
def _handle_node_added(self, args):
print('Z-Wave Notification NodeAdded : %s' % args)
self.nodes[args['nodeId']] = {}
def _handle_scene_event(self, args):
print('Z-Wave Notification SceneEvent : %s' % args)
def _handle_node_event(self, args):
print('Z-Wave Notification NodeEvent : %s' % args)
def _handle_node_naming(self, args):
print('Z-Wave Notification NodeNaming : %s' % args)
self.nodes[args['nodeId']] = {'NodeNaming' : True }
def _handle_node_new(self, args):
print('Z-Wave Notification NodeNew : %s' % args)
def _handle_node_protocol_info(self, args):
print('Z-Wave Notification NodeProtocolInfo : %s' % args)
def _handle_node_removed(self, args):
print('Z-Wave Notification NodeRemoved : %s' % args)
def _handle_essential_node_queries_complete(self, args):
print('Z-Wave Notification EssentialNodeQueriesComplete : %s' % args)
def _handle_node_queries_complete(self, args):
print('Z-Wave Notification NodeQueriesComplete : %s' % args)
def _handle_all_nodes_queried(self, args):
self.network_state = "AllNodesQueried"
self.network_ready = True
self.ready = True
print('Z-Wave Notification AllNodesQueried : %s' % args)
def _handle_all_nodes_queried_some_dead(self, args):
self.network_state = "AllNodesQueriedSomeDead"
self.network_ready = True
self.ready = True
print('Z-Wave Notification AllNodesQueriedSomeDead : %s' % args)
def _handle_awake_nodes_queried(self, args):
self.network_state = "AwakeNodesQueried"
self.network_awake = True
print('Z-Wave Notification AwakeNodesQueried : %s' % args)
def _handle_polling_disabled(self, args):
print('Z-Wave Notification PollingDisabled : %s' % args)
def _handle_polling_enabled(self, args):
print('Z-Wave Notification PollingEnabled : %s' % args)
def _handle_create_button(self, args):
print('Z-Wave Notification CreateButton : %s' % args)
def _handle_delete_button(self, args):
print('Z-Wave Notification DeleteButton : %s' % args)
def _handle_button_on(self, args):
print('Z-Wave Notification ButtonOn : %s' % args)
def _handle_button_off(self, args):
print('Z-Wave Notification ButtonOff : %s' % args)
def _handle_value_added(self, args):
print('Z-Wave Notification ValueAdded : %s' % args)
def _handle_value_changed(self, args):
print('Z-Wave Notification ValueChanged : %s' % args)
def _handle_value_refreshed(self, args):
print('Z-Wave Notification ValueRefreshed : %s' % args)
def _handle_value_removed(self, args):
print('Z-Wave Notification ValueRemoved : %s' % args)
def _handle_notification(self, args):
print('Z-Wave Notification : %s' % args)
def _handle_controller_command(self, args):
print('Z-Wave ControllerCommand : %s' % args)
def _handle_msg_complete(self, args):
print('Z-Wave Notification MsgComplete : %s' % args)