mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-11-22 05:53:33 +00:00
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.
This commit is contained in:
@@ -539,8 +539,3 @@ class IotBulb(IotDevice):
|
|||||||
return await self._query_helper(
|
return await self._query_helper(
|
||||||
"smartlife.iot.common.system", "set_dev_alias", {"alias": alias}
|
"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
|
|
||||||
|
|||||||
@@ -735,6 +735,8 @@ class IotDevice(Device):
|
|||||||
# and have even less room right after a state change.
|
# and have even less room right after a state change.
|
||||||
if self.config.connection_type.encryption_type is DeviceEncryptionType.Klap:
|
if self.config.connection_type.encryption_type is DeviceEncryptionType.Klap:
|
||||||
return 2048
|
return 2048
|
||||||
|
if self.device_type in {DeviceType.Bulb, DeviceType.LightStrip}:
|
||||||
|
return 4096
|
||||||
return 16 * 1024
|
return 16 * 1024
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ from kasa.exceptions import (
|
|||||||
_RetryableError,
|
_RetryableError,
|
||||||
)
|
)
|
||||||
from kasa.httpclient import HttpClient
|
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.protocols import IotProtocol, SmartProtocol
|
||||||
from kasa.transports.aestransport import AesTransport
|
from kasa.transports.aestransport import AesTransport
|
||||||
from kasa.transports.klaptransport import (
|
from kasa.transports.klaptransport import (
|
||||||
@@ -50,6 +50,7 @@ KLAP_CONNECTION = DeviceConnectionParameters(
|
|||||||
)
|
)
|
||||||
|
|
||||||
ks200m_iot = parametrize("ks200m", model_filter={"KS200M"}, protocol_filter={"IOT"})
|
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
|
# Transport tests are not designed for real devices
|
||||||
pytestmark = [pytest.mark.requires_dummy]
|
pytestmark = [pytest.mark.requires_dummy]
|
||||||
@@ -756,23 +757,28 @@ async def test_port_override() -> None:
|
|||||||
async def test_max_device_response_size_klap(
|
async def test_max_device_response_size_klap(
|
||||||
dev: IotDevice, mocker: MockerFixture
|
dev: IotDevice, mocker: MockerFixture
|
||||||
) -> None:
|
) -> 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)
|
is_bulb = isinstance(dev, IotBulb)
|
||||||
assert dev.max_device_response_size == (4096 if is_bulb else 16 * 1024)
|
assert dev.max_device_response_size == (4096 if is_bulb else 16 * 1024)
|
||||||
|
|
||||||
mocker.patch.object(dev.config, "connection_type", KLAP_CONNECTION)
|
mocker.patch.object(dev.config, "connection_type", KLAP_CONNECTION)
|
||||||
expected = 4096 if is_bulb else 2048
|
assert dev.max_device_response_size == 2048
|
||||||
assert dev.max_device_response_size == expected
|
|
||||||
for child in dev.children:
|
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."""
|
"""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)
|
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
|
@ks200m_iot
|
||||||
@@ -793,3 +799,26 @@ async def test_ks200m_update_split_klap(
|
|||||||
["schedule"],
|
["schedule"],
|
||||||
["anti_theft", "cnCloud", "smartlife.iot.LAS", "smartlife.iot.PIR", "time"],
|
["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
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user