mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Generalize smartdevice child support (#775)
* Initialize children's modules (and features) using the child component negotiation results * Set device_type based on the device response * Print out child features in cli 'state' * Add --child option to cli 'command' to allow targeting child devices * Guard "generic" features like rssi, ssid, etc. only to devices which have this information Note, we do not currently perform queries on child modules so some data may not be available. At the moment, a stop-gap solution to use parent's data is used but this is not always correct; even if the device shares the same clock and cloud connectivity, it may have its own firmware updates.
This commit is contained in:
@@ -47,7 +47,6 @@ async def test_childdevice_properties(dev: SmartChildDevice):
|
||||
assert len(dev.children) > 0
|
||||
|
||||
first = dev.children[0]
|
||||
assert first.is_strip_socket
|
||||
|
||||
# children do not have children
|
||||
assert not first.children
|
||||
|
@@ -19,6 +19,7 @@ from kasa.cli import (
|
||||
alias,
|
||||
brightness,
|
||||
cli,
|
||||
cmd_command,
|
||||
emeter,
|
||||
raw_command,
|
||||
reboot,
|
||||
@@ -136,6 +137,32 @@ async def test_raw_command(dev, mocker):
|
||||
assert "Usage" in res.output
|
||||
|
||||
|
||||
async def test_command_with_child(dev, mocker):
|
||||
"""Test 'command' command with --child."""
|
||||
runner = CliRunner()
|
||||
update_mock = mocker.patch.object(dev, "update")
|
||||
|
||||
dummy_child = mocker.create_autospec(IotDevice)
|
||||
query_mock = mocker.patch.object(
|
||||
dummy_child, "_query_helper", return_value={"dummy": "response"}
|
||||
)
|
||||
|
||||
mocker.patch.object(dev, "_children", {"XYZ": dummy_child})
|
||||
mocker.patch.object(dev, "get_child_device", return_value=dummy_child)
|
||||
|
||||
res = await runner.invoke(
|
||||
cmd_command,
|
||||
["--child", "XYZ", "command", "'params'"],
|
||||
obj=dev,
|
||||
catch_exceptions=False,
|
||||
)
|
||||
|
||||
update_mock.assert_called()
|
||||
query_mock.assert_called()
|
||||
assert '{"dummy": "response"}' in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@device_smart
|
||||
async def test_reboot(dev, mocker):
|
||||
"""Test that reboot works on SMART devices."""
|
||||
|
@@ -37,6 +37,7 @@ from .conftest import (
|
||||
lightstrip,
|
||||
no_emeter_iot,
|
||||
plug,
|
||||
strip,
|
||||
turn_on,
|
||||
)
|
||||
from .fakeprotocol_iot import FakeIotProtocol
|
||||
@@ -201,13 +202,12 @@ async def test_representation(dev):
|
||||
assert pattern.match(str(dev))
|
||||
|
||||
|
||||
@device_iot
|
||||
async def test_childrens(dev):
|
||||
"""Make sure that children property is exposed by every device."""
|
||||
if dev.is_strip:
|
||||
assert len(dev.children) > 0
|
||||
else:
|
||||
assert len(dev.children) == 0
|
||||
@strip
|
||||
def test_children_api(dev):
|
||||
"""Test the child device API."""
|
||||
first = dev.children[0]
|
||||
first_by_get_child_device = dev.get_child_device(first.device_id)
|
||||
assert first == first_by_get_child_device
|
||||
|
||||
|
||||
@device_iot
|
||||
@@ -215,10 +215,8 @@ async def test_children(dev):
|
||||
"""Make sure that children property is exposed by every device."""
|
||||
if dev.is_strip:
|
||||
assert len(dev.children) > 0
|
||||
assert dev.has_children is True
|
||||
else:
|
||||
assert len(dev.children) == 0
|
||||
assert dev.has_children is False
|
||||
|
||||
|
||||
@device_iot
|
||||
@@ -260,7 +258,9 @@ async def test_device_class_ctors(device_class_name_obj):
|
||||
klass = device_class_name_obj[1]
|
||||
if issubclass(klass, SmartChildDevice):
|
||||
parent = SmartDevice(host, config=config)
|
||||
dev = klass(parent, 1)
|
||||
dev = klass(
|
||||
parent, {"dummy": "info", "device_id": "dummy"}, {"dummy": "components"}
|
||||
)
|
||||
else:
|
||||
dev = klass(host, config=config)
|
||||
assert dev.host == host
|
||||
|
Reference in New Issue
Block a user