forked from OpenZWave/python-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.py
More file actions
executable file
·223 lines (176 loc) · 6.3 KB
/
Copy pathscene.py
File metadata and controls
executable file
·223 lines (176 loc) · 6.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
# -*- coding: utf-8 -*-
"""
.. module:: openzwave.scene
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 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())
class ZWaveScene(ZWaveObject):
"""
Represents a single scene within the Z-Wave Network
"""
def __init__(self, scene_id, network=None):
"""
Initialize zwave scene
:param scene_id: ID of the scene
:type scene_id: int
:param network: The network object to access the manager
:type network: ZWaveNetwork
"""
ZWaveObject.__init__(self, scene_id, network)
logger.debug("Create object scene (scene_id:%s)", scene_id)
self.values = dict()
def __str__(self):
"""
The string representation of the scene.
:rtype: str
"""
return 'scene_id: [%s] label: [%s]' % (self.scene_id, self.label)
@property
def scene_id(self):
"""
The id of the scene.
:rtype: int
"""
return self._object_id
@property
def label(self):
"""
The label of the scene.
:rtype: str
"""
return self._network.manager.getSceneLabel(self.object_id)
@label.setter
def label(self, value):
"""
Set the label of the scene.
:param value: The new label of the scene
:type value: str
"""
self._network.manager.setSceneLabel(self.object_id, value)
def create(self, label=None):
"""
Create a new zwave scene on the network and update the object_id field
If label is set, also change the label of the scene
:param label: The new label
:type label: str or None
:returns: return the id of scene on the network. Return 0 if fails
:rtype: int
"""
scene_id = self._network.manager.createScene()
if scene_id != 0:
self._object_id = scene_id
if label is not None:
self.label = label
return scene_id
def add_value(self, value_id, value_data):
"""
Add a value with data value_data to the zwave scene.
:param value_id: The id of the value to add
:type value_id: int
:param value_data: The data of the value to add
:type value_data: variable
"""
ret = self._network.manager.addSceneValue(self.scene_id, value_id, value_data)
if ret == 1:
return True
return False
def set_value(self, value_id, value_data):
"""
Set a value data to value_data in the zwave scene.
:param value_id: The id of the value to add
:type value_id: int
:param value_data: The data of the value to add
:type value_data: variable
"""
ret = self._network.manager.setSceneValue(self.scene_id, value_id, value_data)
if ret == 1:
return True
return False
def get_values(self):
"""
Get all the values of the scene
:returns: A dict of values : {value_id={'value'=ZWaveValue, 'data'=data}, ...}.
:rtype: dict()
"""
ret = dict()
values = self._network.manager.sceneGetValues(self.scene_id)
if values is None:
return ret
for val in values:
value = self._network.get_value(val)
ret[val] = {'value':value, 'data':values[val]}
return ret
def get_values_by_node(self):
"""
Get all the values of the scene grouped by nodes
:returns: A dict of values : {node_id={value_id={'value'=ZWaveValue, 'data'=data}, ...},...}.
:rtype: dict()
"""
ret = dict()
values = self._network.manager.sceneGetValues(self.scene_id)
if values is None:
return ret
for val in values:
value = self._network.get_value(val)
if value is not None:
if value.node.node_id not in ret:
ret[value.node.node_id] = {}
ret[value.node.node_id][val] = {'value':value, 'data':values[val]}
return ret
def remove_value(self, value_id):
"""
Remove a value from the scene.
:param value_id: The id of the value to change
:type value_id: int
:returns: True if the scene is removed. False otherwise.
:rtype: bool
"""
return self._network.manager.removeSceneValue(self.scene_id, value_id)
def activate(self):
"""
Activate the zwave scene.
:returns: True if the scene is activated. False otherwise.
:rtype: bool
"""
return self._network.manager.activateScene(self.object_id)
def to_dict(self, extras=['kvals']):
"""
Return a dict representation of the node.
:param extras: The extra inforamtions to add
:type extras: []
:returns: A dict
:rtype: dict()
"""
ret = {}
ret['label'] = self.label
ret['scene_id'] = self.scene_id
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