GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make parent attribute on device consistent across iot and smart (#1023) · python-kasa/python-kasa@2687c71

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • kasa
    • device.py
    • iot
      • iotstrip.py
    • tests
      • test_childdevice.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎kasa/device.py‎

Copy file name to clipboardExpand all lines: kasa/device.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ async def _raw_query(self, request: str | dict) -> Any:
328328
"""Send a raw query to the device."""
329329
return await self.protocol.query(request=request)
330330

331+
@property
332+
def parent(self) -> Device | None:
333+
"""Return the parent on child devices."""
334+
return self._parent
335+
331336
@property
332337
def children(self) -> Sequence[Device]:
333338
"""Returns the child devices."""
Collapse file

‎kasa/iot/iotstrip.py‎

Copy file name to clipboardExpand all lines: kasa/iot/iotstrip.py
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,12 @@ class IotStripPlug(IotPlug):
307307
The plug inherits (most of) the system information from the parent.
308308
"""
309309

310+
_parent: IotStrip
311+
310312
def __init__(self, host: str, parent: IotStrip, child_id: str) -> None:
311313
super().__init__(host)
312314

313-
self.parent = parent
315+
self._parent = parent
314316
self.child_id = child_id
315317
self._last_update = parent._last_update
316318
self._set_sys_info(parent.sys_info)
@@ -380,7 +382,7 @@ async def _query_helper(
380382
self, target: str, cmd: str, arg: dict | None = None, child_ids=None
381383
) -> Any:
382384
"""Override query helper to include the child_ids."""
383-
return await self.parent._query_helper(
385+
return await self._parent._query_helper(
384386
target, cmd, arg, child_ids=[self.child_id]
385387
)
386388

@@ -441,13 +443,15 @@ def on_since(self) -> datetime | None:
441443
@requires_update
442444
def model(self) -> str:
443445
"""Return device model for a child socket."""
444-
sys_info = self.parent.sys_info
446+
sys_info = self._parent.sys_info
445447
return f"Socket for {sys_info['model']}"
446448

447449
def _get_child_info(self) -> dict:
448450
"""Return the subdevice information for this device."""
449-
for plug in self.parent.sys_info["children"]:
451+
for plug in self._parent.sys_info["children"]:
450452
if plug["id"] == self.child_id:
451453
return plug
452454

453-
raise KasaException(f"Unable to find children {self.child_id}")
455+
raise KasaException(
456+
f"Unable to find children {self.child_id}"
457+
) # pragma: no cover
Collapse file

‎kasa/tests/test_childdevice.py‎

Copy file name to clipboardExpand all lines: kasa/tests/test_childdevice.py
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33

44
import pytest
55

6+
from kasa import Device
67
from kasa.device_type import DeviceType
78
from kasa.smart.smartchilddevice import SmartChildDevice
89
from kasa.smart.smartdevice import NON_HUB_PARENT_ONLY_MODULES
910
from kasa.smartprotocol import _ChildProtocolWrapper
1011

11-
from .conftest import parametrize, parametrize_subtract, strip_smart
12+
from .conftest import (
13+
parametrize,
14+
parametrize_combine,
15+
parametrize_subtract,
16+
strip_iot,
17+
strip_smart,
18+
)
1219

1320
has_children_smart = parametrize(
1421
"has children", component_filter="control_child", protocol_filter={"SMART"}
@@ -18,6 +25,8 @@
1825
)
1926
non_hub_parent_smart = parametrize_subtract(has_children_smart, hub_smart)
2027

28+
has_children = parametrize_combine([has_children_smart, strip_iot])
29+
2130

2231
@strip_smart
2332
def test_childdevice_init(dev, dummy_protocol, mocker):
@@ -100,3 +109,14 @@ async def test_parent_only_modules(dev, dummy_protocol, mocker):
100109
for child in dev.children:
101110
for module in NON_HUB_PARENT_ONLY_MODULES:
102111
assert module not in [type(module) for module in child.modules.values()]
112+
113+
114+
@has_children
115+
async def test_parent_property(dev: Device):
116+
"""Test a child device exposes it's parent."""
117+
if not dev.children:
118+
pytest.skip(f"Device {dev} fixture does not have any children")
119+
120+
assert dev.parent is None
121+
for child in dev.children:
122+
assert child.parent == dev

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.