Support multiple child requests (#795)

This commit is contained in:
Steven B 2024-02-26 16:13:46 +00:00 committed by GitHub
parent cbf82c9498
commit d82747d73f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -340,10 +340,16 @@ class _ChildProtocolWrapper(SmartProtocol):
result = response.get("control_child") result = response.get("control_child")
# Unwrap responseData for control_child # Unwrap responseData for control_child
if result and (response_data := result.get("responseData")): if result and (response_data := result.get("responseData")):
self._handle_response_error_code(response_data, "control_child")
result = response_data.get("result") result = response_data.get("result")
if result and (multi_responses := result.get("responses")):
ret_val = {}
for multi_response in multi_responses:
method = multi_response["method"]
self._handle_response_error_code(multi_response, method)
ret_val[method] = multi_response.get("result")
return ret_val
# TODO: handle multipleRequest unwrapping self._handle_response_error_code(response_data, "control_child")
return {method: result} return {method: result}

View File

@ -122,7 +122,6 @@ async def test_childdevicewrapper_error(dummy_protocol, mocker):
await wrapped_protocol.query(DUMMY_QUERY) await wrapped_protocol.query(DUMMY_QUERY)
@pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker): async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker):
"""Test that unwrapping multiplerequest works correctly.""" """Test that unwrapping multiplerequest works correctly."""
mock_response = { mock_response = {
@ -146,13 +145,12 @@ async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, moc
} }
}, },
} }
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response) mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
resp = await dummy_protocol.query(DUMMY_QUERY) resp = await wrapped_protocol.query(DUMMY_QUERY)
assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}} assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}}
@pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker): async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
"""Test that errors inside multipleRequest response of responseData raise an exception.""" """Test that errors inside multipleRequest response of responseData raise an exception."""
mock_response = { mock_response = {
@ -172,7 +170,7 @@ async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
} }
}, },
} }
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response) mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
with pytest.raises(KasaException): with pytest.raises(KasaException):
await dummy_protocol.query(DUMMY_QUERY) await wrapped_protocol.query(DUMMY_QUERY)