Debian Codegraph @

tests/test_switch.py

raw symbols SCIP
1 """Blebox switch tests."""
2
3 from blebox_uniapi.box_types import get_latest_api_level
4
5 from .conftest import CommonEntity, DefaultBoxTest, future_date
6
7 DEVICE_CLASS_SWITCH = "switch"
8
9
10 class BleBoxSwitchEntity(CommonEntity):
11 @property
12 def device_class(self):
13 types = {"relay": DEVICE_CLASS_SWITCH}
14 return types[self._feature.device_class]
15
16 @property
17 def is_on(self):
18 return self._feature.is_on
19
20 async def async_turn_on(self, **kwargs):
21 return await self._feature.async_turn_on()
22
23 async def async_turn_off(self, **kwargs):
24 return await self._feature.async_turn_off()
25
26
27 class TestSwitchBox0(DefaultBoxTest):
28 """Tests for BleBox switchBox."""
29
30 DEVCLASS = "switches"
31 ENTITY_CLASS = BleBoxSwitchEntity
32
33 DEV_INFO_PATH = "api/relay/state"
34
35 DEVICE_INFO = {
36 "device": {
37 "deviceName": "My switchBox",
38 "type": "switchBox",
39 "fv": "0.247",
40 "hv": "0.2",
41 "id": "1afe34e750b8",
42 "ip": "192.168.1.239",
43 "apiLevel": "20180604",
44 }
45 }
46 DEVICE_INFO_FUTURE = {
47 "device": {
48 "deviceName": "My switchBox",
49 "type": "switchBox",
50 "fv": "0.247",
51 "hv": "0.2",
52 "id": "1afe34e750b8",
53 "ip": "192.168.1.239",
54 "apiLevel": future_date(),
55 }
56 }
57 DEVICE_INFO_LATEST = {
58 "device": {
59 "deviceName": "My switchBox",
60 "type": "switchBox",
61 "fv": "0.247",
62 "hv": "0.2",
63 "id": "1afe34e750b8",
64 "ip": "192.168.1.239",
65 "apiLevel": get_latest_api_level("switchBox"),
66 }
67 }
68 DEVICE_INFO_UNSUPPORTED = {
69 "device": {
70 "deviceName": "My switchBox",
71 "type": "switchBox",
72 "fv": "0.247",
73 "hv": "0.2",
74 "id": "1afe34e750b8",
75 "ip": "192.168.1.239",
76 "apiLevel": 20180603,
77 }
78 }
79 DEVICE_INFO_UNSPECIFIED_API = {
80 "device": {
81 "deviceName": "My switchBox",
82 "type": "switchBox",
83 "fv": "0.247",
84 "hv": "0.2",
85 "id": "1afe34e750b8",
86 "ip": "192.168.1.239",
87 }
88 }
89
90 STATE_OFF = [{"relay": 0, "state": 0, "stateAfterRestart": 0}]
91 STATE_ON = [{"relay": 0, "state": 1, "stateAfterRestart": 0}]
92 STATE_DEFAULT = STATE_OFF
93
94 async def test_init(self, aioclient_mock):
95 """Test switch default state."""
96 await self.allow_get_info(aioclient_mock)
97 entity = (await self.async_entities(aioclient_mock))[0]
98
99 assert entity.name == "My switchBox (switchBox#0.relay)"
100 assert entity.unique_id == "BleBox-switchBox-1afe34e750b8-0.relay"
101 assert entity.device_class == DEVICE_CLASS_SWITCH
102 assert entity.is_on is None
103
104 async def test_device_info(self, aioclient_mock):
105 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
106 entity = (await self.async_entities(aioclient_mock))[0]
107
108 assert entity.device_info["name"] == "My switchBox"
109 assert entity.device_info["mac"] == "1afe34e750b8"
110 assert entity.device_info["manufacturer"] == "BleBox"
111 assert entity.device_info["model"] == "switchBox"
112 assert entity.device_info["sw_version"] == "0.247"
113
114 async def test_update_when_off(self, aioclient_mock):
115 """Test switch updating when off."""
116 entity = await self.updated(aioclient_mock, self.STATE_OFF)
117 assert entity.is_on is False
118
119 async def test_update_when_on(self, aioclient_mock):
120 """Test switch updating when on."""
121 entity = await self.updated(aioclient_mock, self.STATE_ON)
122 assert entity.is_on is True
123
124 async def test_on(self, aioclient_mock):
125 """Test turning switch on."""
126 entity = await self.updated(aioclient_mock, self.STATE_OFF)
127 self.allow_get(aioclient_mock, "/s/1", self.STATE_ON)
128 await entity.async_turn_on()
129 assert entity.is_on is True
130
131 async def test_off(self, aioclient_mock):
132 """Test turning switch off."""
133 entity = await self.updated(aioclient_mock, self.STATE_ON)
134 self.allow_get(aioclient_mock, "/s/0", self.STATE_OFF)
135 await entity.async_turn_off()
136 assert entity.is_on is False
137
138
139 class TestSwitchBox(DefaultBoxTest):
140 """Tests for BleBox switchBox."""
141
142 DEVCLASS = "switches"
143 ENTITY_CLASS = BleBoxSwitchEntity
144
145 DEV_INFO_PATH = "api/relay/extended/state"
146 DEVICE_INFO = {
147 "device": {
148 "deviceName": "My switchBox",
149 "type": "switchBox",
150 "fv": "0.247",
151 "hv": "0.2",
152 "id": "1afe34e750b8",
153 "ip": "192.168.1.239",
154 "apiLevel": "20190808",
155 }
156 }
157
158 def patch_version(apiLevel):
159 """Generate a patch for a JSON state fixture."""
160 return f"""
161 {{ "device": {{ "apiLevel": {apiLevel} }} }}
162 """
163
164 DEVICE_INFO_FUTURE = {
165 "device": {
166 "deviceName": "My switchBox",
167 "type": "switchBox",
168 "fv": "0.247",
169 "hv": "0.2",
170 "id": "1afe34e750b8",
171 "ip": "192.168.1.239",
172 "apiLevel": future_date(),
173 }
174 }
175 DEVICE_INFO_LATEST = {
176 "device": {
177 "deviceName": "My switchBox",
178 "type": "switchBox",
179 "fv": "0.247",
180 "hv": "0.2",
181 "id": "1afe34e750b8",
182 "ip": "192.168.1.239",
183 "apiLevel": get_latest_api_level("switchBox"),
184 }
185 }
186 DEVICE_INFO_UNSUPPORTED = {
187 "device": {
188 "deviceName": "My switchBox",
189 "type": "switchBox",
190 "fv": "0.247",
191 "hv": "0.2",
192 "id": "1afe34e750b8",
193 "ip": "192.168.1.239",
194 # since below it 20180808 it switches to switchBox0
195 "apiLevel": 20180604 - 1,
196 }
197 }
198
199 DEVICE_INFO_UNSPECIFIED_API = {
200 "device": {
201 "deviceName": "My switchBox",
202 "type": "switchBox",
203 "fv": "0.247",
204 "hv": "0.2",
205 "id": "1afe34e750b8",
206 "ip": "192.168.1.239",
207 }
208 }
209
210 STATE_OFF = {"relays": [{"relay": 0, "state": 0, "stateAfterRestart": 0}]}
211 STATE_ON = {"relays": [{"relay": 0, "state": 1, "stateAfterRestart": 0}]}
212 STATE_DEFAULT = STATE_OFF
213
214 async def test_init(self, aioclient_mock):
215 """Test switch default state."""
216
217 await self.allow_get_info(aioclient_mock)
218
219 entity = (await self.async_entities(aioclient_mock))[0]
220
221 assert entity.name == "My switchBox (switchBox#0.relay)"
222 assert entity.unique_id == "BleBox-switchBox-1afe34e750b8-0.relay"
223
224 assert entity.device_class == DEVICE_CLASS_SWITCH
225
226 assert entity.is_on is None
227
228 async def test_device_info(self, aioclient_mock):
229 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
230 entity = (await self.async_entities(aioclient_mock))[0]
231 assert entity.device_info["name"] == "My switchBox"
232 assert entity.device_info["mac"] == "1afe34e750b8"
233 assert entity.device_info["manufacturer"] == "BleBox"
234 assert entity.device_info["model"] == "switchBox"
235 assert entity.device_info["sw_version"] == "0.247"
236
237 async def test_update_when_off(self, aioclient_mock):
238 """Test switch updating when off."""
239
240 entity = await self.updated(aioclient_mock, self.STATE_OFF)
241 assert entity.is_on is False
242
243 async def test_update_when_on(self, aioclient_mock):
244 """Test switch updating when on."""
245
246 entity = await self.updated(aioclient_mock, self.STATE_ON)
247 assert entity.is_on is True
248
249 async def test_on(self, aioclient_mock):
250 """Test turning switch on."""
251
252 entity = await self.updated(aioclient_mock, self.STATE_OFF)
253 self.allow_get(aioclient_mock, "/s/1", self.STATE_ON)
254 await entity.async_turn_on()
255 assert entity.is_on is True
256
257 async def test_off(self, aioclient_mock):
258 """Test turning switch off."""
259
260 entity = await self.updated(aioclient_mock, self.STATE_ON)
261 self.allow_get(aioclient_mock, "/s/0", self.STATE_OFF)
262 await entity.async_turn_off()
263 assert entity.is_on is False
264
265
266 class TestSwitchBoxD(DefaultBoxTest):
267 """Tests for BleBox switchBoxD."""
268
269 DEVCLASS = "switches"
270 ENTITY_CLASS = BleBoxSwitchEntity
271 DEV_INFO_PATH = "state/extended"
272 DEVICE_INFO = {
273 "device": {
274 "deviceName": "My switchBoxD",
275 "type": "switchBoxD",
276 "fv": "0.200",
277 "hv": "0.7",
278 "id": "1afe34e750b8",
279 "apiLevel": "20200831",
280 }
281 }
282 DEVICE_INFO_FUTURE = {
283 "device": {
284 "deviceName": "My switchBoxD",
285 "type": "switchBoxD",
286 "fv": "0.200",
287 "hv": "0.7",
288 "id": "1afe34e750b8",
289 "apiLevel": future_date(),
290 }
291 }
292 DEVICE_INFO_LATEST = {
293 "device": {
294 "deviceName": "My switchBoxD",
295 "type": "switchBoxD",
296 "fv": "0.200",
297 "hv": "0.7",
298 "id": "1afe34e750b8",
299 "apiLevel": get_latest_api_level("switchBoxD"),
300 }
301 }
302
303 DEVICE_INFO_UNSUPPORTED = {
304 "device": {
305 "deviceName": "My switchBoxD",
306 "type": "switchBoxD",
307 "fv": "0.200",
308 "hv": "0.7",
309 "id": "1afe34e750b8",
310 "apiLevel": 20190807,
311 }
312 }
313
314 DEVICE_INFO_UNSPECIFIED_API = {
315 "device": {
316 "deviceName": "My switchBoxD",
317 "type": "switchBoxD",
318 "fv": "0.200",
319 "hv": "0.7",
320 "id": "1afe34e750b8",
321 }
322 }
323 STATE_BOTH_OFF = {
324 "relays": [
325 {"relay": 0, "state": 0, "stateAfterRestart": 0, "name": "output 1"},
326 {"relay": 1, "state": 0, "stateAfterRestart": 0, "name": "output 2"},
327 ]
328 }
329 STATE_FIRST_ON = {
330 "relays": [
331 {"relay": 0, "state": 1, "stateAfterRestart": 0, "name": "output 1"},
332 {"relay": 1, "state": 0, "stateAfterRestart": 0, "name": "output 2"},
333 ]
334 }
335 STATE_SECOND_ON = {
336 "relays": [
337 {"relay": 0, "state": 0, "stateAfterRestart": 0, "name": "output 1"},
338 {"relay": 1, "state": 1, "stateAfterRestart": 0, "name": "output 2"},
339 ]
340 }
341 STATE_BOTH_ON = {
342 "relays": [
343 {"relay": 0, "state": 1, "stateAfterRestart": 0, "name": "output 1"},
344 {"relay": 1, "state": 1, "stateAfterRestart": 0, "name": "output 2"},
345 ]
346 }
347 STATE_DEFAULT = STATE_BOTH_OFF
348 DEVICE_EXTENDED_INFO = STATE_BOTH_OFF
349 DEVICE_EXTENDED_INFO_PATH = "/state/extended"
350
351 async def test_init(self, aioclient_mock):
352 """Test switch default state."""
353 await self.allow_get_info(aioclient_mock)
354 entities = await self.async_entities(aioclient_mock)
355
356 entity = entities[0]
357 assert entity.unique_id == "BleBox-switchBoxD-1afe34e750b8-0.relay_0"
358 assert entity.device_class == DEVICE_CLASS_SWITCH
359 assert entity._feature.index == 0
360 assert entity._feature.name == "output 1"
361
362 entity = entities[1]
363 assert entity.unique_id == "BleBox-switchBoxD-1afe34e750b8-0.relay_1"
364 assert entity.device_class == DEVICE_CLASS_SWITCH
365 assert entity._feature.index == 1
366 assert entity._feature.name == "output 2"
367
368 async def test_device_info(self, aioclient_mock):
369 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
370 entity = (await self.async_entities(aioclient_mock))[0]
371 assert entity.device_info["name"] == "My switchBoxD"
372 assert entity.device_info["mac"] == "1afe34e750b8"
373 assert entity.device_info["manufacturer"] == "BleBox"
374 assert entity.device_info["model"] == "switchBoxD"
375 assert entity.device_info["sw_version"] == "0.200"
376
377 async def test_update_when_off(self, aioclient_mock):
378 """Test switch updating when off."""
379 await self.allow_get_info(aioclient_mock)
380 entities = await self.async_entities(aioclient_mock)
381
382 self.allow_get_state(aioclient_mock, self.STATE_BOTH_OFF)
383
384 # updating any one is fine
385 await entities[0].async_update()
386
387 assert entities[0].is_on is False
388 assert entities[1].is_on is False
389
390 async def test_update_when_second_off(self, aioclient_mock):
391 """Test switch updating when off."""
392 await self.allow_get_info(aioclient_mock)
393 entities = await self.async_entities(aioclient_mock)
394
395 self.allow_get_state(aioclient_mock, self.STATE_FIRST_ON)
396
397 # updating any one is fine
398 await entities[0].async_update()
399
400 assert entities[0].is_on is True
401 assert entities[1].is_on is False
402
403 async def test_first_on(self, aioclient_mock):
404 """Test turning switch on."""
405 await self.allow_get_info(aioclient_mock)
406 entities = await self.async_entities(aioclient_mock)
407
408 self.allow_get(aioclient_mock, "/s/0/1", self.STATE_FIRST_ON)
409 await entities[0].async_turn_on()
410 assert entities[0].is_on is True
411 assert entities[1].is_on is False
412
413 async def test_second_on(self, aioclient_mock):
414 """Test turning switch on."""
415 await self.allow_get_info(aioclient_mock)
416 entities = await self.async_entities(aioclient_mock)
417
418 self.allow_get(aioclient_mock, "/s/1/1", self.STATE_SECOND_ON)
419 await entities[1].async_turn_on()
420 assert entities[0].is_on is False
421 assert entities[1].is_on is True
422
423 async def test_first_off(self, aioclient_mock):
424 """Test turning switch on."""
425 await self.allow_get_info(aioclient_mock)
426 entities = await self.async_entities(aioclient_mock)
427
428 self.allow_get_state(aioclient_mock, self.STATE_BOTH_ON)
429 await entities[0].async_update()
430
431 self.allow_get(aioclient_mock, "/s/0/0", self.STATE_SECOND_ON)
432 await entities[0].async_turn_off()
433 assert entities[0].is_on is False
434 assert entities[1].is_on is True
435
436 async def test_second_off(self, aioclient_mock):
437 """Test turning switch on."""
438 await self.allow_get_info(aioclient_mock)
439 entities = await self.async_entities(aioclient_mock)
440
441 self.allow_get_state(aioclient_mock, self.STATE_BOTH_ON)
442 await entities[0].async_update()
443
444 self.allow_get(aioclient_mock, "/s/1/0", self.STATE_FIRST_ON)
445 await entities[1].async_turn_off()
446 assert entities[0].is_on is True
447 assert entities[1].is_on is False