forked from OpenZWave/python-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.py
More file actions
executable file
·571 lines (459 loc) · 16.3 KB
/
Copy pathvalue.py
File metadata and controls
executable file
·571 lines (459 loc) · 16.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# -*- coding: utf-8 -*-
"""
.. module:: openzwave.value
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 <[email protected]>
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.
"""
from six import string_types
from openzwave.object import ZWaveObject
# 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())
# TODO: don't report controller node as sleeping
# TODO: allow value identification by device/index/instance
class ZWaveValue(ZWaveObject):
"""
Represents a single value.
"""
def __init__(self, value_id, network=None, parent=None):
"""
Initialize value
.. code-block:: python
n['valueId'] = {'home_id' : v.GetHomeId(),
* 'parent_id' : v.GetNodeId(),
* 'commandClass' : PyManager.COMMAND_CLASS_DESC[v.GetCommandClassId()],
* 'instance' : v.GetInstance(),
* 'index' : v.GetIndex(),
* 'id' : v.GetId(),
* 'genre' : PyGenres[v.GetGenre()],
* 'type' : PyValueTypes[v.GetType()],
* #'value' : value.c_str(),
* 'value' : getValueFromType(manager,v.GetId()),
* 'label' : label.c_str(),
* 'units' : units.c_str(),
* 'readOnly': manager.IsValueReadOnly(v),
}
:param value_id: ID of the value
:type value_id: int
:param network: The network object to access the manager
:type network: ZWaveNetwork
"""
ZWaveObject.__init__(self, value_id, network=network)
logger.debug("Create object value (valueId:%s)", value_id)
self._parent = parent
def __str__(self):
"""
The string representation of the value.
:rtype: str
"""
return 'home_id: [%s] id: [%s] parent_id: [%s] label: [%s] data: [%s]' % \
(self._network.home_id_str, self._object_id, self.parent_id, self.label, self.data)
@property
def parent_id(self):
"""
Get the parent_id of the value.
"""
return self._parent.object_id
@property
def value_id(self):
"""
Get the value_id of the value.
"""
return self._object_id
@property
def id_on_network(self):
"""
Get an unique id for this value.
The scenes use this to retrieve values
.. code-block:: xml
<Scene id="1" label="scene1">
<Value homeId="0x014d0ef5" nodeId="2" genre="user" commandClassId="38" instance="1" index="0" type="byte">54</Value>
</Scene>
The format is :
home_id.node_id.command_class.instance.index
"""
separator = self._network.id_separator
return "%0.8x%s%s%s%0.2x%s%s%s%s" % (self._network.home_id, \
separator, self.parent_id, \
separator, self.command_class, \
separator, self.instance, \
separator, self.index)
@property
def node(self):
"""
The value_id of the value.
"""
return self._parent
@property
def label(self):
"""
Get the label of the value.
:rtype: str
"""
return self._network.manager.getValueLabel(self.value_id)
@label.setter
def label(self, value):
"""
Set the label of the value.
:param value: The new label value
:type value: str
"""
self._network.manager.setValueLabel(self.value_id, value)
@property
def help(self):
"""
Gets a help string describing the value's purpose and usage.
:rtype: str
"""
return self._network.manager.getValueHelp(self.value_id)
@help.setter
def help(self, value):
"""
Sets a help string describing the value's purpose and usage..
:param value: The new help value
:type value: str
"""
self._network.manager.setValueHelp(self.value_id, value)
@property
def units(self):
"""
Gets the units that the value is measured in.
:rtype: str
"""
return self._network.manager.getValueUnits(self.value_id)
@units.setter
def units(self, value):
"""
Sets the units that the value is measured in.
:param value: The new units value
:type value: str
"""
self._network.manager.setValueUnits(self.value_id, value)
@property
def max(self):
"""
Gets the maximum that this value may contain.
:rtype: int
"""
return self._network.manager.getValueMax(self.value_id)
@property
def min(self):
"""
Gets the minimum that this value may contain.
:rtype: int
"""
return self._network.manager.getValueMin(self.value_id)
@property
def type(self):
"""
Get the type of the value. The type describes the data held by the value
and enables the user to select the correct value accessor method in the
Manager class.
:return: type of the value
:rtype: str
"""
return self._network.manager.getValueType(self.value_id)
@property
def genre(self):
"""
Get the genre of the value. The genre classifies a value to enable
low-level system or configuration parameters to be filtered out
by the application
:return: genre of the value (Basic, User, Config, System)
:rtype: str
"""
return self._network.manager.getValueGenre(self.value_id)
@property
def index(self):
"""
Get the value index. The index is used to identify one of multiple
values created and managed by a command class. In the case of configurable
parameters (handled by the configuration command class), the index is the
same as the parameter ID.
:return: index of the value
:rtype: int
"""
return self._network.manager.getValueIndex(self.value_id)
@property
def instance(self):
"""
Get the command class instance of this value. It is possible for there to be
multiple instances of a command class, although currently it appears that
only the SensorMultilevel command class ever does this.
:return: instance of the value
:rtype: int
"""
return self._network.manager.getValueInstance(self.value_id)
@property
def data(self):
"""
Get the current data of the value.
:return: The data of the value
:rtype: depending of the type of the value
"""
return self._network.manager.getValue(self.value_id)
@data.setter
def data(self, value):
"""
Set the data of the value.
Best practice: Use check_data before setting it:
new_val = value.check_data(some_data)
if new_val != None:
value.data = new_val
:param value: The new data value
:type value:
"""
self._network.manager.setValue(self.value_id, value)
@property
def data_as_string(self):
"""
Get the value data as String.
:rtype: str
"""
return self._network.manager.getValueAsString(self.value_id)
@property
def data_items(self):
"""
When type of value is list, data_items contains a list of valid values
:return: The valid values or a help string
:rtype: string or set
"""
if self.is_read_only:
return "Read only"
if self.type == "Bool":
return "True or False"
elif self.type == "Byte":
return "A byte between %s and %s" % (self.min, self.max)
elif self.type == "Decimal":
return "A decimal"
elif self.type == "Int":
return "An integer between %s and %s" % (self.min, self.max)
elif self.type == "Short":
return "A short between %s and %s" % (self.min, self.max)
elif self.type == "String":
return "A string"
elif self.type == "Button":
return "True or False"
elif self.type == "List":
return self._network.manager.getValueListItems(self.value_id)
else:
return "Unknown"
def check_data(self, data):
"""
Check that data is correct for this value.
Return the data in a correct type. None is data is incorrect.
:param data: The data value to check
:type data: lambda
:return: A variable of the good type if the data is correct. None otherwise.
:rtype: variable
"""
if self.is_read_only:
return None
new_data = None
logger.debug("check_data type :%s", self.type)
if self.type == "Bool":
if isinstance(data, string_types):
if data in ["False", "false", "FALSE", "0"]:
new_data = False
else:
new_data = True
else:
try:
new_data = bool(data)
except:
new_data = None
elif self.type == "Byte":
try:
new_data = int(data)
except:
new_data = None
if new_data is not None:
if new_data < 0:
new_data = 0
elif new_data > 255:
new_data = 255
elif self.type == "Decimal":
try:
new_data = float(data)
except:
new_data = None
elif self.type == "Int":
try:
new_data = int(data)
except:
new_data = None
if new_data is not None:
if new_data < -2147483648:
new_data = -2147483648
elif new_data > 2147483647:
new_data = 2147483647
elif self.type == "Short":
try:
new_data = int(data)
except:
new_data = None
if new_data is not None:
if new_data < -32768:
new_data = -32768
elif new_data > 32767:
new_data = 32767
elif self.type == "String":
new_data = data
elif self.type == "Button":
if isinstance(data, string_types):
if data in ["False", "false", "FALSE", "0"]:
new_data = False
else:
new_data = True
else:
try:
new_data = bool(data)
except:
new_data = None
elif self.type == "List":
if isinstance(data, string_types):
if data in self.data_items:
new_data = data
else:
new_data = None
return new_data
@property
def is_set(self):
"""
Test whether the value has been set.
:return: True if the value has actually been set by a status message
from the device, rather than simply being the default.
:rtype: bool
"""
return self._network.manager.isValueSet(self.value_id)
@property
def is_read_only(self):
"""
Test whether the value is read-only.
:return: True if the value cannot be changed by the user.
:rtype: bool
"""
return self._network.manager.isValueReadOnly(self.value_id)
@property
def is_write_only(self):
"""
Test whether the value is write-only.
:return: True if the value can only be written to and not read.
:rtype: bool
"""
return self._network.manager.isValueWriteOnly(self.value_id)
def enable_poll(self, intensity=1):
"""
Enable the polling of a device's state.
:param intensity: The intensity of the poll
:type intensity: int
:return: True if polling was enabled.
:rtype: bool
"""
return self._network.manager.enablePoll(self.value_id, intensity)
def disable_poll(self):
"""
Disable poll off this value.
:return: True if polling was disabled.
:rtype: bool
"""
return self._network.manager.disablePoll(self.value_id)
@property
def poll_intensity(self):
"""
The poll intensity of the value.
:returns: 0=none, 1=every time through the list, 2-every other time, etc
:rtype: int
"""
#always ask to manager to get poll intensity
return self._network.manager.getPollIntensity(self.value_id)
@property
def is_polled(self):
"""
Verify that the value is polled.
:rtype: bool
"""
return self._network.manager.isPolled(self.value_id)
@property
def command_class(self):
"""
The command class of the value.
:returns: The command class of this value
:rtype: int
"""
return self._network.manager.getValueCommandClass(self.value_id)
def refresh(self):
"""
Refresh the value.
:returns: True if the command was transmitted to controller
:rtype: bool
"""
return self._network.manager.refreshValue(self.value_id)
@property
def precision(self):
"""
Gets a float value's precision.
:returns: a float value's precision
:rtype: int
"""
return self._network.manager.getValueFloatPrecision(self.value_id)
def is_change_verified(self):
"""
determine if value changes upon a refresh should be verified.
If so, the library will immediately refresh the value a second time whenever a change is observed.
This helps to filter out spurious data reported occasionally by some devices.
"""
return self._network.manager.getChangeVerified(self.value_id)
def set_change_verified(self, verify):
"""
Sets a flag indicating whether value changes noted upon a refresh should be verified.
If so, the library will immediately refresh the value a second time whenever a change is observed.
This helps to filter out spurious data reported occasionally by some devices.
:param verify: if true, verify changes; if false, don't verify changes.
:type verify: bool
"""
logger.debug('Set change verified %s for valueId [%s]', verify, self.value_id)
self._network.manager.setChangeVerified(self.value_id, verify)
def to_dict(self, extras=['all']):
"""
Return a dict representation of the node.
:param extras: The extra inforamtions to add
:type extras: []
:returns: A dict
:rtype: dict()
"""
if 'all' in extras:
extras = ['kvals']
ret={}
ret['label'] = self.label
ret['value_id'] = self.value_id
ret['node_id'] = self.node.node_id
ret['units'] = self.units
ret['genre'] = self.genre
ret['data'] = self.data
if 'kvals' in extras and self.network.dbcon is not None:
vals = self.kvals
for key in vals.keys():
ret[key]=vals[key]
return ret