Use dict as store for child devices

This allows accessing child devices directly by their device_id,
which will be necessary to improve the child device support.
This commit is contained in:
Teemu Rytilahti
2024-02-18 18:47:39 +01:00
parent 9ab9420ad6
commit e27d5a3dec
8 changed files with 57 additions and 51 deletions

View File

@@ -15,7 +15,7 @@ def test_childdevice_init(dev, dummy_protocol, mocker):
assert len(dev.children) > 0
assert dev.is_strip
first = dev.children[0]
first = list(dev.children.values())[0]
assert isinstance(first.protocol, _ChildProtocolWrapper)
assert first._info["category"] == "plug.powerstrip.sub-plug"
@@ -29,7 +29,7 @@ async def test_childdevice_update(dev, dummy_protocol, mocker):
child_list = child_info["child_device_list"]
assert len(dev.children) == child_info["sum"]
first = dev.children[0]
first = list(dev.children.values())[0]
await dev.update()
@@ -46,8 +46,7 @@ async def test_childdevice_properties(dev: SmartChildDevice):
"""Check that accessing childdevice properties do not raise exceptions."""
assert len(dev.children) > 0
first = dev.children[0]
assert first.is_strip_socket
first = list(dev.children.values())[0]
# children do not have children
assert not first.children
@@ -60,10 +59,15 @@ async def test_childdevice_properties(dev: SmartChildDevice):
)
for prop in properties:
name, _ = prop
try:
_ = getattr(first, name)
except Exception as ex:
exceptions.append(ex)
if (
name.startswith("emeter_")
or name.startswith("time")
or name.startswith("on_since")
):
try:
_ = getattr(first, name)
except Exception as ex:
exceptions.append(ex)
return exceptions

View File

@@ -241,7 +241,8 @@ async def test_emeter(dev: Device, mocker):
assert "Index and name are only for power strips!" in res.output
if dev.is_strip and len(dev.children) > 0:
realtime_emeter = mocker.patch.object(dev.children[0], "get_emeter_realtime")
first_child = list(dev.children.values())[0]
realtime_emeter = mocker.patch.object(first_child, "get_emeter_realtime")
realtime_emeter.return_value = EmeterStatus({"voltage_mv": 122066})
res = await runner.invoke(emeter, ["--index", "0"], obj=dev)
@@ -249,7 +250,7 @@ async def test_emeter(dev: Device, mocker):
realtime_emeter.assert_called()
assert realtime_emeter.call_count == 1
res = await runner.invoke(emeter, ["--name", dev.children[0].alias], obj=dev)
res = await runner.invoke(emeter, ["--name", first_child.alias], obj=dev)
assert "Voltage: 122.066 V" in res.output
assert realtime_emeter.call_count == 2

View File

@@ -12,7 +12,7 @@ from .conftest import handle_turn_on, strip, turn_on
@turn_on
async def test_children_change_state(dev, turn_on):
await handle_turn_on(dev, turn_on)
for plug in dev.children:
for plug in dev.children.values():
orig_state = plug.is_on
if orig_state:
await plug.turn_off()
@@ -39,7 +39,7 @@ async def test_children_change_state(dev, turn_on):
@strip
async def test_children_alias(dev):
test_alias = "TEST1234"
for plug in dev.children:
for plug in dev.children.values():
original = plug.alias
await plug.set_alias(alias=test_alias)
await dev.update() # TODO: set_alias does not call parent's update()..
@@ -53,7 +53,7 @@ async def test_children_alias(dev):
@strip
async def test_children_on_since(dev):
on_sinces = []
for plug in dev.children:
for plug in dev.children.values():
if plug.is_on:
on_sinces.append(plug.on_since)
assert isinstance(plug.on_since, datetime)
@@ -70,8 +70,9 @@ async def test_children_on_since(dev):
@strip
async def test_get_plug_by_name(dev: IotStrip):
name = dev.children[0].alias
assert dev.get_plug_by_name(name) == dev.children[0] # type: ignore[arg-type]
children = list(dev.children.values())
name = children[0].alias
assert dev.get_plug_by_name(name) == children[0] # type: ignore[arg-type]
with pytest.raises(SmartDeviceException):
dev.get_plug_by_name("NONEXISTING NAME")
@@ -79,7 +80,7 @@ async def test_get_plug_by_name(dev: IotStrip):
@strip
async def test_get_plug_by_index(dev: IotStrip):
assert dev.get_plug_by_index(0) == dev.children[0]
assert dev.get_plug_by_index(0) == list(dev.children.values())[0]
with pytest.raises(SmartDeviceException):
dev.get_plug_by_index(-1)