8000 Handle missing mgt_encryption_schm in discovery (#1318) · python-kasa/python-kasa@5ef8f21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ef8f21

Browse files
authored
Handle missing mgt_encryption_schm in discovery (#1318)
1 parent fcb604e commit 5ef8f21

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

kasa/cli/discover.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ def _conditional_echo(label, value):
230230
_conditional_echo("Supports IOT Cloud", dr.is_support_iot_cloud)
231231
_conditional_echo("OBD Src", dr.owner)
232232
_conditional_echo("Factory Default", dr.factory_default)
233-
_conditional_echo("Encrypt Type", dr.mgt_encrypt_schm.encrypt_type)
234233
_conditional_echo("Encrypt Type", dr.encrypt_type)
235-
_conditional_echo("Supports HTTPS", dr.mgt_encrypt_schm.is_support_https)
236-
_conditional_echo("HTTP Port", dr.mgt_encrypt_schm.http_port)
234+
if mgt_encrypt_schm := dr.mgt_encrypt_schm:
235+
_conditional_echo("Encrypt Type", mgt_encrypt_schm.encrypt_type)
236+
_conditional_echo("Supports HTTPS", mgt_encrypt_schm.is_support_https)
237+
_conditional_echo("HTTP Port", mgt_encrypt_schm.http_port)
238+
_conditional_echo("Login version", mgt_encrypt_schm.lv)
237239
_conditional_echo("Encrypt info", pf(dr.encrypt_info) if dr.encrypt_info else None)
238240
_conditional_echo("Decrypted", pf(dr.decrypted_data) if dr.decrypted_data else None)
239241

kasa/discover.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class ConnectAttempt(NamedTuple):
156156
"device_id": lambda x: "REDACTED_" + x[9::],
157157
"owner": lambda x: "REDACTED_" + x[9::],
158158
"mac": mask_mac,
159+
"master_device_id": lambda x: "REDACTED_" + x[9::],
160+
"group_id": lambda x: "REDACTED_" + x[9::],
161+
"group_name": lambda x: "I01BU0tFRF9TU0lEIw==",
159162
}
160163

161164

@@ -643,7 +646,11 @@ def _get_device_class(info: dict) -> type[Device]:
643646
"""Find SmartDevice subclass for device described by passed data."""
644647
if "result" in info:
645648
discovery_result = DiscoveryResult.from_dict(info["result"])
646-
https = discovery_result.mgt_encrypt_schm.is_support_https
649+
https = (
650+
discovery_result.mgt_encrypt_schm.is_support_https
651+
if discovery_result.mgt_encrypt_schm
652+
else False
653+
)
647654
dev_class = get_device_class_from_family(
648655
discovery_result.device_type, https=https
649656
)
@@ -747,7 +754,13 @@ def _get_device_instance(
747754
)
748755

749756
type_ = discovery_result.device_type
750-
encrypt_schm = discovery_result.mgt_encrypt_schm
757+
if (encrypt_schm := discovery_result.mgt_encrypt_schm) is None:
758+
raise UnsupportedDeviceError(
759+
f"Unsupported device {config.host} of type {type_} "
760+
"with no mgt_encrypt_schm",
761+
discovery_result=discovery_result.to_dict(),
762+
host=config.host,
763+
)
751764

752765
try:
753766
if not (encrypt_type := encrypt_schm.encrypt_type) and (
@@ -765,13 +778,13 @@ def _get_device_instance(
765778
config.connection_type = DeviceConnectionParameters.from_values(
766779
type_,
767780
encrypt_type,
768-
discovery_result.mgt_encrypt_schm.lv,
769-
discovery_result.mgt_encrypt_schm.is_support_https,
781+
encrypt_schm.lv,
782+
encrypt_schm.is_support_https,
770783
)
771784
except KasaException as ex:
772785
raise UnsupportedDeviceError(
773786
f"Unsupported device {config.host} of type {type_} "
774-
+ f"with encrypt_type {discovery_result.mgt_encrypt_schm.encrypt_type}",
787+
+ f"with encrypt_type {encrypt_schm.encrypt_type}",
775788
discovery_result=discovery_result.to_dict(),
776789
host=config.host,
777790
) from< 8000 /span> ex
@@ -854,7 +867,7 @@ class DiscoveryResult(_DiscoveryBaseMixin):
854867
device_id: str
855868
ip: str
856869
mac: str
857-
mgt_encrypt_schm: EncryptionScheme
870+
mgt_encrypt_schm: EncryptionScheme | None = None
858871
device_name: str | None = None
859872
encrypt_info: EncryptionInfo | None = None
860873
encrypt_type: list[str] | None = None

tests/discovery_fixtures.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ class DiscoveryResponse(TypedDict):
2222
error_code: int
2323

2424

25+
UNSUPPORTED_HOMEWIFISYSTEM = {
26+
"error_code": 0,
27+
"result": {
28+
"channel_2g": "10",
29+
"channel_5g": "44",
30+
"device_id": "REDACTED_51f72a752213a6c45203530",
31+
"device_model": "X20",
32+
"device_type": "HOMEWIFISYSTEM",
33+
"factory_default": False,
34+
"group_id": "REDACTED_07d902da02fa9beab8a64",
35+
"group_name": "I01BU0tFRF9TU0lEIw==", # '#MASKED_SSID#'
36+
"hardware_version": "3.0",
37+
"ip": "192.168.1.192",
38+
"mac": "24:2F:D0:00:00:00",
39+
"master_device_id": "REDACTED_51f72a752213a6c45203530",
40+
"need_account_digest": True,
41+
"owner": "REDACTED_341c020d7e8bda184e56a90",
42+
"role": "master",
43+
"tmp_port": [20001],
44+
},
45+
}
46+
47+
2548
def _make_unsupported(
2649
device_family,
2750
encrypt_type,
@@ -75,13 +98,14 @@ def _make_unsupported(
7598
"unable_to_parse": _make_unsupported(
7699
"SMART.TAPOBULB",
77100
"FOO",
78-
omit_keys={"mgt_encrypt_schm": None},
101+
omit_keys={"device_id": None},
79102
),
80103
"invalidinstance": _make_unsupported(
81104
"IOT.SMARTPLUGSWITCH",
82105
"KLAP",
83106
https=True,
84107
),
108+
"homewifi": UNSUPPORTED_HOMEWIFISYSTEM,
85109
}
86110

87111

0 commit comments

Comments
 (0)
0