mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-22 12:40:17 +00:00
Use _get_device_info methods for smart and iot devs in devtools (#1265)
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from json import loads as json_loads
|
||||
from typing import Any
|
||||
|
||||
from kasa import Credentials, DeviceConfig, SmartProtocol
|
||||
from kasa.protocols.smartcameraprotocol import SmartCameraProtocol
|
||||
@@ -11,9 +12,11 @@ from .fakeprotocol_smart import FakeSmartTransport
|
||||
|
||||
|
||||
class FakeSmartCameraProtocol(SmartCameraProtocol):
|
||||
def __init__(self, info, fixture_name, *, is_child=False):
|
||||
def __init__(self, info, fixture_name, *, is_child=False, verbatim=False):
|
||||
super().__init__(
|
||||
transport=FakeSmartCameraTransport(info, fixture_name, is_child=is_child),
|
||||
transport=FakeSmartCameraTransport(
|
||||
info, fixture_name, is_child=is_child, verbatim=verbatim
|
||||
),
|
||||
)
|
||||
|
||||
async def query(self, request, retry_count: int = 3):
|
||||
@@ -30,6 +33,7 @@ class FakeSmartCameraTransport(BaseTransport):
|
||||
*,
|
||||
list_return_size=10,
|
||||
is_child=False,
|
||||
verbatim=False,
|
||||
):
|
||||
super().__init__(
|
||||
config=DeviceConfig(
|
||||
@@ -41,6 +45,9 @@ class FakeSmartCameraTransport(BaseTransport):
|
||||
),
|
||||
)
|
||||
self.fixture_name = fixture_name
|
||||
# When True verbatim will bypass any extra processing of missing
|
||||
# methods and is used to test the fixture creation itself.
|
||||
self.verbatim = verbatim
|
||||
if not is_child:
|
||||
self.info = copy.deepcopy(info)
|
||||
self.child_protocols = FakeSmartTransport._get_child_protocols(
|
||||
@@ -70,11 +77,11 @@ class FakeSmartCameraTransport(BaseTransport):
|
||||
responses = []
|
||||
for request in params["requests"]:
|
||||
response = await self._send_request(request) # type: ignore[arg-type]
|
||||
response["method"] = request["method"] # type: ignore[index]
|
||||
responses.append(response)
|
||||
# Devices do not continue after error
|
||||
if response["error_code"] != 0:
|
||||
break
|
||||
response["method"] = request["method"] # type: ignore[index]
|
||||
responses.append(response)
|
||||
return {"result": {"responses": responses}, "error_code": 0}
|
||||
else:
|
||||
return await self._send_request(request_dict)
|
||||
@@ -129,6 +136,15 @@ class FakeSmartCameraTransport(BaseTransport):
|
||||
],
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _get_second_key(request_dict: dict[str, Any]) -> str:
|
||||
assert (
|
||||
len(request_dict) == 2
|
||||
), f"Unexpected dict {request_dict}, should be length 2"
|
||||
it = iter(request_dict)
|
||||
next(it, None)
|
||||
return next(it)
|
||||
|
||||
async def _send_request(self, request_dict: dict):
|
||||
method = request_dict["method"]
|
||||
|
||||
@@ -175,6 +191,14 @@ class FakeSmartCameraTransport(BaseTransport):
|
||||
return {"error_code": -1}
|
||||
break
|
||||
return {"error_code": 0}
|
||||
elif method == "get":
|
||||
module = self._get_second_key(request_dict)
|
||||
get_method = f"get_{module}"
|
||||
if get_method in info:
|
||||
result = copy.deepcopy(info[get_method]["get"])
|
||||
return {**result, "error_code": 0}
|
||||
else:
|
||||
return {"error_code": -1}
|
||||
elif method[:3] == "get":
|
||||
params = request_dict.get("params")
|
||||
if method in info:
|
||||
|
||||
Reference in New Issue
Block a user