From 78fefb4a6ae834aeb9489cc62c4d074284d41e3a Mon Sep 17 00:00:00 2001 From: noahhusby <32528627+noahhusby@users.noreply.github.com> Date: Sun, 29 Dec 2024 18:34:08 -0500 Subject: [PATCH] Breakout zone and source metadata loading --- aiorussound/rio.py | 96 +++++++++++++++++++++++----------------------- pyproject.toml | 2 +- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/aiorussound/rio.py b/aiorussound/rio.py index becee9d..e40308b 100644 --- a/aiorussound/rio.py +++ b/aiorussound/rio.py @@ -180,54 +180,7 @@ async def _connect_handler(self, res): if controller: self.controllers[controller_id] = controller - subscribe_state_updates = { - self.subscribe(self._async_handle_system, "System") - } - - # Load source structure - for source_id in range(1, MAX_SOURCE): - try: - device_str = source_device_str(source_id) - name = await self.get_variable(device_str, "name") - if name: - subscribe_state_updates.add( - self.subscribe(self._async_handle_source, device_str) - ) - except CommandError: - break - - for controller_id, controller in self.controllers.items(): - for zone_id in range(1, get_max_zones(controller.controller_type) + 1): - try: - device_str = zone_device_str(controller_id, zone_id) - name = await self.get_variable(device_str, "name") - if name: - subscribe_state_updates.add( - self.subscribe(self._async_handle_zone, device_str) - ) - except CommandError: - break - - subscribe_tasks = set() - for state_update in subscribe_state_updates: - subscribe_tasks.add(asyncio.create_task(state_update)) - await asyncio.wait(subscribe_tasks) - - if is_feature_supported( - self.rio_version, FeatureFlag.SUPPORT_ZONE_SOURCE_EXCLUSION - ): - _LOGGER.debug( - "Zone source exclusion is supported. Fetching excluded sources." - ) - await self._load_zone_source_exclusion() - # Reload zones from state - await self._async_handle_zone() - self._do_state_update = True - await self.do_state_update_callbacks(CallbackType.CONNECTION) - - # Delay to ensure async TTL - await asyncio.sleep(0.2) self._attempt_reconnection = True if not res.done(): res.set_result(True) @@ -259,6 +212,55 @@ async def _connect_handler(self, res): except asyncio.CancelledError: pass + async def load_zone_source_metadata(self) -> None: + """Fetches and subscribes to all the zone and source metadata""" + + subscribe_state_updates = {self.subscribe(self._async_handle_system, "System")} + + # Load source structure + for source_id in range(1, MAX_SOURCE): + try: + device_str = source_device_str(source_id) + name = await self.get_variable(device_str, "name") + if name: + subscribe_state_updates.add( + self.subscribe(self._async_handle_source, device_str) + ) + except CommandError: + break + + for controller_id, controller in self.controllers.items(): + for zone_id in range(1, get_max_zones(controller.controller_type) + 1): + try: + device_str = zone_device_str(controller_id, zone_id) + name = await self.get_variable(device_str, "name") + if name: + subscribe_state_updates.add( + self.subscribe(self._async_handle_zone, device_str) + ) + except CommandError: + break + + subscribe_tasks = set() + for state_update in subscribe_state_updates: + subscribe_tasks.add(asyncio.create_task(state_update)) + await asyncio.wait(subscribe_tasks) + + if is_feature_supported( + self.rio_version, FeatureFlag.SUPPORT_ZONE_SOURCE_EXCLUSION + ): + _LOGGER.debug( + "Zone source exclusion is supported. Fetching excluded sources." + ) + await self._load_zone_source_exclusion() + # Reload zones from state + await self._async_handle_zone() + + await self.do_state_update_callbacks(CallbackType.STATE) + + # Delay to ensure async TTL + await asyncio.sleep(0.5) + @staticmethod def process_response(res: bytes) -> Optional[RussoundMessage]: """Process an incoming string of bytes into a RussoundMessage""" diff --git a/pyproject.toml b/pyproject.toml index 4f8d0f9..9527e78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aiorussound" -version = "4.2.0" +version = "4.3.0" description = "Asyncio client for Russound RIO devices." authors = ["Noah Husby "] maintainers = ["Noah Husby "]