mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Split queries to avoid overflowing device buffers (#502)
Several KASA devices seem to have pretty strict buffer size limitations on incoming/outgoing data transfers. Testing on KL125-US and HL103 has shown that sending a request size larger than about ~768 bytes will immediately crash the device. Additionally, a query that generates a response larger than ~4096 bytes will crash the KL125-US. I was unable to generate such a large response to test the HL103. The KL125-US will only return such large queries when its monthly usage stats have been populated. This means that a new bulb would work fine, but after a month of data collection the bulb would break the 4K limit and start to crash. To work around this issue, an estimated worst-case response size is calculated before sending a request by summing up all modules estimated response size. If the estimated size is greater than the device's max_response_payload_size then the query will be split into multiple queries. This PR implements splitting queries expected to have large responses and also removes the module 'skip list' which was a previous workaround to the crash (which worked by simply reducing the number of modules queried, which prevented the overflow) since it is no longer necessary. This PR does not attempt to address the "input buffer size limit." Thus far this limit has not been an issue.
This commit is contained in:
@@ -39,7 +39,9 @@ async def test_initial_update_emeter(dev, mocker):
|
||||
dev._last_update = None
|
||||
spy = mocker.spy(dev.protocol, "query")
|
||||
await dev.update()
|
||||
assert spy.call_count == 2 + len(dev.children)
|
||||
# Devices with small buffers may require 3 queries
|
||||
expected_queries = 2 if dev.max_device_response_size > 4096 else 3
|
||||
assert spy.call_count == expected_queries + len(dev.children)
|
||||
|
||||
|
||||
@no_emeter
|
||||
@@ -164,6 +166,17 @@ async def test_features(dev):
|
||||
assert dev.features == set()
|
||||
|
||||
|
||||
async def test_max_device_response_size(dev):
|
||||
"""Make sure every device return has a set max response size."""
|
||||
assert dev.max_device_response_size > 0
|
||||
|
||||
|
||||
async def test_estimated_response_sizes(dev):
|
||||
"""Make sure every module has an estimated response size set."""
|
||||
for mod in dev.modules.values():
|
||||
assert mod.estimated_query_response_size > 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_class", smart_device_classes)
|
||||
def test_device_class_ctors(device_class):
|
||||
"""Make sure constructor api not broken for new and existing SmartDevices."""
|
||||
|
Reference in New Issue
Block a user