Skip to content

Commit fe58813

Browse files
committed
test: add testing of S4 decoding
1 parent cf2ce44 commit fe58813

File tree

3 files changed

+81
-58
lines changed

3 files changed

+81
-58
lines changed

tests/unit/test_decode.py

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,88 @@
11
import pytest
2-
from typing import ClassVar
2+
from typing import List, Type, Union
33

44
import tion_btle
55

66

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,
3028
0xe7, 0x00, 0x26, 0x1b, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
3129
0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x14, 0x19, 0x02, 0x04, 0x06,
3230
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+
]
5968

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

tion_btle/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
from .s3 import S3
22
from .lite import Lite
3+
from .s4 import S4
4+
from .tion import tion as Tion

tion_btle/s4.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ def _packages(self) -> list:
9090
bytearray([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x98, 0x5d])
9191
]
9292

93-
@property
94-
def _dummy_data(self) -> bytearray:
95-
full_message = bytearray([0x00, 0x2f, 0x00, 0x3a, 0x27, 0x31, 0x32, 0x72, 0x7b, 0x64, 0xd7, 0x31, 0xea, 0x58,
96-
0x3a, 0x2f, 0x51, 0x00, 0x19, 0x04, 0x0e, 0x10, 0x1b, 0x26, 0x3b, 0x6e, 0x07, 0x00,
97-
0xfa, 0x4e, 0x07, 0x00, 0x06, 0xff, 0xe5, 0x00, 0xa6, 0xe9, 0x22, 0x00, 0x00, 0x00,
98-
0x00, 0x00, 0x06, 0x00, 0x98, 0x5d])
99-
return full_message[15:-2]
100-
10193
@property
10294
def command_getStatus(self) -> bytearray:
10395
return bytearray([LiteFamily.SINGLE_PACKET_ID, 0x10, 0x00, self.MAGIC_NUMBER, 0xa1] +

0 commit comments

Comments
 (0)