forked from OpenZWave/python-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyozw_check.py
More file actions
284 lines (260 loc) · 12.3 KB
/
Copy pathpyozw_check.py
File metadata and controls
284 lines (260 loc) · 12.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
#!python
# -*- coding: utf-8 -*-
"""
This file is part of **python-openzwave** project https://github.com/OpenZWave/python-openzwave.
:platform: Unix, Windows, MacOS X
.. 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 logging
h = logging.NullHandler()
logger = logging.getLogger(__name__).addHandler(h)
import time
import argparse
network_state = None
home_id = None
args = None
def imports(args):
if args.timeout is None:
args.timeout = 2
if args.output == 'txt':
print("-------------------------------------------------------------------------------")
print("Import libs")
print("Try to import libopenzwave")
import libopenzwave
print("Try to import libopenzwave.PyLogLevels")
from libopenzwave import PyLogLevels
print("Try to get options")
options = libopenzwave.PyOptions(user_path=".", cmd_line="--logging false")
options.lock()
time.sleep(0.5)
print("Try to get manager")
manager = libopenzwave.PyManager()
manager.create()
print("Try to get python_openzwave version")
print(manager.getPythonLibraryVersionNumber())
print("Try to get python_openzwave full version")
print(manager.getPythonLibraryVersion())
print("Try to get openzwave version")
print(manager.getPythonLibraryVersion())
print("Try to get openzwave version")
print(manager.getOzwLibraryVersion())
print("Try to get default config path")
print(libopenzwave.configPath())
print("Try to destroy manager")
manager.destroy()
print("Try to destroy options")
options.destroy()
time.sleep(0.5)
print("Try to import openzwave (API)")
import openzwave
elif args.output == 'raw':
import libopenzwave
from libopenzwave import PyLogLevels
options = libopenzwave.PyOptions(user_path=".", cmd_line="--logging false")
options.lock()
time.sleep(0.5)
manager = libopenzwave.PyManager()
manager.create()
print("{0}|{1}|{2}|{3}".format(
manager.getOzwLibraryVersion(),
manager.getPythonLibraryVersionNumber(),
manager.getPythonLibraryVersion(),
libopenzwave.configPath()
))
manager.destroy()
options.destroy()
time.sleep(0.5)
import openzwave
def zwcallback(zwargs):
import libopenzwave
global args
if args.output == 'txt':
print('--------------------------------- zwcallback with args=[%s]' % zwargs)
notify_type = zwargs['notificationType']
global network_state
network_state = notify_type
if notify_type == "DriverReady":
global home_id
home_id = zwargs['homeId']
#~ print("Received {0} : {1}".format(notify_type,libopenzwave.PyNotifications[notify_type].doc))
if args.output == 'txt':
print("Received {0}".format(notify_type))
def init_device(args):
global home_id
if args.timeout is None:
args.timeout = 15
if args.output == 'txt':
print("-------------------------------------------------------------------------------")
print("Intialize device {0}".format(args.device))
import libopenzwave
print("Try to get options")
options = libopenzwave.PyOptions(config_path=args.config_path, user_path=args.user_path, cmd_line="--logging true")
options.lock()
time.sleep(1.0)
print("Try to get manager")
manager = libopenzwave.PyManager()
manager.create()
print("Try to get python_openzwave version")
print(manager.getPythonLibraryVersionNumber())
print("Try to get python_openzwave full version")
print(manager.getPythonLibraryVersion())
print("Try to get openzwave version")
print(manager.getOzwLibraryVersion())
print("Try to get default config path")
print(libopenzwave.configPath())
print("Try to add watcher")
manager.addWatcher(zwcallback)
time.sleep(1.0)
manager.addDriver(args.device)
print("Wait for notifications ({0}s)".format(args.timeout))
next_print = args.timeout/10
delta = 0.1
for i in range(0, int(args.timeout/delta)):
time.sleep(delta)
next_print -= delta
if next_print < 0:
next_print = args.timeout/10
print('.')
print("Try to remove driver")
manager.removeDriver(args.device)
time.sleep(3.0)
print("Try to remove watcher")
manager.removeWatcher(zwcallback)
print("Try to destroy manager")
manager.destroy()
time.sleep(0.2)
print("Try to destroy options")
options.destroy()
time.sleep(1.0)
print("Retrieve HomeID")
print("{0:08x}".format(home_id))
elif args.output == 'raw':
import libopenzwave
options = libopenzwave.PyOptions(config_path=args.config_path, user_path=args.user_path, cmd_line="--logging false")
options.lock()
time.sleep(1.0)
manager = libopenzwave.PyManager()
manager.create()
manager.addWatcher(zwcallback)
time.sleep(1.0)
manager.addDriver(args.device)
next_print = args.timeout/10
delta = 0.1
for i in range(0, int(args.timeout/delta)):
time.sleep(delta)
print("{0}|{1}|{2}|{3}|{4:08x}".format(
manager.getOzwLibraryVersion(),
manager.getPythonLibraryVersionNumber(),
manager.getPythonLibraryVersion(),
libopenzwave.configPath(),
home_id,
))
manager.removeDriver(args.device)
time.sleep(0.5)
manager.removeWatcher(zwcallback)
manager.destroy()
time.sleep(0.2)
options.destroy()
def list_nodes(args):
global home_id
if args.timeout is None:
args.timeout = 60*60*4+1
import openzwave
from openzwave.node import ZWaveNode
from openzwave.value import ZWaveValue
from openzwave.scene import ZWaveScene
from openzwave.controller import ZWaveController
from openzwave.network import ZWaveNetwork
from openzwave.option import ZWaveOption
#Define some manager options
print("-------------------------------------------------------------------------------")
print("Define options for device {0}".format(args.device))
options = ZWaveOption(device=args.device, config_path=args.config_path, user_path=args.user_path)
options.set_log_file("OZW_Log.log")
options.set_append_log_file(False)
options.set_console_output(False)
options.set_save_log_level("Debug")
options.set_logging(True)
options.lock()
print("Start network")
network = ZWaveNetwork(options, log=None)
delta = 0.5
for i in range(0, int(args.timeout/delta)):
time.sleep(delta)
if network.state >= network.STATE_AWAKED:
break
print("-------------------------------------------------------------------------------")
print("Network is awaked. Talk to controller.")
print("Get python_openzwave version : {}".format(network.controller.python_library_version))
print("Get python_openzwave config version : {}".format(network.controller.python_library_config_version))
print("Get python_openzwave flavor : {}".format(network.controller.python_library_flavor))
print("Get openzwave version : {}".format(network.controller.ozw_library_version))
print("Get config path : {}".format(network.controller.library_config_path))
print("Controller capabilities : {}".format(network.controller.capabilities))
print("Controller node capabilities : {}".format(network.controller.node.capabilities))
print("Nodes in network : {}".format(network.nodes_count))
print("-------------------------------------------------------------------------------")
if args.timeout > 1800:
print("You defined a really long timneout. Please use --help to change this feature.")
print("Wait for network ready ({0}s)".format(args.timeout))
for i in range(0, int(args.timeout/delta)):
time.sleep(delta)
if network.state == network.STATE_READY:
break
print("-------------------------------------------------------------------------------")
if network.state == network.STATE_READY:
print("Network is ready. Get nodes")
elif network.state == network.STATE_AWAKED:
print("Network is awake. Some sleeping devices may miss. You can increase timeout to get them. But will continue.")
else:
print("Network is still starting. You MUST increase timeout. But will continue.")
for node in network.nodes:
print("------------------------------------------------------------")
print("{} - Name : {} ( Location : {} )".format(network.nodes[node].node_id, network.nodes[node].name, network.nodes[node].location))
print(" {} - Ready : {} / Awake : {} / Failed : {}".format(network.nodes[node].node_id, network.nodes[node].is_ready, network.nodes[node].is_awake, network.nodes[node].is_failed))
print(" {} - Manufacturer : {} ( id : {} )".format(network.nodes[node].node_id, network.nodes[node].manufacturer_name, network.nodes[node].manufacturer_id))
print(" {} - Product : {} ( id : {} / type : {} / Version : {})".format(network.nodes[node].node_id, network.nodes[node].product_name, network.nodes[node].product_id, network.nodes[node].product_type, network.nodes[node].version))
print(" {} - Command classes : {}".format(network.nodes[node].node_id, network.nodes[node].command_classes_as_string))
print(" {} - Capabilities : {}".format(network.nodes[node].node_id, network.nodes[node].capabilities))
print(" {} - Neighbors : {} / Power level : {}".format(network.nodes[node].node_id, network.nodes[node].neighbors, network.nodes[node].get_power_level()))
print(" {} - Is sleeping : {} / Can wake-up : {} / Battery level : {}".format(network.nodes[node].node_id, network.nodes[node].is_sleeping, network.nodes[node].can_wake_up(), network.nodes[node].get_battery_level()))
print("------------------------------------------------------------")
print("Driver statistics : {}".format(network.controller.stats))
print("------------------------------------------------------------")
print("Stop network")
network.stop()
print("Exit")
def pyozw_parser():
parser = argparse.ArgumentParser(description='Run python_openzwave basics checks.')
parser.add_argument('-o', '--output', action='store', help='The format (txt, raw, ...)', choices=['txt', 'raw'], default='txt')
parser.add_argument('-d', '--device', action='store', help='The device port', default='/dev/ttyUSB0')
parser.add_argument('-m', '--imports', action='store_true', help='Import all libs', default=True)
parser.add_argument('-i', '--init_device', action='store_true', help='Intialize the device', default=False)
parser.add_argument('-l', '--list_nodes', action='store_true', help='List the nodes on zwave network', default=False)
parser.add_argument('-t', '--timeout', action='store',type=int, help='The default timeout for zwave network sniffing', default=None)
parser.add_argument('--config_path', action='store', help='The config_path for openzwave', default=None)
parser.add_argument('--user_path', action='store', help='The user_path for openzwave', default=".")
return parser
def main():
parser = pyozw_parser()
global args
args = parser.parse_args()
if args.init_device:
init_device(args)
elif args.list_nodes:
list_nodes(args)
elif args.imports:
imports(args)
if __name__ == '__main__':
main()