File: test_switch.py

package info (click to toggle)
blebox-uniapi 2.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: python: 4,994; makefile: 85; sh: 5
file content (446 lines) | stat: -rw-r--r-- 14,758 bytes parent folder | download
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
"""Blebox switch tests."""

from blebox_uniapi.box_types import get_latest_api_level

from .conftest import CommonEntity, DefaultBoxTest, future_date

DEVICE_CLASS_SWITCH = "switch"


class BleBoxSwitchEntity(CommonEntity):
    @property
    def device_class(self):
        types = {"relay": DEVICE_CLASS_SWITCH}
        return types[self._feature.device_class]

    @property
    def is_on(self):
        return self._feature.is_on

    async def async_turn_on(self, **kwargs):
        return await self._feature.async_turn_on()

    async def async_turn_off(self, **kwargs):
        return await self._feature.async_turn_off()


class TestSwitchBox0(DefaultBoxTest):
    """Tests for BleBox switchBox."""

    DEVCLASS = "switches"
    ENTITY_CLASS = BleBoxSwitchEntity

    DEV_INFO_PATH = "api/relay/state"

    DEVICE_INFO = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": "20180604",
        }
    }
    DEVICE_INFO_FUTURE = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": future_date(),
        }
    }
    DEVICE_INFO_LATEST = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": get_latest_api_level("switchBox"),
        }
    }
    DEVICE_INFO_UNSUPPORTED = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": 20180603,
        }
    }
    DEVICE_INFO_UNSPECIFIED_API = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
        }
    }

    STATE_OFF = [{"relay": 0, "state": 0, "stateAfterRestart": 0}]
    STATE_ON = [{"relay": 0, "state": 1, "stateAfterRestart": 0}]
    STATE_DEFAULT = STATE_OFF

    async def test_init(self, aioclient_mock):
        """Test switch default state."""
        await self.allow_get_info(aioclient_mock)
        entity = (await self.async_entities(aioclient_mock))[0]

        assert entity.name == "My switchBox (switchBox#0.relay)"
        assert entity.unique_id == "BleBox-switchBox-1afe34e750b8-0.relay"
        assert entity.device_class == DEVICE_CLASS_SWITCH
        assert entity.is_on is None

    async def test_device_info(self, aioclient_mock):
        await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
        entity = (await self.async_entities(aioclient_mock))[0]

        assert entity.device_info["name"] == "My switchBox"
        assert entity.device_info["mac"] == "1afe34e750b8"
        assert entity.device_info["manufacturer"] == "BleBox"
        assert entity.device_info["model"] == "switchBox"
        assert entity.device_info["sw_version"] == "0.247"

    async def test_update_when_off(self, aioclient_mock):
        """Test switch updating when off."""
        entity = await self.updated(aioclient_mock, self.STATE_OFF)
        assert entity.is_on is False

    async def test_update_when_on(self, aioclient_mock):
        """Test switch updating when on."""
        entity = await self.updated(aioclient_mock, self.STATE_ON)
        assert entity.is_on is True

    async def test_on(self, aioclient_mock):
        """Test turning switch on."""
        entity = await self.updated(aioclient_mock, self.STATE_OFF)
        self.allow_get(aioclient_mock, "/s/1", self.STATE_ON)
        await entity.async_turn_on()
        assert entity.is_on is True

    async def test_off(self, aioclient_mock):
        """Test turning switch off."""
        entity = await self.updated(aioclient_mock, self.STATE_ON)
        self.allow_get(aioclient_mock, "/s/0", self.STATE_OFF)
        await entity.async_turn_off()
        assert entity.is_on is False


class TestSwitchBox(DefaultBoxTest):
    """Tests for BleBox switchBox."""

    DEVCLASS = "switches"
    ENTITY_CLASS = BleBoxSwitchEntity

    DEV_INFO_PATH = "api/relay/extended/state"
    DEVICE_INFO = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": "20190808",
        }
    }

    def patch_version(apiLevel):
        """Generate a patch for a JSON state fixture."""
        return f"""
        {{ "device": {{ "apiLevel": {apiLevel} }} }}
        """

    DEVICE_INFO_FUTURE = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": future_date(),
        }
    }
    DEVICE_INFO_LATEST = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            "apiLevel": get_latest_api_level("switchBox"),
        }
    }
    DEVICE_INFO_UNSUPPORTED = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
            # since below it 20180808 it switches to switchBox0
            "apiLevel": 20180604 - 1,
        }
    }

    DEVICE_INFO_UNSPECIFIED_API = {
        "device": {
            "deviceName": "My switchBox",
            "type": "switchBox",
            "fv": "0.247",
            "hv": "0.2",
            "id": "1afe34e750b8",
            "ip": "192.168.1.239",
        }
    }

    STATE_OFF = {"relays": [{"relay": 0, "state": 0, "stateAfterRestart": 0}]}
    STATE_ON = {"relays": [{"relay": 0, "state": 1, "stateAfterRestart": 0}]}
    STATE_DEFAULT = STATE_OFF

    async def test_init(self, aioclient_mock):
        """Test switch default state."""

        await self.allow_get_info(aioclient_mock)

        entity = (await self.async_entities(aioclient_mock))[0]

        assert entity.name == "My switchBox (switchBox#0.relay)"
        assert entity.unique_id == "BleBox-switchBox-1afe34e750b8-0.relay"

        assert entity.device_class == DEVICE_CLASS_SWITCH

        assert entity.is_on is None

    async def test_device_info(self, aioclient_mock):
        await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
        entity = (await self.async_entities(aioclient_mock))[0]
        assert entity.device_info["name"] == "My switchBox"
        assert entity.device_info["mac"] == "1afe34e750b8"
        assert entity.device_info["manufacturer"] == "BleBox"
        assert entity.device_info["model"] == "switchBox"
        assert entity.device_info["sw_version"] == "0.247"

    async def test_update_when_off(self, aioclient_mock):
        """Test switch updating when off."""

        entity = await self.updated(aioclient_mock, self.STATE_OFF)
        assert entity.is_on is False

    async def test_update_when_on(self, aioclient_mock):
        """Test switch updating when on."""

        entity = await self.updated(aioclient_mock, self.STATE_ON)
        assert entity.is_on is True

    async def test_on(self, aioclient_mock):
        """Test turning switch on."""

        entity = await self.updated(aioclient_mock, self.STATE_OFF)
        self.allow_get(aioclient_mock, "/s/1", self.STATE_ON)
        await entity.async_turn_on()
        assert entity.is_on is True

    async def test_off(self, aioclient_mock):
        """Test turning switch off."""

        entity = await self.updated(aioclient_mock, self.STATE_ON)
        self.allow_get(aioclient_mock, "/s/0", self.STATE_OFF)
        await entity.async_turn_off()
        assert entity.is_on is False


class TestSwitchBoxD(DefaultBoxTest):
    """Tests for BleBox switchBoxD."""

    DEVCLASS = "switches"
    ENTITY_CLASS = BleBoxSwitchEntity
    DEV_INFO_PATH = "state/extended"
    DEVICE_INFO = {
        "device": {
            "deviceName": "My switchBoxD",
            "type": "switchBoxD",
            "fv": "0.200",
            "hv": "0.7",
            "id": "1afe34e750b8",
            "apiLevel": "20200831",
        }
    }
    DEVICE_INFO_FUTURE = {
        "device": {
            "deviceName": "My switchBoxD",
            "type": "switchBoxD",
            "fv": "0.200",
            "hv": "0.7",
            "id": "1afe34e750b8",
            "apiLevel": future_date(),
        }
    }
    DEVICE_INFO_LATEST = {
        "device": {
            "deviceName": "My switchBoxD",
            "type": "switchBoxD",
            "fv": "0.200",
            "hv": "0.7",
            "id": "1afe34e750b8",
            "apiLevel": get_latest_api_level("switchBoxD"),
        }
    }

    DEVICE_INFO_UNSUPPORTED = {
        "device": {
            "deviceName": "My switchBoxD",
            "type": "switchBoxD",
            "fv": "0.200",
            "hv": "0.7",
            "id": "1afe34e750b8",
            "apiLevel": 20190807,
        }
    }

    DEVICE_INFO_UNSPECIFIED_API = {
        "device": {
            "deviceName": "My switchBoxD",
            "type": "switchBoxD",
            "fv": "0.200",
            "hv": "0.7",
            "id": "1afe34e750b8",
        }
    }
    STATE_BOTH_OFF = {
        "relays": [
            {"relay": 0, "state": 0, "stateAfterRestart": 0, "name": "output 1"},
            {"relay": 1, "state": 0, "stateAfterRestart": 0, "name": "output 2"},
        ]
    }
    STATE_FIRST_ON = {
        "relays": [
            {"relay": 0, "state": 1, "stateAfterRestart": 0, "name": "output 1"},
            {"relay": 1, "state": 0, "stateAfterRestart": 0, "name": "output 2"},
        ]
    }
    STATE_SECOND_ON = {
        "relays": [
            {"relay": 0, "state": 0, "stateAfterRestart": 0, "name": "output 1"},
            {"relay": 1, "state": 1, "stateAfterRestart": 0, "name": "output 2"},
        ]
    }
    STATE_BOTH_ON = {
        "relays": [
            {"relay": 0, "state": 1, "stateAfterRestart": 0, "name": "output 1"},
            {"relay": 1, "state": 1, "stateAfterRestart": 0, "name": "output 2"},
        ]
    }
    STATE_DEFAULT = STATE_BOTH_OFF

    async def test_init(self, aioclient_mock):
        """Test switch default state."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        entity = entities[0]
        # TODO: include output names?
        assert entity.name == "My switchBoxD (switchBoxD#0.relay)"
        assert entity.unique_id == "BleBox-switchBoxD-1afe34e750b8-0.relay"
        assert entity.device_class == DEVICE_CLASS_SWITCH
        assert entity.is_on is None

        entity = entities[1]
        assert entity.name == "My switchBoxD (switchBoxD#1.relay)"
        assert entity.unique_id == "BleBox-switchBoxD-1afe34e750b8-1.relay"
        assert entity.device_class == DEVICE_CLASS_SWITCH
        assert entity.is_on is None

    async def test_device_info(self, aioclient_mock):
        await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
        entity = (await self.async_entities(aioclient_mock))[0]
        assert entity.device_info["name"] == "My switchBoxD"
        assert entity.device_info["mac"] == "1afe34e750b8"
        assert entity.device_info["manufacturer"] == "BleBox"
        assert entity.device_info["model"] == "switchBoxD"
        assert entity.device_info["sw_version"] == "0.200"

    async def test_update_when_off(self, aioclient_mock):
        """Test switch updating when off."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get_state(aioclient_mock, self.STATE_BOTH_OFF)

        # updating any one is fine
        await entities[0].async_update()

        assert entities[0].is_on is False
        assert entities[1].is_on is False

    async def test_update_when_second_off(self, aioclient_mock):
        """Test switch updating when off."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get_state(aioclient_mock, self.STATE_FIRST_ON)

        # updating any one is fine
        await entities[0].async_update()

        assert entities[0].is_on is True
        assert entities[1].is_on is False

    async def test_first_on(self, aioclient_mock):
        """Test turning switch on."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get(aioclient_mock, "/s/0/1", self.STATE_FIRST_ON)
        await entities[0].async_turn_on()
        assert entities[0].is_on is True
        assert entities[1].is_on is False

    async def test_second_on(self, aioclient_mock):
        """Test turning switch on."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get(aioclient_mock, "/s/1/1", self.STATE_SECOND_ON)
        await entities[1].async_turn_on()
        assert entities[0].is_on is False
        assert entities[1].is_on is True

    async def test_first_off(self, aioclient_mock):
        """Test turning switch on."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get_state(aioclient_mock, self.STATE_BOTH_ON)
        await entities[0].async_update()

        self.allow_get(aioclient_mock, "/s/0/0", self.STATE_SECOND_ON)
        await entities[0].async_turn_off()
        assert entities[0].is_on is False
        assert entities[1].is_on is True

    async def test_second_off(self, aioclient_mock):
        """Test turning switch on."""
        await self.allow_get_info(aioclient_mock)
        entities = await self.async_entities(aioclient_mock)

        self.allow_get_state(aioclient_mock, self.STATE_BOTH_ON)
        await entities[0].async_update()

        self.allow_get(aioclient_mock, "/s/1/0", self.STATE_FIRST_ON)
        await entities[1].async_turn_off()
        assert entities[0].is_on is True
        assert entities[1].is_on is False