/
test_device.py
503 lines (429 loc) · 13.5 KB
/
test_device.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from datetime import datetime
import pytest
from pytradfri import error
from pytradfri.const import (
ROOT_DEVICES,
ATTR_NAME,
ATTR_LIGHT_CONTROL,
ATTR_LAST_SEEN,
ATTR_LIGHT_COLOR_SATURATION,
ATTR_LIGHT_COLOR_HUE,
ATTR_LIGHT_DIMMER,
ATTR_TRANSITION_TIME,
ATTR_LIGHT_COLOR_X,
ATTR_LIGHT_COLOR_Y,
ATTR_LIGHT_MIREDS,
ATTR_DEVICE_STATE,
ATTR_LIGHT_COLOR_HEX,
ATTR_SWITCH_PLUG
)
from pytradfri.device import Device
from devices import (
LIGHT_W, LIGHT_WS, LIGHT_CWS, LIGHT_PHILIPS, REMOTE_CONTROL,
MOTION_SENSOR, OUTLET, BLIND)
@pytest.fixture
def device():
return Device(LIGHT_WS)
input_devices = (
("comment", "device"),
[
("Remote control", Device(REMOTE_CONTROL)),
("Motion sensor", Device(MOTION_SENSOR)),
]
)
output_devices = (
("comment", "device"),
[
("White fixed color bulb", Device(LIGHT_W)),
("White spectrum bulb", Device(LIGHT_WS)),
("Full color bulb", Device(LIGHT_CWS)),
("Philips Hue bulb", Device(LIGHT_PHILIPS)),
]
)
wall_plugs = (
("comment", "device"),
[
("Wall plug", Device(OUTLET))
]
)
roller_blinds = (
("comment", "device"),
[
("Blind", Device(BLIND))
]
)
lamp_value_setting_test_cases = [
["function_name", "comment", "test_input", "expected_result"],
[
[
"set_hsb", "valid", {
'hue': 300,
'saturation': 200,
'brightness': 100
}, {
ATTR_LIGHT_COLOR_HUE: 300,
ATTR_LIGHT_COLOR_SATURATION: 200,
ATTR_LIGHT_DIMMER: 100
}
],
[
"set_hsb", "without_setting_brightness", {
'hue': 300,
'saturation': 200
}, {
ATTR_LIGHT_COLOR_HUE: 300,
ATTR_LIGHT_COLOR_SATURATION: 200
},
],
[
"set_hsb", "setting_brightness_none", {
'hue': 300,
'saturation': 200,
'brightness': None
}, {
ATTR_LIGHT_COLOR_HUE: 300,
ATTR_LIGHT_COLOR_SATURATION: 200
},
],
[
"set_hsb", "setting_hue_none", {
'hue': None,
'saturation': 200
}, {
ATTR_LIGHT_COLOR_HUE: None,
ATTR_LIGHT_COLOR_SATURATION: 200
},
],
[
"set_hsb", "with_transitiontime", {
'hue': 300,
'saturation': 200,
'brightness': 100,
'transition_time': 2
}, {
ATTR_LIGHT_COLOR_HUE: 300,
ATTR_LIGHT_COLOR_SATURATION: 200,
ATTR_LIGHT_DIMMER: 100,
ATTR_TRANSITION_TIME: 2
},
],
[
"set_hsb", "with_faulty_transitiontime", {
'hue': 300,
'saturation': 200,
'transition_time': -2
}, {
ATTR_LIGHT_COLOR_HUE: 300,
ATTR_LIGHT_COLOR_SATURATION: 200,
ATTR_TRANSITION_TIME: -2
},
],
[
"set_xy_color", "valid", {
'color_x': 300,
'color_y': 200,
}, {
ATTR_LIGHT_COLOR_X: 300,
ATTR_LIGHT_COLOR_Y: 200,
},
],
[
"set_xy_color", "without_x", {
'color_x': None,
'color_y': 200,
}, {
ATTR_LIGHT_COLOR_X: None,
ATTR_LIGHT_COLOR_Y: 200,
},
],
[
"set_xy_color", "without_xy", {
'color_x': None,
'color_y': None,
}, {
ATTR_LIGHT_COLOR_X: None,
ATTR_LIGHT_COLOR_Y: None,
},
],
[
"set_xy_color", "with_transitiontime", {
'color_x': 300,
'color_y': 200,
'transition_time': 2
}, {
ATTR_LIGHT_COLOR_X: 300,
ATTR_LIGHT_COLOR_Y: 200,
ATTR_TRANSITION_TIME: 2
},
],
[
"set_color_temp", "valid", {
'color_temp': 300,
}, {
ATTR_LIGHT_MIREDS: 300,
},
],
[
"set_color_temp", "none", {
'color_temp': None,
}, {
ATTR_LIGHT_MIREDS: None,
},
],
[
"set_color_temp", "with_transitiontime", {
'color_temp': 300,
'transition_time': 2
}, {
ATTR_LIGHT_MIREDS: 300,
ATTR_TRANSITION_TIME: 2
},
],
[
"set_dimmer", "valid", {
'dimmer': 200,
}, {
ATTR_LIGHT_DIMMER: 200,
},
],
[
"set_dimmer", "none", {
'dimmer': None,
}, {
ATTR_LIGHT_DIMMER: None,
},
],
[
"set_dimmer", "with_transitiontime", {
'dimmer': 200,
'transition_time': 2
}, {
ATTR_LIGHT_DIMMER: 200,
ATTR_TRANSITION_TIME: 2
},
],
[
"set_state", "true", {
'state': True,
}, {
ATTR_DEVICE_STATE: True,
},
],
[
"set_state", "false", {
'state': False,
}, {
ATTR_DEVICE_STATE: False,
},
],
[
"set_hex_color", "valid", {
'color': '4a418a',
}, {
ATTR_LIGHT_COLOR_HEX: '4a418a',
},
],
[
"set_hex_color", "invalid", {
'color': 'RandomString',
}, {
ATTR_LIGHT_COLOR_HEX: 'RandomString',
},
],
[
"set_hex_color", "with_transitiontime", {
'color': '4a418a',
'transition_time': 2
}, {
ATTR_LIGHT_COLOR_HEX: '4a418a',
ATTR_TRANSITION_TIME: 2
},
],
[
"set_predefined_color", "valid", {
'colorname': 'Saturated Purple'
}, {
ATTR_LIGHT_COLOR_HEX: '8f2686',
},
],
[
"set_predefined_color", "with_transitiontime", {
'colorname': 'Saturated Purple',
'transition_time': 2
}, {
ATTR_LIGHT_COLOR_HEX: '8f2686',
ATTR_TRANSITION_TIME: 2
},
],
]
]
socket_value_setting_test_cases = [
["function_name", "comment", "test_input", "expected_result"],
[
[
"set_state", "true", {
'state': True,
}, {
ATTR_DEVICE_STATE: True,
},
],
[
"set_state", "false", {
'state': False,
}, {
ATTR_DEVICE_STATE: False,
},
],
]
]
# Combine lamp_value_setting_test_cases and output_devices where:
# len(new) = len(a) * len(b)
src = lamp_value_setting_test_cases[1] * len(output_devices[1])
newList = []
for i in range(len(src)):
index = int((i / len(src)) * len(output_devices[1]))
newList.append([
src[i][0], src[i][1], src[i][2], src[i][3], output_devices[1][index]
])
lamp_value_setting_test_cases[0].append("device")
lamp_value_setting_test_cases[1] = newList
@pytest.mark.parametrize(*lamp_value_setting_test_cases)
def test_lamp_value_setting(function_name, comment,
test_input, expected_result, device):
function = getattr(device[1].light_control, function_name)
command = function(**test_input)
data = command.data[ATTR_LIGHT_CONTROL][0]
assert data == expected_result
@pytest.mark.parametrize(*socket_value_setting_test_cases)
def test_socket_value_setting(function_name, comment,
test_input, expected_result, device):
if device.has_socket_control:
function = getattr(device[0].socket_control, function_name)
command = function(**test_input)
data = command.data[ATTR_SWITCH_PLUG][0]
assert data == expected_result
def test_socket_state_off(device):
if device.has_socket_control:
socket = Device(device.raw.copy()).socket_control.socket[0]
socket.raw[ATTR_DEVICE_STATE] = 0
assert socket.state is False
def test_socket_state_on(device):
if device.has_socket_control:
socket = Device(device.raw.copy()).socket_control.socket[0]
socket.raw[ATTR_DEVICE_STATE] = 1
assert socket.state is True
def test_set_state_none(device):
with pytest.raises(TypeError):
device.light_control.set_state(None)
def test_set_predefined_color_invalid(device):
with pytest.raises(error.ColorError):
device.light_control.set_predefined_color("RandomString")
def test_device_properties(device):
assert device.application_type == 2
assert device.name == 'Löng name containing viking lättårs [letters]'
assert device.id == 65539
assert device.created_at == datetime.utcfromtimestamp(1509923713)
assert device.reachable
assert device.path == [ROOT_DEVICES, 65539]
def test_device_info_properties(device):
info = device.device_info
assert info.manufacturer == 'IKEA of Sweden'
assert info.model_number == 'TRADFRI bulb E27 WS opal 980lm'
assert info.firmware_version == '1.2.217'
assert info.power_source == 1
assert info.power_source_str == 'Internal Battery'
def test_set_name(device):
command = device.set_name('New name')
assert command.method == 'put'
assert command.path == device.path
assert command.data == {ATTR_NAME: 'New name'}
def test_binary_division():
dev_ws = Device(LIGHT_WS).light_control.lights[0]
dev_color = Device(LIGHT_CWS).light_control.lights[0]
assert dev_ws.dimmer == 254
assert dev_ws.color_temp == 400
assert dev_color.hex_color == 'f1e0b5'
assert dev_color.xy_color == (30015, 26870)
# Test has_light_control function
def test_has_light_control_true(device):
dev = Device(device.raw.copy())
dev.raw[ATTR_LIGHT_CONTROL] = {1: 2}
assert dev.has_light_control is True
def test_has_light_control_false(device):
dev = Device(device.raw.copy())
dev.raw[ATTR_LIGHT_CONTROL] = {}
assert dev.has_light_control is False
# Test light state function
def test_light_state_on(device):
light = Device(device.raw.copy()).light_control.lights[0]
light.raw[ATTR_DEVICE_STATE] = 1
assert light.state is True
def test_light_state_off(device):
light = Device(device.raw.copy()).light_control.lights[0]
light.raw[ATTR_DEVICE_STATE] = 0
assert light.state is False
def test_light_state_mangled(device):
light = Device(device.raw.copy()).light_control.lights[0]
light.raw[ATTR_DEVICE_STATE] = "RandomString"
assert light.state is False
# Test light hsb_xy_color function
def test_light_hsb_xy_color(device):
"""
Very basic test, just to touch it.
"""
light = Device(device.raw.copy()).light_control.lights[0]
assert len(light.hsb_xy_color) == 5
# Test last_seen function
def test_last_seen_valid(device):
assert device.last_seen is not None
def test_last_seen_none():
tmp = LIGHT_WS
del tmp[ATTR_LAST_SEEN]
dev = Device(tmp)
assert dev.last_seen is None
# Test _value_validate function
def test_value_validate_lower_edge(device):
rnge = (10, 100)
with pytest.raises(ValueError):
device.light_control._value_validate(9, rnge)
assert device.light_control._value_validate(10, rnge) is None
assert device.light_control._value_validate(11, rnge) is None
def test_value_validate_upper_edge(device):
rnge = (10, 100)
assert device.light_control._value_validate(99, rnge) is None
assert device.light_control._value_validate(100, rnge) is None
with pytest.raises(ValueError):
device.light_control._value_validate(101, rnge)
def test_value_validate_none(device):
rnge = (10, 100)
assert device.light_control._value_validate(None, rnge) is None
# Test deviceInfo serial function
def test_deviceinfo_serial(device):
info = Device(device.raw.copy()).device_info
info.raw['2'] = "SomeRandomSerial"
assert info.serial == "SomeRandomSerial"
# Test deviceInfo power_source_str function
def test_deviceinfo_power_source_str_known(device):
info = Device(device.raw.copy()).device_info
assert info.power_source_str is not None
assert info.power_source_str != 'Unknown'
def test_deviceinfo_power_source_str_unknown(device):
info = Device(device.raw.copy()).device_info
info.raw['6'] = None
assert info.power_source_str == 'Unknown'
def test_deviceinfo_power_source_not_present(device):
info = Device(device.raw.copy()).device_info
del info.raw['6']
assert info.power_source_str is None
# Test deviceInfo battery_level function
@pytest.mark.parametrize(*input_devices)
def test_deviceinfo_battery_level(comment, device):
info = Device(device.raw.copy()).device_info
assert type(info.battery_level) is int
assert info.battery_level >= 0
assert info.battery_level <= 100
@pytest.mark.parametrize(*input_devices)
def test_deviceinfo_battery_level_unkown(comment, device):
info = Device(device.raw.copy()).device_info
info.raw['9'] = None
assert info.battery_level is None