Skip to content

Commit

Permalink
Allow for advanced networking in the Sonos provider.
Browse files Browse the repository at this point in the history
A new advanced config option where the user can specity IPs to the speakers was added.
  • Loading branch information
Icelk committed Jan 18, 2025
1 parent 01f41d0 commit 18bf323
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
24 changes: 21 additions & 3 deletions music_assistant/providers/sonos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import logging
from typing import TYPE_CHECKING

from music_assistant_models.config_entries import ConfigEntry, ConfigEntryType

from music_assistant.constants import VERBOSE_LOG_LEVEL

from .provider import SonosPlayerProvider
from .provider import CONF_IPS, SonosPlayerProvider

if TYPE_CHECKING:
from music_assistant_models.config_entries import ConfigEntry, ConfigValueType, ProviderConfig
from music_assistant_models.config_entries import ConfigValueType, ProviderConfig
from music_assistant_models.provider import ProviderManifest

from music_assistant import MusicAssistant
Expand Down Expand Up @@ -49,4 +51,20 @@ async def get_config_entries(
values: the (intermediate) raw values for config entries sent with the action.
"""
# ruff: noqa: ARG001
return ()
return (
ConfigEntry(
key=CONF_IPS,
type=ConfigEntryType.STRING,
label="IP addresses (ADVANCED, NOT SUPPORTED)",
required=False,
description="Additional fixed IP addresses for speakers. "
"Should be formatted as a comma separated list of IP addresses "
"(e.g. '10.0.0.42, 10.0.0.45').\n"
"Invalid addresses may result in the Sonos provider "
"becoming unresponsive and server crashes.\n"
"Bidirectional unicast communication to and between all IPs is required.\n"
"NOT SUPPORTED, USE ON YOU'RE OWN RISK",
category="advanced",
default_value="",
),
)
20 changes: 20 additions & 0 deletions music_assistant/providers/sonos/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
if TYPE_CHECKING:
from zeroconf.asyncio import AsyncServiceInfo

CONF_IPS = "ips"


class SonosPlayerProvider(PlayerProvider):
"""Sonos Player provider."""
Expand Down Expand Up @@ -67,6 +69,24 @@ async def handle_async_init(self) -> None:
"/sonos_queue/v2.3/timePlayed", self._handle_sonos_queue_time_played
)

ips = self.config.get_value(CONF_IPS).split(",")
for raw_ip in ips:
ip = raw_ip.strip()
if ip == "":
continue
try:
discovery_info = await get_discovery_info(self.mass.http_session, ip)
except ClientError as err:
self.logger.debug(
"Ignoring %s in static IP as it is not reachable: %s", ip, str(err)
)
continue
player_id = discovery_info["device"]["id"]
self.sonos_players[player_id] = sonos_player = SonosPlayer(
self, player_id, discovery_info=discovery_info, ip_address=ip
)
self.mass.call_later(5, sonos_player.setup, task_id=f"setup sonos {ip}")

async def unload(self, is_removed: bool = False) -> None:
"""Handle close/cleanup of the provider."""
# disconnect all players
Expand Down

0 comments on commit 18bf323

Please sign in to comment.