2023-12-20 17:08:04 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ..credentials import Credentials
|
2023-12-29 19:17:15 +00:00
|
|
|
from ..deviceconfig import DeviceConfig
|
2023-12-20 17:08:04 +00:00
|
|
|
from ..exceptions import (
|
|
|
|
SMART_RETRYABLE_ERRORS,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2023-12-20 17:08:04 +00:00
|
|
|
SmartErrorCode,
|
|
|
|
)
|
2024-01-29 23:15:58 +00:00
|
|
|
from ..smartprotocol import _ChildProtocolWrapper
|
2023-12-20 17:08:04 +00:00
|
|
|
|
|
|
|
DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
|
2024-01-29 10:55:54 +00:00
|
|
|
DUMMY_MULTIPLE_QUERY = {
|
|
|
|
"foobar": {"foo": "bar", "bar": "foo"},
|
|
|
|
"barfoo": {"foo": "bar", "bar": "foo"},
|
|
|
|
}
|
2023-12-20 17:08:04 +00:00
|
|
|
ERRORS = [e for e in SmartErrorCode if e != 0]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
2024-01-29 16:11:29 +00:00
|
|
|
async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
2023-12-20 17:08:04 +00:00
|
|
|
mock_response = {"result": {"great": "success"}, "error_code": error_code.value}
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
send_mock = mocker.patch.object(
|
|
|
|
dummy_protocol._transport, "send", return_value=mock_response
|
|
|
|
)
|
2023-12-20 17:08:04 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-01-29 16:11:29 +00:00
|
|
|
await dummy_protocol.query(DUMMY_QUERY, retry_count=2)
|
2023-12-20 17:08:04 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
expected_calls = 3 if error_code in SMART_RETRYABLE_ERRORS else 1
|
2023-12-20 17:08:04 +00:00
|
|
|
assert send_mock.call_count == expected_calls
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
2024-01-29 16:11:29 +00:00
|
|
|
async def test_smart_device_errors_in_multiple_request(
|
|
|
|
dummy_protocol, mocker, error_code
|
|
|
|
):
|
2023-12-20 17:08:04 +00:00
|
|
|
mock_response = {
|
|
|
|
"result": {
|
|
|
|
"responses": [
|
|
|
|
{"method": "foobar1", "result": {"great": "success"}, "error_code": 0},
|
|
|
|
{
|
|
|
|
"method": "foobar2",
|
|
|
|
"result": {"great": "success"},
|
|
|
|
"error_code": error_code.value,
|
|
|
|
},
|
|
|
|
{"method": "foobar3", "result": {"great": "success"}, "error_code": 0},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
send_mock = mocker.patch.object(
|
|
|
|
dummy_protocol._transport, "send", return_value=mock_response
|
|
|
|
)
|
2024-02-15 18:10:34 +00:00
|
|
|
|
|
|
|
resp_dict = await dummy_protocol.query(DUMMY_MULTIPLE_QUERY, retry_count=2)
|
|
|
|
assert resp_dict["foobar2"] == error_code
|
|
|
|
assert send_mock.call_count == 1
|
2024-01-29 10:55:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("request_size", [1, 3, 5, 10])
|
|
|
|
@pytest.mark.parametrize("batch_size", [1, 2, 3, 4, 5])
|
2024-01-29 16:11:29 +00:00
|
|
|
async def test_smart_device_multiple_request(
|
|
|
|
dummy_protocol, mocker, request_size, batch_size
|
|
|
|
):
|
2024-01-29 10:55:54 +00:00
|
|
|
host = "127.0.0.1"
|
|
|
|
requests = {}
|
|
|
|
mock_response = {
|
|
|
|
"result": {"responses": []},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
for i in range(request_size):
|
|
|
|
method = f"get_method_{i}"
|
|
|
|
requests[method] = {"foo": "bar", "bar": "foo"}
|
|
|
|
mock_response["result"]["responses"].append(
|
|
|
|
{"method": method, "result": {"great": "success"}, "error_code": 0}
|
|
|
|
)
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
send_mock = mocker.patch.object(
|
|
|
|
dummy_protocol._transport, "send", return_value=mock_response
|
|
|
|
)
|
2024-01-29 10:55:54 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host, credentials=Credentials("foo", "bar"), batch_size=batch_size
|
|
|
|
)
|
2024-01-29 16:11:29 +00:00
|
|
|
dummy_protocol._transport._config = config
|
2024-01-29 10:55:54 +00:00
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
await dummy_protocol.query(requests, retry_count=0)
|
2024-01-29 10:55:54 +00:00
|
|
|
expected_count = int(request_size / batch_size) + (request_size % batch_size > 0)
|
|
|
|
assert send_mock.call_count == expected_count
|
2024-01-29 16:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_childdevicewrapper_unwrapping(dummy_protocol, mocker):
|
|
|
|
"""Test that responseData gets unwrapped correctly."""
|
|
|
|
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
|
|
mock_response = {"error_code": 0, "result": {"responseData": {"error_code": 0}}}
|
|
|
|
|
|
|
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
|
|
res = await wrapped_protocol.query(DUMMY_QUERY)
|
|
|
|
assert res == {"foobar": None}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_childdevicewrapper_unwrapping_with_payload(dummy_protocol, mocker):
|
|
|
|
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
|
|
mock_response = {
|
|
|
|
"error_code": 0,
|
|
|
|
"result": {"responseData": {"error_code": 0, "result": {"bar": "bar"}}},
|
|
|
|
}
|
|
|
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
|
|
res = await wrapped_protocol.query(DUMMY_QUERY)
|
|
|
|
assert res == {"foobar": {"bar": "bar"}}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_childdevicewrapper_error(dummy_protocol, mocker):
|
|
|
|
"""Test that errors inside the responseData payload cause an exception."""
|
|
|
|
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
|
|
mock_response = {"error_code": 0, "result": {"responseData": {"error_code": -1001}}}
|
|
|
|
|
|
|
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-01-29 16:11:29 +00:00
|
|
|
await wrapped_protocol.query(DUMMY_QUERY)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker):
|
|
|
|
"""Test that unwrapping multiplerequest works correctly."""
|
|
|
|
mock_response = {
|
|
|
|
"error_code": 0,
|
|
|
|
"result": {
|
|
|
|
"responseData": {
|
|
|
|
"result": {
|
|
|
|
"responses": [
|
|
|
|
{
|
|
|
|
"error_code": 0,
|
|
|
|
"method": "get_device_info",
|
|
|
|
"result": {"foo": "bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error_code": 0,
|
|
|
|
"method": "second_command",
|
|
|
|
"result": {"bar": "foo"},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2024-02-26 16:13:46 +00:00
|
|
|
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
|
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
|
|
resp = await wrapped_protocol.query(DUMMY_QUERY)
|
2024-01-29 16:11:29 +00:00
|
|
|
assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
|
|
|
|
"""Test that errors inside multipleRequest response of responseData raise an exception."""
|
|
|
|
mock_response = {
|
|
|
|
"error_code": 0,
|
|
|
|
"result": {
|
|
|
|
"responseData": {
|
|
|
|
"result": {
|
|
|
|
"responses": [
|
|
|
|
{
|
|
|
|
"error_code": 0,
|
|
|
|
"method": "get_device_info",
|
|
|
|
"result": {"foo": "bar"},
|
|
|
|
},
|
|
|
|
{"error_code": -1001, "method": "invalid_command"},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2024-02-26 16:13:46 +00:00
|
|
|
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
|
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-02-26 16:13:46 +00:00
|
|
|
await wrapped_protocol.query(DUMMY_QUERY)
|