Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Handle missing mgt_encryption_schm in discovery #1318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions kasa/cli/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ def _conditional_echo(label, value):
_conditional_echo("Supports IOT Cloud", dr.is_support_iot_cloud)
_conditional_echo("OBD Src", dr.owner)
_conditional_echo("Factory Default", dr.factory_default)
_conditional_echo("Encrypt Type", dr.mgt_encrypt_schm.encrypt_type)
_conditional_echo("Encrypt Type", dr.encrypt_type)
_conditional_echo("Supports HTTPS", dr.mgt_encrypt_schm.is_support_https)
_conditional_echo("HTTP Port", dr.mgt_encrypt_schm.http_port)
if mgt_encrypt_schm := dr.mgt_encrypt_schm:
_conditional_echo("Encrypt Type", mgt_encrypt_schm.encrypt_type)
_conditional_echo("Supports HTTPS", mgt_encrypt_schm.is_support_https)
_conditional_echo("HTTP Port", mgt_encrypt_schm.http_port)
_conditional_echo("Login version", mgt_encrypt_schm.lv)
_conditional_echo("Encrypt info", pf(dr.encrypt_info) if dr.encrypt_info else None)
_conditional_echo("Decrypted", pf(dr.decrypted_data) if dr.decrypted_data else None)

Expand Down
25 changes: 19 additions & 6 deletions kasa/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class ConnectAttempt(NamedTuple):
"device_id": lambda x: "REDACTED_" + x[9::],
"owner": lambda x: "REDACTED_" + x[9::],
"mac": mask_mac,
"master_device_id": lambda x: "REDACTED_" + x[9::],
"group_id": lambda x: "REDACTED_" + x[9::],
"group_name": lambda x: "I01BU0tFRF9TU0lEIw==",
}


Expand Down Expand Up @@ -643,7 +646,11 @@ def _get_device_class(info: dict) -> type[Device]:
"""Find SmartDevice subclass for device described by passed data."""
if "result" in info:
discovery_result = DiscoveryResult.from_dict(info["result"])
https = discovery_result.mgt_encrypt_schm.is_support_https
https = (
discovery_result.mgt_encrypt_schm.is_support_https
if discovery_result.mgt_encrypt_schm
else False
)
dev_class = get_device_class_from_family(
discovery_result.device_type, https=https
)
Expand Down Expand Up @@ -747,7 +754,13 @@ def _get_device_instance(
)

type_ = discovery_result.device_type
encrypt_schm = discovery_result.mgt_encrypt_schm
if (encrypt_schm := discovery_result.mgt_encrypt_schm) is None:
raise UnsupportedDeviceError(
f"Unsupported device {config.host} of type {type_} "
"with no mgt_encrypt_schm",
discovery_result=discovery_result.to_dict(),
host=config.host,
)

try:
if not (encrypt_type := encrypt_schm.encrypt_type) and (
Expand All @@ -765,13 +778,13 @@ def _get_device_instance(
config.connection_type = DeviceConnectionParameters.from_values(
type_,
encrypt_type,
discovery_result.mgt_encrypt_schm.lv,
discovery_result.mgt_encrypt_schm.is_support_https,
encrypt_schm.lv,
encrypt_schm.is_support_https,
)
except KasaException as ex:
raise UnsupportedDeviceError(
f"Unsupported device {config.host} of type {type_} "
+ f"with encrypt_type {discovery_result.mgt_encrypt_schm.encrypt_type}",
+ f"with encrypt_type {encrypt_schm.encrypt_type}",
discovery_result=discovery_result.to_dict(),
host=config.host,
) from ex
Expand Down Expand Up @@ -854,7 +867,7 @@ class DiscoveryResult(_DiscoveryBaseMixin):
device_id: str
ip: str
mac: str
mgt_encrypt_schm: EncryptionScheme
mgt_encrypt_schm: EncryptionScheme | None = None
device_name: str | None = None
encrypt_info: EncryptionInfo | None = None
encrypt_type: list[str] | None = None
Expand Down
26 changes: 25 additions & 1 deletion tests/discovery_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ class DiscoveryResponse(TypedDict):
error_code: int


UNSUPPORTED_HOMEWIFISYSTEM = {
"error_code": 0,
"result": {
"channel_2g": "10",
"channel_5g": "44",
"device_id": "REDACTED_51f72a752213a6c45203530",
"device_model": "X20",
"device_type": "HOMEWIFISYSTEM",
"factory_default": False,
"group_id": "REDACTED_07d902da02fa9beab8a64",
"group_name": "I01BU0tFRF9TU0lEIw==", # '#MASKED_SSID#'
"hardware_version": "3.0",
"ip": "192.168.1.192",
"mac": "24:2F:D0:00:00:00",
"master_device_id": "REDACTED_51f72a752213a6c45203530",
"need_account_digest": True,
"owner": "REDACTED_341c020d7e8bda184e56a90",
"role": "master",
"tmp_port": [20001],
},
}


def _make_unsupported(
device_family,
encrypt_type,
Expand Down Expand Up @@ -75,13 +98,14 @@ def _make_unsupported(
"unable_to_parse": _make_unsupported(
"SMART.TAPOBULB",
"FOO",
omit_keys={"mgt_encrypt_schm": None},
omit_keys={"device_id": None},
),
"invalidinstance": _make_unsupported(
"IOT.SMARTPLUGSWITCH",
"KLAP",
https=True,
),
"homewifi": UNSUPPORTED_HOMEWIFISYSTEM,
}


Expand Down
Loading