FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Handle hubs that omit the child list when they have no children by IDavinciCode · Pull Request #1785 · python-kasa/python-kasa · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 5 additions & 7 deletions kasa/protocols/smartprotocol.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,12 @@ async def _handle_response_lists(
return

response_list_name = next(
iter(
[
key
for key in response_result
if isinstance(response_result[key], list)
]
)
(key for key, val in response_result.items() if isinstance(val, list)),
None,
)
# Hubs without children may omit the list altogether
if response_list_name is None:
return
while (list_length := len(response_result[response_list_name])) < list_sum:
request = self._get_list_request(method, params, list_length)
response = await self._execute_query(
Expand Down
6 changes: 3 additions & 3 deletions kasa/smart/smartdevice.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ async def _create_delete_children(
changed = False
smart_children_components = {
child["device_id"]: child
for child in child_device_components_resp["child_component_list"]
for child in child_device_components_resp.get("child_component_list", [])
}
children = self._children
child_ids: set[str] = set()
existing_child_ids = set(self._children.keys())

for info in child_device_resp["child_device_list"]:
for info in child_device_resp.get("child_device_list", []):
if (child_id := info.get("device_id")) and (
child_components := smart_children_components.get(child_id)
):
Expand Down Expand Up @@ -244,7 +244,7 @@ async def _update_children_info(self) -> bool:
child_info, self._last_update["get_child_device_component_list"]
)

for info in child_info["child_device_list"]:
for info in child_info.get("child_device_list", []):
child_id = info.get("device_id")
if child_id not in self._children:
# _create_delete_children has already logged a message
Expand Down
2 changes: 1 addition & 1 deletion kasa/smartcam/smartcamdevice.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def _update_children_info(self) -> bool:
child_info, self._last_update["getChildDeviceComponentList"]
)

for info in child_info["child_device_list"]:
for info in child_info.get("child_device_list", []):
child_id = info.get("device_id")
if child_id not in self._children:
# _create_delete_children has already logged a message
Expand Down
18 changes: 18 additions & 0 deletions tests/protocols/test_smartprotocol.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,24 @@ async def test_childdevicewrapper_multiplerequest_error(
assert res["invalid_command"] == SmartErrorCode(-1001)


async def test_smart_protocol_list_response_without_list(
dummy_protocol: SmartProtocol, mocker: MockerFixture
) -> None:
"""Test a paginated response that carries no list.

A hub without paired children answers with only start_index and sum.
"""
result = {"start_index": 0, "sum": 0}
send_mock = mocker.patch.object(
dummy_protocol._transport,
"send",
return_value={"result": result, "error_code": 0},
)
resp = await dummy_protocol.query({"get_child_device_list": None})
assert resp == {"get_child_device_list": result}
assert send_mock.call_count == 1


@pytest.mark.parametrize("list_sum", [5, 10, 30])
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
async def test_smart_protocol_lists_single_request(
Expand Down
13 changes: 13 additions & 0 deletions tests/smart/test_smartdevice.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,19 @@ def mock_get_child_device_queries(method, params):

assert len(dev.children) == 0

# Test a hub that omits the lists when it has no child devices
del mock_child_device_info["child_device_list"]
del mock_child_device_components["child_component_list"]

caplog.clear()
with patch.object(
transport, "get_child_device_queries", side_effect=mock_get_child_device_queries
):
await dev.update()

assert len(dev.children) == 0
assert "Error" not in caplog.text

# Logging tests are only for smartcam hubs as smart hubs do not test categories
if not isinstance(dev, SmartCamDevice):
return
Expand Down
Loading

Back | FazBrowse Home | New Git URL