From a4fe376c54ced19f95d24f7e31b1f4158469ed7c Mon Sep 17 00:00:00 2001 From: GhostCoder6969 Date: Mon, 12 Oct 2026 23:31:01 +0200 Subject: [PATCH] Raise UnsupportedDeviceError instead of crashing on unknown device types (#1781) Use get() instead of index accessor to avoid crashing on unknown devices. --------- Co-authored-by: GhostCoder6969 --- kasa/device_factory.py | 8 +++++++- tests/test_device_factory.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/kasa/device_factory.py b/kasa/device_factory.py index 6b4b8c96..332ff9bc 100644 --- a/kasa/device_factory.py +++ b/kasa/device_factory.py @@ -142,7 +142,13 @@ def get_device_class_from_sys_info(sysinfo: dict[str, Any]) -> type[IotDevice]: # Disabled until properly implemented # DeviceType.Camera: IotCamera, } - return TYPE_TO_CLASS[IotDevice._get_device_type_from_sys_info(sysinfo)] + device_type = IotDevice._get_device_type_from_sys_info(sysinfo) + if (cls := TYPE_TO_CLASS.get(device_type)) is None: + raise UnsupportedDeviceError( + f"Unsupported device type: {device_type}", + discovery_result=sysinfo, + ) + return cls def get_device_class_from_family( diff --git a/tests/test_device_factory.py b/tests/test_device_factory.py index dceca476..137000d3 100644 --- a/tests/test_device_factory.py +++ b/tests/test_device_factory.py @@ -21,6 +21,7 @@ from kasa import ( KasaException, SmartCamProtocol, SmartProtocol, + UnsupportedDeviceError, ) from kasa.device_factory import ( Device, @@ -31,6 +32,7 @@ from kasa.device_factory import ( SmartDevice, connect, get_device_class_from_family, + get_device_class_from_sys_info, get_protocol, ) from kasa.deviceconfig import ( @@ -212,6 +214,26 @@ async def test_device_types(dev: Device) -> None: assert dev.device_type == res +def test_device_class_from_unsupported_legacy_camera() -> None: + """IOT.IPCAMERA sysinfo raises UnsupportedDeviceError instead of KeyError.""" + info = { + "system": { + "get_sysinfo": { + "system": { + "type": "IOT.IPCAMERA", + "model": "EC60(US)", + "alias": "Kasa Cam", + "dev_name": "Kasa Spot", + } + } + } + } + with pytest.raises(UnsupportedDeviceError, match="Unsupported device type"): + get_device_class_from_sys_info(info) + with pytest.raises(UnsupportedDeviceError, match="Unsupported device type"): + Discover._get_device_class(info) + + @pytest.mark.xdist_group(name="caplog") async def test_device_class_from_unknown_family( caplog: pytest.LogCaptureFixture,