-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathoption.py
More file actions
executable file
·391 lines (293 loc) · 13.2 KB
/
option.py
File metadata and controls
executable file
·391 lines (293 loc) · 13.2 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
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
# -*- coding: utf-8 -*-
"""
.. module:: openzwave.option
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 os
from platform import system as platform_system
import libopenzwave
from libopenzwave import PyLogLevels
from openzwave.object import ZWaveException
from openzwave.singleton import Singleton
# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
"""NullHandler logger for python 2.6"""
def emit(self, record):
pass
logger = logging.getLogger('openzwave')
logger.addHandler(NullHandler())
VENDOR_IDS = ('0658',)
def _get_z_stick():
try:
import serial.tools.list_ports
except ImportError:
return None
for port in serial.tools.list_ports.comports(include_links=False):
if port.vid is None:
continue
if port.product is not None and 'Zigbee' in port.product:
continue
if port.interface is not None and 'Zigbee' in port.interface:
continue
if port.description is not None and 'Zigbee' in port.description:
continue
for vid in VENDOR_IDS:
if vid.upper() == hex(port.vid)[2:].upper().zfill(4):
return port.device
return None
class ZWaveOption(libopenzwave.PyOptions):
"""
Represents a Zwave option used to start the manager.
"""
def __init__(self, device=None, config_path=None, user_path=None, cmd_line=None):
"""
Create an option object and check that parameters are valid.
:param device: The device to use or None for auto detection (pyserial needs to be installed for auto detection).
:type device: str
:param config_path: The openzwave config directory. If None, try to configure automatically.
:type config_path: str
:param user_path: The user directory
:type user_path: str
:param cmd_line: The "command line" options of the openzwave library
:type cmd_line: str
"""
if device is None:
device = _get_z_stick()
if platform_system() == 'Windows':
if device and not device.startswith('\\\\.\\'):
device = '\\\\.\\' + device
self._device = device
else:
#For linux
try:
if os.path.exists(device):
if os.access(device, os.R_OK) and os.access(device, os.W_OK):
self._device = device
else:
import sys, traceback
raise ZWaveException(u"Can't write to device %s : %s" % (device, traceback.format_exception(*sys.exc_info())))
else:
import sys, traceback
raise ZWaveException(u"Can't find device %s : %s" % (device, traceback.format_exception(*sys.exc_info())))
except:
import sys, traceback
raise ZWaveException(u"Error when retrieving device %s : %s" % (device, traceback.format_exception(*sys.exc_info())))
libopenzwave.PyOptions.__init__(self, config_path=config_path, user_path=user_path, cmd_line=cmd_line)
def set_log_file(self, logfile):
"""
Set the log file location.
:param logfile: The location of the log file
:type logfile: str
"""
return self.addOptionString("LogFileName", logfile, False)
def set_log_file_path(self, logfilePath):
"""
Set the log file location (directory). The logfile parameter just sets
the name, and defaults to userpath. Uncool if you want logs in RAM, e.g.
:param logfilePath: The location of the log file
:type logfilePath: str
"""
return self.addOptionString("LogFilePath", logfilePath, False)
def set_logging(self, status):
"""
Set the status of logging.
:param status: True to activate logs, False to disable
:type status: bool
"""
return self.addOptionBool("Logging", status)
def set_append_log_file(self, status):
"""
Append new session logs to existing log file (false = overwrite).
:param status:
:type status: bool
"""
return self.addOptionBool("AppendLogFile", status)
def set_console_output(self, status):
"""
Display log information on console (as well as save to disk).
:param status:
:type status: bool
"""
return self.addOptionBool("ConsoleOutput", status)
def set_save_log_level(self, level):
"""
Save (to file) log messages equal to or above LogLevel_Detail.
:param level:
:type level: PyLogLevels
* 'None':"Disable all logging"
* 'Always':"These messages should always be shown"
* 'Fatal':"A likely fatal issue in the library"
* 'Error':"A serious issue with the library or the network"
* 'Warning':"A minor issue from which the library should be able to recover"
* 'Alert':"Something unexpected by the library about which the controlling application should be aware"
* 'Info':"Everything Is working fine...these messages provide streamlined feedback on each message"
* 'Detail':"Detailed information on the progress of each message" /
* 'Debug':"Very detailed information on progress that will create a huge log file quickly"
* 'StreamDetail':"Will include low-level byte transfers from controller to buffer to application and back"
* 'Internal':"Used only within the log class (uses existing timestamp, etc.)"
"""
return self.addOptionInt("SaveLogLevel", PyLogLevels[level]['value'])
def set_queue_log_level(self, level):
"""
Save (in RAM) log messages equal to or above LogLevel_Debug.
:param level:
:type level: PyLogLevels
* 'None':"Disable all logging"
* 'Always':"These messages should always be shown"
* 'Fatal':"A likely fatal issue in the library"
* 'Error':"A serious issue with the library or the network"
* 'Warning':"A minor issue from which the library should be able to recover"
* 'Alert':"Something unexpected by the library about which the controlling application should be aware"
* 'Info':"Everything Is working fine...these messages provide streamlined feedback on each message"
* 'Detail':"Detailed information on the progress of each message" /
* 'Debug':"Very detailed information on progress that will create a huge log file quickly"
* 'StreamDetail':"Will include low-level byte transfers from controller to buffer to application and back"
* 'Internal':"Used only within the log class (uses existing timestamp, etc.)"
"""
return self.addOptionInt("QueueLogLevel", PyLogLevels[level])
def set_dump_trigger_level(self, level):
"""
Default is to never dump RAM-stored log messages.
:param level:
:type level: PyLogLevels
* 'None':"Disable all logging"
* 'Always':"These messages should always be shown"
* 'Fatal':"A likely fatal issue in the library"
* 'Error':"A serious issue with the library or the network"
* 'Warning':"A minor issue from which the library should be able to recover"
* 'Alert':"Something unexpected by the library about which the controlling application should be aware"
* 'Info':"Everything Is working fine...these messages provide streamlined feedback on each message"
* 'Detail':"Detailed information on the progress of each message" /
* 'Debug':"Very detailed information on progress that will create a huge log file quickly"
* 'StreamDetail':"Will include low-level byte transfers from controller to buffer to application and back"
* 'Internal':"Used only within the log class (uses existing timestamp, etc.)"
"""
return self.addOptionInt("DumpTriggerLevel", PyLogLevels[level])
def set_associate(self, status):
"""
Enable automatic association of the controller with group one of every device.
:param status: True to enable logs, False to disable
:type status: bool
"""
return self.addOptionBool("Associate", status)
def set_exclude(self, commandClass):
"""
Remove support for the seted command classes.
:param commandClass: The command class to exclude
:type commandClass: str
"""
return self.addOptionString("Exclude", commandClass, True)
def set_include(self, commandClass):
"""
Only handle the specified command classes. The Exclude option is ignored if anything is seted here.
:param commandClass: The location of the log file
:type commandClass: str
"""
return self.addOptionString("Include", commandClass, True)
def set_notify_transactions(self, status):
"""
Notifications when transaction complete is reported.
:param status: True to enable, False to disable
:type status: bool
"""
return self.addOptionBool("NotifyTransactions", status)
def set_interface(self, port):
"""
Identify the serial port to be accessed (TODO: change the code so more than one serial port can be specified and HID).
:param port: The serial port
:type port: str
"""
return self.addOptionString("Interface", port, True)
def set_save_configuration(self, status):
"""
Save the XML configuration upon driver close.
:param status: True to enable, False to disable
:type status: bool
"""
return self.addOptionBool("SaveConfiguration", status)
def set_driver_max_attempts(self, attempts):
"""
Set the driver max attempts before raising an error.
:param attempts: Number of attempts
:type attempts: int
"""
return self.addOptionInt("DriverMaxAttempts", attempts)
def set_poll_interval(self, interval):
"""
30 seconds (can easily poll 30 values in this time; ~120 values is the effective limit for 30 seconds).
:param interval: interval in seconds
:type interval: int
"""
return self.addOptionInt("PollInterval", interval)
def set_interval_between_polls(self, status):
"""
Notifications when transaction complete is reported.
:param status: if false, try to execute the entire poll set within the PollInterval time frame. If true, wait for PollInterval milliseconds between polls
:type status: bool
"""
return self.addOptionBool("IntervalBetweenPolls", status)
def set_suppress_value_refresh(self, status):
"""
if true, notifications for refreshed (but unchanged) values will not be sent.
:param status: True to enable, False to disable
:type status: bool
"""
return self.addOptionBool("SuppressValueRefresh", status)
def set_security_strategy(self, strategy='SUPPORTED'):
"""
Should we encrypt CC's that are available via both clear text and Security CC?
:param strategy: The security strategy : SUPPORTED|ESSENTIAL|CUSTOM
:type strategy: str
"""
return self.addOptionString("SecurityStrategy", strategy, False)
def set_custom_secured_cc(self, custom_cc='0x62,0x4c,0x63'):
"""
What List of Custom CC should we always encrypt if SecurityStrategy is CUSTOM.
:param custom_cc: List of Custom CC
:type custom_cc: str
"""
return self.addOptionString("CustomSecuredCC", custom_cc, False)
@property
def device(self):
"""
The device used by the controller.
:rtype: str
"""
return self._device
@property
def config_path(self):
"""
The config path.
:rtype: str
"""
return self._config_path
@property
def user_path(self):
"""
The config path.
:rtype: str
"""
return self._user_path
class ZWaveOptionSingleton(ZWaveOption):
"""
Represents a singleton Zwave option used to start the manager.
"""
__metaclass__ = Singleton