Debian Codegraph @

tests/test_groups.py

raw symbols SCIP
1 """Test pydeCONZ groups.
2
3 pytest --cov-report term-missing --cov=pydeconz.group tests/test_groups.py
4 """
5
6 from unittest.mock import Mock
7
8 import pytest
9
10 from pydeconz.models.light.light import LightAlert, LightColorMode, LightEffect
11
12
13 async def test_handler_group(mock_aioresponse, deconz_called_with, deconz_session):
14 """Verify that groups works."""
15 groups = deconz_session.groups
16
17 # Set attributes
18
19 mock_aioresponse.put("http://host:80/api/apikey/groups/0")
20 await groups.set_attributes(
21 id="0",
22 hidden=False,
23 light_sequence=["1", "2"],
24 lights=["3", "4"],
25 multi_device_ids=["5", "6"],
26 name="Group",
27 )
28 assert deconz_called_with(
29 "put",
30 path="/groups/0",
31 json={
32 "hidden": False,
33 "lightsequence": ["1", "2"],
34 "lights": ["3", "4"],
35 "multideviceids": ["5", "6"],
36 "name": "Group",
37 },
38 )
39
40 mock_aioresponse.put("http://host:80/api/apikey/groups/0")
41 await groups.set_attributes(id="0", name="Group")
42 assert deconz_called_with(
43 "put",
44 path="/groups/0",
45 json={"name": "Group"},
46 )
47
48 # Set state
49
50 mock_aioresponse.put("http://host:80/api/apikey/groups/0/action")
51 await groups.set_state(
52 id="0",
53 alert=LightAlert.SHORT,
54 brightness=200,
55 color_loop_speed=10,
56 color_temperature=400,
57 effect=LightEffect.COLOR_LOOP,
58 hue=1000,
59 on=True,
60 on_time=100,
61 saturation=150,
62 toggle=True,
63 transition_time=250,
64 xy=(0.1, 0.1),
65 )
66 assert deconz_called_with(
67 "put",
68 path="/groups/0/action",
69 json={
70 "alert": "select",
71 "bri": 200,
72 "colorloopspeed": 10,
73 "ct": 400,
74 "effect": "colorloop",
75 "hue": 1000,
76 "on": True,
77 "ontime": 100,
78 "sat": 150,
79 "toggle": True,
80 "transitiontime": 250,
81 "xy": (0.1, 0.1),
82 },
83 )
84
85 mock_aioresponse.put("http://host:80/api/apikey/groups/0/action")
86 await groups.set_state("0", on=True)
87 assert deconz_called_with(
88 "put",
89 path="/groups/0/action",
90 json={"on": True},
91 )
92
93
94 async def test_group(deconz_refresh_state):
95 """Verify that groups works."""
96 deconz_session = await deconz_refresh_state(
97 groups={
98 "0": {
99 "action": {
100 "bri": 132,
101 "colormode": "hs",
102 "ct": 0,
103 "effect": "none",
104 "hue": 0,
105 "on": False,
106 "sat": 127,
107 "scene": None,
108 "xy": [0, 0],
109 },
110 "devicemembership": [],
111 "etag": "e31c23b3bd9ece918f23ee17ef430304",
112 "id": "11",
113 "lights": ["14", "15", "12"],
114 "name": "Hall",
115 "scenes": [
116 {
117 "id": "1",
118 "name": "warmlight",
119 "lightcount": 3,
120 "transitiontime": 10,
121 }
122 ],
123 "state": {"all_on": False, "any_on": True},
124 "type": "LightGroup",
125 }
126 }
127 )
128
129 group = deconz_session.groups["0"]
130
131 assert group.state is True
132 assert group.all_on is False
133 assert group.any_on is True
134 assert group.device_membership == []
135 assert group.hidden is None
136 assert group.id == "11"
137 assert group.lights == ["14", "15", "12"]
138 assert group.light_sequence is None
139 assert group.multi_device_ids is None
140
141 assert group.brightness == 132
142 assert group.hue == 0
143 assert group.saturation == 127
144 assert group.color_temp == 0
145 assert group.xy == (0, 0)
146 assert group.color_mode == LightColorMode.HS
147 assert group.effect == LightEffect.NONE
148 assert group.reachable is True
149
150 assert group.deconz_id == "/groups/0"
151 assert group.etag == "e31c23b3bd9ece918f23ee17ef430304"
152 assert group.manufacturer == ""
153 assert group.model_id == ""
154 assert group.name == "Hall"
155 assert group.software_version == ""
156 assert group.type == "LightGroup"
157 assert group.unique_id == ""
158
159 group.register_callback(mock_callback := Mock())
160 assert group._callbacks
161
162 event = {"state": {"all_on": False, "any_on": False}}
163 group.update(event)
164
165 assert group.all_on is False
166 assert group.any_on is False
167 mock_callback.assert_called_once()
168 assert group.changed_keys == {"state", "all_on", "any_on"}
169
170 group.remove_callback(mock_callback)
171 assert not group._callbacks
172
173 group.raw["action"]["xy"] = (65555, 65555)
174 assert group.xy == (1, 1)
175
176 del group.raw["action"]["xy"]
177 assert group.xy is None
178
179
180 ENUM_PROPERTY_DATA = [
181 (
182 ("action", "colormode"),
183 "color_mode",
184 {
185 "ct": LightColorMode.CT,
186 "hs": LightColorMode.HS,
187 "xy": LightColorMode.XY,
188 None: LightColorMode.UNKNOWN,
189 },
190 ),
191 (
192 ("action", "effect"),
193 "effect",
194 {
195 "colorloop": LightEffect.COLOR_LOOP,
196 "none": LightEffect.NONE,
197 None: LightEffect.UNKNOWN,
198 },
199 ),
200 ]
201
202
203 @pytest.mark.parametrize(("path", "property", "data"), ENUM_PROPERTY_DATA)
204 async def test_enum_group_properties(deconz_refresh_state, path, property, data):
205 """Verify enum properties return expected values or None."""
206 deconz_session = await deconz_refresh_state(
207 groups={"0": {"action": {}, "scenes": []}}
208 )
209 group = deconz_session.groups["0"]
210
211 assert getattr(group, property) is None
212
213 for input, output in data.items():
214 data = {path[0]: input}
215 if len(path) == 2:
216 data = {path[0]: {path[1]: input}}
217 group.update(data)
218 assert getattr(group, property) == output