2021-05-10 00:19:00 +00:00
|
|
|
"""This script generates devinfo files for the test suite.
|
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
If you have new, yet unsupported device or a device with no devinfo file under
|
|
|
|
kasa/tests/fixtures, feel free to run this script and create a PR to add the file
|
|
|
|
to the repository.
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
Executing this script will several modules and methods one by one,
|
|
|
|
and finally execute a query to query all of them at once.
|
|
|
|
"""
|
2023-12-02 16:33:35 +00:00
|
|
|
import base64
|
2021-05-10 00:19:00 +00:00
|
|
|
import collections.abc
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import re
|
2024-02-14 19:43:10 +00:00
|
|
|
import traceback
|
2021-05-10 00:19:00 +00:00
|
|
|
from collections import defaultdict, namedtuple
|
2024-01-20 13:20:08 +00:00
|
|
|
from pathlib import Path
|
2021-05-10 00:19:00 +00:00
|
|
|
from pprint import pprint
|
2024-01-23 10:33:07 +00:00
|
|
|
from typing import Dict, List, Union
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2023-11-29 19:01:20 +00:00
|
|
|
import asyncclick as click
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2024-02-14 19:43:10 +00:00
|
|
|
from devtools.helpers.smartrequests import SmartRequest, get_component_requests
|
2024-01-24 09:40:36 +00:00
|
|
|
from kasa import (
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
2024-01-24 09:40:36 +00:00
|
|
|
Credentials,
|
2024-02-04 15:20:08 +00:00
|
|
|
Device,
|
2024-01-24 09:40:36 +00:00
|
|
|
Discover,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
|
|
|
TimeoutError,
|
2024-01-24 09:40:36 +00:00
|
|
|
)
|
2023-11-29 19:01:20 +00:00
|
|
|
from kasa.discover import DiscoveryResult
|
2024-01-02 17:20:53 +00:00
|
|
|
from kasa.exceptions import SmartErrorCode
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.smart import SmartDevice
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
Call = namedtuple("Call", "module method")
|
2024-01-02 17:20:53 +00:00
|
|
|
SmartCall = namedtuple("SmartCall", "module request should_succeed")
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2024-02-14 19:43:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
def scrub(res):
|
|
|
|
"""Remove identifiers from the given dict."""
|
|
|
|
keys_to_scrub = [
|
|
|
|
"deviceId",
|
|
|
|
"fwId",
|
|
|
|
"hwId",
|
|
|
|
"oemId",
|
|
|
|
"mac",
|
|
|
|
"mic_mac",
|
|
|
|
"latitude_i",
|
|
|
|
"longitude_i",
|
|
|
|
"latitude",
|
|
|
|
"longitude",
|
2024-02-18 23:08:39 +00:00
|
|
|
"la", # lat on ks240
|
|
|
|
"lo", # lon on ks240
|
2023-11-29 19:01:20 +00:00
|
|
|
"owner",
|
|
|
|
"device_id",
|
|
|
|
"ip",
|
|
|
|
"ssid",
|
|
|
|
"hw_id",
|
|
|
|
"fw_id",
|
|
|
|
"oem_id",
|
2023-12-08 13:29:07 +00:00
|
|
|
"nickname",
|
|
|
|
"alias",
|
2024-01-02 17:20:53 +00:00
|
|
|
"bssid",
|
|
|
|
"channel",
|
2024-02-23 23:12:19 +00:00
|
|
|
"original_device_id", # for child devices on strips
|
|
|
|
"parent_device_id", # for hub children
|
|
|
|
"setup_code", # matter
|
|
|
|
"setup_payload", # matter
|
2021-05-10 00:19:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for k, v in res.items():
|
|
|
|
if isinstance(v, collections.abc.Mapping):
|
|
|
|
res[k] = scrub(res.get(k))
|
2024-01-02 17:20:53 +00:00
|
|
|
elif (
|
|
|
|
isinstance(v, list)
|
|
|
|
and len(v) > 0
|
|
|
|
and isinstance(v[0], collections.abc.Mapping)
|
|
|
|
):
|
|
|
|
res[k] = [scrub(vi) for vi in v]
|
2021-05-10 00:19:00 +00:00
|
|
|
else:
|
|
|
|
if k in keys_to_scrub:
|
2024-01-20 13:20:08 +00:00
|
|
|
if k in ["mac", "mic_mac"]:
|
|
|
|
# Some macs have : or - as a separator and others do not
|
|
|
|
if len(v) == 12:
|
|
|
|
v = f"{v[:6]}000000"
|
|
|
|
else:
|
|
|
|
delim = ":" if ":" in v else "-"
|
|
|
|
rest = delim.join(
|
|
|
|
format(s, "02x") for s in bytes.fromhex("000000")
|
|
|
|
)
|
|
|
|
v = f"{v[:8]}{delim}{rest}"
|
|
|
|
elif k in ["latitude", "latitude_i", "longitude", "longitude_i"]:
|
2021-05-10 00:19:00 +00:00
|
|
|
v = 0
|
2023-11-29 19:01:20 +00:00
|
|
|
elif k in ["ip"]:
|
|
|
|
v = "127.0.0.123"
|
2023-12-02 16:33:35 +00:00
|
|
|
elif k in ["ssid"]:
|
|
|
|
# Need a valid base64 value here
|
2023-12-08 13:29:07 +00:00
|
|
|
v = base64.b64encode(b"#MASKED_SSID#").decode()
|
|
|
|
elif k in ["nickname"]:
|
|
|
|
v = base64.b64encode(b"#MASKED_NAME#").decode()
|
|
|
|
elif k in ["alias"]:
|
|
|
|
v = "#MASKED_NAME#"
|
2024-01-02 17:20:53 +00:00
|
|
|
elif isinstance(res[k], int):
|
|
|
|
v = 0
|
2024-02-01 18:27:01 +00:00
|
|
|
elif k == "device_id" and len(v) > 40:
|
|
|
|
# retain the last two chars when scrubbing child ids
|
|
|
|
end = v[-2:]
|
|
|
|
v = re.sub(r"\w", "0", v)
|
|
|
|
v = v[:40] + end
|
2021-05-10 00:19:00 +00:00
|
|
|
else:
|
|
|
|
v = re.sub(r"\w", "0", v)
|
|
|
|
|
|
|
|
res[k] = v
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def default_to_regular(d):
|
|
|
|
"""Convert nested defaultdicts to regular ones.
|
|
|
|
|
|
|
|
From https://stackoverflow.com/a/26496899
|
|
|
|
"""
|
|
|
|
if isinstance(d, defaultdict):
|
|
|
|
d = {k: default_to_regular(v) for k, v in d.items()}
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
async def handle_device(basedir, autosave, device: Device, batch_size: int):
|
2024-01-20 13:20:08 +00:00
|
|
|
"""Create a fixture for a single device instance."""
|
2024-02-04 15:20:08 +00:00
|
|
|
if isinstance(device, SmartDevice):
|
2024-01-23 10:33:07 +00:00
|
|
|
filename, copy_folder, final = await get_smart_fixture(device, batch_size)
|
2024-01-20 13:20:08 +00:00
|
|
|
else:
|
|
|
|
filename, copy_folder, final = await get_legacy_fixture(device)
|
|
|
|
|
|
|
|
save_filename = Path(basedir) / copy_folder / filename
|
|
|
|
|
|
|
|
pprint(scrub(final))
|
|
|
|
if autosave:
|
|
|
|
save = "y"
|
|
|
|
else:
|
|
|
|
save = click.prompt(
|
|
|
|
f"Do you want to save the above content to {save_filename} (y/n)"
|
|
|
|
)
|
|
|
|
if save == "y":
|
|
|
|
click.echo(f"Saving info to {save_filename}")
|
|
|
|
|
|
|
|
with open(save_filename, "w") as f:
|
|
|
|
json.dump(final, f, sort_keys=True, indent=4)
|
|
|
|
f.write("\n")
|
|
|
|
else:
|
|
|
|
click.echo("Not saving.")
|
|
|
|
|
|
|
|
|
2021-05-10 00:19:00 +00:00
|
|
|
@click.command()
|
2024-01-20 13:20:08 +00:00
|
|
|
@click.option("--host", required=False, help="Target host.")
|
|
|
|
@click.option(
|
|
|
|
"--target",
|
|
|
|
required=False,
|
|
|
|
default="255.255.255.255",
|
|
|
|
help="Target network for discovery.",
|
|
|
|
)
|
2023-11-29 19:01:20 +00:00
|
|
|
@click.option(
|
|
|
|
"--username",
|
2023-12-29 19:42:02 +00:00
|
|
|
default="",
|
2023-11-29 19:01:20 +00:00
|
|
|
required=False,
|
2024-01-04 18:28:59 +00:00
|
|
|
envvar="KASA_USERNAME",
|
2023-11-29 19:01:20 +00:00
|
|
|
help="Username/email address to authenticate to device.",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--password",
|
2023-12-29 19:42:02 +00:00
|
|
|
default="",
|
2023-11-29 19:01:20 +00:00
|
|
|
required=False,
|
2024-01-04 18:28:59 +00:00
|
|
|
envvar="KASA_PASSWORD",
|
2023-11-29 19:01:20 +00:00
|
|
|
help="Password to use to authenticate to device.",
|
|
|
|
)
|
2024-01-20 13:20:08 +00:00
|
|
|
@click.option("--basedir", help="Base directory for the git repository", default=".")
|
|
|
|
@click.option("--autosave", is_flag=True, default=False, help="Save without prompting")
|
2024-01-23 10:33:07 +00:00
|
|
|
@click.option(
|
|
|
|
"--batch-size", default=5, help="Number of batched requests to send at once"
|
|
|
|
)
|
2021-06-19 19:46:36 +00:00
|
|
|
@click.option("-d", "--debug", is_flag=True)
|
2024-01-23 10:33:07 +00:00
|
|
|
async def cli(host, target, basedir, autosave, debug, username, password, batch_size):
|
2024-01-20 13:20:08 +00:00
|
|
|
"""Generate devinfo files for devices.
|
|
|
|
|
|
|
|
Use --host (for a single device) or --target (for a complete network).
|
|
|
|
"""
|
2021-05-10 00:19:00 +00:00
|
|
|
if debug:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
2023-12-02 16:33:35 +00:00
|
|
|
credentials = Credentials(username=username, password=password)
|
2024-01-20 13:20:08 +00:00
|
|
|
if host is not None:
|
|
|
|
click.echo("Host given, performing discovery on %s." % host)
|
|
|
|
device = await Discover.discover_single(host, credentials=credentials)
|
2024-01-23 10:33:07 +00:00
|
|
|
await handle_device(basedir, autosave, device, batch_size)
|
2023-12-02 16:33:35 +00:00
|
|
|
else:
|
2023-12-05 15:45:09 +00:00
|
|
|
click.echo(
|
2024-01-20 13:20:08 +00:00
|
|
|
"No --host given, performing discovery on %s. Use --target to override."
|
|
|
|
% target
|
2023-12-05 15:45:09 +00:00
|
|
|
)
|
2024-01-20 13:20:08 +00:00
|
|
|
devices = await Discover.discover(target=target, credentials=credentials)
|
|
|
|
click.echo("Detected %s devices" % len(devices))
|
|
|
|
for dev in devices.values():
|
2024-01-23 10:33:07 +00:00
|
|
|
await handle_device(basedir, autosave, dev, batch_size)
|
2023-12-02 16:33:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_legacy_fixture(device):
|
|
|
|
"""Get fixture for legacy IOT style protocol."""
|
2021-05-10 00:19:00 +00:00
|
|
|
items = [
|
|
|
|
Call(module="system", method="get_sysinfo"),
|
|
|
|
Call(module="emeter", method="get_realtime"),
|
|
|
|
Call(module="smartlife.iot.dimmer", method="get_dimmer_parameters"),
|
|
|
|
Call(module="smartlife.iot.common.emeter", method="get_realtime"),
|
|
|
|
Call(
|
|
|
|
module="smartlife.iot.smartbulb.lightingservice", method="get_light_state"
|
|
|
|
),
|
2022-01-29 17:28:14 +00:00
|
|
|
Call(module="smartlife.iot.LAS", method="get_config"),
|
|
|
|
Call(module="smartlife.iot.PIR", method="get_config"),
|
2021-05-10 00:19:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
successes = []
|
|
|
|
|
2023-11-29 19:01:20 +00:00
|
|
|
for test_call in items:
|
2021-05-10 00:19:00 +00:00
|
|
|
try:
|
|
|
|
click.echo(f"Testing {test_call}..", nl=False)
|
2023-11-29 19:01:20 +00:00
|
|
|
info = await device.protocol.query(
|
|
|
|
{test_call.module: {test_call.method: None}}
|
|
|
|
)
|
2021-05-10 00:19:00 +00:00
|
|
|
resp = info[test_call.module]
|
|
|
|
except Exception as ex:
|
|
|
|
click.echo(click.style(f"FAIL {ex}", fg="red"))
|
|
|
|
else:
|
|
|
|
if "err_msg" in resp:
|
|
|
|
click.echo(click.style(f"FAIL {resp['err_msg']}", fg="red"))
|
|
|
|
else:
|
|
|
|
click.echo(click.style("OK", fg="green"))
|
|
|
|
successes.append((test_call, info))
|
2024-02-14 19:43:10 +00:00
|
|
|
finally:
|
|
|
|
await device.protocol.close()
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
final_query = defaultdict(defaultdict)
|
|
|
|
final = defaultdict(defaultdict)
|
|
|
|
for succ, resp in successes:
|
|
|
|
final_query[succ.module][succ.method] = None
|
|
|
|
final[succ.module][succ.method] = resp
|
|
|
|
|
|
|
|
final = default_to_regular(final)
|
|
|
|
|
|
|
|
try:
|
2023-11-29 19:01:20 +00:00
|
|
|
final = await device.protocol.query(final_query)
|
2021-05-10 00:19:00 +00:00
|
|
|
except Exception as ex:
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(f"Unable to query all successes at once: {ex}", bold=True, fg="red")
|
2024-02-14 19:43:10 +00:00
|
|
|
finally:
|
|
|
|
await device.protocol.close()
|
2024-01-02 17:20:53 +00:00
|
|
|
if device._discovery_info and not device._discovery_info.get("system"):
|
2023-11-29 19:01:20 +00:00
|
|
|
# Need to recreate a DiscoverResult here because we don't want the aliases
|
|
|
|
# in the fixture, we want the actual field names as returned by the device.
|
|
|
|
dr = DiscoveryResult(**device._discovery_info)
|
|
|
|
final["discovery_result"] = dr.dict(
|
|
|
|
by_alias=False, exclude_unset=True, exclude_none=True, exclude_defaults=True
|
|
|
|
)
|
|
|
|
|
2021-05-10 00:19:00 +00:00
|
|
|
click.echo("Got %s successes" % len(successes))
|
|
|
|
click.echo(click.style("## device info file ##", bold=True))
|
|
|
|
|
|
|
|
sysinfo = final["system"]["get_sysinfo"]
|
|
|
|
model = sysinfo["model"]
|
|
|
|
hw_version = sysinfo["hw_ver"]
|
|
|
|
sw_version = sysinfo["sw_ver"]
|
|
|
|
sw_version = sw_version.split(" ", maxsplit=1)[0]
|
2023-12-05 15:45:09 +00:00
|
|
|
save_filename = f"{model}_{hw_version}_{sw_version}.json"
|
|
|
|
copy_folder = "kasa/tests/fixtures/"
|
|
|
|
return save_filename, copy_folder, final
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2024-01-24 09:40:36 +00:00
|
|
|
def _echo_error(msg: str):
|
|
|
|
click.echo(
|
|
|
|
click.style(
|
|
|
|
msg,
|
|
|
|
bold=True,
|
|
|
|
fg="red",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
async def _make_requests_or_exit(
|
2024-01-23 10:33:07 +00:00
|
|
|
device: SmartDevice,
|
|
|
|
requests: List[SmartRequest],
|
|
|
|
name: str,
|
|
|
|
batch_size: int,
|
2024-01-02 17:20:53 +00:00
|
|
|
) -> Dict[str, Dict]:
|
|
|
|
final = {}
|
|
|
|
try:
|
|
|
|
end = len(requests)
|
2024-01-23 10:33:07 +00:00
|
|
|
step = batch_size # Break the requests down as there seems to be a size limit
|
2024-01-02 17:20:53 +00:00
|
|
|
for i in range(0, end, step):
|
|
|
|
x = i
|
|
|
|
requests_step = requests[x : x + step]
|
2024-01-23 10:33:07 +00:00
|
|
|
request: Union[List[SmartRequest], SmartRequest] = (
|
|
|
|
requests_step[0] if len(requests_step) == 1 else requests_step
|
|
|
|
)
|
2024-01-02 17:20:53 +00:00
|
|
|
responses = await device.protocol.query(
|
2024-01-23 10:33:07 +00:00
|
|
|
SmartRequest._create_request_dict(request)
|
2024-01-02 17:20:53 +00:00
|
|
|
)
|
|
|
|
for method, result in responses.items():
|
|
|
|
final[method] = result
|
|
|
|
return final
|
2024-02-21 15:52:55 +00:00
|
|
|
except AuthenticationError as ex:
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(
|
|
|
|
f"Unable to query the device due to an authentication error: {ex}",
|
|
|
|
)
|
|
|
|
exit(1)
|
2024-02-21 15:52:55 +00:00
|
|
|
except KasaException as ex:
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(
|
|
|
|
f"Unable to query {name} at once: {ex}",
|
2024-01-02 17:20:53 +00:00
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
if isinstance(ex, TimeoutError):
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(
|
|
|
|
"Timeout, try reducing the batch size via --batch-size option.",
|
|
|
|
)
|
2024-01-02 17:20:53 +00:00
|
|
|
exit(1)
|
|
|
|
except Exception as ex:
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(
|
|
|
|
f"Unexpected exception querying {name} at once: {ex}",
|
2024-01-02 17:20:53 +00:00
|
|
|
)
|
2024-02-14 19:43:10 +00:00
|
|
|
if _LOGGER.isEnabledFor(logging.DEBUG):
|
|
|
|
traceback.print_stack()
|
2024-01-02 17:20:53 +00:00
|
|
|
exit(1)
|
2024-02-14 19:43:10 +00:00
|
|
|
finally:
|
|
|
|
await device.protocol.close()
|
2024-01-02 17:20:53 +00:00
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
async def get_smart_fixture(device: SmartDevice, batch_size: int):
|
2023-12-02 16:33:35 +00:00
|
|
|
"""Get fixture for new TAPO style protocol."""
|
2024-01-02 17:20:53 +00:00
|
|
|
extra_test_calls = [
|
|
|
|
SmartCall(
|
|
|
|
module="temp_humidity_records",
|
|
|
|
request=SmartRequest.get_raw_request("get_temp_humidity_records"),
|
|
|
|
should_succeed=False,
|
2023-12-08 13:29:07 +00:00
|
|
|
),
|
2024-01-02 17:20:53 +00:00
|
|
|
SmartCall(
|
|
|
|
module="child_device_list",
|
|
|
|
request=SmartRequest.get_raw_request("get_child_device_list"),
|
|
|
|
should_succeed=False,
|
|
|
|
),
|
|
|
|
SmartCall(
|
2023-12-02 16:33:35 +00:00
|
|
|
module="child_device_component_list",
|
2024-01-02 17:20:53 +00:00
|
|
|
request=SmartRequest.get_raw_request("get_child_device_component_list"),
|
|
|
|
should_succeed=False,
|
|
|
|
),
|
|
|
|
SmartCall(
|
|
|
|
module="trigger_logs",
|
|
|
|
request=SmartRequest.get_raw_request(
|
|
|
|
"get_trigger_logs", SmartRequest.GetTriggerLogsParams(5, 0)
|
|
|
|
),
|
|
|
|
should_succeed=False,
|
2023-12-02 16:33:35 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
successes = []
|
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
click.echo("Testing component_nego call ..", nl=False)
|
|
|
|
responses = await _make_requests_or_exit(
|
2024-01-23 10:33:07 +00:00
|
|
|
device, [SmartRequest.component_nego()], "component_nego call", batch_size
|
2024-01-02 17:20:53 +00:00
|
|
|
)
|
|
|
|
component_info_response = responses["component_nego"]
|
|
|
|
click.echo(click.style("OK", fg="green"))
|
|
|
|
successes.append(
|
|
|
|
SmartCall(
|
|
|
|
module="component_nego",
|
|
|
|
request=SmartRequest("component_nego"),
|
|
|
|
should_succeed=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
test_calls = []
|
|
|
|
should_succeed = []
|
|
|
|
|
|
|
|
for item in component_info_response["component_list"]:
|
|
|
|
component_id = item["id"]
|
2024-02-14 19:43:10 +00:00
|
|
|
ver_code = item["ver_code"]
|
|
|
|
if (requests := get_component_requests(component_id, ver_code)) is not None:
|
2024-01-02 17:20:53 +00:00
|
|
|
component_test_calls = [
|
|
|
|
SmartCall(module=component_id, request=request, should_succeed=True)
|
|
|
|
for request in requests
|
|
|
|
]
|
|
|
|
test_calls.extend(component_test_calls)
|
|
|
|
should_succeed.extend(component_test_calls)
|
2024-02-14 19:43:10 +00:00
|
|
|
else:
|
2024-01-02 17:20:53 +00:00
|
|
|
click.echo(f"Skipping {component_id}..", nl=False)
|
|
|
|
click.echo(click.style("UNSUPPORTED", fg="yellow"))
|
|
|
|
|
|
|
|
test_calls.extend(extra_test_calls)
|
|
|
|
|
|
|
|
for test_call in test_calls:
|
|
|
|
click.echo(f"Testing {test_call.module}..", nl=False)
|
2023-12-02 16:33:35 +00:00
|
|
|
try:
|
|
|
|
click.echo(f"Testing {test_call}..", nl=False)
|
2024-01-02 17:20:53 +00:00
|
|
|
response = await device.protocol.query(
|
|
|
|
SmartRequest._create_request_dict(test_call.request)
|
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
except AuthenticationError as ex:
|
2024-01-24 09:40:36 +00:00
|
|
|
_echo_error(
|
|
|
|
f"Unable to query the device due to an authentication error: {ex}",
|
2023-12-29 19:42:02 +00:00
|
|
|
)
|
|
|
|
exit(1)
|
2023-12-02 16:33:35 +00:00
|
|
|
except Exception as ex:
|
2024-01-02 17:20:53 +00:00
|
|
|
if (
|
|
|
|
not test_call.should_succeed
|
|
|
|
and hasattr(ex, "error_code")
|
2024-02-14 19:43:10 +00:00
|
|
|
and ex.error_code
|
|
|
|
in [
|
|
|
|
SmartErrorCode.UNKNOWN_METHOD_ERROR,
|
|
|
|
SmartErrorCode.TRANSPORT_NOT_AVAILABLE_ERROR,
|
|
|
|
]
|
2024-01-02 17:20:53 +00:00
|
|
|
):
|
|
|
|
click.echo(click.style("FAIL - EXPECTED", fg="green"))
|
|
|
|
else:
|
|
|
|
click.echo(click.style(f"FAIL {ex}", fg="red"))
|
2023-12-02 16:33:35 +00:00
|
|
|
else:
|
|
|
|
if not response:
|
2024-01-02 17:20:53 +00:00
|
|
|
click.echo(click.style("FAIL no response", fg="red"))
|
2023-12-02 16:33:35 +00:00
|
|
|
else:
|
2024-01-02 17:20:53 +00:00
|
|
|
if not test_call.should_succeed:
|
|
|
|
click.echo(click.style("OK - EXPECTED FAIL", fg="red"))
|
|
|
|
else:
|
|
|
|
click.echo(click.style("OK", fg="green"))
|
2023-12-02 16:33:35 +00:00
|
|
|
successes.append(test_call)
|
2024-02-14 19:43:10 +00:00
|
|
|
finally:
|
|
|
|
await device.protocol.close()
|
2023-12-02 16:33:35 +00:00
|
|
|
|
|
|
|
requests = []
|
|
|
|
for succ in successes:
|
2024-01-02 17:20:53 +00:00
|
|
|
requests.append(succ.request)
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2024-01-23 10:33:07 +00:00
|
|
|
final = await _make_requests_or_exit(
|
|
|
|
device, requests, "all successes at once", batch_size
|
|
|
|
)
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2023-12-05 15:45:09 +00:00
|
|
|
# Need to recreate a DiscoverResult here because we don't want the aliases
|
|
|
|
# in the fixture, we want the actual field names as returned by the device.
|
|
|
|
dr = DiscoveryResult(**device._discovery_info) # type: ignore
|
|
|
|
final["discovery_result"] = dr.dict(
|
|
|
|
by_alias=False, exclude_unset=True, exclude_none=True, exclude_defaults=True
|
|
|
|
)
|
2023-12-02 16:33:35 +00:00
|
|
|
|
|
|
|
click.echo("Got %s successes" % len(successes))
|
|
|
|
click.echo(click.style("## device info file ##", bold=True))
|
|
|
|
|
|
|
|
hw_version = final["get_device_info"]["hw_ver"]
|
|
|
|
sw_version = final["get_device_info"]["fw_ver"]
|
2023-12-05 15:45:09 +00:00
|
|
|
model = final["discovery_result"]["device_model"]
|
2023-12-02 16:33:35 +00:00
|
|
|
sw_version = sw_version.split(" ", maxsplit=1)[0]
|
|
|
|
|
2023-12-05 15:45:09 +00:00
|
|
|
save_filename = f"{model}_{hw_version}_{sw_version}.json"
|
|
|
|
copy_folder = "kasa/tests/fixtures/smart/"
|
|
|
|
return save_filename, copy_folder, final
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|