2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
import os
|
2024-06-19 18:24:12 +00:00
|
|
|
from collections.abc import AsyncGenerator
|
2024-05-02 13:32:06 +00:00
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from kasa import (
|
|
|
|
Credentials,
|
|
|
|
Device,
|
2024-03-06 16:18:52 +00:00
|
|
|
DeviceType,
|
2024-02-27 17:39:04 +00:00
|
|
|
Discover,
|
|
|
|
)
|
2024-03-01 18:32:45 +00:00
|
|
|
from kasa.iot import IotBulb, IotDimmer, IotLightStrip, IotPlug, IotStrip, IotWallSwitch
|
2024-04-29 17:19:44 +00:00
|
|
|
from kasa.smart import SmartDevice
|
2024-11-23 08:07:47 +00:00
|
|
|
from kasa.smartcam import SmartCamDevice
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
from .fakeprotocol_iot import FakeIotProtocol
|
|
|
|
from .fakeprotocol_smart import FakeSmartProtocol
|
2024-11-23 08:07:47 +00:00
|
|
|
from .fakeprotocol_smartcam import FakeSmartCamProtocol
|
2024-06-27 17:52:54 +00:00
|
|
|
from .fixtureinfo import (
|
|
|
|
FIXTURE_DATA,
|
|
|
|
ComponentFilter,
|
|
|
|
FixtureInfo,
|
|
|
|
filter_fixtures,
|
|
|
|
idgenerator,
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
# Tapo bulbs
|
|
|
|
BULBS_SMART_VARIABLE_TEMP = {"L530E", "L930-5"}
|
|
|
|
BULBS_SMART_LIGHT_STRIP = {"L900-5", "L900-10", "L920-5", "L930-5"}
|
|
|
|
BULBS_SMART_COLOR = {"L530E", *BULBS_SMART_LIGHT_STRIP}
|
|
|
|
BULBS_SMART_DIMMABLE = {"L510B", "L510E"}
|
|
|
|
BULBS_SMART = (
|
|
|
|
BULBS_SMART_VARIABLE_TEMP.union(BULBS_SMART_COLOR)
|
|
|
|
.union(BULBS_SMART_DIMMABLE)
|
|
|
|
.union(BULBS_SMART_LIGHT_STRIP)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Kasa (IOT-prefixed) bulbs
|
|
|
|
BULBS_IOT_LIGHT_STRIP = {"KL400L5", "KL430", "KL420L5"}
|
|
|
|
BULBS_IOT_VARIABLE_TEMP = {
|
|
|
|
"LB120",
|
|
|
|
"LB130",
|
|
|
|
"KL120",
|
|
|
|
"KL125",
|
|
|
|
"KL130",
|
|
|
|
"KL135",
|
|
|
|
"KL430",
|
|
|
|
}
|
|
|
|
BULBS_IOT_COLOR = {"LB130", "KL125", "KL130", "KL135", *BULBS_IOT_LIGHT_STRIP}
|
|
|
|
BULBS_IOT_DIMMABLE = {"KL50", "KL60", "LB100", "LB110", "KL110"}
|
|
|
|
BULBS_IOT = (
|
|
|
|
BULBS_IOT_VARIABLE_TEMP.union(BULBS_IOT_COLOR)
|
|
|
|
.union(BULBS_IOT_DIMMABLE)
|
|
|
|
.union(BULBS_IOT_LIGHT_STRIP)
|
|
|
|
)
|
|
|
|
|
|
|
|
BULBS_VARIABLE_TEMP = {*BULBS_SMART_VARIABLE_TEMP, *BULBS_IOT_VARIABLE_TEMP}
|
|
|
|
BULBS_COLOR = {*BULBS_SMART_COLOR, *BULBS_IOT_COLOR}
|
|
|
|
|
|
|
|
|
|
|
|
LIGHT_STRIPS = {*BULBS_SMART_LIGHT_STRIP, *BULBS_IOT_LIGHT_STRIP}
|
|
|
|
BULBS = {
|
|
|
|
*BULBS_IOT,
|
|
|
|
*BULBS_SMART,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PLUGS_IOT = {
|
|
|
|
"HS100",
|
|
|
|
"HS103",
|
|
|
|
"HS105",
|
|
|
|
"HS110",
|
|
|
|
"EP10",
|
|
|
|
"KP100",
|
|
|
|
"KP105",
|
|
|
|
"KP115",
|
|
|
|
"KP125",
|
|
|
|
"KP401",
|
|
|
|
}
|
|
|
|
# P135 supports dimming, but its not currently support
|
|
|
|
# by the library
|
|
|
|
PLUGS_SMART = {
|
|
|
|
"P100",
|
|
|
|
"P110",
|
2024-11-23 12:30:39 +00:00
|
|
|
"P110M",
|
2024-06-04 17:18:23 +00:00
|
|
|
"P115",
|
2024-02-27 17:39:04 +00:00
|
|
|
"KP125M",
|
|
|
|
"EP25",
|
|
|
|
"P125M",
|
|
|
|
"TP15",
|
|
|
|
}
|
|
|
|
PLUGS = {
|
|
|
|
*PLUGS_IOT,
|
|
|
|
*PLUGS_SMART,
|
|
|
|
}
|
2024-03-01 18:32:45 +00:00
|
|
|
SWITCHES_IOT = {
|
|
|
|
"HS200",
|
|
|
|
"HS210",
|
|
|
|
"KS200M",
|
|
|
|
}
|
|
|
|
SWITCHES_SMART = {
|
2024-11-23 12:31:27 +00:00
|
|
|
"HS200",
|
2024-03-01 18:32:45 +00:00
|
|
|
"KS205",
|
|
|
|
"KS225",
|
2024-04-24 18:17:49 +00:00
|
|
|
"KS240",
|
2024-03-01 18:32:45 +00:00
|
|
|
"S500D",
|
|
|
|
"S505",
|
2024-06-03 16:52:15 +00:00
|
|
|
"S505D",
|
2024-03-01 18:32:45 +00:00
|
|
|
}
|
|
|
|
SWITCHES = {*SWITCHES_IOT, *SWITCHES_SMART}
|
2024-02-27 17:39:04 +00:00
|
|
|
STRIPS_IOT = {"HS107", "HS300", "KP303", "KP200", "KP400", "EP40"}
|
2024-11-09 00:47:56 +00:00
|
|
|
STRIPS_SMART = {"P300", "P304M", "TP25", "EP40M"}
|
2024-02-27 17:39:04 +00:00
|
|
|
STRIPS = {*STRIPS_IOT, *STRIPS_SMART}
|
|
|
|
|
2024-11-09 01:50:21 +00:00
|
|
|
DIMMERS_IOT = {"ES20M", "HS220", "KS220", "KS220M", "KS230", "KP405"}
|
2024-07-07 12:22:43 +00:00
|
|
|
DIMMERS_SMART = {"HS220", "KS225", "S500D", "P135"}
|
2024-02-27 17:39:04 +00:00
|
|
|
DIMMERS = {
|
|
|
|
*DIMMERS_IOT,
|
|
|
|
*DIMMERS_SMART,
|
|
|
|
}
|
|
|
|
|
2024-04-22 14:24:15 +00:00
|
|
|
HUBS_SMART = {"H100", "KH100"}
|
2024-10-29 09:37:34 +00:00
|
|
|
SENSORS_SMART = {"T310", "T315", "T300", "T100", "T110", "S200B", "S200D"}
|
2024-04-22 14:24:15 +00:00
|
|
|
THERMOSTATS_SMART = {"KE100"}
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
WITH_EMETER_IOT = {"HS110", "HS300", "KP115", "KP125", *BULBS_IOT}
|
2024-11-23 12:30:39 +00:00
|
|
|
WITH_EMETER_SMART = {"P110", "P110M", "P115", "KP125M", "EP25", "P304M"}
|
2024-02-27 17:39:04 +00:00
|
|
|
WITH_EMETER = {*WITH_EMETER_IOT, *WITH_EMETER_SMART}
|
|
|
|
|
|
|
|
DIMMABLE = {*BULBS, *DIMMERS}
|
|
|
|
|
2024-03-01 18:32:45 +00:00
|
|
|
ALL_DEVICES_IOT = (
|
|
|
|
BULBS_IOT.union(PLUGS_IOT).union(STRIPS_IOT).union(DIMMERS_IOT).union(SWITCHES_IOT)
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
ALL_DEVICES_SMART = (
|
|
|
|
BULBS_SMART.union(PLUGS_SMART)
|
|
|
|
.union(STRIPS_SMART)
|
|
|
|
.union(DIMMERS_SMART)
|
|
|
|
.union(HUBS_SMART)
|
2024-03-06 18:04:09 +00:00
|
|
|
.union(SENSORS_SMART)
|
2024-03-01 18:32:45 +00:00
|
|
|
.union(SWITCHES_SMART)
|
2024-04-22 14:24:15 +00:00
|
|
|
.union(THERMOSTATS_SMART)
|
2024-02-27 17:39:04 +00:00
|
|
|
)
|
|
|
|
ALL_DEVICES = ALL_DEVICES_IOT.union(ALL_DEVICES_SMART)
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
IP_FIXTURE_CACHE: dict[str, FixtureInfo] = {}
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def parametrize_combine(parametrized: list[pytest.MarkDecorator]):
|
2024-03-06 16:18:52 +00:00
|
|
|
"""Combine multiple pytest parametrize dev marks into one set of fixtures."""
|
|
|
|
fixtures = set()
|
|
|
|
for param in parametrized:
|
|
|
|
if param.args[0] != "dev":
|
|
|
|
raise Exception(f"Supplied mark is not for dev fixture: {param.args[0]}")
|
|
|
|
fixtures.update(param.args[1])
|
|
|
|
return pytest.mark.parametrize(
|
|
|
|
"dev",
|
|
|
|
sorted(list(fixtures)),
|
|
|
|
indirect=True,
|
|
|
|
ids=idgenerator,
|
|
|
|
)
|
2024-06-10 05:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parametrize_subtract(params: pytest.MarkDecorator, subtract: pytest.MarkDecorator):
|
|
|
|
"""Combine multiple pytest parametrize dev marks into one set of fixtures."""
|
|
|
|
if params.args[0] != "dev" or subtract.args[0] != "dev":
|
|
|
|
raise Exception(
|
|
|
|
f"Supplied mark is not for dev fixture: {params.args[0]} {subtract.args[0]}"
|
|
|
|
)
|
|
|
|
fixtures = []
|
|
|
|
for param in params.args[1]:
|
|
|
|
if param not in subtract.args[1]:
|
|
|
|
fixtures.append(param)
|
|
|
|
return pytest.mark.parametrize(
|
|
|
|
"dev",
|
|
|
|
sorted(fixtures),
|
|
|
|
indirect=True,
|
|
|
|
ids=idgenerator,
|
|
|
|
)
|
2024-03-06 16:18:52 +00:00
|
|
|
|
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
def parametrize(
|
|
|
|
desc,
|
|
|
|
*,
|
|
|
|
model_filter=None,
|
|
|
|
protocol_filter=None,
|
2024-06-27 17:52:54 +00:00
|
|
|
component_filter: str | ComponentFilter | None = None,
|
2024-02-27 17:39:04 +00:00
|
|
|
data_root_filter=None,
|
2024-03-06 16:18:52 +00:00
|
|
|
device_type_filter=None,
|
2024-02-27 17:39:04 +00:00
|
|
|
ids=None,
|
2024-11-18 14:53:11 +00:00
|
|
|
fixture_name="dev",
|
2024-02-27 17:39:04 +00:00
|
|
|
):
|
|
|
|
if ids is None:
|
|
|
|
ids = idgenerator
|
|
|
|
return pytest.mark.parametrize(
|
2024-11-18 14:53:11 +00:00
|
|
|
fixture_name,
|
2024-02-27 17:39:04 +00:00
|
|
|
filter_fixtures(
|
|
|
|
desc,
|
|
|
|
model_filter=model_filter,
|
|
|
|
protocol_filter=protocol_filter,
|
|
|
|
component_filter=component_filter,
|
|
|
|
data_root_filter=data_root_filter,
|
2024-03-06 16:18:52 +00:00
|
|
|
device_type_filter=device_type_filter,
|
2024-02-27 17:39:04 +00:00
|
|
|
),
|
|
|
|
indirect=True,
|
|
|
|
ids=ids,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
has_emeter = parametrize(
|
|
|
|
"has emeter", model_filter=WITH_EMETER, protocol_filter={"SMART", "IOT"}
|
|
|
|
)
|
|
|
|
no_emeter = parametrize(
|
|
|
|
"no emeter",
|
|
|
|
model_filter=ALL_DEVICES - WITH_EMETER,
|
|
|
|
protocol_filter={"SMART", "IOT"},
|
|
|
|
)
|
2024-11-28 16:56:20 +00:00
|
|
|
has_emeter_smart = parametrize(
|
|
|
|
"has emeter smart", model_filter=WITH_EMETER_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
has_emeter_iot = parametrize(
|
|
|
|
"has emeter iot", model_filter=WITH_EMETER_IOT, protocol_filter={"IOT"}
|
|
|
|
)
|
|
|
|
no_emeter_iot = parametrize(
|
|
|
|
"no emeter iot",
|
|
|
|
model_filter=ALL_DEVICES_IOT - WITH_EMETER_IOT,
|
|
|
|
protocol_filter={"IOT"},
|
|
|
|
)
|
|
|
|
|
2024-03-01 18:32:45 +00:00
|
|
|
plug = parametrize("plugs", model_filter=PLUGS, protocol_filter={"IOT", "SMART"})
|
|
|
|
plug_iot = parametrize("plugs iot", model_filter=PLUGS, protocol_filter={"IOT"})
|
|
|
|
wallswitch = parametrize(
|
|
|
|
"wall switches", model_filter=SWITCHES, protocol_filter={"IOT", "SMART"}
|
|
|
|
)
|
|
|
|
wallswitch_iot = parametrize(
|
|
|
|
"wall switches iot", model_filter=SWITCHES, protocol_filter={"IOT"}
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
strip = parametrize("strips", model_filter=STRIPS, protocol_filter={"SMART", "IOT"})
|
2024-05-13 16:34:44 +00:00
|
|
|
dimmer_iot = parametrize("dimmers", model_filter=DIMMERS, protocol_filter={"IOT"})
|
|
|
|
lightstrip_iot = parametrize(
|
2024-02-27 17:39:04 +00:00
|
|
|
"lightstrips", model_filter=LIGHT_STRIPS, protocol_filter={"IOT"}
|
|
|
|
)
|
|
|
|
|
|
|
|
# bulb types
|
2024-05-13 16:34:44 +00:00
|
|
|
dimmable_iot = parametrize("dimmable", model_filter=DIMMABLE, protocol_filter={"IOT"})
|
|
|
|
non_dimmable_iot = parametrize(
|
2024-02-27 17:39:04 +00:00
|
|
|
"non-dimmable", model_filter=BULBS - DIMMABLE, protocol_filter={"IOT"}
|
|
|
|
)
|
|
|
|
variable_temp = parametrize(
|
|
|
|
"variable color temp",
|
|
|
|
model_filter=BULBS_VARIABLE_TEMP,
|
|
|
|
protocol_filter={"SMART", "IOT"},
|
|
|
|
)
|
|
|
|
non_variable_temp = parametrize(
|
|
|
|
"non-variable color temp",
|
|
|
|
model_filter=BULBS - BULBS_VARIABLE_TEMP,
|
|
|
|
protocol_filter={"SMART", "IOT"},
|
|
|
|
)
|
|
|
|
color_bulb = parametrize(
|
|
|
|
"color bulbs", model_filter=BULBS_COLOR, protocol_filter={"SMART", "IOT"}
|
|
|
|
)
|
|
|
|
non_color_bulb = parametrize(
|
|
|
|
"non-color bulbs",
|
|
|
|
model_filter=BULBS - BULBS_COLOR,
|
|
|
|
protocol_filter={"SMART", "IOT"},
|
|
|
|
)
|
|
|
|
|
|
|
|
color_bulb_iot = parametrize(
|
|
|
|
"color bulbs iot", model_filter=BULBS_IOT_COLOR, protocol_filter={"IOT"}
|
|
|
|
)
|
|
|
|
variable_temp_iot = parametrize(
|
|
|
|
"variable color temp iot",
|
|
|
|
model_filter=BULBS_IOT_VARIABLE_TEMP,
|
|
|
|
protocol_filter={"IOT"},
|
|
|
|
)
|
2024-04-20 15:18:35 +00:00
|
|
|
variable_temp_smart = parametrize(
|
|
|
|
"variable color temp smart",
|
|
|
|
model_filter=BULBS_SMART_VARIABLE_TEMP,
|
|
|
|
protocol_filter={"SMART"},
|
|
|
|
)
|
2024-03-06 16:18:52 +00:00
|
|
|
|
|
|
|
bulb_smart = parametrize(
|
|
|
|
"bulb devices smart",
|
|
|
|
device_type_filter=[DeviceType.Bulb, DeviceType.LightStrip],
|
|
|
|
protocol_filter={"SMART"},
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
bulb_iot = parametrize(
|
|
|
|
"bulb devices iot", model_filter=BULBS_IOT, protocol_filter={"IOT"}
|
|
|
|
)
|
2024-03-06 16:18:52 +00:00
|
|
|
bulb = parametrize_combine([bulb_smart, bulb_iot])
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
strip_iot = parametrize(
|
|
|
|
"strip devices iot", model_filter=STRIPS_IOT, protocol_filter={"IOT"}
|
|
|
|
)
|
|
|
|
strip_smart = parametrize(
|
|
|
|
"strip devices smart", model_filter=STRIPS_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
|
|
|
|
|
|
|
plug_smart = parametrize(
|
|
|
|
"plug devices smart", model_filter=PLUGS_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
2024-03-01 18:32:45 +00:00
|
|
|
switch_smart = parametrize(
|
|
|
|
"switch devices smart", model_filter=SWITCHES_SMART, protocol_filter={"SMART"}
|
2024-02-27 17:39:04 +00:00
|
|
|
)
|
|
|
|
dimmers_smart = parametrize(
|
|
|
|
"dimmer devices smart", model_filter=DIMMERS_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
|
|
|
hubs_smart = parametrize(
|
|
|
|
"hubs smart", model_filter=HUBS_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
2024-03-06 18:04:09 +00:00
|
|
|
sensors_smart = parametrize(
|
|
|
|
"sensors smart", model_filter=SENSORS_SMART, protocol_filter={"SMART.CHILD"}
|
|
|
|
)
|
2024-04-22 14:24:15 +00:00
|
|
|
thermostats_smart = parametrize(
|
|
|
|
"thermostats smart", model_filter=THERMOSTATS_SMART, protocol_filter={"SMART.CHILD"}
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
device_smart = parametrize(
|
|
|
|
"devices smart", model_filter=ALL_DEVICES_SMART, protocol_filter={"SMART"}
|
|
|
|
)
|
|
|
|
device_iot = parametrize(
|
|
|
|
"devices iot", model_filter=ALL_DEVICES_IOT, protocol_filter={"IOT"}
|
|
|
|
)
|
2024-11-23 08:07:47 +00:00
|
|
|
device_smartcam = parametrize("devices smartcam", protocol_filter={"SMARTCAM"})
|
|
|
|
camera_smartcam = parametrize(
|
|
|
|
"camera smartcam",
|
2024-10-24 08:36:18 +00:00
|
|
|
device_type_filter=[DeviceType.Camera],
|
2024-11-23 08:07:47 +00:00
|
|
|
protocol_filter={"SMARTCAM"},
|
2024-10-24 08:36:18 +00:00
|
|
|
)
|
2024-11-23 08:07:47 +00:00
|
|
|
hub_smartcam = parametrize(
|
|
|
|
"hub smartcam",
|
2024-10-24 08:36:18 +00:00
|
|
|
device_type_filter=[DeviceType.Hub],
|
2024-11-23 08:07:47 +00:00
|
|
|
protocol_filter={"SMARTCAM"},
|
2024-10-24 08:36:18 +00:00
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_categories():
|
|
|
|
"""Check that every fixture file is categorized."""
|
|
|
|
categorized_fixtures = set(
|
2024-05-13 16:34:44 +00:00
|
|
|
dimmer_iot.args[1]
|
2024-02-27 17:39:04 +00:00
|
|
|
+ strip.args[1]
|
|
|
|
+ plug.args[1]
|
|
|
|
+ bulb.args[1]
|
2024-03-01 18:32:45 +00:00
|
|
|
+ wallswitch.args[1]
|
2024-05-13 16:34:44 +00:00
|
|
|
+ lightstrip_iot.args[1]
|
2024-02-27 17:39:04 +00:00
|
|
|
+ bulb_smart.args[1]
|
|
|
|
+ dimmers_smart.args[1]
|
|
|
|
+ hubs_smart.args[1]
|
2024-03-06 18:04:09 +00:00
|
|
|
+ sensors_smart.args[1]
|
2024-04-22 14:24:15 +00:00
|
|
|
+ thermostats_smart.args[1]
|
2024-11-23 08:07:47 +00:00
|
|
|
+ camera_smartcam.args[1]
|
|
|
|
+ hub_smartcam.args[1]
|
2024-02-27 17:39:04 +00:00
|
|
|
)
|
2024-04-17 13:39:24 +00:00
|
|
|
diffs: set[FixtureInfo] = set(FIXTURE_DATA) - set(categorized_fixtures)
|
2024-02-27 17:39:04 +00:00
|
|
|
if diffs:
|
|
|
|
print(diffs)
|
|
|
|
for diff in diffs:
|
|
|
|
print(
|
|
|
|
f"No category for file {diff.name} protocol {diff.protocol}, add to the corresponding set (BULBS, PLUGS, ..)"
|
|
|
|
)
|
|
|
|
raise Exception(f"Missing category for {diff.name}")
|
|
|
|
|
|
|
|
|
|
|
|
check_categories()
|
|
|
|
|
|
|
|
|
|
|
|
def device_for_fixture_name(model, protocol):
|
2024-10-24 08:36:18 +00:00
|
|
|
if protocol in {"SMART", "SMART.CHILD"}:
|
2024-04-29 17:19:44 +00:00
|
|
|
return SmartDevice
|
2024-11-23 08:07:47 +00:00
|
|
|
elif protocol == "SMARTCAM":
|
|
|
|
return SmartCamDevice
|
2024-02-27 17:39:04 +00:00
|
|
|
else:
|
|
|
|
for d in STRIPS_IOT:
|
|
|
|
if d in model:
|
|
|
|
return IotStrip
|
|
|
|
|
|
|
|
for d in PLUGS_IOT:
|
|
|
|
if d in model:
|
|
|
|
return IotPlug
|
2024-03-01 18:32:45 +00:00
|
|
|
for d in SWITCHES_IOT:
|
|
|
|
if d in model:
|
|
|
|
return IotWallSwitch
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
# Light strips are recognized also as bulbs, so this has to go first
|
|
|
|
for d in BULBS_IOT_LIGHT_STRIP:
|
|
|
|
if d in model:
|
|
|
|
return IotLightStrip
|
|
|
|
|
|
|
|
for d in BULBS_IOT:
|
|
|
|
if d in model:
|
|
|
|
return IotBulb
|
|
|
|
|
|
|
|
for d in DIMMERS_IOT:
|
|
|
|
if d in model:
|
|
|
|
return IotDimmer
|
|
|
|
|
|
|
|
raise Exception("Unable to find type for %s", model)
|
|
|
|
|
|
|
|
|
2024-05-02 13:32:06 +00:00
|
|
|
async def _update_and_close(d) -> Device:
|
2024-02-27 17:39:04 +00:00
|
|
|
await d.update()
|
|
|
|
await d.protocol.close()
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2024-05-02 13:32:06 +00:00
|
|
|
async def _discover_update_and_close(ip, username, password) -> Device:
|
2024-02-27 17:39:04 +00:00
|
|
|
if username and password:
|
|
|
|
credentials = Credentials(username=username, password=password)
|
|
|
|
else:
|
|
|
|
credentials = None
|
|
|
|
d = await Discover.discover_single(ip, timeout=10, credentials=credentials)
|
|
|
|
return await _update_and_close(d)
|
|
|
|
|
|
|
|
|
2024-11-18 14:53:11 +00:00
|
|
|
async def get_device_for_fixture(
|
|
|
|
fixture_data: FixtureInfo, *, verbatim=False, update_after_init=True
|
|
|
|
) -> Device:
|
2024-02-27 17:39:04 +00:00
|
|
|
# if the wanted file is not an absolute path, prepend the fixtures directory
|
|
|
|
|
|
|
|
d = device_for_fixture_name(fixture_data.name, fixture_data.protocol)(
|
|
|
|
host="127.0.0.123"
|
|
|
|
)
|
2024-10-24 08:36:18 +00:00
|
|
|
if fixture_data.protocol in {"SMART", "SMART.CHILD"}:
|
2024-11-18 14:53:11 +00:00
|
|
|
d.protocol = FakeSmartProtocol(
|
|
|
|
fixture_data.data, fixture_data.name, verbatim=verbatim
|
|
|
|
)
|
2024-11-23 08:07:47 +00:00
|
|
|
elif fixture_data.protocol == "SMARTCAM":
|
|
|
|
d.protocol = FakeSmartCamProtocol(
|
2024-11-18 14:53:11 +00:00
|
|
|
fixture_data.data, fixture_data.name, verbatim=verbatim
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
else:
|
2024-11-18 14:53:11 +00:00
|
|
|
d.protocol = FakeIotProtocol(fixture_data.data, verbatim=verbatim)
|
2024-03-06 14:54:55 +00:00
|
|
|
|
|
|
|
discovery_data = None
|
2024-03-01 18:32:45 +00:00
|
|
|
if "discovery_result" in fixture_data.data:
|
2024-11-18 14:53:11 +00:00
|
|
|
discovery_data = fixture_data.data["discovery_result"]
|
2024-03-06 14:54:55 +00:00
|
|
|
elif "system" in fixture_data.data:
|
2024-03-01 18:32:45 +00:00
|
|
|
discovery_data = {
|
|
|
|
"system": {"get_sysinfo": fixture_data.data["system"]["get_sysinfo"]}
|
|
|
|
}
|
2024-03-06 18:04:09 +00:00
|
|
|
|
2024-03-15 15:55:48 +00:00
|
|
|
if discovery_data: # Child devices do not have discovery info
|
2024-03-06 14:54:55 +00:00
|
|
|
d.update_from_discover_info(discovery_data)
|
|
|
|
|
2024-11-18 14:53:11 +00:00
|
|
|
if update_after_init:
|
|
|
|
await _update_and_close(d)
|
2024-02-27 17:39:04 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
async def get_device_for_fixture_protocol(fixture, protocol):
|
|
|
|
finfo = FixtureInfo(name=fixture, protocol=protocol, data={})
|
|
|
|
for fixture_info in FIXTURE_DATA:
|
|
|
|
if finfo == fixture_info:
|
|
|
|
return await get_device_for_fixture(fixture_info)
|
|
|
|
|
|
|
|
|
2024-06-03 09:14:10 +00:00
|
|
|
def get_fixture_info(fixture, protocol):
|
|
|
|
finfo = FixtureInfo(name=fixture, protocol=protocol, data={})
|
|
|
|
for fixture_info in FIXTURE_DATA:
|
|
|
|
if finfo == fixture_info:
|
|
|
|
return fixture_info
|
|
|
|
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
def get_nearest_fixture_to_ip(dev):
|
|
|
|
if isinstance(dev, SmartDevice):
|
|
|
|
protocol_fixtures = filter_fixtures("", protocol_filter={"SMART"})
|
2024-11-23 08:07:47 +00:00
|
|
|
elif isinstance(dev, SmartCamDevice):
|
|
|
|
protocol_fixtures = filter_fixtures("", protocol_filter={"SMARTCAM"})
|
2024-11-11 17:41:31 +00:00
|
|
|
else:
|
|
|
|
protocol_fixtures = filter_fixtures("", protocol_filter={"IOT"})
|
|
|
|
assert protocol_fixtures, "Unknown device type"
|
|
|
|
|
|
|
|
# This will get the best fixture with a match on model region
|
|
|
|
if model_region_fixtures := filter_fixtures(
|
|
|
|
"", model_filter={dev._model_region}, fixture_list=protocol_fixtures
|
|
|
|
):
|
|
|
|
return next(iter(model_region_fixtures))
|
|
|
|
|
|
|
|
# This will get the best fixture based on model starting with the name.
|
|
|
|
if "(" in dev.model:
|
|
|
|
model, _, _ = dev.model.partition("(")
|
|
|
|
else:
|
|
|
|
model = dev.model
|
|
|
|
if model_fixtures := filter_fixtures(
|
|
|
|
"", model_startswith_filter=model, fixture_list=protocol_fixtures
|
|
|
|
):
|
|
|
|
return next(iter(model_fixtures))
|
|
|
|
|
|
|
|
if device_type_fixtures := filter_fixtures(
|
|
|
|
"", device_type_filter={dev.device_type}, fixture_list=protocol_fixtures
|
|
|
|
):
|
|
|
|
return next(iter(device_type_fixtures))
|
|
|
|
|
|
|
|
return next(iter(protocol_fixtures))
|
|
|
|
|
|
|
|
|
2024-03-06 18:04:09 +00:00
|
|
|
@pytest.fixture(params=filter_fixtures("main devices"), ids=idgenerator)
|
2024-05-02 13:32:06 +00:00
|
|
|
async def dev(request) -> AsyncGenerator[Device, None]:
|
2024-02-27 17:39:04 +00:00
|
|
|
"""Device fixture.
|
|
|
|
|
|
|
|
Provides a device (given --ip) or parametrized fixture for the supported devices.
|
|
|
|
The initial update is called automatically before returning the device.
|
|
|
|
"""
|
|
|
|
fixture_data: FixtureInfo = request.param
|
2024-05-02 13:32:06 +00:00
|
|
|
dev: Device
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
ip = request.config.getoption("--ip")
|
2024-11-11 17:41:31 +00:00
|
|
|
username = request.config.getoption("--username") or os.environ.get("KASA_USERNAME")
|
|
|
|
password = request.config.getoption("--password") or os.environ.get("KASA_PASSWORD")
|
2024-02-27 17:39:04 +00:00
|
|
|
if ip:
|
2024-11-11 17:41:31 +00:00
|
|
|
fixture = IP_FIXTURE_CACHE.get(ip)
|
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
d = None
|
2024-11-11 17:41:31 +00:00
|
|
|
if not fixture:
|
2024-02-27 17:39:04 +00:00
|
|
|
d = await _discover_update_and_close(ip, username, password)
|
2024-11-11 17:41:31 +00:00
|
|
|
IP_FIXTURE_CACHE[ip] = fixture = get_nearest_fixture_to_ip(d)
|
|
|
|
assert fixture
|
|
|
|
if fixture.name != fixture_data.name:
|
2024-02-27 17:39:04 +00:00
|
|
|
pytest.skip(f"skipping file {fixture_data.name}")
|
2024-11-11 17:41:31 +00:00
|
|
|
dev = None
|
|
|
|
else:
|
|
|
|
dev = d if d else await _discover_update_and_close(ip, username, password)
|
2024-02-27 17:39:04 +00:00
|
|
|
else:
|
2024-05-02 13:32:06 +00:00
|
|
|
dev = await get_device_for_fixture(fixture_data)
|
2024-02-27 17:39:04 +00:00
|
|
|
|
|
|
|
yield dev
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
if dev:
|
|
|
|
await dev.disconnect()
|
2024-06-10 04:59:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_parent_and_child_modules(device: Device, module_name):
|
|
|
|
"""Return iterator of module if exists on parent and children.
|
|
|
|
|
|
|
|
Useful for testing devices that have components listed on the parent that are only
|
|
|
|
supported on the children, i.e. ks240.
|
|
|
|
"""
|
|
|
|
if module_name in device.modules:
|
|
|
|
yield device.modules[module_name]
|
|
|
|
for child in device.children:
|
|
|
|
if module_name in child.modules:
|
|
|
|
yield child.modules[module_name]
|