mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Initial support for tapos with child devices (#720)
* Add ChildDevice and ChildProtocolWrapper * Initialize & update children * Fix circular imports * Add dummy_protocol fixture and tests for unwrapping responseData * Use dummy_protocol for existing smartprotocol tests * Move _ChildProtocolWrapper to smartprotocol.py * Use dummy_protocol for test multiple requests * Use device_id instead of position for selecting the child * Fix wrapping for regular requests * Remove unused imports * tweak * rename child_device to childdevice * Fix import
This commit is contained in:
@@ -482,6 +482,9 @@ def _get_subclasses(of_class):
|
||||
"class_name_obj", _get_subclasses(BaseProtocol), ids=lambda t: t[0]
|
||||
)
|
||||
def test_protocol_init_signature(class_name_obj):
|
||||
if class_name_obj[0].startswith("_"):
|
||||
pytest.skip("Skipping internal protocols")
|
||||
return
|
||||
params = list(inspect.signature(class_name_obj[1].__init__).parameters.values())
|
||||
|
||||
assert len(params) == 2
|
||||
|
@@ -1,16 +1,8 @@
|
||||
import errno
|
||||
import json
|
||||
import logging
|
||||
import secrets
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
from contextlib import nullcontext as does_not_raise
|
||||
from itertools import chain
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
|
||||
from ..aestransport import AesTransport
|
||||
from ..credentials import Credentials
|
||||
from ..deviceconfig import DeviceConfig
|
||||
from ..exceptions import (
|
||||
@@ -19,9 +11,8 @@ from ..exceptions import (
|
||||
SmartDeviceException,
|
||||
SmartErrorCode,
|
||||
)
|
||||
from ..iotprotocol import IotProtocol
|
||||
from ..klaptransport import KlapEncryptionSession, KlapTransport, _sha256
|
||||
from ..smartprotocol import SmartProtocol
|
||||
from ..protocol import BaseTransport
|
||||
from ..smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
||||
|
||||
DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
|
||||
DUMMY_MULTIPLE_QUERY = {
|
||||
@@ -31,20 +22,45 @@ DUMMY_MULTIPLE_QUERY = {
|
||||
ERRORS = [e for e in SmartErrorCode if e != 0]
|
||||
|
||||
|
||||
# TODO: this could be moved to conftest to make it available for other tests?
|
||||
@pytest.fixture()
|
||||
def dummy_protocol():
|
||||
"""Return a smart protocol instance with a mocking-ready dummy transport."""
|
||||
|
||||
class DummyTransport(BaseTransport):
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
return -1
|
||||
|
||||
@property
|
||||
def credentials_hash(self) -> str:
|
||||
return "dummy hash"
|
||||
|
||||
async def send(self, request: str) -> Dict:
|
||||
return {}
|
||||
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
async def reset(self) -> None:
|
||||
pass
|
||||
|
||||
transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
|
||||
protocol = SmartProtocol(transport=transport)
|
||||
|
||||
return protocol
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
||||
async def test_smart_device_errors(mocker, error_code):
|
||||
host = "127.0.0.1"
|
||||
async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
||||
mock_response = {"result": {"great": "success"}, "error_code": error_code.value}
|
||||
|
||||
mocker.patch.object(AesTransport, "perform_handshake")
|
||||
mocker.patch.object(AesTransport, "perform_login")
|
||||
send_mock = mocker.patch.object(
|
||||
dummy_protocol._transport, "send", return_value=mock_response
|
||||
)
|
||||
|
||||
send_mock = mocker.patch.object(AesTransport, "send", return_value=mock_response)
|
||||
|
||||
config = DeviceConfig(host, credentials=Credentials("foo", "bar"))
|
||||
protocol = SmartProtocol(transport=AesTransport(config=config))
|
||||
with pytest.raises(SmartDeviceException):
|
||||
await protocol.query(DUMMY_QUERY, retry_count=2)
|
||||
await dummy_protocol.query(DUMMY_QUERY, retry_count=2)
|
||||
|
||||
if error_code in chain(SMART_TIMEOUT_ERRORS, SMART_RETRYABLE_ERRORS):
|
||||
expected_calls = 3
|
||||
@@ -54,8 +70,9 @@ async def test_smart_device_errors(mocker, error_code):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
||||
async def test_smart_device_errors_in_multiple_request(mocker, error_code):
|
||||
host = "127.0.0.1"
|
||||
async def test_smart_device_errors_in_multiple_request(
|
||||
dummy_protocol, mocker, error_code
|
||||
):
|
||||
mock_response = {
|
||||
"result": {
|
||||
"responses": [
|
||||
@@ -71,14 +88,11 @@ async def test_smart_device_errors_in_multiple_request(mocker, error_code):
|
||||
"error_code": 0,
|
||||
}
|
||||
|
||||
mocker.patch.object(AesTransport, "perform_handshake")
|
||||
mocker.patch.object(AesTransport, "perform_login")
|
||||
|
||||
send_mock = mocker.patch.object(AesTransport, "send", return_value=mock_response)
|
||||
config = DeviceConfig(host, credentials=Credentials("foo", "bar"))
|
||||
protocol = SmartProtocol(transport=AesTransport(config=config))
|
||||
send_mock = mocker.patch.object(
|
||||
dummy_protocol._transport, "send", return_value=mock_response
|
||||
)
|
||||
with pytest.raises(SmartDeviceException):
|
||||
await protocol.query(DUMMY_MULTIPLE_QUERY, retry_count=2)
|
||||
await dummy_protocol.query(DUMMY_MULTIPLE_QUERY, retry_count=2)
|
||||
if error_code in chain(SMART_TIMEOUT_ERRORS, SMART_RETRYABLE_ERRORS):
|
||||
expected_calls = 3
|
||||
else:
|
||||
@@ -88,7 +102,9 @@ async def test_smart_device_errors_in_multiple_request(mocker, error_code):
|
||||
|
||||
@pytest.mark.parametrize("request_size", [1, 3, 5, 10])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 4, 5])
|
||||
async def test_smart_device_multiple_request(mocker, request_size, batch_size):
|
||||
async def test_smart_device_multiple_request(
|
||||
dummy_protocol, mocker, request_size, batch_size
|
||||
):
|
||||
host = "127.0.0.1"
|
||||
requests = {}
|
||||
mock_response = {
|
||||
@@ -102,15 +118,101 @@ async def test_smart_device_multiple_request(mocker, request_size, batch_size):
|
||||
{"method": method, "result": {"great": "success"}, "error_code": 0}
|
||||
)
|
||||
|
||||
mocker.patch.object(AesTransport, "perform_handshake")
|
||||
mocker.patch.object(AesTransport, "perform_login")
|
||||
|
||||
send_mock = mocker.patch.object(AesTransport, "send", return_value=mock_response)
|
||||
send_mock = mocker.patch.object(
|
||||
dummy_protocol._transport, "send", return_value=mock_response
|
||||
)
|
||||
config = DeviceConfig(
|
||||
host, credentials=Credentials("foo", "bar"), batch_size=batch_size
|
||||
)
|
||||
protocol = SmartProtocol(transport=AesTransport(config=config))
|
||||
dummy_protocol._transport._config = config
|
||||
|
||||
await protocol.query(requests, retry_count=0)
|
||||
await dummy_protocol.query(requests, retry_count=0)
|
||||
expected_count = int(request_size / batch_size) + (request_size % batch_size > 0)
|
||||
assert send_mock.call_count == expected_count
|
||||
|
||||
|
||||
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)
|
||||
with pytest.raises(SmartDeviceException):
|
||||
await wrapped_protocol.query(DUMMY_QUERY)
|
||||
|
||||
|
||||
@pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
|
||||
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"},
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response)
|
||||
resp = await dummy_protocol.query(DUMMY_QUERY)
|
||||
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):
|
||||
"""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"},
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response)
|
||||
with pytest.raises(SmartDeviceException):
|
||||
await dummy_protocol.query(DUMMY_QUERY)
|
||||
|
Reference in New Issue
Block a user