diff --git a/kasa/protocols/smartprotocol.py b/kasa/protocols/smartprotocol.py index 96f88185..d9543ec4 100644 --- a/kasa/protocols/smartprotocol.py +++ b/kasa/protocols/smartprotocol.py @@ -224,18 +224,9 @@ class SmartProtocol(BaseProtocol): # Break the requests down as there can be a size limit step = self._multi_request_batch_size if step == 1: - # If step is 1 do not send request batches - for request in multi_requests: - method = request["method"] - req = self.get_smart_request(method, request.get("params")) - resp = await self._transport.send(req) - self._handle_response_error_code( - resp, method, raise_on_error=raise_on_error - ) - multi_result[method] = resp["result"] - return multi_result - - for batch_num, i in enumerate(range(0, end, step)): + # Batching is off, so the loop at the end sends each request on its own + multi_requests = [] + for batch_num, i in enumerate(range(0, len(multi_requests), step)): requests_step = multi_requests[i : i + step] smart_params = {"requests": requests_step} @@ -254,8 +245,7 @@ class SmartProtocol(BaseProtocol): if self.REQUIRES_MULTIREQUESTS: raise # P300 does not respond to some batched requests (e.g. both child - # list requests together) so disable batching. Batch size is never - # 1 here as that case sends single requests above. + # list requests together) so disable batching. self._multi_request_batch_size = 1 raise _RetryableError( "Timeout during multi request, multi requests disabled" @@ -328,7 +318,12 @@ class SmartProtocol(BaseProtocol): self._handle_response_error_code( resp, method, raise_on_error=raise_on_error ) - multi_result[method] = resp.get("result") + result = resp.get("result") + if iterate_list_pages and result: + await self._handle_response_lists( + result, method, params, retry_count=retry_count + ) + multi_result[method] = result return multi_result async def _execute_query( diff --git a/tests/protocols/test_smartprotocol.py b/tests/protocols/test_smartprotocol.py index 42012481..f6940a21 100644 --- a/tests/protocols/test_smartprotocol.py +++ b/tests/protocols/test_smartprotocol.py @@ -494,8 +494,9 @@ async def test_smart_protocol_lists_single_request( @pytest.mark.parametrize("list_sum", [5, 10, 30]) @pytest.mark.parametrize("batch_size", [1, 2, 3, 50]) +@pytest.mark.parametrize("multi_request_batch_size", [5, 1]) async def test_smart_protocol_lists_multiple_request( - mocker: MockerFixture, list_sum: int, batch_size: int + mocker: MockerFixture, list_sum: int, batch_size: int, multi_request_batch_size: int ) -> None: child_list = [{"foo": i} for i in range(list_sum)] response = { @@ -520,6 +521,7 @@ async def test_smart_protocol_lists_multiple_request( get_child_fixtures=False, ) protocol = SmartProtocol(transport=ft) + protocol._multi_request_batch_size = multi_request_batch_size query_spy = mocker.spy(protocol, "_execute_query") resp = await protocol.query(request) expected_count = 1 + 2 * ( @@ -728,3 +730,39 @@ async def test_no_multiple_methods( send_spy = mocker.spy(protocol._transport, "send") await protocol.query(req) assert send_spy.call_count == 2 + + +async def test_smart_protocol_lists_requeried_after_batch_error() -> None: + """Test that a list requeried after an error in its batch is complete.""" + child_list = { + "child_device_list": [{"foo": i} for i in range(30)], + "start_index": 0, + "sum": 30, + } + ft = FakeSmartTransport( + {"get_child_device_list": child_list}, + "foobar", + component_nego_not_included=True, + get_child_fixtures=False, + warn_fixture_missing_methods=False, + ) + protocol = SmartProtocol(transport=ft) + resp = await protocol.query({"get_foo": None, "get_child_device_list": None}) + assert resp["get_child_device_list"] == child_list + + +async def test_force_single_method_leaves_no_empty_batch( + dummy_protocol: SmartProtocol, mocker: MockerFixture +) -> None: + """Test that a method sent on its own does not add an empty batch.""" + dummy_protocol._multi_request_batch_size = 5 + methods = [f"get_method_{i}" for i in range(5)] + responses = [{"method": m, "result": {}, "error_code": 0} for m in methods] + send_mock = mocker.patch.object( + dummy_protocol._transport, + "send", + return_value={"result": {"responses": responses}, "error_code": 0}, + ) + + await dummy_protocol.query(dict.fromkeys([*methods, "scanApList"]), retry_count=0) + assert send_mock.call_count == 2