2024-02-02 16:29:14 +00:00
|
|
|
import inspect
|
|
|
|
import sys
|
2024-10-23 15:17:27 +00:00
|
|
|
from datetime import datetime, timezone
|
2024-02-02 16:29:14 +00:00
|
|
|
|
|
|
|
import pytest
|
2024-10-23 15:17:27 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2024-02-02 16:29:14 +00:00
|
|
|
|
2024-07-01 10:51:06 +00:00
|
|
|
from kasa import Device
|
2024-06-10 05:21:21 +00:00
|
|
|
from kasa.device_type import DeviceType
|
2024-11-13 17:50:21 +00:00
|
|
|
from kasa.protocols.smartprotocol import _ChildProtocolWrapper
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.smart.smartchilddevice import SmartChildDevice
|
2024-06-10 05:21:21 +00:00
|
|
|
from kasa.smart.smartdevice import NON_HUB_PARENT_ONLY_MODULES
|
2024-01-29 19:26:39 +00:00
|
|
|
|
2024-07-01 10:51:06 +00:00
|
|
|
from .conftest import (
|
|
|
|
parametrize,
|
|
|
|
parametrize_combine,
|
|
|
|
parametrize_subtract,
|
|
|
|
strip_iot,
|
|
|
|
strip_smart,
|
|
|
|
)
|
2024-06-10 05:21:21 +00:00
|
|
|
|
|
|
|
has_children_smart = parametrize(
|
|
|
|
"has children", component_filter="control_child", protocol_filter={"SMART"}
|
|
|
|
)
|
|
|
|
hub_smart = parametrize(
|
|
|
|
"smart hub", device_type_filter=[DeviceType.Hub], protocol_filter={"SMART"}
|
|
|
|
)
|
|
|
|
non_hub_parent_smart = parametrize_subtract(has_children_smart, hub_smart)
|
2024-01-29 19:26:39 +00:00
|
|
|
|
2024-07-01 10:51:06 +00:00
|
|
|
has_children = parametrize_combine([has_children_smart, strip_iot])
|
|
|
|
|
2024-01-29 19:26:39 +00:00
|
|
|
|
|
|
|
@strip_smart
|
|
|
|
def test_childdevice_init(dev, dummy_protocol, mocker):
|
|
|
|
"""Test that child devices get initialized and use protocol wrapper."""
|
|
|
|
assert len(dev.children) > 0
|
|
|
|
|
|
|
|
first = dev.children[0]
|
|
|
|
assert isinstance(first.protocol, _ChildProtocolWrapper)
|
|
|
|
|
|
|
|
assert first._info["category"] == "plug.powerstrip.sub-plug"
|
|
|
|
assert "position" in first._info
|
|
|
|
|
|
|
|
|
|
|
|
@strip_smart
|
|
|
|
async def test_childdevice_update(dev, dummy_protocol, mocker):
|
|
|
|
"""Test that parent update updates children."""
|
2024-03-15 16:18:13 +00:00
|
|
|
child_info = dev.internal_state["get_child_device_list"]
|
2024-02-02 16:29:14 +00:00
|
|
|
child_list = child_info["child_device_list"]
|
|
|
|
|
|
|
|
assert len(dev.children) == child_info["sum"]
|
2024-01-29 19:26:39 +00:00
|
|
|
first = dev.children[0]
|
|
|
|
|
|
|
|
await dev.update()
|
|
|
|
|
2024-04-17 10:07:16 +00:00
|
|
|
assert dev._info != first._info
|
|
|
|
assert child_list[0] == first._info
|
2024-02-02 16:29:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strip_smart
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 11),
|
|
|
|
reason="exceptiongroup requires python3.11+",
|
|
|
|
)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_childdevice_properties(dev: SmartChildDevice):
|
2024-02-02 16:29:14 +00:00
|
|
|
"""Check that accessing childdevice properties do not raise exceptions."""
|
|
|
|
assert len(dev.children) > 0
|
|
|
|
|
|
|
|
first = dev.children[0]
|
|
|
|
|
|
|
|
# children do not have children
|
|
|
|
assert not first.children
|
|
|
|
|
|
|
|
def _test_property_getters():
|
|
|
|
"""Try accessing all properties and return a list of encountered exceptions."""
|
|
|
|
exceptions = []
|
|
|
|
properties = inspect.getmembers(
|
|
|
|
first.__class__, lambda o: isinstance(o, property)
|
|
|
|
)
|
|
|
|
for prop in properties:
|
|
|
|
name, _ = prop
|
2024-02-19 17:01:31 +00:00
|
|
|
# Skip emeter and time properties
|
|
|
|
# TODO: needs API cleanup, emeter* should probably be removed in favor
|
|
|
|
# of access through features/modules, handling of time* needs decision.
|
2024-04-29 17:19:44 +00:00
|
|
|
if (
|
|
|
|
name.startswith("emeter_")
|
|
|
|
or name.startswith("time")
|
|
|
|
or name.startswith("fan")
|
|
|
|
or name.startswith("color")
|
|
|
|
or name.startswith("brightness")
|
|
|
|
or name.startswith("valid_temperature_range")
|
|
|
|
or name.startswith("hsv")
|
|
|
|
or name.startswith("effect")
|
|
|
|
):
|
2024-02-19 17:01:31 +00:00
|
|
|
continue
|
2024-02-02 16:29:14 +00:00
|
|
|
try:
|
|
|
|
_ = getattr(first, name)
|
|
|
|
except Exception as ex:
|
|
|
|
exceptions.append(ex)
|
|
|
|
|
|
|
|
return exceptions
|
|
|
|
|
|
|
|
exceptions = list(_test_property_getters())
|
|
|
|
if exceptions:
|
|
|
|
raise ExceptionGroup("Accessing child properties caused exceptions", exceptions)
|
2024-06-10 05:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@non_hub_parent_smart
|
|
|
|
async def test_parent_only_modules(dev, dummy_protocol, mocker):
|
|
|
|
"""Test that parent only modules are not available on children."""
|
|
|
|
for child in dev.children:
|
|
|
|
for module in NON_HUB_PARENT_ONLY_MODULES:
|
|
|
|
assert module not in [type(module) for module in child.modules.values()]
|
2024-07-01 10:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@has_children
|
|
|
|
async def test_parent_property(dev: Device):
|
|
|
|
"""Test a child device exposes it's parent."""
|
|
|
|
if not dev.children:
|
|
|
|
pytest.skip(f"Device {dev} fixture does not have any children")
|
|
|
|
|
|
|
|
assert dev.parent is None
|
|
|
|
for child in dev.children:
|
|
|
|
assert child.parent == dev
|
2024-10-23 15:17:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@has_children_smart
|
2024-11-11 17:41:31 +00:00
|
|
|
@pytest.mark.requires_dummy()
|
2024-10-23 15:17:27 +00:00
|
|
|
async def test_child_time(dev: Device, freezer: FrozenDateTimeFactory):
|
2024-11-11 17:41:31 +00:00
|
|
|
"""Test a child device gets the time from it's parent module.
|
|
|
|
|
|
|
|
This is excluded from real device testing as the test often fail if the
|
|
|
|
device time is not in the past.
|
|
|
|
"""
|
2024-10-23 15:17:27 +00:00
|
|
|
if not dev.children:
|
|
|
|
pytest.skip(f"Device {dev} fixture does not have any children")
|
|
|
|
|
|
|
|
fallback_time = datetime.now(timezone.utc).astimezone().replace(microsecond=0)
|
|
|
|
assert dev.parent is None
|
|
|
|
for child in dev.children:
|
|
|
|
assert child.time != fallback_time
|