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

Implement bridge discovery via multiroom #7

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/linkplay/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,4 @@ class MultiroomAttribute(StrEnum):
NUM_FOLLOWERS = "slaves"
FOLLOWER_LIST = "slave_list"
UUID = "uuid"
IP = "ip"
35 changes: 29 additions & 6 deletions src/linkplay/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def linkplay_factory_bridge(ip_address: str, session: ClientSession) -> Li

async def discover_linkplay_bridges(session: ClientSession) -> list[LinkPlayBridge]:
"""Attempts to discover LinkPlay devices on the local network."""
devices: list[LinkPlayBridge] = []
bridges: dict[str, LinkPlayBridge] = {}

async def add_linkplay_device_to_list(upnp_device: CaseInsensitiveDict):
ip_address: str | None = upnp_device.get('_host')
Expand All @@ -32,19 +32,27 @@ async def add_linkplay_device_to_list(upnp_device: CaseInsensitiveDict):
return

if bridge := await linkplay_factory_bridge(ip_address, session):
devices.append(bridge)
bridges[bridge.device.uuid] = bridge

await async_search(
search_target=UPNP_DEVICE_TYPE,
async_callback=add_linkplay_device_to_list
)

return devices
# Discover additional bridges through grouped multirooms
multiroom_discovered_bridges: dict[str, LinkPlayBridge] = {}
for bridge in bridges.values():
for new_bridge in await discover_bridges_through_multiroom(bridge, session):
multiroom_discovered_bridges[new_bridge.device.uuid] = new_bridge

bridges = bridges | multiroom_discovered_bridges
return list(bridges.values())


async def discover_multirooms(bridges: list[LinkPlayBridge]) -> list[LinkPlayMultiroom]:
"""Discovers multirooms through the list of provided bridges."""
multirooms: list[LinkPlayMultiroom] = []
bridges_dict: dict[str, LinkPlayBridge] = {bridge.device.uuid: bridge for bridge in bridges}

for bridge in bridges:
properties: dict[Any, Any] = await bridge.json_request(LinkPlayCommand.MULTIROOM_LIST)
Expand All @@ -54,10 +62,25 @@ async def discover_multirooms(bridges: list[LinkPlayBridge]) -> list[LinkPlayMul

followers: list[LinkPlayBridge] = []
for follower in properties[MultiroomAttribute.FOLLOWER_LIST]:
follower_uuid = follower[MultiroomAttribute.UUID]
if follower_bridge := next((b for b in bridges if b.device.uuid == follower_uuid), None):
followers.append(follower_bridge)
if follower[MultiroomAttribute.UUID] in bridges_dict:
followers.append(bridges_dict[follower[MultiroomAttribute.UUID]])

multirooms.append(LinkPlayMultiroom(bridge, followers))

return multirooms


async def discover_bridges_through_multiroom(bridge: LinkPlayBridge,
session: ClientSession) -> list[LinkPlayBridge]:
"""Discovers bridges through the multiroom of the provided bridge."""
properties: dict[Any, Any] = await bridge.json_request(LinkPlayCommand.MULTIROOM_LIST)

if int(properties[MultiroomAttribute.NUM_FOLLOWERS]) == 0:
return []

followers: list[LinkPlayBridge] = []
for follower in properties[MultiroomAttribute.FOLLOWER_LIST]:
if new_bridge := await linkplay_factory_bridge(follower[MultiroomAttribute.IP], session):
followers.append(new_bridge)

return followers