Skip to content

Commit ed1278a

Browse files
authored
Get all response pages for Shelly.GetComponents (#722)
* Get all pages of paginated response to GetComponents * Allow up to 10 iterations
1 parent 1f600b9 commit ed1278a

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

aioshelly/rpc_device/device.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
)
5050
from .wsrpc import RPCSource, WsRPC, WsServer
5151

52+
MAX_ITERATIONS = 10
53+
5254
_LOGGER = logging.getLogger(__name__)
5355

5456

@@ -317,9 +319,22 @@ async def _init_calls(self) -> None:
317319
if fetch_status:
318320
self._status = results.pop(0)
319321
if fetch_dynamic:
320-
components = results.pop(0)
321-
self._parse_dynamic_components(components)
322-
await self._retrieve_blutrv_components(components)
322+
all_pages = await self.get_all_pages(results.pop(0))
323+
self._parse_dynamic_components(all_pages)
324+
await self._retrieve_blutrv_components(all_pages)
325+
326+
async def get_all_pages(self, first_page: dict[str, Any]) -> dict[str, Any]:
327+
"""Get all pages of paginated response to GetComponents."""
328+
total = first_page["total"]
329+
counter = 0
330+
while len(first_page["components"]) < total and counter < MAX_ITERATIONS:
331+
counter += 1
332+
offset = len(first_page["components"])
333+
next_page = await self.call_rpc(
334+
"Shelly.GetComponents", {"dynamic_only": True, "offset": offset}
335+
)
336+
first_page["components"].extend(next_page["components"])
337+
return first_page
323338

324339
async def script_list(self) -> list[ShellyScript]:
325340
"""Get a list of scripts from 'Script.List'."""
@@ -511,9 +526,10 @@ async def get_dynamic_components(self) -> None:
511526
"""Return a list of dynamic components."""
512527
if not self._supports_dynamic_components():
513528
return
514-
components = await self.call_rpc("Shelly.GetComponents", {"dynamic_only": True})
515-
self._parse_dynamic_components(components)
516-
await self._retrieve_blutrv_components(components)
529+
first_page = await self.call_rpc("Shelly.GetComponents", {"dynamic_only": True})
530+
all_pages = await self.get_all_pages(first_page)
531+
self._parse_dynamic_components(all_pages)
532+
await self._retrieve_blutrv_components(all_pages)
517533

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

0 commit comments

Comments
 (0)