forked from andreasofthings/python-lightify
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_lightify.py
305 lines (254 loc) · 9.07 KB
/
test_lightify.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import time
import pytest
from lightify import Lightify
# adjust these variables according to your installation
# the group should contain at least two lights
LIGHT_SLEEP = 2
GROUP_SLEEP = 2
SCENE_SLEEP = 2
GATEWAY_ADDR = "192.168.1.100"
LIGHT1 = 'Lightstrip'
RGB_LIGHT1 = 'Lightstrip'
GROUP1 = 'bedroom'
GROUP1_LIGHTS = ['Schlafzimmer1', 'Schlafzimmer2']
RGB_GROUP1 = 'bedroom'
SCENE1 = 'Test'
SCENE1_LIGHT_ON = 'Schlafzimmer1'
SCENE1_LIGHT_OFF = 'Schlafzimmer2'
#GATEWAY_ADDR = "192.168.1.46"
#LIGHT1 = 'Hall C L'
#RGB_LIGHT1 = ''
#GROUP1 = '[Hall Center]'
#GROUP1_LIGHTS = ['Hall C L', 'Hall C R']
#RGB_GROUP1 = ''
#SCENE1 = 'Kitchen'
#SCENE1_LIGHT_ON = 'Kitchen C'
#SCENE1_LIGHT_OFF = 'Hall C L'
def colors_almost_equal(color1, color2):
r1, g1, b1 = color1
r2, g2, b2 = color2
return abs(r1 - r2) <= 2 and abs(g1 - g2) <= 2 and abs(b1 - b2) <= 2
@pytest.fixture(scope="module")
def gateway():
gateway = Lightify(GATEWAY_ADDR)
gateway.update_all_light_status()
gateway.update_group_list()
gateway.update_scene_list()
return gateway
def test_connection(gateway):
# tests if a connection is possible and some lights are returned
print("test_connection")
assert gateway.lights()
def test_groups(gateway):
# tests that our group is returned and the lights in it are the same
# as in GROUP1_LIGHTS
assert GROUP1 in gateway.groups()
group = gateway.groups()[GROUP1]
lights = group.lights()
light_names = sorted([gateway.lights()[addr].name() for addr in lights])
assert light_names == sorted(GROUP1_LIGHTS)
def test_group_info(gateway):
# tests if the function gateway.group_info() returns the lights in
# GROUP1_LIGHTS
group = gateway.groups()[GROUP1]
lights = ([gateway.lights()[addr] for addr in gateway.group_info(group)])
light_names = sorted([light.name() for light in lights])
assert light_names == sorted(GROUP1_LIGHTS)
def get_addr_of_light(light_name, gateway):
for light in gateway.lights().values():
if light_name == light.name():
return light
return None
def test_turn_light_on_and_off(gateway):
# tests if a light can be turned off, on and off again and the state
# is updated accordingly
light = get_addr_of_light(LIGHT1, gateway)
light.set_onoff(0)
time.sleep(LIGHT_SLEEP)
assert not light.on()
gateway.update_all_light_status()
assert not light.on()
light.set_onoff(1)
assert light.on()
time.sleep(LIGHT_SLEEP)
gateway.update_all_light_status()
assert light.on()
light.set_onoff(0)
assert not light.on()
time.sleep(LIGHT_SLEEP)
gateway.update_all_light_status()
assert not light.on()
def test_turn_group_on_and_off(gateway):
# tests if a group can be turned off, on and off again
# and the state of each light in the group is updated accordingly
group = gateway.groups()[GROUP1]
lights = ([gateway.lights()[addr] for addr in group.lights()])
group.set_onoff(0)
for light in lights:
assert not light.on()
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert not light.on()
group.set_onoff(1)
for light in lights:
assert light.on()
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert light.on()
group.set_onoff(0)
for light in lights:
assert not light.on()
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert not light.on()
def test_scenes(gateway):
# tests that our scene is returned
assert SCENE1 in gateway.scenes()
def test_activate_scene(gateway):
# tests that our scene is successfully activated
light_on = get_addr_of_light(SCENE1_LIGHT_ON, gateway)
light_on.set_onoff(0)
light_off = get_addr_of_light(SCENE1_LIGHT_OFF, gateway)
light_off.set_onoff(1)
time.sleep(LIGHT_SLEEP)
assert not light_on.on()
assert light_off.on()
gateway.scenes()[SCENE1].activate()
time.sleep(SCENE_SLEEP)
gateway.update_all_light_status()
assert light_on.on()
assert not light_off.on()
def test_update_single_light(gateway):
# test if the update_light_status function updates the on variable
# correctly when turning the light off, on and off again
light = get_addr_of_light(LIGHT1, gateway)
light.set_onoff(0)
assert not light.on()
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert not on
light.set_onoff(1)
assert light.on()
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert on
light.set_onoff(0)
assert not light.on()
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert not on
@pytest.mark.skip(reason="requires walking in front of the motion sensor")
def test_motion_sensor(gateway):
# test motion sensor
motion_sensor_name = "MotionSensor1"
motion_sensor = get_addr_of_light(motion_sensor_name, gateway)
for i in range(1000):
time.sleep(0.1)
result = gateway.update_light_status(motion_sensor)
print(result)
def test_light_by_name(gateway):
# test if the function light_byname returns the correct light
light = gateway.light_byname(LIGHT1)
assert light.name() == LIGHT1
def test_colors_single_light(gateway):
# switches the light to red, green and blue and checks if the status
# is updated accordingly
if not RGB_LIGHT1:
pytest.skip('no RGB lights')
light = gateway.light_byname(RGB_LIGHT1)
old_on, old_lum, temp, old_r, old_g, old_b = gateway.update_light_status(
light)
light.set_onoff(1)
light.set_luminance(255, 0)
light.set_rgb(255, 0, 0, 0)
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert colors_almost_equal((r, g, b), (255, 0, 0))
light.set_rgb(0, 255, 0, 0)
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert colors_almost_equal((r, g, b), (0, 255, 0))
light.set_rgb(0, 0, 255, 0)
time.sleep(LIGHT_SLEEP)
on, lum, temp, r, g, b = gateway.update_light_status(light)
assert colors_almost_equal((r, g, b), (0, 0, 255))
light.set_rgb(old_r, old_g, old_b, 0)
light.set_luminance(old_lum, 0)
light.set_onoff(old_on)
def test_colors_group(gateway):
# turns the lights in the group to red, then green, then blue and
# checks if the status is updated accordingly
if not RGB_GROUP1:
pytest.skip('no RGB lights')
group = gateway.groups()[RGB_GROUP1]
lights = ([gateway.lights()[addr] for addr in group.lights()])
group.set_onoff(1)
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
old_rgbs = []
for light in lights:
old_rgbs.append(light.rgb())
group.set_rgb(255, 0, 0, 0)
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert colors_almost_equal(light.rgb(), (255, 0, 0))
group.set_rgb(0, 255, 0, 0)
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert colors_almost_equal(light.rgb(), (0, 255, 0))
group.set_rgb(0, 0, 255, 0)
time.sleep(GROUP_SLEEP)
gateway.update_all_light_status()
for light in lights:
assert colors_almost_equal(light.rgb(), (0, 0, 255))
for light, old_rgb in zip(lights, old_rgbs):
r, g, b = old_rgb
light.set_rgb(r, g, b, 0)
@pytest.mark.skip(reason="takes very long (about 30s)")
def test_color_cyle(gateway):
# changes the color of a light in 255 steps and the repeats it once more.
# this checks if an overflow occurs after 255 steps (was a bug in an older
# version of python lightify)
if not RGB_LIGHT1:
pytest.skip('no RGB lights')
light = gateway.light_byname(RGB_LIGHT1)
old_on, old_lum, temp, old_r, old_g, old_b = gateway.update_light_status(
light)
light.set_onoff(1)
for i in range(512):
col_val = i % 256
light.set_rgb(255 - col_val, 255 - col_val, col_val, 0)
# time.sleep(0.05)
light.set_rgb(old_r, old_g, old_b, 0)
light.set_luminance(old_lum, 0)
light.set_onoff(old_on)
def test_temperature_cyle(gateway):
# changes the color temperature of a light from 2200 kelvin to 6500
# in steps of 100.
light = gateway.light_byname(LIGHT1)
old_on, old_lum, old_temp, old_r, old_g, old_b = gateway.update_light_status(
light)
light.set_onoff(1)
time.sleep(0.2)
for i in range(2200, 6501, 100):
light.set_temperature(i, 0)
time.sleep(0.1)
light.set_temperature(old_temp, 0)
light.set_onoff(old_on)
def test_luminance(gateway):
# changes the luminance of a light from 0 to 100 in steps of 5
light = gateway.light_byname(LIGHT1)
old_on, old_lum, old_temp, old_r, old_g, old_b = gateway.update_light_status(
light)
light.set_onoff(1)
time.sleep(0.2)
for i in range(0, 101, 5):
light.set_luminance(i, 0)
time.sleep(0.1)
light.set_luminance(old_lum, 0)
light.set_onoff(old_on)