Skip to content

Commit

Permalink
Merge pull request #102 from noahhusby/feat/toggle-mode
Browse files Browse the repository at this point in the history
Breakout zone and source metadata loading
  • Loading branch information
noahhusby authored Dec 29, 2024
2 parents 21949a2 + 78fefb4 commit 9c680bb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
96 changes: 49 additions & 47 deletions aiorussound/rio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
maintainers = ["Noah Husby <[email protected]>"]
Expand Down

0 comments on commit 9c680bb

Please sign in to comment.