forked from mpapi/lazylights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifx_OpenHAB.py
executable file
·134 lines (111 loc) · 4.14 KB
/
lifx_OpenHAB.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
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
"""
Adopted from VinzzB on https://github.com/mpapi/lazylights/issues/3
-------------------------------
Argument:
1 = power all bulbs
0 = set all bulbs off
-------------------------------
"""
import lazylights
import time
import struct
import sys
import string
import binascii
import colorsys
if sys.version_info[0] > 2:
PY3K = True
else:
PY3K = False
def setBulbPower(bulbs, power):
elapsedTime = 0
# exit if bulb is already in requested state
bulbStatePre = lazylights.get_state(bulbs)
if powerState(bulbStatePre) and power == 1:
return
if not powerState(bulbStatePre) and power == 0:
return
lazylights.set_power(bulbs, power)
# wait for status update to ensure action was completed
while elapsedTime < 5:
bulbState = lazylights.get_state(bulbs)
if powerState(bulbState) == power:
print(getLabels(bulbState) + 'Bulbs powered ' + ('ON' if power else 'OFF'))
return
time.sleep(.5)
elapsedTime += 1
def powerState(bulbList):
"""Returns boolean result for bulbs power status"""
power = False
for bulb in lazylights.get_state(bulbList):
power |= bulb.power > 0
return power
def bulbState(bulbList):
results = lazylights.get_state(bulbList)
for k in results:
print 'Gateway MAC: ' + str(k.bulb.gateway_mac)
print 'MAC: ' + str(k.bulb.mac)
print 'Address: ' + str(k.bulb.addr[0]) + ':' + str(k.bulb.addr[1])
print 'Hue: ' + str(k.hue)
print 'Saturatuion: ' + str(k.saturation)
print 'Brightness: ' + str(k.brightness)
print 'Kelvin: ' + str(k.kelvin)
print 'Power: ' + str(k.power)
print 'Label: ' + str(k.label)
def getLabels(bulbs):
r = ""
for bulb in bulbs:
if PY3K:
r += ("" if r == "" else ", ") + str(bulb.label, 'ASCII').split('\x00')[0]
else:
r += ("" if r == "" else ", ") + str(bulb.label).split('\x00')[0]
return r
def createBulb(ip, macString, port=56700):
return lazylights.Bulb(b'LIFXV2', binascii.unhexlify(macString.replace(':', '')), (ip, port))
def setBulbColor(bulb, r, g, b, kelvin, fade):
"""Sets bulb color based on RGB color instead of HSV values"""
r /= float(255)
g /= float(255)
b /= float(255)
(huevalue, saturationvalue, brightnessvalue) = colorsys.rgb_to_hsv(r, g, b)
huevalue *= float(360)
lazylights.set_state(bulbs, huevalue, saturationvalue, brightnessvalue, kelvin, fade, raw=False)
if __name__ == "__main__":
# Bulb List
#'name':{'ipAddress':'###.###.###.###', 'macAddress':'##:##:##:##:##:##'}
bulbInventory = {
'lifx1':{'ipAddress':'10.168.1.17', 'macAddress':'D0:73:D5:02:64:11'}
}
# parse bulb selection and create bulbs list
selectedBulbs = []
for bulb in bulbInventory:
if bulb == sys.argv[1]:
ipAddr = str(bulbInventory[bulb]['ipAddress'])
macAddr = str(bulbInventory[bulb]['macAddress'])
selectedBulbs = [createBulb(ipAddr, macAddr)]
continue
if not selectedBulbs:
print('Incorrect bulb specified, terminating execution')
sys.exit()
# parse function
if sys.argv[2].lower() == 'off':
lazylights.set_power(selectedBulbs, 0)
elif sys.argv[2].lower() == 'on':
lazylights.set_power(selectedBulbs, 1)
elif sys.argv[2].lower() == 'status':
if powerState(selectedBulbs):
sys.exit('ON')
else:
sys.exit('OFF')
elif sys.argv[2].lower() == 'state':
bulbState(selectedBulbs)
else:
commandInput = sys.argv[2].split(',')
hue = int(float(commandInput[0]))
saturation = int(float(commandInput[1]))
brightness = int(float(commandInput[2]))
kelvin = int(float(commandInput[3]))
fade = int(float(commandInput[4]))
if not powerState(selectedBulbs):
lazylights.set_power(selectedBulbs, 1)
lazylights.set_state(selectedBulbs, hue, saturation, brightness, kelvin, fade, raw=False)