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
|
2024-11-11 10:11:31 +00:00
|
|
|
tests/fixtures, feel free to run this script and create a PR to add the file
|
2023-10-29 22:15:42 +00:00
|
|
|
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.
|
|
|
|
"""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-02 16:33:35 +00:00
|
|
|
import base64
|
2021-05-10 00:19:00 +00:00
|
|
|
import collections.abc
|
2024-10-24 12:11:28 +00:00
|
|
|
import dataclasses
|
2021-05-10 00:19:00 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import re
|
2024-04-29 13:33:46 +00:00
|
|
|
import sys
|
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
|
|
|
|
|
2023-11-29 19:01:20 +00:00
|
|
|
import asyncclick as click
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2024-10-24 12:11:28 +00:00
|
|
|
from devtools.helpers.smartcamerarequests import SMARTCAMERA_REQUESTS
|
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-10-18 11:06:22 +00:00
|
|
|
DeviceConfig,
|
|
|
|
DeviceConnectionParameters,
|
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
|
|
|
)
|
2024-10-18 11:06:22 +00:00
|
|
|
from kasa.device_factory import get_protocol
|
|
|
|
from kasa.deviceconfig import DeviceEncryptionType, DeviceFamily
|
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-10-18 11:06:22 +00:00
|
|
|
from kasa.experimental.smartcameraprotocol import (
|
|
|
|
SmartCameraProtocol,
|
|
|
|
_ChildCameraProtocolWrapper,
|
|
|
|
)
|
|
|
|
from kasa.smart import SmartChildDevice
|
|
|
|
from kasa.smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
Call = namedtuple("Call", "module method")
|
2024-02-28 16:04:57 +00:00
|
|
|
FixtureResult = namedtuple("FixtureResult", "filename, folder, data")
|
|
|
|
|
2024-11-11 10:11:31 +00:00
|
|
|
SMART_FOLDER = "tests/fixtures/smart/"
|
|
|
|
SMARTCAMERA_FOLDER = "tests/fixtures/smartcamera/"
|
|
|
|
SMART_CHILD_FOLDER = "tests/fixtures/smart/child/"
|
|
|
|
IOT_FOLDER = "tests/fixtures/"
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
ENCRYPT_TYPES = [encrypt_type.value for encrypt_type in DeviceEncryptionType]
|
|
|
|
|
2024-02-14 19:43:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-10 00:19:00 +00:00
|
|
|
|
2024-10-24 12:11:28 +00:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class SmartCall:
|
|
|
|
"""Class for smart and smartcamera calls."""
|
|
|
|
|
|
|
|
module: str
|
|
|
|
request: dict
|
|
|
|
should_succeed: bool
|
|
|
|
child_device_id: str
|
|
|
|
supports_multiple: bool = True
|
|
|
|
|
|
|
|
|
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
|
2024-02-28 16:04:57 +00:00
|
|
|
"mfi_setup_code", # mfi_ for homekit
|
|
|
|
"mfi_setup_id",
|
|
|
|
"mfi_token_token",
|
|
|
|
"mfi_token_uuid",
|
2024-10-18 11:06:22 +00:00
|
|
|
"dev_id",
|
|
|
|
"device_name",
|
|
|
|
"device_alias",
|
|
|
|
"connect_ssid",
|
|
|
|
"encrypt_info",
|
|
|
|
"local_ip",
|
2021-05-10 00:19:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for k, v in res.items():
|
|
|
|
if isinstance(v, collections.abc.Mapping):
|
2024-10-18 11:06:22 +00:00
|
|
|
if k == "encrypt_info":
|
|
|
|
if "data" in v:
|
|
|
|
v["data"] = ""
|
|
|
|
if "key" in v:
|
|
|
|
v["key"] = ""
|
|
|
|
else:
|
|
|
|
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
|
2024-10-18 11:06:22 +00:00
|
|
|
elif k in ["ip", "local_ip"]:
|
2023-11-29 19:01:20 +00:00
|
|
|
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()
|
2024-10-24 12:11:28 +00:00
|
|
|
elif k in ["alias", "device_alias", "device_name"]:
|
2023-12-08 13:29:07 +00:00
|
|
|
v = "#MASKED_NAME#"
|
2024-01-02 17:20:53 +00:00
|
|
|
elif isinstance(res[k], int):
|
|
|
|
v = 0
|
2024-10-18 11:06:22 +00:00
|
|
|
elif k in ["device_id", "dev_id"] and "SCRUBBED" in v:
|
2024-02-28 16:04:57 +00:00
|
|
|
pass # already scrubbed
|
2024-10-18 11:06:22 +00:00
|
|
|
elif k == ["device_id", "dev_id"] and len(v) > 40:
|
2024-02-01 18:27:01 +00:00
|
|
|
# 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-10-18 11:06:22 +00:00
|
|
|
async def handle_device(
|
|
|
|
basedir, autosave, protocol, *, discovery_info=None, batch_size: int
|
|
|
|
):
|
2024-01-20 13:20:08 +00:00
|
|
|
"""Create a fixture for a single device instance."""
|
2024-10-18 11:06:22 +00:00
|
|
|
if isinstance(protocol, SmartProtocol):
|
2024-04-17 13:39:24 +00:00
|
|
|
fixture_results: list[FixtureResult] = await get_smart_fixtures(
|
2024-10-18 11:06:22 +00:00
|
|
|
protocol, discovery_info=discovery_info, batch_size=batch_size
|
2024-02-28 16:04:57 +00:00
|
|
|
)
|
2024-01-20 13:20:08 +00:00
|
|
|
else:
|
2024-10-18 11:06:22 +00:00
|
|
|
fixture_results = [
|
|
|
|
await get_legacy_fixture(protocol, discovery_info=discovery_info)
|
|
|
|
]
|
2024-01-20 13:20:08 +00:00
|
|
|
|
2024-02-28 16:04:57 +00:00
|
|
|
for fixture_result in fixture_results:
|
|
|
|
save_filename = Path(basedir) / fixture_result.folder / fixture_result.filename
|
2024-01-20 13:20:08 +00:00
|
|
|
|
2024-02-28 16:04:57 +00:00
|
|
|
pprint(scrub(fixture_result.data))
|
|
|
|
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}")
|
2024-01-20 13:20:08 +00:00
|
|
|
|
2024-05-03 11:57:43 +00:00
|
|
|
with save_filename.open("w") as f:
|
2024-02-28 16:04:57 +00:00
|
|
|
json.dump(fixture_result.data, f, sort_keys=True, indent=4)
|
|
|
|
f.write("\n")
|
|
|
|
else:
|
|
|
|
click.echo("Not saving.")
|
2024-01-20 13:20:08 +00:00
|
|
|
|
|
|
|
|
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-02-28 16:04:57 +00:00
|
|
|
@click.option(
|
|
|
|
"-di",
|
|
|
|
"--discovery-info",
|
|
|
|
help=(
|
|
|
|
"Bypass discovery by passing an accurate discovery result json escaped string."
|
|
|
|
+ " Do not use this flag unless you are sure you know what it means."
|
|
|
|
),
|
|
|
|
)
|
2024-10-18 11:06:22 +00:00
|
|
|
@click.option(
|
|
|
|
"--discovery-timeout",
|
|
|
|
envvar="KASA_DISCOVERY_TIMEOUT",
|
|
|
|
default=10,
|
|
|
|
required=False,
|
|
|
|
show_default=True,
|
|
|
|
help="Timeout for discovery.",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-e",
|
|
|
|
"--encrypt-type",
|
|
|
|
envvar="KASA_ENCRYPT_TYPE",
|
|
|
|
default=None,
|
|
|
|
type=click.Choice(ENCRYPT_TYPES, case_sensitive=False),
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-df",
|
|
|
|
"--device-family",
|
|
|
|
envvar="KASA_DEVICE_FAMILY",
|
|
|
|
default="SMART.TAPOPLUG",
|
|
|
|
help="Device family type, e.g. `SMART.KASASWITCH`.",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-lv",
|
|
|
|
"--login-version",
|
|
|
|
envvar="KASA_LOGIN_VERSION",
|
|
|
|
default=2,
|
|
|
|
type=int,
|
|
|
|
help="The login version for device authentication. Defaults to 2",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--https/--no-https",
|
|
|
|
envvar="KASA_HTTPS",
|
|
|
|
default=False,
|
|
|
|
is_flag=True,
|
|
|
|
type=bool,
|
|
|
|
help="Set flag if the device encryption uses https.",
|
|
|
|
)
|
2024-06-03 15:46:38 +00:00
|
|
|
@click.option("--port", help="Port override", type=int)
|
2024-02-28 16:04:57 +00:00
|
|
|
async def cli(
|
|
|
|
host,
|
|
|
|
target,
|
|
|
|
basedir,
|
|
|
|
autosave,
|
|
|
|
debug,
|
|
|
|
username,
|
2024-10-18 11:06:22 +00:00
|
|
|
discovery_timeout,
|
2024-02-28 16:04:57 +00:00
|
|
|
password,
|
|
|
|
batch_size,
|
|
|
|
discovery_info,
|
2024-10-18 11:06:22 +00:00
|
|
|
encrypt_type,
|
|
|
|
https,
|
|
|
|
device_family,
|
|
|
|
login_version,
|
2024-02-28 16:04:57 +00:00
|
|
|
port,
|
|
|
|
):
|
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)
|
|
|
|
|
2024-10-29 09:30:30 +00:00
|
|
|
from kasa.experimental import Experimental
|
2024-10-18 11:06:22 +00:00
|
|
|
|
2024-10-29 09:30:30 +00:00
|
|
|
Experimental.set_enabled(True)
|
2024-10-18 11:06:22 +00:00
|
|
|
|
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:
|
2024-02-28 16:04:57 +00:00
|
|
|
if discovery_info:
|
|
|
|
click.echo("Host and discovery info given, trying connect on %s." % host)
|
|
|
|
|
|
|
|
di = json.loads(discovery_info)
|
|
|
|
dr = DiscoveryResult(**di)
|
2024-06-03 18:06:54 +00:00
|
|
|
connection_type = DeviceConnectionParameters.from_values(
|
2024-02-28 16:04:57 +00:00
|
|
|
dr.device_type,
|
|
|
|
dr.mgt_encrypt_schm.encrypt_type,
|
|
|
|
dr.mgt_encrypt_schm.lv,
|
|
|
|
)
|
|
|
|
dc = DeviceConfig(
|
|
|
|
host=host,
|
|
|
|
connection_type=connection_type,
|
|
|
|
port_override=port,
|
|
|
|
credentials=credentials,
|
|
|
|
)
|
|
|
|
device = await Device.connect(config=dc)
|
2024-10-18 11:06:22 +00:00
|
|
|
await handle_device(
|
|
|
|
basedir,
|
|
|
|
autosave,
|
|
|
|
device.protocol,
|
|
|
|
discovery_info=dr.get_dict(),
|
|
|
|
batch_size=batch_size,
|
|
|
|
)
|
|
|
|
elif device_family and encrypt_type:
|
|
|
|
ctype = DeviceConnectionParameters(
|
|
|
|
DeviceFamily(device_family),
|
|
|
|
DeviceEncryptionType(encrypt_type),
|
|
|
|
login_version,
|
|
|
|
https,
|
|
|
|
)
|
|
|
|
config = DeviceConfig(
|
|
|
|
host=host,
|
|
|
|
port_override=port,
|
|
|
|
credentials=credentials,
|
|
|
|
connection_type=ctype,
|
|
|
|
)
|
|
|
|
if protocol := get_protocol(config):
|
|
|
|
await handle_device(basedir, autosave, protocol, batch_size=batch_size)
|
|
|
|
else:
|
|
|
|
raise KasaException(
|
|
|
|
"Could not find a protocol for the given parameters. "
|
|
|
|
+ "Maybe you need to enable --experimental."
|
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
else:
|
|
|
|
click.echo("Host given, performing discovery on %s." % host)
|
|
|
|
device = await Discover.discover_single(
|
2024-10-18 11:06:22 +00:00
|
|
|
host,
|
|
|
|
credentials=credentials,
|
|
|
|
port=port,
|
|
|
|
discovery_timeout=discovery_timeout,
|
|
|
|
)
|
|
|
|
await handle_device(
|
|
|
|
basedir,
|
|
|
|
autosave,
|
|
|
|
device.protocol,
|
|
|
|
discovery_info=device._discovery_info,
|
|
|
|
batch_size=batch_size,
|
2024-02-28 16:04:57 +00:00
|
|
|
)
|
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-10-18 11:06:22 +00:00
|
|
|
devices = await Discover.discover(
|
|
|
|
target=target, credentials=credentials, discovery_timeout=discovery_timeout
|
|
|
|
)
|
2024-01-20 13:20:08 +00:00
|
|
|
click.echo("Detected %s devices" % len(devices))
|
|
|
|
for dev in devices.values():
|
2024-10-18 11:06:22 +00:00
|
|
|
await handle_device(
|
|
|
|
basedir,
|
|
|
|
autosave,
|
|
|
|
dev.protocol,
|
|
|
|
discovery_info=dev._discovery_info,
|
|
|
|
batch_size=batch_size,
|
|
|
|
)
|
2023-12-02 16:33:35 +00:00
|
|
|
|
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
async def get_legacy_fixture(protocol, *, discovery_info):
|
2023-12-02 16:33:35 +00:00
|
|
|
"""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"),
|
2024-10-29 09:30:13 +00:00
|
|
|
Call(module="smartlife.iot.LAS", method="get_current_brt"),
|
2022-01-29 17:28:14 +00:00
|
|
|
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)
|
2024-10-18 11:06:22 +00:00
|
|
|
info = await protocol.query({test_call.module: {test_call.method: {}}})
|
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:
|
2024-10-18 11:06:22 +00:00
|
|
|
await protocol.close()
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
final_query = defaultdict(defaultdict)
|
|
|
|
final = defaultdict(defaultdict)
|
|
|
|
for succ, resp in successes:
|
2024-10-01 11:47:36 +00:00
|
|
|
final_query[succ.module][succ.method] = {}
|
2021-05-10 00:19:00 +00:00
|
|
|
final[succ.module][succ.method] = resp
|
|
|
|
|
|
|
|
final = default_to_regular(final)
|
|
|
|
|
|
|
|
try:
|
2024-10-18 11:06:22 +00:00
|
|
|
final = await 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:
|
2024-10-18 11:06:22 +00:00
|
|
|
await protocol.close()
|
|
|
|
if discovery_info and not 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.
|
2024-10-18 11:06:22 +00:00
|
|
|
dr = DiscoveryResult(**protocol._discovery_info)
|
2023-11-29 19:01:20 +00:00
|
|
|
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"
|
2024-02-28 16:04:57 +00:00
|
|
|
copy_folder = IOT_FOLDER
|
|
|
|
return FixtureResult(filename=save_filename, folder=copy_folder, data=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-04-29 13:33:46 +00:00
|
|
|
def format_exception(e):
|
|
|
|
"""Print full exception stack as if it hadn't been caught.
|
|
|
|
|
|
|
|
https://stackoverflow.com/a/12539332
|
|
|
|
"""
|
|
|
|
exception_list = traceback.format_stack()
|
|
|
|
exception_list = exception_list[:-2]
|
|
|
|
exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
|
|
|
|
exception_list.extend(
|
|
|
|
traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])
|
|
|
|
)
|
|
|
|
|
|
|
|
exception_str = "Traceback (most recent call last):\n"
|
|
|
|
exception_str += "".join(exception_list)
|
|
|
|
# Removing the last \n
|
|
|
|
exception_str = exception_str[:-1]
|
|
|
|
|
|
|
|
return exception_str
|
|
|
|
|
|
|
|
|
2024-10-24 12:11:28 +00:00
|
|
|
async def _make_final_calls(
|
|
|
|
protocol: SmartProtocol,
|
|
|
|
calls: list[SmartCall],
|
|
|
|
name: str,
|
|
|
|
batch_size: int,
|
|
|
|
*,
|
|
|
|
child_device_id: str,
|
|
|
|
) -> dict[str, dict]:
|
|
|
|
"""Call all successes again.
|
|
|
|
|
|
|
|
After trying each call individually make the calls again either as a
|
|
|
|
multiple request or as single requests for those that don't support
|
|
|
|
multiple queries.
|
|
|
|
"""
|
|
|
|
multiple_requests = {
|
|
|
|
key: smartcall.request[key]
|
|
|
|
for smartcall in calls
|
|
|
|
if smartcall.supports_multiple and (key := next(iter(smartcall.request)))
|
|
|
|
}
|
|
|
|
final = await _make_requests_or_exit(
|
|
|
|
protocol,
|
|
|
|
multiple_requests,
|
|
|
|
name + " - multiple",
|
|
|
|
batch_size,
|
|
|
|
child_device_id=child_device_id,
|
|
|
|
)
|
|
|
|
single_calls = [smartcall for smartcall in calls if not smartcall.supports_multiple]
|
|
|
|
for smartcall in single_calls:
|
|
|
|
final[smartcall.module] = await _make_requests_or_exit(
|
|
|
|
protocol,
|
|
|
|
smartcall.request,
|
|
|
|
f"{name} + {smartcall.module}",
|
|
|
|
batch_size,
|
|
|
|
child_device_id=child_device_id,
|
|
|
|
)
|
|
|
|
return final
|
|
|
|
|
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
async def _make_requests_or_exit(
|
2024-10-18 11:06:22 +00:00
|
|
|
protocol: SmartProtocol,
|
|
|
|
requests: dict,
|
2024-01-23 10:33:07 +00:00
|
|
|
name: str,
|
|
|
|
batch_size: int,
|
2024-02-28 16:04:57 +00:00
|
|
|
*,
|
|
|
|
child_device_id: str,
|
2024-04-17 13:39:24 +00:00
|
|
|
) -> dict[str, dict]:
|
2024-01-02 17:20:53 +00:00
|
|
|
final = {}
|
2024-10-18 11:06:22 +00:00
|
|
|
# Calling close on child protocol wrappers is a noop
|
|
|
|
protocol_to_close = protocol
|
|
|
|
if child_device_id:
|
|
|
|
if isinstance(protocol, SmartCameraProtocol):
|
|
|
|
protocol = _ChildCameraProtocolWrapper(child_device_id, protocol)
|
|
|
|
else:
|
|
|
|
protocol = _ChildProtocolWrapper(child_device_id, protocol)
|
2024-01-02 17:20:53 +00:00
|
|
|
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-10-18 11:06:22 +00:00
|
|
|
keys = [key for key in requests]
|
2024-01-02 17:20:53 +00:00
|
|
|
for i in range(0, end, step):
|
|
|
|
x = i
|
2024-10-18 11:06:22 +00:00
|
|
|
requests_step = {key: requests[key] for key in keys[x : x + step]}
|
|
|
|
responses = await protocol.query(requests_step)
|
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):
|
2024-04-29 13:33:46 +00:00
|
|
|
_echo_error(format_exception(ex))
|
2024-01-02 17:20:53 +00:00
|
|
|
exit(1)
|
2024-02-14 19:43:10 +00:00
|
|
|
finally:
|
2024-10-18 11:06:22 +00:00
|
|
|
await protocol_to_close.close()
|
2024-01-02 17:20:53 +00:00
|
|
|
|
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
async def get_smart_camera_test_calls(protocol: SmartProtocol):
|
|
|
|
"""Get the list of test calls to make."""
|
|
|
|
test_calls: list[SmartCall] = []
|
|
|
|
successes: list[SmartCall] = []
|
|
|
|
|
|
|
|
test_calls = []
|
2024-10-24 12:11:28 +00:00
|
|
|
for request in SMARTCAMERA_REQUESTS:
|
|
|
|
method = next(iter(request))
|
|
|
|
if method == "get":
|
|
|
|
module = method + "_" + next(iter(request[method]))
|
|
|
|
else:
|
|
|
|
module = method
|
2024-10-18 11:06:22 +00:00
|
|
|
test_calls.append(
|
|
|
|
SmartCall(
|
2024-10-24 12:11:28 +00:00
|
|
|
module=module,
|
|
|
|
request=request,
|
2024-10-18 11:06:22 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id="",
|
2024-10-24 12:11:28 +00:00
|
|
|
supports_multiple=(method != "get"),
|
2024-10-18 11:06:22 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now get the child device requests
|
2024-10-24 12:11:28 +00:00
|
|
|
child_request = {
|
|
|
|
"getChildDeviceList": {"childControl": {"start_index": 0}},
|
|
|
|
}
|
2024-10-18 11:06:22 +00:00
|
|
|
try:
|
|
|
|
child_response = await protocol.query(child_request)
|
|
|
|
except Exception:
|
|
|
|
_LOGGER.debug("Device does not have any children.")
|
|
|
|
else:
|
|
|
|
successes.append(
|
|
|
|
SmartCall(
|
|
|
|
module="getChildDeviceList",
|
|
|
|
request=child_request,
|
|
|
|
should_succeed=True,
|
|
|
|
child_device_id="",
|
2024-10-24 12:11:28 +00:00
|
|
|
supports_multiple=True,
|
2024-10-18 11:06:22 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
child_list = child_response["getChildDeviceList"]["child_device_list"]
|
|
|
|
for child in child_list:
|
|
|
|
child_id = child.get("device_id") or child.get("dev_id")
|
|
|
|
if not child_id:
|
|
|
|
_LOGGER.error("Could not find child device id in %s", child)
|
|
|
|
# If category is in the child device map the protocol is smart.
|
|
|
|
if (
|
|
|
|
category := child.get("category")
|
|
|
|
) and category in SmartChildDevice.CHILD_DEVICE_TYPE_MAP:
|
|
|
|
child_protocol = _ChildCameraProtocolWrapper(child_id, protocol)
|
|
|
|
try:
|
|
|
|
nego_response = await child_protocol.query({"component_nego": None})
|
|
|
|
except Exception as ex:
|
|
|
|
_LOGGER.error("Error calling component_nego: %s", ex)
|
|
|
|
continue
|
|
|
|
if "component_nego" not in nego_response:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Could not find component_nego in device response: %s",
|
|
|
|
nego_response,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
successes.append(
|
|
|
|
SmartCall(
|
|
|
|
module="component_nego",
|
|
|
|
request={"component_nego": None},
|
|
|
|
should_succeed=True,
|
|
|
|
child_device_id=child_id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
child_components = {
|
|
|
|
item["id"]: item["ver_code"]
|
|
|
|
for item in nego_response["component_nego"]["component_list"]
|
|
|
|
}
|
|
|
|
for component_id, ver_code in child_components.items():
|
|
|
|
if (
|
|
|
|
requests := get_component_requests(component_id, ver_code)
|
|
|
|
) is not None:
|
|
|
|
component_test_calls = [
|
|
|
|
SmartCall(
|
|
|
|
module=component_id,
|
|
|
|
request={key: val},
|
|
|
|
should_succeed=True,
|
|
|
|
child_device_id=child_id,
|
|
|
|
)
|
|
|
|
for key, val in requests.items()
|
|
|
|
]
|
|
|
|
test_calls.extend(component_test_calls)
|
|
|
|
else:
|
|
|
|
click.echo(f"Skipping {component_id}..", nl=False)
|
|
|
|
click.echo(click.style("UNSUPPORTED", fg="yellow"))
|
|
|
|
else: # Not a smart protocol device so assume camera protocol
|
2024-10-24 12:11:28 +00:00
|
|
|
for request in SMARTCAMERA_REQUESTS:
|
|
|
|
method = next(iter(request))
|
|
|
|
if method == "get":
|
|
|
|
method = method + "_" + next(iter(request[method]))
|
2024-10-18 11:06:22 +00:00
|
|
|
test_calls.append(
|
|
|
|
SmartCall(
|
|
|
|
module=method,
|
2024-10-24 12:11:28 +00:00
|
|
|
request=request,
|
2024-10-18 11:06:22 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id=child_id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
await protocol.close()
|
|
|
|
return test_calls, successes
|
|
|
|
|
|
|
|
|
|
|
|
async def get_smart_test_calls(protocol: SmartProtocol):
|
2024-02-28 16:04:57 +00:00
|
|
|
"""Get the list of test calls to make."""
|
|
|
|
test_calls = []
|
|
|
|
successes = []
|
|
|
|
child_device_components = {}
|
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
extra_test_calls = [
|
|
|
|
SmartCall(
|
|
|
|
module="temp_humidity_records",
|
2024-10-18 11:06:22 +00:00
|
|
|
request=SmartRequest.get_raw_request("get_temp_humidity_records").to_dict(),
|
2024-01-02 17:20:53 +00:00
|
|
|
should_succeed=False,
|
2024-02-28 16:04:57 +00:00
|
|
|
child_device_id="",
|
2024-01-02 17:20:53 +00:00
|
|
|
),
|
2023-12-02 16:33:35 +00:00
|
|
|
]
|
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
click.echo("Testing component_nego call ..", nl=False)
|
|
|
|
responses = await _make_requests_or_exit(
|
2024-10-18 11:06:22 +00:00
|
|
|
protocol,
|
|
|
|
SmartRequest.component_nego().to_dict(),
|
2024-02-28 16:04:57 +00:00
|
|
|
"component_nego call",
|
|
|
|
batch_size=1,
|
|
|
|
child_device_id="",
|
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",
|
2024-10-18 11:06:22 +00:00
|
|
|
request=SmartRequest("component_nego").to_dict(),
|
2024-01-02 17:20:53 +00:00
|
|
|
should_succeed=True,
|
2024-02-28 16:04:57 +00:00
|
|
|
child_device_id="",
|
2024-01-02 17:20:53 +00:00
|
|
|
)
|
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
components = {
|
|
|
|
item["id"]: item["ver_code"]
|
|
|
|
for item in component_info_response["component_list"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if "child_device" in components:
|
|
|
|
child_components = await _make_requests_or_exit(
|
2024-10-18 11:06:22 +00:00
|
|
|
protocol,
|
|
|
|
SmartRequest.get_child_device_component_list().to_dict(),
|
2024-02-28 16:04:57 +00:00
|
|
|
"child device component list",
|
|
|
|
batch_size=1,
|
|
|
|
child_device_id="",
|
|
|
|
)
|
|
|
|
successes.append(
|
|
|
|
SmartCall(
|
|
|
|
module="child_component_list",
|
2024-10-18 11:06:22 +00:00
|
|
|
request=SmartRequest.get_child_device_component_list().to_dict(),
|
2024-02-28 16:04:57 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id="",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
test_calls.append(
|
|
|
|
SmartCall(
|
|
|
|
module="child_device_list",
|
2024-10-18 11:06:22 +00:00
|
|
|
request=SmartRequest.get_child_device_list().to_dict(),
|
2024-02-28 16:04:57 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id="",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Get list of child components to call
|
|
|
|
if "control_child" in components:
|
|
|
|
child_device_components = {
|
|
|
|
child_component_list["device_id"]: {
|
|
|
|
item["id"]: item["ver_code"]
|
|
|
|
for item in child_component_list["component_list"]
|
|
|
|
}
|
|
|
|
for child_component_list in child_components[
|
|
|
|
"get_child_device_component_list"
|
|
|
|
]["child_component_list"]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get component calls
|
|
|
|
for component_id, ver_code in components.items():
|
|
|
|
if component_id == "child_device":
|
|
|
|
continue
|
2024-02-14 19:43:10 +00:00
|
|
|
if (requests := get_component_requests(component_id, ver_code)) is not None:
|
2024-01-02 17:20:53 +00:00
|
|
|
component_test_calls = [
|
2024-02-28 16:04:57 +00:00
|
|
|
SmartCall(
|
|
|
|
module=component_id,
|
2024-10-18 11:06:22 +00:00
|
|
|
request={key: val},
|
2024-02-28 16:04:57 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id="",
|
|
|
|
)
|
2024-10-18 11:06:22 +00:00
|
|
|
for key, val in requests.items()
|
2024-01-02 17:20:53 +00:00
|
|
|
]
|
|
|
|
test_calls.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)
|
|
|
|
|
2024-02-28 16:04:57 +00:00
|
|
|
# Child component calls
|
|
|
|
for child_device_id, child_components in child_device_components.items():
|
2024-04-22 17:17:11 +00:00
|
|
|
test_calls.append(
|
|
|
|
SmartCall(
|
|
|
|
module="component_nego",
|
2024-10-18 11:06:22 +00:00
|
|
|
request=SmartRequest("component_nego").to_dict(),
|
2024-04-22 17:17:11 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id=child_device_id,
|
|
|
|
)
|
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
for component_id, ver_code in child_components.items():
|
|
|
|
if (requests := get_component_requests(component_id, ver_code)) is not None:
|
|
|
|
component_test_calls = [
|
|
|
|
SmartCall(
|
|
|
|
module=component_id,
|
2024-10-18 11:06:22 +00:00
|
|
|
request={key: val},
|
2024-02-28 16:04:57 +00:00
|
|
|
should_succeed=True,
|
|
|
|
child_device_id=child_device_id,
|
|
|
|
)
|
2024-10-18 11:06:22 +00:00
|
|
|
for key, val in requests.items()
|
2024-02-28 16:04:57 +00:00
|
|
|
]
|
|
|
|
test_calls.extend(component_test_calls)
|
|
|
|
else:
|
|
|
|
click.echo(f"Skipping {component_id}..", nl=False)
|
|
|
|
click.echo(click.style("UNSUPPORTED", fg="yellow"))
|
|
|
|
# Add the extra calls for each child
|
|
|
|
for extra_call in extra_test_calls:
|
2024-10-24 12:11:28 +00:00
|
|
|
extra_child_call = dataclasses.replace(
|
|
|
|
extra_call, child_device_id=child_device_id
|
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
test_calls.append(extra_child_call)
|
|
|
|
|
|
|
|
return test_calls, successes
|
|
|
|
|
|
|
|
|
|
|
|
def get_smart_child_fixture(response):
|
|
|
|
"""Get a seperate fixture for the child device."""
|
|
|
|
info = response["get_device_info"]
|
|
|
|
hw_version = info["hw_ver"]
|
|
|
|
sw_version = info["fw_ver"]
|
|
|
|
sw_version = sw_version.split(" ", maxsplit=1)[0]
|
|
|
|
model = info["model"]
|
|
|
|
if region := info.get("specs"):
|
|
|
|
model += f"({region})"
|
|
|
|
|
|
|
|
save_filename = f"{model}_{hw_version}_{sw_version}.json"
|
2024-03-06 14:54:55 +00:00
|
|
|
return FixtureResult(
|
|
|
|
filename=save_filename, folder=SMART_CHILD_FOLDER, data=response
|
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
|
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
async def get_smart_fixtures(
|
|
|
|
protocol: SmartProtocol, *, discovery_info=None, batch_size: int
|
|
|
|
):
|
2024-02-28 16:04:57 +00:00
|
|
|
"""Get fixture for new TAPO style protocol."""
|
2024-10-18 11:06:22 +00:00
|
|
|
if isinstance(protocol, SmartCameraProtocol):
|
|
|
|
test_calls, successes = await get_smart_camera_test_calls(protocol)
|
|
|
|
child_wrapper: type[_ChildProtocolWrapper | _ChildCameraProtocolWrapper] = (
|
|
|
|
_ChildCameraProtocolWrapper
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
test_calls, successes = await get_smart_test_calls(protocol)
|
|
|
|
child_wrapper = _ChildProtocolWrapper
|
2024-02-28 16:04:57 +00:00
|
|
|
|
2024-01-02 17:20:53 +00:00
|
|
|
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-02-28 16:04:57 +00:00
|
|
|
if test_call.child_device_id == "":
|
2024-10-18 11:06:22 +00:00
|
|
|
response = await protocol.query(test_call.request)
|
2024-02-28 16:04:57 +00:00
|
|
|
else:
|
2024-10-18 11:06:22 +00:00
|
|
|
cp = child_wrapper(test_call.child_device_id, protocol)
|
|
|
|
response = await cp.query(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-02-28 16:04:57 +00:00
|
|
|
SmartErrorCode.UNSPECIFIC_ERROR,
|
2024-02-14 19:43:10 +00:00
|
|
|
]
|
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:
|
2024-10-18 11:06:22 +00:00
|
|
|
await protocol.close()
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2024-10-24 12:11:28 +00:00
|
|
|
device_requests: dict[str, list[SmartCall]] = {}
|
2024-02-28 16:04:57 +00:00
|
|
|
for success in successes:
|
2024-10-24 12:11:28 +00:00
|
|
|
device_request = device_requests.setdefault(success.child_device_id, [])
|
|
|
|
device_request.append(success)
|
2024-02-28 16:04:57 +00:00
|
|
|
|
|
|
|
scrubbed_device_ids = {
|
|
|
|
device_id: f"SCRUBBED_CHILD_DEVICE_ID_{index}"
|
|
|
|
for index, device_id in enumerate(device_requests.keys())
|
|
|
|
if device_id != ""
|
|
|
|
}
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2024-10-24 12:11:28 +00:00
|
|
|
final = await _make_final_calls(
|
|
|
|
protocol, device_requests[""], "All successes", batch_size, child_device_id=""
|
2024-01-23 10:33:07 +00:00
|
|
|
)
|
2024-02-28 16:04:57 +00:00
|
|
|
fixture_results = []
|
|
|
|
for child_device_id, requests in device_requests.items():
|
|
|
|
if child_device_id == "":
|
|
|
|
continue
|
2024-10-24 12:11:28 +00:00
|
|
|
response = await _make_final_calls(
|
2024-10-18 11:06:22 +00:00
|
|
|
protocol,
|
2024-02-28 16:04:57 +00:00
|
|
|
requests,
|
2024-10-24 12:11:28 +00:00
|
|
|
"All child successes",
|
2024-02-28 16:04:57 +00:00
|
|
|
batch_size,
|
|
|
|
child_device_id=child_device_id,
|
|
|
|
)
|
2024-10-24 12:11:28 +00:00
|
|
|
|
2024-02-28 16:04:57 +00:00
|
|
|
scrubbed = scrubbed_device_ids[child_device_id]
|
|
|
|
if "get_device_info" in response and "device_id" in response["get_device_info"]:
|
|
|
|
response["get_device_info"]["device_id"] = scrubbed
|
|
|
|
# If the child is a different model to the parent create a seperate fixture
|
2024-10-18 11:06:22 +00:00
|
|
|
if "get_device_info" in final:
|
|
|
|
parent_model = final["get_device_info"]["model"]
|
|
|
|
elif "getDeviceInfo" in final:
|
|
|
|
parent_model = final["getDeviceInfo"]["device_info"]["basic_info"][
|
|
|
|
"device_model"
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
raise KasaException("Cannot determine parent device model.")
|
2024-02-28 16:04:57 +00:00
|
|
|
if (
|
2024-04-22 17:17:11 +00:00
|
|
|
"component_nego" in response
|
|
|
|
and "get_device_info" in response
|
2024-02-28 16:04:57 +00:00
|
|
|
and (child_model := response["get_device_info"].get("model"))
|
2024-10-18 11:06:22 +00:00
|
|
|
and child_model != parent_model
|
2024-02-28 16:04:57 +00:00
|
|
|
):
|
|
|
|
fixture_results.append(get_smart_child_fixture(response))
|
|
|
|
else:
|
|
|
|
cd = final.setdefault("child_devices", {})
|
|
|
|
cd[scrubbed] = response
|
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
# Scrub the device ids in the parent for smart protocol
|
2024-02-28 16:04:57 +00:00
|
|
|
if gc := final.get("get_child_device_component_list"):
|
|
|
|
for child in gc["child_component_list"]:
|
|
|
|
device_id = child["device_id"]
|
|
|
|
child["device_id"] = scrubbed_device_ids[device_id]
|
|
|
|
for child in final["get_child_device_list"]["child_device_list"]:
|
|
|
|
device_id = child["device_id"]
|
|
|
|
child["device_id"] = scrubbed_device_ids[device_id]
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
# Scrub the device ids in the parent for the smart camera protocol
|
|
|
|
if gc := final.get("getChildDeviceList"):
|
|
|
|
for child in gc["child_device_list"]:
|
|
|
|
if device_id := child.get("device_id"):
|
|
|
|
child["device_id"] = scrubbed_device_ids[device_id]
|
|
|
|
continue
|
|
|
|
if device_id := child.get("dev_id"):
|
|
|
|
child["dev_id"] = scrubbed_device_ids[device_id]
|
|
|
|
continue
|
|
|
|
_LOGGER.error("Could not find a device for the child device: %s", child)
|
|
|
|
|
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.
|
2024-10-18 11:06:22 +00:00
|
|
|
if discovery_info:
|
|
|
|
dr = DiscoveryResult(**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))
|
|
|
|
|
2024-10-18 11:06:22 +00:00
|
|
|
if "get_device_info" in final:
|
2024-10-24 12:11:28 +00:00
|
|
|
# smart protocol
|
2024-10-18 11:06:22 +00:00
|
|
|
hw_version = final["get_device_info"]["hw_ver"]
|
|
|
|
sw_version = final["get_device_info"]["fw_ver"]
|
|
|
|
if discovery_info:
|
|
|
|
model = discovery_info["device_model"]
|
|
|
|
else:
|
|
|
|
model = final["get_device_info"]["model"] + "(XX)"
|
|
|
|
sw_version = sw_version.split(" ", maxsplit=1)[0]
|
2024-10-24 12:11:28 +00:00
|
|
|
copy_folder = SMART_FOLDER
|
2024-10-18 11:06:22 +00:00
|
|
|
else:
|
2024-10-24 12:11:28 +00:00
|
|
|
# smart camera protocol
|
2024-10-28 12:47:24 +00:00
|
|
|
basic_info = final["getDeviceInfo"]["device_info"]["basic_info"]
|
|
|
|
hw_version = basic_info["hw_version"]
|
|
|
|
sw_version = basic_info["sw_version"]
|
|
|
|
model = basic_info["device_model"]
|
|
|
|
region = basic_info.get("region")
|
2024-10-18 11:06:22 +00:00
|
|
|
sw_version = sw_version.split(" ", maxsplit=1)[0]
|
2024-10-28 12:47:24 +00:00
|
|
|
if region is not None:
|
|
|
|
model = f"{model}({region})"
|
2024-10-24 12:11:28 +00:00
|
|
|
copy_folder = SMARTCAMERA_FOLDER
|
2023-12-02 16:33:35 +00:00
|
|
|
|
2023-12-05 15:45:09 +00:00
|
|
|
save_filename = f"{model}_{hw_version}_{sw_version}.json"
|
2024-10-24 12:11:28 +00:00
|
|
|
|
2024-02-28 16:04:57 +00:00
|
|
|
fixture_results.insert(
|
|
|
|
0, FixtureResult(filename=save_filename, folder=copy_folder, data=final)
|
|
|
|
)
|
|
|
|
return fixture_results
|
2021-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|