/
pants.py
executable file
·74 lines (63 loc) · 1.83 KB
/
pants.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
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import sys
import yaml
import subprocess
import re
from time import sleep
import phue
from phue import Bridge
def main():
if len(sys.argv) != 2:
print "Usage: pants.py <config.yaml>"
sys.exit(1)
config = yaml.load(open(sys.argv[1]))
if not 'lights' in config:
print "Config must contain a 'lights' array"
sys.exit(1)
if not 'device' in config:
print "Config must contain a 'device'"
sys.exit(1)
b = None
while b == None:
try:
b = Bridge("10.0.0.31")
except phue.PhueRegistrationException as e:
print str(e)
sleep(5)
lights = b.get_light_objects('name')
xbee = subprocess.Popen(["./xbsh2", config['device']], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
line = xbee.stdout.readline()
p = re.compile('AD3:\s+(\d+)')
while line != None:
m = p.search(line)
if m:
value = int(m.group(1))
print value
saturation = 254
hue = 65000
if value > 1000:
# pants all the way down
brightness = 90
elif value > 900:
# pants 3/4 down
brightness = 130
elif value > 700:
# pants halfway down
brightness = 170
elif value > 400:
# pants 1/4 down
brightness = 210
else:
# pants up
brightness = 254
saturation = 0
for light in config['lights']:
l = lights[light]
l.on = True
l.saturation = saturation
l.hue = hue
l.brightness = brightness
line = xbee.stdout.readline()
if __name__ == '__main__':
main()