This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wemo.py
64 lines (47 loc) · 1.41 KB
/
wemo.py
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
#!/usr/bin/python
import collectd
from ouimeaux.environment import Environment, UnknownDevice
CONFIG=[]
ENV = Environment(with_cache=False)
def init():
collectd.info("start")
ENV.start()
#collectd.info("Discover")
ENV.discover()
def read():
#collectd.info("read")
for name in CONFIG:
while True:
#collectd.info("querying: " + name)
try:
switch = ENV.get_switch(name)
v1 = collectd.Values(plugin='wemo')
v1.type = 'power'
v1.type_instance = 'power'
v1.plugin_instance = name
power = switch.current_power/1000.0
collectd.info("Got power from %s = %fW" % (name, power))
v1.values = [power]
v1.dispatch()
except UnknownDevice:
collectd.error("Unknown device: " + name)
except ConnectionError:
ENV.start()
ENV.discover()
continue
break
env = None
def config_callback(conf):
name = None
for node in conf.children:
key = node.key.lower()
val = node.values[0]
if key == 'name':
#collectd.info("found config name = " + val)
CONFIG.append(val)
else:
collectd.warning('unknown config key: %s' % key)
continue
collectd.register_config(config_callback)
collectd.register_init(init)
collectd.register_read(read)