From 1b4e540ae6f293980e2c883ee65df4833cd034a6 Mon Sep 17 00:00:00 2001 From: ZeliardM <140266236+ZeliardM@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:20:54 -0500 Subject: [PATCH] Apply the KLAP response budget to IoT bulbs (#1852) This centralizes the response budgets in `IotDevice.max_device_response_size` and removes the bulb override: - KLAP: 2048 for every IoT device. - Other transports: 4096 for bulbs and light strips, 16 KiB for other IoT devices. --- kasa/iot/iotbulb.py | 5 --- kasa/iot/iotdevice.py | 2 ++ tests/transports/test_klaptransport.py | 45 +++++++++++++++++++++----- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/kasa/iot/iotbulb.py b/kasa/iot/iotbulb.py index 8be72d26..0822cdf5 100644 --- a/kasa/iot/iotbulb.py +++ b/kasa/iot/iotbulb.py @@ -539,8 +539,3 @@ class IotBulb(IotDevice): return await self._query_helper( "smartlife.iot.common.system", "set_dev_alias", {"alias": alias} ) - - @property - def max_device_response_size(self) -> int: - """Returns the maximum response size the device can safely construct.""" - return 4096 diff --git a/kasa/iot/iotdevice.py b/kasa/iot/iotdevice.py index ba3f0a6b..657d4a70 100755 --- a/kasa/iot/iotdevice.py +++ b/kasa/iot/iotdevice.py @@ -735,6 +735,8 @@ class IotDevice(Device): # and have even less room right after a state change. if self.config.connection_type.encryption_type is DeviceEncryptionType.Klap: return 2048 + if self.device_type in {DeviceType.Bulb, DeviceType.LightStrip}: + return 4096 return 16 * 1024 @property diff --git a/tests/transports/test_klaptransport.py b/tests/transports/test_klaptransport.py index 462a7d8d..e5648c8b 100644 --- a/tests/transports/test_klaptransport.py +++ b/tests/transports/test_klaptransport.py @@ -30,7 +30,7 @@ from kasa.exceptions import ( _RetryableError, ) from kasa.httpclient import HttpClient -from kasa.iot import IotBulb, IotDevice, IotPlug, IotWallSwitch +from kasa.iot import IotBulb, IotDevice, IotLightStrip, IotPlug, IotWallSwitch from kasa.protocols import IotProtocol, SmartProtocol from kasa.transports.aestransport import AesTransport from kasa.transports.klaptransport import ( @@ -50,6 +50,7 @@ KLAP_CONNECTION = DeviceConnectionParameters( ) ks200m_iot = parametrize("ks200m", model_filter={"KS200M"}, protocol_filter={"IOT"}) +kl125_iot = parametrize("kl125", model_filter={"KL125"}, protocol_filter={"IOT"}) # Transport tests are not designed for real devices pytestmark = [pytest.mark.requires_dummy] @@ -756,23 +757,28 @@ async def test_port_override() -> None: async def test_max_device_response_size_klap( dev: IotDevice, mocker: MockerFixture ) -> None: - """Test that devices other than bulbs use a smaller budget over KLAP.""" + """Test that all IoT devices use a smaller budget over KLAP.""" is_bulb = isinstance(dev, IotBulb) assert dev.max_device_response_size == (4096 if is_bulb else 16 * 1024) mocker.patch.object(dev.config, "connection_type", KLAP_CONNECTION) - expected = 4096 if is_bulb else 2048 - assert dev.max_device_response_size == expected + assert dev.max_device_response_size == 2048 for child in dev.children: - assert child.max_device_response_size == expected + assert child.max_device_response_size == 2048 -def test_max_device_response_size_before_update() -> None: +@pytest.mark.parametrize( + ("device_class", "xor_budget"), + [(IotPlug, 16 * 1024), (IotBulb, 4096), (IotLightStrip, 4096)], +) +def test_max_device_response_size_before_update( + device_class: type[IotDevice], xor_budget: int +) -> None: """Test that the budget follows the connection settings before an update.""" - assert IotPlug("127.0.0.1").max_device_response_size == 16 * 1024 + assert device_class("127.0.0.1").max_device_response_size == xor_budget config = DeviceConfig("127.0.0.1", connection_type=KLAP_CONNECTION) - assert IotPlug("127.0.0.1", config=config).max_device_response_size == 2048 + assert device_class("127.0.0.1", config=config).max_device_response_size == 2048 @ks200m_iot @@ -793,3 +799,26 @@ async def test_ks200m_update_split_klap( ["schedule"], ["anti_theft", "cnCloud", "smartlife.iot.LAS", "smartlife.iot.PIR", "time"], ] + + +@kl125_iot +async def test_kl125_update_split_klap(dev: IotBulb, mocker: MockerFixture) -> None: + """Keep sysinfo and energy queries separate on a KL125 over KLAP.""" + spy = mocker.spy(dev.protocol, "query") + await dev.update() + assert any( + "system" in call.args[0] and dev.emeter_type in call.args[0] + for call in spy.call_args_list + ) + + mocker.patch.object(dev.config, "connection_type", KLAP_CONNECTION) + spy.reset_mock() + await dev.update() + + requests = [call.args[0] for call in spy.call_args_list] + assert any("system" in request for request in requests) + assert any(dev.emeter_type in request for request in requests) + assert all( + "system" not in request or dev.emeter_type not in request + for request in requests + )