Skip to content
Open
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
20 changes: 17 additions & 3 deletions kasa/device_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ def _perf_log(has_params: bool, perf_type: str) -> None:
device_class: type[Device] | None
device: Device | None = None

if isinstance(protocol, IotProtocol) and isinstance(
protocol._transport, XorTransport
):
if isinstance(protocol, IotProtocol) and config.connection_type.device_family in {
DeviceFamily.IotSmartPlugSwitch,
DeviceFamily.IotSmartBulb,
}:
info = await protocol.query(GET_SYSINFO_QUERY)
_perf_log(True, "get_sysinfo")
device_class = get_device_class_from_sys_info(info)
Expand Down Expand Up @@ -216,6 +217,19 @@ def get_protocol(config: DeviceConfig, *, strict: bool = False) -> BaseProtocol
):
return SmartProtocol(transport=SslTransport(config=config))

# Some IOT devices advertise KLAP with login_version >= 2 and require
# KlapTransportV2 despite using an IOT device family.
if (
ctype.device_family
in {
DeviceFamily.IotSmartPlugSwitch,
DeviceFamily.IotSmartBulb,
}
and ctype.encryption_type is DeviceEncryptionType.Klap
and (ctype.login_version or 1) >= 2
):
return IotProtocol(transport=KlapTransportV2(config=config))

protocol_transport_key = (
protocol_name
+ "."
Expand Down
45 changes: 45 additions & 0 deletions tests/test_device_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
DeviceFamily,
)
from kasa.discover import DiscoveryResult
from kasa.iot import IotPlug, IotStrip
from kasa.transports import (
AesTransport,
BaseTransport,
Expand Down Expand Up @@ -259,6 +260,12 @@ async def test_device_class_from_unknown_family(caplog):
KlapTransport,
id="iot-klap",
),
pytest.param(
CP(DF.IotSmartPlugSwitch, ET.Klap, login_version=2, https=False),
IotProtocol,
KlapTransportV2,
id="iot-klap-lv2",
),
pytest.param(
CP(DF.IotSmartPlugSwitch, ET.Xor, https=False),
IotProtocol,
Expand Down Expand Up @@ -295,3 +302,41 @@ async def test_get_protocol(
protocol = get_protocol(config)
assert isinstance(protocol, expected_protocol)
assert isinstance(protocol._transport, expected_transport)


async def test_connect_iot_klap_uses_sysinfo_for_device_class(mocker):
"""IOT KLAP devices should derive class from sysinfo (e.g. strip vs plug)."""
host = DISCOVERY_MOCK_IP
config = DeviceConfig(
host=host,
credentials=Credentials("user@example.com", "password"),
connection_type=CP(
DF.IotSmartPlugSwitch,
ET.Klap,
login_version=2,
https=False,
http_port=80,
),
)
query_mock = mocker.patch.object(
IotProtocol,
"query",
return_value={
"system": {
"get_sysinfo": {
"type": "IOT.SMARTPLUGSWITCH",
"model": "HS300(US)",
"children": [],
}
}
},
)
strip_update = mocker.patch.object(IotStrip, "update")
plug_update = mocker.patch.object(IotPlug, "update")

dev = await connect(config=config)

assert isinstance(dev, IotStrip)
assert query_mock.await_count == 1
assert strip_update.await_count == 1
assert plug_update.await_count == 0
Loading