2024-02-27 17:39:04 +00:00
|
|
|
import copy
|
2024-01-29 19:26:39 +00:00
|
|
|
from json import loads as json_loads
|
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from kasa import Credentials, DeviceConfig, SmartProtocol
|
|
|
|
from kasa.exceptions import SmartErrorCode
|
2024-01-29 19:26:39 +00:00
|
|
|
from kasa.protocol import BaseTransport
|
|
|
|
|
|
|
|
|
|
|
|
class FakeSmartProtocol(SmartProtocol):
|
2024-02-27 17:39:04 +00:00
|
|
|
def __init__(self, info, fixture_name):
|
2024-01-29 19:26:39 +00:00
|
|
|
super().__init__(
|
2024-02-27 17:39:04 +00:00
|
|
|
transport=FakeSmartTransport(info, fixture_name),
|
2024-01-29 19:26:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def query(self, request, retry_count: int = 3):
|
|
|
|
"""Implement query here so can still patch SmartProtocol.query."""
|
|
|
|
resp_dict = await self._query(request, retry_count)
|
|
|
|
return resp_dict
|
|
|
|
|
|
|
|
|
|
|
|
class FakeSmartTransport(BaseTransport):
|
2024-02-27 17:39:04 +00:00
|
|
|
def __init__(self, info, fixture_name):
|
2024-01-29 19:26:39 +00:00
|
|
|
super().__init__(
|
|
|
|
config=DeviceConfig(
|
|
|
|
"127.0.0.123",
|
|
|
|
credentials=Credentials(
|
|
|
|
username="dummy_user",
|
|
|
|
password="dummy_password", # noqa: S106
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
self.fixture_name = fixture_name
|
|
|
|
self.info = copy.deepcopy(info)
|
2024-01-29 19:26:39 +00:00
|
|
|
self.components = {
|
|
|
|
comp["id"]: comp["ver_code"]
|
|
|
|
for comp in self.info["component_nego"]["component_list"]
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_port(self):
|
|
|
|
"""Default port for the transport."""
|
|
|
|
return 80
|
|
|
|
|
|
|
|
@property
|
|
|
|
def credentials_hash(self):
|
|
|
|
"""The hashed credentials used by the transport."""
|
|
|
|
return self._credentials.username + self._credentials.password + "hash"
|
|
|
|
|
|
|
|
FIXTURE_MISSING_MAP = {
|
|
|
|
"get_wireless_scan_info": ("wireless", {"ap_list": [], "wep_supported": False}),
|
2024-02-20 00:00:26 +00:00
|
|
|
"get_auto_off_config": ("auto_off", {"delay_min": 10, "enable": False}),
|
2024-02-19 19:59:09 +00:00
|
|
|
"get_led_info": (
|
|
|
|
"led",
|
|
|
|
{
|
|
|
|
"led_rule": "never",
|
|
|
|
"led_status": False,
|
|
|
|
"night_mode": {
|
|
|
|
"end_time": 420,
|
|
|
|
"night_mode_type": "sunrise_sunset",
|
|
|
|
"start_time": 1140,
|
|
|
|
"sunrise_offset": 0,
|
|
|
|
"sunset_offset": 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2024-02-19 19:48:46 +00:00
|
|
|
"get_connect_cloud_state": ("cloud_connect", {"status": 1}),
|
2024-02-19 19:39:20 +00:00
|
|
|
"get_on_off_gradually_info": ("on_off_gradually", {"enable": True}),
|
2024-02-20 00:00:26 +00:00
|
|
|
"get_latest_fw": (
|
|
|
|
"firmware",
|
|
|
|
{
|
|
|
|
"fw_size": 0,
|
|
|
|
"fw_ver": "1.0.5 Build 230801 Rel.095702",
|
|
|
|
"hw_id": "",
|
|
|
|
"need_to_upgrade": False,
|
|
|
|
"oem_id": "",
|
|
|
|
"release_date": "",
|
|
|
|
"release_note": "",
|
|
|
|
"type": 0,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
"get_auto_update_info": (
|
|
|
|
"firmware",
|
|
|
|
{"enable": True, "random_range": 120, "time": 180},
|
|
|
|
),
|
2024-02-23 23:12:19 +00:00
|
|
|
"get_alarm_configure": (
|
|
|
|
"alarm",
|
|
|
|
{
|
|
|
|
"get_alarm_configure": {
|
|
|
|
"duration": 10,
|
|
|
|
"type": "Doorbell Ring 2",
|
|
|
|
"volume": "low",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2024-02-27 17:39:04 +00:00
|
|
|
"get_support_alarm_type_list": (
|
|
|
|
"alarm",
|
|
|
|
{
|
|
|
|
"alarm_type_list": [
|
|
|
|
"Doorbell Ring 1",
|
|
|
|
]
|
|
|
|
},
|
|
|
|
),
|
2024-02-23 23:12:19 +00:00
|
|
|
"get_device_usage": ("device", {}),
|
2024-01-29 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async def send(self, request: str):
|
|
|
|
request_dict = json_loads(request)
|
|
|
|
method = request_dict["method"]
|
|
|
|
params = request_dict["params"]
|
|
|
|
if method == "multipleRequest":
|
|
|
|
responses = []
|
|
|
|
for request in params["requests"]:
|
|
|
|
response = self._send_request(request) # type: ignore[arg-type]
|
|
|
|
response["method"] = request["method"] # type: ignore[index]
|
|
|
|
responses.append(response)
|
|
|
|
return {"result": {"responses": responses}, "error_code": 0}
|
|
|
|
else:
|
|
|
|
return self._send_request(request_dict)
|
|
|
|
|
|
|
|
def _handle_control_child(self, params: dict):
|
|
|
|
"""Handle control_child command."""
|
|
|
|
device_id = params.get("device_id")
|
|
|
|
request_data = params.get("requestData", {})
|
|
|
|
|
|
|
|
child_method = request_data.get("method")
|
|
|
|
child_params = request_data.get("params")
|
|
|
|
|
|
|
|
info = self.info
|
|
|
|
children = info["get_child_device_list"]["child_device_list"]
|
|
|
|
|
|
|
|
for child in children:
|
|
|
|
if child["device_id"] == device_id:
|
|
|
|
info = child
|
|
|
|
break
|
|
|
|
|
|
|
|
# We only support get & set device info for now.
|
|
|
|
if child_method == "get_device_info":
|
|
|
|
return {"result": info, "error_code": 0}
|
|
|
|
elif child_method == "set_device_info":
|
|
|
|
info.update(child_params)
|
|
|
|
return {"error_code": 0}
|
|
|
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Method %s not implemented for children" % child_method
|
|
|
|
)
|
|
|
|
|
|
|
|
def _send_request(self, request_dict: dict):
|
|
|
|
method = request_dict["method"]
|
|
|
|
params = request_dict["params"]
|
|
|
|
|
|
|
|
info = self.info
|
|
|
|
if method == "control_child":
|
|
|
|
return self._handle_control_child(params)
|
|
|
|
elif method == "component_nego" or method[:4] == "get_":
|
|
|
|
if method in info:
|
|
|
|
return {"result": info[method], "error_code": 0}
|
2024-02-27 17:39:04 +00:00
|
|
|
if (
|
|
|
|
# FIXTURE_MISSING is for service calls not in place when
|
|
|
|
# SMART fixtures started to be generated
|
2024-01-29 19:26:39 +00:00
|
|
|
missing_result := self.FIXTURE_MISSING_MAP.get(method)
|
|
|
|
) and missing_result[0] in self.components:
|
2024-02-27 17:39:04 +00:00
|
|
|
retval = {"result": missing_result[1], "error_code": 0}
|
2024-01-29 19:26:39 +00:00
|
|
|
else:
|
2024-02-27 17:39:04 +00:00
|
|
|
# PARAMS error returned for KS240 when get_device_usage called
|
|
|
|
# on parent device. Could be any error code though.
|
|
|
|
# TODO: Try to figure out if there's a way to prevent the KS240 smartdevice
|
|
|
|
# calling the unsupported device in the first place.
|
|
|
|
retval = {
|
|
|
|
"error_code": SmartErrorCode.PARAMS_ERROR.value,
|
|
|
|
"method": "get_device_usage",
|
|
|
|
}
|
|
|
|
# Reduce warning spam by consolidating and reporting at the end of the run
|
|
|
|
if self.fixture_name not in pytest.fixtures_missing_methods:
|
|
|
|
pytest.fixtures_missing_methods[self.fixture_name] = set()
|
|
|
|
pytest.fixtures_missing_methods[self.fixture_name].add(method)
|
|
|
|
return retval
|
2024-01-29 19:26:39 +00:00
|
|
|
elif method == "set_qs_info":
|
|
|
|
return {"error_code": 0}
|
|
|
|
elif method[:4] == "set_":
|
|
|
|
target_method = f"get_{method[4:]}"
|
|
|
|
info[target_method].update(params)
|
|
|
|
return {"error_code": 0}
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def reset(self) -> None:
|
|
|
|
pass
|