2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-18 14:53:11 +00:00
|
|
|
import copy
|
2024-02-27 17:39:04 +00:00
|
|
|
import glob
|
|
|
|
import json
|
|
|
|
import os
|
2024-11-18 18:46:36 +00:00
|
|
|
from collections.abc import Iterable
|
2024-02-27 17:39:04 +00:00
|
|
|
from pathlib import Path
|
2024-11-18 18:46:36 +00:00
|
|
|
from typing import NamedTuple
|
2024-02-27 17:39:04 +00:00
|
|
|
|
2024-11-18 14:53:11 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-03-06 16:18:52 +00:00
|
|
|
from kasa.device_type import DeviceType
|
2024-11-18 14:53:11 +00:00
|
|
|
from kasa.iot import IotDevice
|
2024-03-06 16:18:52 +00:00
|
|
|
from kasa.smart.smartdevice import SmartDevice
|
2024-11-23 08:07:47 +00:00
|
|
|
from kasa.smartcam import SmartCamDevice
|
2024-03-06 16:18:52 +00:00
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
class FixtureInfo(NamedTuple):
|
|
|
|
name: str
|
|
|
|
protocol: str
|
2024-04-17 13:39:24 +00:00
|
|
|
data: dict
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
class ComponentFilter(NamedTuple):
|
|
|
|
component_name: str
|
|
|
|
minimum_version: int = 0
|
|
|
|
maximum_version: int | None = None
|
|
|
|
|
|
|
|
|
2024-04-16 18:21:20 +00:00
|
|
|
FixtureInfo.__hash__ = lambda self: hash((self.name, self.protocol)) # type: ignore[attr-defined, method-assign]
|
2024-02-27 17:39:04 +00:00
|
|
|
FixtureInfo.__eq__ = lambda x, y: hash(x) == hash(y) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
|
|
SUPPORTED_IOT_DEVICES = [
|
|
|
|
(device, "IOT")
|
|
|
|
for device in glob.glob(
|
2024-11-22 20:19:33 +00:00
|
|
|
os.path.dirname(os.path.abspath(__file__)) + "/fixtures/iot/*.json"
|
2024-02-27 17:39:04 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
SUPPORTED_SMART_DEVICES = [
|
|
|
|
(device, "SMART")
|
|
|
|
for device in glob.glob(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)) + "/fixtures/smart/*.json"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2024-03-06 14:54:55 +00:00
|
|
|
SUPPORTED_SMART_CHILD_DEVICES = [
|
|
|
|
(device, "SMART.CHILD")
|
|
|
|
for device in glob.glob(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)) + "/fixtures/smart/child/*.json"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2024-11-23 08:07:47 +00:00
|
|
|
SUPPORTED_SMARTCAM_DEVICES = [
|
|
|
|
(device, "SMARTCAM")
|
2024-10-24 08:36:18 +00:00
|
|
|
for device in glob.glob(
|
2024-11-23 08:07:47 +00:00
|
|
|
os.path.dirname(os.path.abspath(__file__)) + "/fixtures/smartcam/*.json"
|
2024-10-24 08:36:18 +00:00
|
|
|
)
|
|
|
|
]
|
2024-02-27 17:39:04 +00:00
|
|
|
|
2024-03-06 14:54:55 +00:00
|
|
|
SUPPORTED_DEVICES = (
|
2024-10-24 08:36:18 +00:00
|
|
|
SUPPORTED_IOT_DEVICES
|
|
|
|
+ SUPPORTED_SMART_DEVICES
|
|
|
|
+ SUPPORTED_SMART_CHILD_DEVICES
|
2024-11-23 08:07:47 +00:00
|
|
|
+ SUPPORTED_SMARTCAM_DEVICES
|
2024-03-06 14:54:55 +00:00
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def idgenerator(paramtuple: FixtureInfo):
|
|
|
|
try:
|
|
|
|
return paramtuple.name + (
|
|
|
|
"" if paramtuple.protocol == "IOT" else "-" + paramtuple.protocol
|
|
|
|
)
|
|
|
|
except: # TODO: HACK as idgenerator is now used by default # noqa: E722
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def get_fixture_info() -> list[FixtureInfo]:
|
2024-02-27 17:39:04 +00:00
|
|
|
"""Return raw discovery file contents as JSON. Used for discovery tests."""
|
|
|
|
fixture_data = []
|
|
|
|
for file, protocol in SUPPORTED_DEVICES:
|
|
|
|
p = Path(file)
|
|
|
|
folder = Path(__file__).parent / "fixtures"
|
|
|
|
if protocol == "SMART":
|
|
|
|
folder = folder / "smart"
|
2024-03-06 14:54:55 +00:00
|
|
|
if protocol == "SMART.CHILD":
|
|
|
|
folder = folder / "smart/child"
|
2024-02-27 17:39:04 +00:00
|
|
|
p = folder / file
|
|
|
|
|
|
|
|
with open(p) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
fixture_name = p.name
|
|
|
|
fixture_data.append(
|
|
|
|
FixtureInfo(data=data, protocol=protocol, name=fixture_name)
|
|
|
|
)
|
|
|
|
return fixture_data
|
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
FIXTURE_DATA: list[FixtureInfo] = get_fixture_info()
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filter_fixtures(
|
|
|
|
desc,
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
data_root_filter: str | None = None,
|
|
|
|
protocol_filter: set[str] | None = None,
|
|
|
|
model_filter: set[str] | None = None,
|
2024-11-11 17:41:31 +00:00
|
|
|
model_startswith_filter: str | None = None,
|
2024-06-27 17:52:54 +00:00
|
|
|
component_filter: str | ComponentFilter | None = None,
|
2024-10-24 08:36:18 +00:00
|
|
|
device_type_filter: Iterable[DeviceType] | None = None,
|
2024-11-11 17:41:31 +00:00
|
|
|
fixture_list: list[FixtureInfo] = FIXTURE_DATA,
|
2024-02-27 17:39:04 +00:00
|
|
|
):
|
|
|
|
"""Filter the fixtures based on supplied parameters.
|
|
|
|
|
|
|
|
data_root_filter: return fixtures containing the supplied top
|
|
|
|
level key, i.e. discovery_result
|
2024-03-06 18:04:09 +00:00
|
|
|
protocol_filter: set of protocols to match, IOT, SMART, SMART.CHILD
|
2024-02-27 17:39:04 +00:00
|
|
|
model_filter: set of device models to match
|
|
|
|
component_filter: filter SMART fixtures that have the provided
|
|
|
|
component in component_nego details.
|
|
|
|
"""
|
|
|
|
|
2024-10-24 08:36:18 +00:00
|
|
|
def _model_match(fixture_data: FixtureInfo, model_filter: set[str]):
|
2024-10-27 12:08:02 +00:00
|
|
|
if isinstance(model_filter, str):
|
|
|
|
model_filter = {model_filter}
|
|
|
|
assert isinstance(model_filter, set), "model filter must be a set"
|
2024-10-24 08:36:18 +00:00
|
|
|
model_filter_list = [mf for mf in model_filter]
|
2024-10-27 12:08:02 +00:00
|
|
|
if (
|
|
|
|
len(model_filter_list) == 1
|
|
|
|
and (model := model_filter_list[0])
|
|
|
|
and len(model.split("_")) == 3
|
|
|
|
):
|
2024-11-11 17:41:31 +00:00
|
|
|
# filter string includes hw and fw, return exact match
|
2024-10-27 12:08:02 +00:00
|
|
|
return fixture_data.name == f"{model}.json"
|
2024-02-27 17:39:04 +00:00
|
|
|
file_model_region = fixture_data.name.split("_")[0]
|
|
|
|
file_model = file_model_region.split("(")[0]
|
|
|
|
return file_model in model_filter
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
def _model_startswith_match(fixture_data: FixtureInfo, starts_with: str):
|
|
|
|
return fixture_data.name.startswith(starts_with)
|
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
def _component_match(
|
|
|
|
fixture_data: FixtureInfo, component_filter: str | ComponentFilter
|
|
|
|
):
|
2024-02-27 17:39:04 +00:00
|
|
|
if (component_nego := fixture_data.data.get("component_nego")) is None:
|
|
|
|
return False
|
|
|
|
components = {
|
|
|
|
component["id"]: component["ver_code"]
|
|
|
|
for component in component_nego["component_list"]
|
|
|
|
}
|
2024-06-27 17:52:54 +00:00
|
|
|
if isinstance(component_filter, str):
|
|
|
|
return component_filter in components
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
(ver_code := components.get(component_filter.component_name))
|
|
|
|
and ver_code >= component_filter.minimum_version
|
|
|
|
and (
|
|
|
|
component_filter.maximum_version is None
|
|
|
|
or ver_code <= component_filter.maximum_version
|
|
|
|
)
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
|
2024-03-06 16:18:52 +00:00
|
|
|
def _device_type_match(fixture_data: FixtureInfo, device_type):
|
2024-10-24 08:36:18 +00:00
|
|
|
if fixture_data.protocol in {"SMART", "SMART.CHILD"}:
|
|
|
|
info = fixture_data.data["get_device_info"]
|
|
|
|
component_nego = fixture_data.data["component_nego"]
|
|
|
|
components = [
|
|
|
|
component["id"] for component in component_nego["component_list"]
|
|
|
|
]
|
2024-03-06 16:18:52 +00:00
|
|
|
return (
|
2024-10-24 08:36:18 +00:00
|
|
|
SmartDevice._get_device_type_from_components(components, info["type"])
|
2024-03-06 16:18:52 +00:00
|
|
|
in device_type
|
|
|
|
)
|
2024-10-24 08:36:18 +00:00
|
|
|
elif fixture_data.protocol == "IOT":
|
2024-11-18 14:53:11 +00:00
|
|
|
return (
|
|
|
|
IotDevice._get_device_type_from_sys_info(fixture_data.data)
|
|
|
|
in device_type
|
|
|
|
)
|
2024-11-23 08:07:47 +00:00
|
|
|
elif fixture_data.protocol == "SMARTCAM":
|
2024-10-24 08:36:18 +00:00
|
|
|
info = fixture_data.data["getDeviceInfo"]["device_info"]["basic_info"]
|
2024-11-23 08:07:47 +00:00
|
|
|
return SmartCamDevice._get_device_type_from_sysinfo(info) in device_type
|
2024-03-06 16:18:52 +00:00
|
|
|
return False
|
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
filtered = []
|
|
|
|
if protocol_filter is None:
|
2024-11-23 08:07:47 +00:00
|
|
|
protocol_filter = {"IOT", "SMART", "SMARTCAM"}
|
2024-11-11 17:41:31 +00:00
|
|
|
for fixture_data in fixture_list:
|
2024-02-27 17:39:04 +00:00
|
|
|
if data_root_filter and data_root_filter not in fixture_data.data:
|
|
|
|
continue
|
|
|
|
if fixture_data.protocol not in protocol_filter:
|
|
|
|
continue
|
|
|
|
if model_filter is not None and not _model_match(fixture_data, model_filter):
|
|
|
|
continue
|
2024-11-11 17:41:31 +00:00
|
|
|
if model_startswith_filter is not None and not _model_startswith_match(
|
|
|
|
fixture_data, model_startswith_filter
|
|
|
|
):
|
|
|
|
continue
|
2024-02-27 17:39:04 +00:00
|
|
|
if component_filter and not _component_match(fixture_data, component_filter):
|
|
|
|
continue
|
2024-03-06 16:18:52 +00:00
|
|
|
if device_type_filter and not _device_type_match(
|
|
|
|
fixture_data, device_type_filter
|
|
|
|
):
|
|
|
|
continue
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
filtered.append(fixture_data)
|
|
|
|
|
2024-03-11 10:17:12 +00:00
|
|
|
filtered.sort()
|
2024-02-27 17:39:04 +00:00
|
|
|
return filtered
|
2024-11-18 14:53:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(
|
|
|
|
params=filter_fixtures("all fixture infos"),
|
|
|
|
ids=idgenerator,
|
|
|
|
)
|
|
|
|
def fixture_info(request, mocker):
|
|
|
|
"""Return raw discovery file contents as JSON. Used for discovery tests."""
|
|
|
|
fixture_info = request.param
|
|
|
|
fixture_data = copy.deepcopy(fixture_info.data)
|
|
|
|
return FixtureInfo(fixture_info.name, fixture_info.protocol, fixture_data)
|