2024-02-02 16:29:14 +00:00
|
|
|
import inspect
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.smart.smartchilddevice import SmartChildDevice
|
2024-01-29 19:26:39 +00:00
|
|
|
from kasa.smartprotocol import _ChildProtocolWrapper
|
|
|
|
|
|
|
|
from .conftest import strip_smart
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
assert dev.is_strip
|
|
|
|
|
2024-02-18 17:47:39 +00:00
|
|
|
first = list(dev.children.values())[0]
|
2024-01-29 19:26:39 +00:00
|
|
|
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-02-02 16:29:14 +00:00
|
|
|
child_info = dev._last_update["child_info"]
|
|
|
|
child_list = child_info["child_device_list"]
|
|
|
|
|
|
|
|
assert len(dev.children) == child_info["sum"]
|
2024-02-18 17:47:39 +00:00
|
|
|
first = list(dev.children.values())[0]
|
2024-01-29 19:26:39 +00:00
|
|
|
|
|
|
|
await dev.update()
|
|
|
|
|
|
|
|
assert dev._last_update != first._last_update
|
2024-02-02 16:29:14 +00:00
|
|
|
assert child_list[0] == first._last_update
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2024-02-18 17:47:39 +00:00
|
|
|
first = list(dev.children.values())[0]
|
2024-02-02 16:29:14 +00:00
|
|
|
|
|
|
|
# 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-18 17:47:39 +00:00
|
|
|
if (
|
|
|
|
name.startswith("emeter_")
|
|
|
|
or name.startswith("time")
|
|
|
|
or name.startswith("on_since")
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
_ = getattr(first, name)
|
|
|
|
except Exception as ex:
|
|
|
|
exceptions.append(ex)
|
2024-02-02 16:29:14 +00:00
|
|
|
|
|
|
|
return exceptions
|
|
|
|
|
|
|
|
exceptions = list(_test_property_getters())
|
|
|
|
if exceptions:
|
|
|
|
raise ExceptionGroup("Accessing child properties caused exceptions", exceptions)
|