-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathtest_devtools.py
More file actions
159 lines (130 loc) Β· 5.48 KB
/
Copy pathtest_devtools.py
File metadata and controls
159 lines (130 loc) Β· 5.48 KB
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
"""Module for dump_devinfo tests."""
import copy
import pytest
from devtools.dump_devinfo import (
_wrap_redactors,
get_legacy_fixture,
get_smart_fixtures,
)
from kasa.iot import IotDevice
from kasa.protocols import IotProtocol
from kasa.protocols.protocol import redact_data
from kasa.protocols.smartprotocol import REDACTORS as SMART_REDACTORS
from kasa.smart import SmartDevice
from kasa.smartcam import SmartCamDevice
from kasa.smartcam.smartcamchild import CHILD_INFO_FROM_PARENT
from .conftest import (
FixtureInfo,
get_device_for_fixture,
get_fixture_info,
parametrize,
)
smart_fixtures = parametrize(
"smart fixtures", protocol_filter={"SMART"}, fixture_name="fixture_info"
)
smartcam_fixtures = parametrize(
"smartcam fixtures", protocol_filter={"SMARTCAM"}, fixture_name="fixture_info"
)
iot_fixtures = parametrize(
"iot fixtures", protocol_filter={"IOT"}, fixture_name="fixture_info"
)
async def test_fixture_names(fixture_info: FixtureInfo):
"""Test that device info gets the right fixture names."""
if fixture_info.protocol in {"SMARTCAM"}:
device_info = SmartCamDevice._get_device_info(
fixture_info.data,
fixture_info.data.get("discovery_result", {}).get("result"),
)
elif fixture_info.protocol in {"SMART"}:
device_info = SmartDevice._get_device_info(
fixture_info.data,
fixture_info.data.get("discovery_result", {}).get("result"),
)
elif fixture_info.protocol in {"SMART.CHILD"}:
device_info = SmartDevice._get_device_info(fixture_info.data, None)
else:
device_info = IotDevice._get_device_info(fixture_info.data, None)
region = f"({device_info.region})" if device_info.region else ""
expected = f"{device_info.long_name}{region}_{device_info.hardware_version}_{device_info.firmware_version}.json"
assert fixture_info.name == expected
@smart_fixtures
async def test_smart_fixtures(fixture_info: FixtureInfo):
"""Test that smart fixtures are created the same."""
dev = await get_device_for_fixture(fixture_info, verbatim=True)
assert isinstance(dev, SmartDevice)
if dev.children:
pytest.skip("Test not currently implemented for devices with children.")
fixtures = await get_smart_fixtures(
dev.protocol,
discovery_info=fixture_info.data.get("discovery_result"),
batch_size=5,
)
fixture_result = fixtures[0]
assert fixture_info.data == fixture_result.data
def _normalize_child_device_ids(info: dict):
"""Scrubbed child device ids in hubs may not match ids in child fixtures.
Different hub fixtures could create the same child fixture so we scrub
them again for the purpose of the test.
"""
if dev_info := info.get("get_device_info"):
dev_info["device_id"] = "SCRUBBED"
elif (
dev_info := info.get("getDeviceInfo", {})
.get("device_info", {})
.get("basic_info")
):
dev_info["dev_id"] = "SCRUBBED"
@smartcam_fixtures
async def test_smartcam_fixtures(fixture_info: FixtureInfo):
"""Test that smartcam fixtures are created the same."""
dev = await get_device_for_fixture(fixture_info, verbatim=True)
assert isinstance(dev, SmartCamDevice)
created_fixtures = await get_smart_fixtures(
dev.protocol,
discovery_info=fixture_info.data.get("discovery_result"),
batch_size=5,
)
fixture_result = created_fixtures.pop(0)
assert fixture_info.data == fixture_result.data
for created_child_fixture in created_fixtures:
child_fixture_info = get_fixture_info(
created_child_fixture.filename + ".json",
created_child_fixture.protocol_suffix,
)
assert child_fixture_info
_normalize_child_device_ids(created_child_fixture.data)
saved_fixture_data = copy.deepcopy(child_fixture_info.data)
_normalize_child_device_ids(saved_fixture_data)
saved_fixture_data = {
key: val for key, val in saved_fixture_data.items() if val != -1001
}
# Remove the child info from parent from the comparison because the
# child may have been created by a different parent fixture
saved_fixture_data.pop(CHILD_INFO_FROM_PARENT, None)
created_cifp = created_child_fixture.data.pop(CHILD_INFO_FROM_PARENT, None)
# Still check that the created child info from parent was redacted.
# only smartcam children generate child_info_from_parent
if created_cifp:
redacted_cifp = redact_data(created_cifp, _wrap_redactors(SMART_REDACTORS))
assert created_cifp == redacted_cifp
assert saved_fixture_data == created_child_fixture.data
@iot_fixtures
async def test_iot_fixtures(fixture_info: FixtureInfo):
"""Test that iot fixtures are created the same."""
# Iot fixtures often do not have enough data to perform a device update()
# without missing info being added to suppress the update
dev = await get_device_for_fixture(
fixture_info, verbatim=True, update_after_init=False
)
assert isinstance(dev.protocol, IotProtocol)
fixture = await get_legacy_fixture(
dev.protocol, discovery_info=fixture_info.data.get("discovery_result")
)
fixture_result = fixture
created_fixture = {
key: val for key, val in fixture_result.data.items() if "err_code" not in val
}
saved_fixture = {
key: val for key, val in fixture_info.data.items() if "err_code" not in val
}
assert saved_fixture == created_fixture