forked from OpenZWave/python-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.txt
More file actions
194 lines (124 loc) · 5.46 KB
/
Copy pathhello_world.txt
File metadata and controls
194 lines (124 loc) · 5.46 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
:orphan:
============================
python-openzwave hello_world
============================
Here is the hello_world example for the python-openzwave API. In this
tutorial we will implement a simple sniffer. The full code is available
in the examples directory : hello_world.py.
Some theory
===========
OpenZwave uses the mechanism of notifications to manage events network.
When the driver is ready, the node is added, a value is modified, ... openzwave
generates a notification you must catch with a callback function.
Python-openzwave API catch the notification and send a louie signal.
Some words about python-louie : it's a simple mechanism where a dispatcher
send a signal on a channel, and clients connect to him.
Here is the way a sender dispatch a signal :
.. code-block:: python
dispatcher.send(self.SIGNAL_NODE, **{'network': self, 'node':self.nodes[args['nodeId']]})
Here is the way a client connect to a channel :
.. code-block:: python
dispatcher.connect(louie_node_update, ZWaveNetwork.SIGNAL_NODE)
The first argument is a call back function :
.. code-block:: python
def louie_node_update(network, node):
print('Louie signal : Node update : %s.' % node)
It receives as parameters the ones populate by the sender : the network ans a node in ths case.
The callback must set its parameter names as the same as the sender.
Hello from ZWave
================
The import clauses :
.. code-block:: python
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
import time
from louie import dispatcher, All
First thing to do is to defined some options for the manager :
.. code-block:: python
options = ZWaveOption(device, config_path="../openzwave/config", user_path=".", cmd_line="")
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)
network = None
We also defined a network object, we will populate it later.
To use this options, you must lock them. No further modification could be done on them.
.. code-block:: python
options.lock()
We can now create the network :
.. code-block:: python
network = ZWaveNetwork(options, autostart=False)
The signals listeners
=====================
We will now create some connection to the louie dispatcher. We will then to 3
main signals, they will notify us about the state of the network :
.. code-block:: python
dispatcher.connect(louie_network_started, ZWaveNetwork.SIGNAL_NETWORK_STARTED)
dispatcher.connect(louie_network_failed, ZWaveNetwork.SIGNAL_NETWORK_FAILED)
dispatcher.connect(louie_network_ready, ZWaveNetwork.SIGNAL_NETWORK_READY)
To do that we use 3 callback functions :
When the driver is ready, we simply print some options on the screen :
.. code-block:: python
def louie_network_started(network):
print("Hello from network : I'm started : homeid %0.8x - %d nodes were found." % (network.home_id, network.nodes_count))
When the driver fails, we reports the error to screen :
.. code-block:: python
def louie_network_failed(network):
print("Hello from network : can't load :(.")
This an important event. It means that all nodes have been queried on the network.
You can now use the network object pass in parameter to query nodes. To do that we connect
to some louie signals. It also sleep during 5 minutes. After that, the scripts
will continue and the network will be stopped.
.. code-block:: python
def louie_network_ready(network):
print("Hello from network : I'm ready : %d nodes were found." % network.nodes_count)
print("Hello from network : my controller is : %s" % network.controller)
dispatcher.connect(louie_node_update, ZWaveNetwork.SIGNAL_NODE)
dispatcher.connect(louie_value_update, ZWaveNetwork.SIGNAL_VALUE)
When a node is updated, added, removed, ...
.. code-block:: python
def louie_node_update(network, node):
print('Hello from node : %s.' % node)
When a value is updated, added, removed, ...
.. code-block:: python
def louie_value_update(network, node, value):
print('Hello from value : %s.' % value)
The start code
==============
We start the network
.. code-block:: python
network.start()
And we wait for the network. You MUST NOT use the network objects before
network is ready.
.. code-block:: python
for i in range(0,90):
if network.state>=network.STATE_READY:
print "***** Network is ready"
break
else:
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1.0)
We now change the name of the controller. You will have a notification.
.. code-block:: python
network.controller.node.name = "Hello name"
time.sleep(10.0)
Same when changing the location.
.. code-block:: python
network.controller.node.location = "Hello location"
time.sleep(120.0)
And we wait for 2 minutes. If you have sensors on your network, you will
see the value notifications on the screen. If you have switch or dimmers,
activate them manually, ...
Now stop the network and release objects.
.. code-block:: python
network.stop()
That's all :)
Full source code is in examples/hello_world.py
To see a more functionnal example, look at ozwsh code.