|
1 | 1 | import pytest |
2 | | -from typing import ClassVar |
| 2 | +from typing import List, Type, Union |
3 | 3 |
|
4 | 4 | import tion_btle |
5 | 5 |
|
6 | 6 |
|
7 | | -@pytest.mark.parametrize( |
8 | | - "tion_instance, data, result", |
9 | | - [ |
10 | | - [ |
11 | | - tion_btle.s3.S3, |
12 | | - [0xb3, 0x10, 0x24, 0x14, 0x03, 0x00, 0x15, 0x14, 0x14, 0x8f, |
13 | | - 0x00, 0x0c, 0x0a, 0x00, 0x4b, 0x0a, 0x00, 0x33, 0x00, 0x5a], |
14 | | - { |
15 | | - 'state': 'on', |
16 | | - 'heater': 'on', |
17 | | - 'sound': 'off', |
18 | | - 'mode': 'outside', |
19 | | - 'out_temp': 20, |
20 | | - 'in_temp': 20, |
21 | | - 'target_temp': 20, |
22 | | - 'fan_speed': 4, |
23 | | - '_filter_remain': 143, |
24 | | - 'model': 'S3', |
25 | | - } |
26 | | - ], |
27 | | - [ |
28 | | - tion_btle.lite.Lite, |
29 | | - [0xcf, 0xd8, 0x02, 0x0f, 0x04, 0x09, 0x0f, 0x1a, 0x80, 0x8e, 0x05, 0x00, 0xe9, 0x8b, 0x05, 0x00, 0x17, 0xc2, |
| 7 | +scenarios: List[dict[str, Union[Type[tion_btle.Tion], bytearray, dict]]] = [ |
| 8 | + { |
| 9 | + "instance": tion_btle.s3.S3, |
| 10 | + "data": [0xb3, 0x10, 0x24, 0x14, 0x03, 0x00, 0x15, 0x14, 0x14, 0x8f, 0x00, 0x0c, 0x0a, 0x00, 0x4b, 0x0a, 0x00, |
| 11 | + 0x33, 0x00, 0x5a], |
| 12 | + "results": { |
| 13 | + 'state': 'on', |
| 14 | + 'heater': 'on', |
| 15 | + 'sound': 'off', |
| 16 | + 'mode': 'outside', |
| 17 | + 'out_temp': 20, |
| 18 | + 'in_temp': 20, |
| 19 | + 'target_temp': 20, |
| 20 | + 'fan_speed': 4, |
| 21 | + '_filter_remain': 143, |
| 22 | + 'model': 'S3', |
| 23 | + } |
| 24 | + }, |
| 25 | + { |
| 26 | + "instance": tion_btle.lite.Lite, |
| 27 | + "data": [0xcf, 0xd8, 0x02, 0x0f, 0x04, 0x09, 0x0f, 0x1a, 0x80, 0x8e, 0x05, 0x00, 0xe9, 0x8b, 0x05, 0x00, 0x17, 0xc2, |
30 | 28 | 0xe7, 0x00, 0x26, 0x1b, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
31 | 29 | 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x14, 0x19, 0x02, 0x04, 0x06, |
32 | 30 | 0x06, 0x18, 0x00], |
33 | | - { |
34 | | - 'state': 'on', |
35 | | - 'heater': 'on', |
36 | | - 'sound': 'on', |
37 | | - 'mode': 'outside', |
38 | | - 'out_temp': 15, |
39 | | - 'in_temp': 9, |
40 | | - 'target_temp': 15, |
41 | | - 'fan_speed': 4, |
42 | | - '_filter_remain': 175.7928587962963, |
43 | | - 'model': 'Lite', |
44 | | - '_device_work_time': 18.284884259259258, |
45 | | - '_electronic_work_time': 4.214814814814815, |
46 | | - '_electronic_temp': 26, |
47 | | - '_co2_auto_control': 0, |
48 | | - '_filter_change_required': 0, |
49 | | - 'light': 'on', |
50 | | - } |
51 | | - ] |
52 | | - ] |
53 | | -) |
54 | | -def test_decode(tion_instance: ClassVar[tion_btle.tion.tion], data: bytearray, result: dict): |
55 | | - tion = tion_instance(mac="") |
56 | | - tion._decode_response(data) |
57 | | - for parameter in result.keys(): |
58 | | - assert getattr(tion, parameter) == result[parameter] |
| 31 | + "results": { |
| 32 | + 'state': 'on', |
| 33 | + 'heater': 'on', |
| 34 | + 'sound': 'on', |
| 35 | + 'mode': 'outside', |
| 36 | + 'out_temp': 15, |
| 37 | + 'in_temp': 9, |
| 38 | + 'target_temp': 15, |
| 39 | + 'fan_speed': 4, |
| 40 | + '_filter_remain': 175.7928587962963, |
| 41 | + 'model': 'Lite', |
| 42 | + '_device_work_time': 18.284884259259258, |
| 43 | + '_electronic_work_time': 4.214814814814815, |
| 44 | + '_electronic_temp': 26, |
| 45 | + '_co2_auto_control': 0, |
| 46 | + '_filter_change_required': 0, |
| 47 | + 'light': 'on', |
| 48 | + } |
| 49 | + }, |
| 50 | + { |
| 51 | + "instance": tion_btle.S4, |
| 52 | + "data": [0x2f, 0x51, 0x00, 0x19, 0x04, 0x0e, 0x10, 0x1b, 0x26, 0x3b, 0x6e, 0x07, 0x00, 0xfa, 0x4e, 0x07, 0x00, |
| 53 | + 0x06, 0xff, 0xe5, 0x00, 0xa6, 0xe9, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00], |
| 54 | + "results": { |
| 55 | + 'state': 'on', |
| 56 | + 'heater': 'on', |
| 57 | + 'sound': 'on', |
| 58 | + 'mode': 'outside', |
| 59 | + 'out_temp': 16, |
| 60 | + 'in_temp': 14, |
| 61 | + 'target_temp': 25, |
| 62 | + 'fan_speed': 4, |
| 63 | + '_filter_remain': 174.45636574074075, |
| 64 | + 'model': 'S4', |
| 65 | + } |
| 66 | + } |
| 67 | +] |
59 | 68 |
|
| 69 | + |
| 70 | +def pytest_generate_tests(metafunc): |
| 71 | + global scenarios |
| 72 | + tests = [] |
| 73 | + |
| 74 | + for scenario in scenarios: |
| 75 | + tion_type: Type[tion_btle.Tion] = scenario["instance"] |
| 76 | + tion: tion_btle.Tion = tion_type(mac="") |
| 77 | + tion._decode_response(response=scenario["data"]) |
| 78 | + |
| 79 | + for k in scenario["results"].keys(): |
| 80 | + v = scenario["results"][k] |
| 81 | + _id = f"{tion.__class__.__name__}-{k}" |
| 82 | + tests.append(pytest.param(_id, getattr(tion, k), v, id=_id)) |
| 83 | + |
| 84 | + metafunc.parametrize("parameter,decoded_value,expected_value", tests) |
| 85 | + |
| 86 | + |
| 87 | +def test_param(parameter, decoded_value, expected_value): |
| 88 | + assert decoded_value == expected_value |
0 commit comments