From 53ea5f051f7c296106953671520f7d9b987a06db Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 09:26:58 +0100 Subject: [PATCH] fix(driver): stop guessing whether a speaker is wired The wired check read device_properties, which SoCo does not have (deviceProperties is the UPnP service, and none of its actions says wired or wireless), and swallowed the AttributeError, so eth0 was never reported and the guess served nothing. It is gone: a speaker reports its one connection as wlan0, described as of unknown medium, and the README and the getter docstrings say that wired vs. wireless is not known. Closes #2 --- CHANGELOG.md | 4 +++ README.md | 7 ++++- napalm_sonos/sonos_driver.py | 59 ++++++++++-------------------------- tests/test_sonos.py | 12 ++++++++ 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72272e4..7292bce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ All notable changes to this driver. The format follows instead of returning as if the speaker had done it (#2). - `get_arp_table()` takes NAPALM's `vrf` argument (#2). - The driver module carries its docstring again (`__doc__` was `None`) (#2). +- `get_facts()`, `get_interfaces()` and `get_interfaces_ip()` no longer guess whether the + speaker is wired: SoCo offers no reading for it, and the check looked for an attribute + SoCo does not have, so it never found `eth0` anyway. The speaker reports one + interface, `wlan0`, described as "Network (wired or wireless unknown)" (#2). - `get_playback_state()` reports the speaker's real transport state (`PLAYING`, `PAUSED_PLAYBACK`, `STOPPED`, `TRANSITIONING`); it called a method SoCo does not have and answered `STOPPED` for every speaker (#9). diff --git a/README.md b/README.md index 5c6cb0a..2bd50ff 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,17 @@ with driver("192.0.2.20") as speaker: | Getter / action | | |---|---| | `get_facts` | from the speaker info read at `open()` | -| `get_interfaces`, `get_interfaces_ip` | `eth0` when the speaker is wired, always `wlan0` | +| `get_interfaces`, `get_interfaces_ip` | one interface, `wlan0`, for the speaker's network connection (see below) | | `get_playback_state`, `get_current_track` | yes | | `get_volume`, `set_volume`, `get_mute`, `set_mute` | yes | | `play`, `pause`, `stop`, `next_track`, `previous_track` | yes | | `get_group_info`, `get_zone_info` | yes | +Whether a speaker is wired or wireless is **not known**: SoCo offers no reading for it +(`deviceProperties` is the UPnP service of that name, and none of its actions says), so +the driver reports no `eth0`. The one interface keeps the name `wlan0`; it stands for the +speaker's connection, whatever its medium. + ## Not supported Configuration management (`load_replace_candidate` and `load_merge_candidate` raise diff --git a/napalm_sonos/sonos_driver.py b/napalm_sonos/sonos_driver.py index 6046aff..a45257c 100644 --- a/napalm_sonos/sonos_driver.py +++ b/napalm_sonos/sonos_driver.py @@ -24,6 +24,9 @@ _T = TypeVar("_T") logger = logging.getLogger(__name__) +#: The speaker's one network connection. Wired or wireless is not known (#2). +_INTERFACE = "wlan0" + class SonosDriver(MediaDriver): """NAPALM driver for controlling Sonos speakers. @@ -110,54 +113,29 @@ class SonosDriver(MediaDriver): "hostname": info.get("player_name", self.hostname), "fqdn": info.get("ip_address", self.hostname), "uptime": 0, - "interface_list": self._detect_interfaces(info), + "interface_list": [_INTERFACE], } - def _detect_interfaces(self, info: dict[str, Any]) -> list[str]: - ifaces: list[str] = [] - has_eth = self._check_wired_connection() - if has_eth: - ifaces.append("eth0") - ifaces.append("wlan0") - return ifaces - - def _check_wired_connection(self) -> bool: - if self._device is None: - return False - try: - props = self._device.device_properties - if isinstance(props, dict): - wifimode = props.get("WifiMode", "") - return wifimode in ("0", "", None) - except Exception: - # Broad catch: SoCo device_properties may fail for many reasons - logger.debug("Sonos device properties: ignored", exc_info=True) - return False - def get_interfaces(self) -> dict[str, dict[str, Any]]: - """Return interface information for this speaker.""" + """Return the speaker's one network connection, as ``wlan0``. + + Whether the speaker is wired or wireless is not known: SoCo offers no + reading for it (``deviceProperties`` is a UPnP service whose actions do + not say), so the driver reports no ``eth0`` and does not guess. The + name ``wlan0`` is kept for continuity; it stands for the connection, + whatever its medium. + """ if not self._speaker_info: return {} info = self._speaker_info mac = info.get("mac_address", "") is_up = self.is_alive().get("is_alive", False) - has_eth = self._check_wired_connection() interfaces: dict[str, dict[str, Any]] = {} - if has_eth: - interfaces["eth0"] = { - "is_up": is_up, - "is_enabled": True, - "description": "Ethernet", - "last_flapped": -1.0, - "speed": 100, - "mtu": 1500, - "mac_address": mac, - } - interfaces["wlan0"] = { + interfaces[_INTERFACE] = { "is_up": is_up, "is_enabled": True, - "description": "WiFi", + "description": "Network (wired or wireless unknown)", "last_flapped": -1.0, "speed": 0, "mtu": 1500, @@ -166,23 +144,18 @@ class SonosDriver(MediaDriver): return interfaces def get_interfaces_ip(self) -> dict[str, dict[str, Any]]: - """Return IP address information for the speaker interfaces.""" + """Return the speaker's address, on its one connection (see :meth:`get_interfaces`).""" if not self._speaker_info: return {} info = self._speaker_info ip = info.get("ip_address", self.hostname) - has_eth = self._check_wired_connection() ipv4_entry = { ip: { "prefix_length": 24, } } - interfaces: dict[str, Any] = {} - if has_eth: - interfaces["eth0"] = {"ipv4": ipv4_entry, "ipv6": {}} - interfaces["wlan0"] = {"ipv4": ipv4_entry, "ipv6": {}} - return interfaces + return {_INTERFACE: {"ipv4": ipv4_entry, "ipv6": {}}} def get_arp_table(self, vrf: str = "") -> list[dict[str, Any]]: """Return the ARP table (not supported on Sonos).""" diff --git a/tests/test_sonos.py b/tests/test_sonos.py index 4acd460..31459f3 100644 --- a/tests/test_sonos.py +++ b/tests/test_sonos.py @@ -106,6 +106,18 @@ class TestSonosDriver: assert ifaces["wlan0"]["is_up"] is True assert ifaces["wlan0"]["mac_address"] == "00:0E:58:AB:CD:EF" + def test_a_speaker_reports_no_eth0(self): + """SoCo has no reading that tells wired from wireless (#2): the driver no + longer guesses, not even from a dict-like WifiMode it used to look for.""" + self.mock_device.device_properties = {"WifiMode": "0"} + + with patch("socket.create_connection"): + interfaces = self.driver.get_interfaces() + assert "eth0" not in self.driver.get_facts()["interface_list"] + assert "eth0" not in interfaces + assert "eth0" not in self.driver.get_interfaces_ip() + assert list(interfaces) == ["wlan0"] + def test_get_interfaces_ip(self): ifaces_ip = self.driver.get_interfaces_ip() assert "wlan0" in ifaces_ip