/
test_light.py
313 lines (279 loc) · 9.92 KB
/
test_light.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
"""
Test for the SmartThings light platform.
The only mocking required is of the underlying SmartThings API object so
real HTTP calls are not initiated during testing.
"""
from pysmartthings import Attribute, Capability
import pytest
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
ATTR_TRANSITION,
DOMAIN as LIGHT_DOMAIN,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_TRANSITION,
)
from homeassistant.components.smartthings import light
from homeassistant.components.smartthings.const import DOMAIN, SIGNAL_SMARTTHINGS_UPDATE
from homeassistant.const import ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .conftest import setup_platform
@pytest.fixture(name="light_devices")
def light_devices_fixture(device_factory):
"""Fixture returns a set of mock light devices."""
return [
device_factory(
"Dimmer 1",
capabilities=[Capability.switch, Capability.switch_level],
status={Attribute.switch: "on", Attribute.level: 100},
),
device_factory(
"Color Dimmer 1",
capabilities=[
Capability.switch,
Capability.switch_level,
Capability.color_control,
],
status={
Attribute.switch: "off",
Attribute.level: 0,
Attribute.hue: 76.0,
Attribute.saturation: 55.0,
},
),
device_factory(
"Color Dimmer 2",
capabilities=[
Capability.switch,
Capability.switch_level,
Capability.color_control,
Capability.color_temperature,
],
status={
Attribute.switch: "on",
Attribute.level: 100,
Attribute.hue: 76.0,
Attribute.saturation: 55.0,
Attribute.color_temperature: 4500,
},
),
]
async def test_async_setup_platform():
"""Test setup platform does nothing (it uses config entries)."""
await light.async_setup_platform(None, None, None)
async def test_entity_state(hass, light_devices):
"""Tests the state attributes properly match the light types."""
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Dimmer 1
state = hass.states.get("light.dimmer_1")
assert state.state == "on"
assert (
state.attributes[ATTR_SUPPORTED_FEATURES]
== SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
)
assert isinstance(state.attributes[ATTR_BRIGHTNESS], int)
assert state.attributes[ATTR_BRIGHTNESS] == 255
# Color Dimmer 1
state = hass.states.get("light.color_dimmer_1")
assert state.state == "off"
assert (
state.attributes[ATTR_SUPPORTED_FEATURES]
== SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION | SUPPORT_COLOR
)
# Color Dimmer 2
state = hass.states.get("light.color_dimmer_2")
assert state.state == "on"
assert (
state.attributes[ATTR_SUPPORTED_FEATURES]
== SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION | SUPPORT_COLOR | SUPPORT_COLOR_TEMP
)
assert state.attributes[ATTR_BRIGHTNESS] == 255
assert state.attributes[ATTR_HS_COLOR] == (273.6, 55.0)
assert isinstance(state.attributes[ATTR_COLOR_TEMP], int)
assert state.attributes[ATTR_COLOR_TEMP] == 222
async def test_entity_and_device_attributes(hass, device_factory):
"""Test the attributes of the entity are correct."""
# Arrange
device = device_factory("Light 1", [Capability.switch, Capability.switch_level])
entity_registry = await hass.helpers.entity_registry.async_get_registry()
device_registry = await hass.helpers.device_registry.async_get_registry()
# Act
await setup_platform(hass, LIGHT_DOMAIN, devices=[device])
# Assert
entry = entity_registry.async_get("light.light_1")
assert entry
assert entry.unique_id == device.device_id
entry = device_registry.async_get_device({(DOMAIN, device.device_id)}, [])
assert entry
assert entry.name == device.label
assert entry.model == device.device_type_name
assert entry.manufacturer == "Unavailable"
async def test_turn_off(hass, light_devices):
"""Test the light turns of successfully."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light", "turn_off", {"entity_id": "light.color_dimmer_2"}, blocking=True
)
# Assert
state = hass.states.get("light.color_dimmer_2")
assert state is not None
assert state.state == "off"
async def test_turn_off_with_transition(hass, light_devices):
"""Test the light turns of successfully with transition."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light",
"turn_off",
{ATTR_ENTITY_ID: "light.color_dimmer_2", ATTR_TRANSITION: 2},
blocking=True,
)
# Assert
state = hass.states.get("light.color_dimmer_2")
assert state is not None
assert state.state == "off"
async def test_turn_on(hass, light_devices):
"""Test the light turns of successfully."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light", "turn_on", {ATTR_ENTITY_ID: "light.color_dimmer_1"}, blocking=True
)
# Assert
state = hass.states.get("light.color_dimmer_1")
assert state is not None
assert state.state == "on"
async def test_turn_on_with_brightness(hass, light_devices):
"""Test the light turns on to the specified brightness."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light",
"turn_on",
{
ATTR_ENTITY_ID: "light.color_dimmer_1",
ATTR_BRIGHTNESS: 75,
ATTR_TRANSITION: 2,
},
blocking=True,
)
# Assert
state = hass.states.get("light.color_dimmer_1")
assert state is not None
assert state.state == "on"
# round-trip rounding error (expected)
assert state.attributes[ATTR_BRIGHTNESS] == 74
async def test_turn_on_with_minimal_brightness(hass, light_devices):
"""
Test lights set to lowest brightness when converted scale would be zero.
SmartThings light brightness is a percentage (0-100), but HASS uses a
0-255 scale. This tests if a really low value (1-2) is passed, we don't
set the level to zero, which turns off the lights in SmartThings.
"""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light",
"turn_on",
{ATTR_ENTITY_ID: "light.color_dimmer_1", ATTR_BRIGHTNESS: 2},
blocking=True,
)
# Assert
state = hass.states.get("light.color_dimmer_1")
assert state is not None
assert state.state == "on"
# round-trip rounding error (expected)
assert state.attributes[ATTR_BRIGHTNESS] == 3
async def test_turn_on_with_color(hass, light_devices):
"""Test the light turns on with color."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light",
"turn_on",
{ATTR_ENTITY_ID: "light.color_dimmer_2", ATTR_HS_COLOR: (180, 50)},
blocking=True,
)
# Assert
state = hass.states.get("light.color_dimmer_2")
assert state is not None
assert state.state == "on"
assert state.attributes[ATTR_HS_COLOR] == (180, 50)
async def test_turn_on_with_color_temp(hass, light_devices):
"""Test the light turns on with color temp."""
# Arrange
await setup_platform(hass, LIGHT_DOMAIN, devices=light_devices)
# Act
await hass.services.async_call(
"light",
"turn_on",
{ATTR_ENTITY_ID: "light.color_dimmer_2", ATTR_COLOR_TEMP: 300},
blocking=True,
)
# Assert
state = hass.states.get("light.color_dimmer_2")
assert state is not None
assert state.state == "on"
assert state.attributes[ATTR_COLOR_TEMP] == 300
async def test_update_from_signal(hass, device_factory):
"""Test the light updates when receiving a signal."""
# Arrange
device = device_factory(
"Color Dimmer 2",
capabilities=[
Capability.switch,
Capability.switch_level,
Capability.color_control,
Capability.color_temperature,
],
status={
Attribute.switch: "off",
Attribute.level: 100,
Attribute.hue: 76.0,
Attribute.saturation: 55.0,
Attribute.color_temperature: 4500,
},
)
await setup_platform(hass, LIGHT_DOMAIN, devices=[device])
await device.switch_on(True)
# Act
async_dispatcher_send(hass, SIGNAL_SMARTTHINGS_UPDATE, [device.device_id])
# Assert
await hass.async_block_till_done()
state = hass.states.get("light.color_dimmer_2")
assert state is not None
assert state.state == "on"
async def test_unload_config_entry(hass, device_factory):
"""Test the light is removed when the config entry is unloaded."""
# Arrange
device = device_factory(
"Color Dimmer 2",
capabilities=[
Capability.switch,
Capability.switch_level,
Capability.color_control,
Capability.color_temperature,
],
status={
Attribute.switch: "off",
Attribute.level: 100,
Attribute.hue: 76.0,
Attribute.saturation: 55.0,
Attribute.color_temperature: 4500,
},
)
config_entry = await setup_platform(hass, LIGHT_DOMAIN, devices=[device])
# Act
await hass.config_entries.async_forward_entry_unload(config_entry, "light")
# Assert
assert not hass.states.get("light.color_dimmer_2")