Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all response pages for Shelly.GetComponents #722

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions aioshelly/rpc_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
)
from .wsrpc import RPCSource, WsRPC, WsServer

MAX_ITERATIONS = 10

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -309,7 +311,21 @@
if fetch_status:
self._status = results.pop(0)
if fetch_dynamic:
self._parse_dynamic_components(results.pop(0))
all_pages = await self.get_all_pages(results.pop(0))
self._parse_dynamic_components(all_pages)

Check warning on line 315 in aioshelly/rpc_device/device.py

View check run for this annotation

Codecov / codecov/patch

aioshelly/rpc_device/device.py#L314-L315

Added lines #L314 - L315 were not covered by tests

async def get_all_pages(self, first_page: dict[str, Any]) -> dict[str, Any]:
"""Get all pages of paginated response to GetComponents."""
total = first_page["total"]
counter = 0
while len(first_page["components"]) < total and counter < MAX_ITERATIONS:
counter += 1
offset = len(first_page["components"])
next_page = await self.call_rpc(

Check warning on line 324 in aioshelly/rpc_device/device.py

View check run for this annotation

Codecov / codecov/patch

aioshelly/rpc_device/device.py#L319-L324

Added lines #L319 - L324 were not covered by tests
"Shelly.GetComponents", {"dynamic_only": True, "offset": offset}
)
first_page["components"].extend(next_page["components"])
return first_page

Check warning on line 328 in aioshelly/rpc_device/device.py

View check run for this annotation

Codecov / codecov/patch

aioshelly/rpc_device/device.py#L327-L328

Added lines #L327 - L328 were not covered by tests

async def script_list(self) -> list[ShellyScript]:
"""Get a list of scripts from 'Script.List'."""
Expand Down Expand Up @@ -507,8 +523,9 @@
"""Return a list of dynamic components."""
if not self._supports_dynamic_components():
return
components = await self.call_rpc("Shelly.GetComponents", {"dynamic_only": True})
self._parse_dynamic_components(components)
first_page = await self.call_rpc("Shelly.GetComponents", {"dynamic_only": True})
all_pages = await self.get_all_pages(first_page)
self._parse_dynamic_components(all_pages)

Check warning on line 528 in aioshelly/rpc_device/device.py

View check run for this annotation

Codecov / codecov/patch

aioshelly/rpc_device/device.py#L526-L528

Added lines #L526 - L528 were not covered by tests

def _supports_dynamic_components(self) -> bool:
"""Return True if device supports dynamic components."""
Expand Down