-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
204 lines (170 loc) · 5.56 KB
/
main.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
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
from PHueManager import Hue
import yaml
import time
import random
CONFIG_FILE = 'config.yml'
PHILIPS_HUE = 'PhilipsHue'
APP_KEY_NAME = 'HUE-APPLICATION-KEY'
IPADDR = 'IPADDR'
S_BULB_TYPE = 'sultan_bulb'
def get_config(config_file):
"""
Read in the configuration
:param config_file:
:return:
"""
try:
config = ''
with open(config_file, "r") as yaml_file:
config = yaml.load(yaml_file, Loader=yaml.FullLoader)
print("Config read successful, {}".format(config))
except Exception as e:
print("Ooops! Error occurred reading configs, {}".format(e))
return config
def random_color():
x = random.uniform(0, 1)
y = random.uniform(0, 1)
return {"xy": {"x": x, "y": y}}
def random_brightness():
dim_val = random.uniform(0, 100)
return {"brightness": dim_val}
def build_crazy_body():
body = {
"dimming": random_brightness(),
"color": random_color()
}
return body
def convert_names_to_rids(hue=None, names=None):
"""
Converts a list of names to a list of rid values
:param hue:
:parm names: optional names list
:return:
"""
rids = []
if not names:
names = hue.fetch_names_of_type(type=S_BULB_TYPE)
for name in names:
rid = hue.get_device_rid(name=name, rtype='light')
rids.append(rid['rid'])
return rids
def random_sync(hue=None, duration=None, sleep_val=None):
"""
Random lighting using API, all available lights sync'd
:param hue: instance of Hue class
:param duration: loop duration
:return:
"""
y = 0
names = hue.fetch_names_of_type(type=S_BULB_TYPE)
while y < duration:
body = build_crazy_body()
for light in names:
control = hue.light_body(name=light, method='put', resource='resource/light', body=body)
if sleep_val:
time.sleep(sleep_val)
y += 1
return
def random_lights(hue=None, duration=None, sleep_val=None):
"""
Random lights, each light is not sync'd
:param hue: instance of Hue class
:param duration: loop / number of changes
:param sleep_val: sleep between changes
:return:
"""
y = 0
names = hue.fetch_names_of_type(type=S_BULB_TYPE)
while y < duration:
for light in names:
control = hue.light_body(name=light, method='put', resource='resource/light', body=build_crazy_body())
if sleep_val:
time.sleep(sleep_val)
y += 1
return
def random_lights_by_rid(hue=None, duration=None, sleep_val=None):
"""
Random lights, each light is not sync'd
:param hue: instance of Hue class
:param duration: loop / number of changes
:param sleep_val: sleep between changes
:return:
"""
y = 0
rids = []
rids = convert_names_to_rids(hue=hue)
while y < duration:
for rid in rids:
control = hue.light_body(rid=rid, method='put', resource='resource/light', body=build_crazy_body())
if sleep_val:
time.sleep(sleep_val)
y += 1
return
def bounce_brilliance(hue=None, duration=None, color=None, names=None, increment=None):
rids = convert_names_to_rids(hue=hue)
dim_val = 1
y = 0
direction_up = True
if not increment:
increment = 1
while y < duration:
if color:
body = {
"color": {"xy": {"x": color["x"], "y": color["y"]}},
"dimming": {"brightness": dim_val}
}
else:
body = {"dimming": {"brightness": dim_val}}
for rid in rids:
control = hue.light_body(rid=rid, method='put', resource='resource/light', body=body)
print("DIM VAL {} DIRECT UP {}".format(dim_val, direction_up))
if dim_val < 100 and direction_up:
dim_val += increment
if dim_val > 99:
direction_up = False
dim_val -= increment
if dim_val > 0 and not direction_up:
dim_val -= increment
if dim_val < 99 and not direction_up:
dim_val -= increment
if dim_val < 0:
direction_up = True
dim_val = 0
y += 1
return
def main():
configs = False
hue_application_key = ''
ipaddr = ''
config = get_config(CONFIG_FILE)
if config:
hue_application_key = config[0][PHILIPS_HUE][APP_KEY_NAME]
ipaddr = config[0][PHILIPS_HUE][IPADDR]
configs = True
if configs:
hue = Hue(address=ipaddr, app_key=hue_application_key)
names = hue.fetch_names_of_type(type=S_BULB_TYPE)
print(names)
for dv in hue.devices:
print(dv.__dict__)
print(hue.ent_configs)
test = hue.ent_status_name(method='put', name='Test', resource="resource/entertainment_configuration",
action=True)
print(test.json())
test2 = True
if test2:
random_lights_by_rid(hue=hue, duration=100)
#bounce_brilliance(hue=hue, duration=100, increment=10, color={'x': 0.4, 'y':0.1 })
test = False
if test:
x = 0
while x < 50:
list = ["Lounge Lamp stand", "Lounge Rear ceiling", "Lounge Front ceiling"]
for item in list:
control = hue.light_state(name=item, method='put', resource='resource/light', on=False)
print(control.reason)
time.sleep(1)
control = hue.light_state(name=item, method='put', resource='resource/light', on=True)
x += 1
if __name__ == '__main__':
main()