fix(driver): refuse media controls when not connected
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m18s
driver / test (3.12) (pull_request) Successful in 3m14s
driver / test (3.13) (pull_request) Successful in 2m54s
driver / lowest (pull_request) Successful in 2m23s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Successful in 4m53s
driver / publish (pull_request) Skipped

play() and the other controls returned silently without a device, so
a control that did nothing looked like it had worked. They now raise
ConnectionClosedException. get_arp_table() takes NAPALM's vrf
argument, and the module docstring moves above the imports so
__doc__ is set again.

The wired-speaker part of the issue stays open: SoCo's
deviceProperties is a UPnP service, not a dict with WifiMode, so
renaming the attribute alone would still never report eth0.

Refs #2
This commit is contained in:
Christian Manivong committed 2026-11-01 21:53:55 +01:00
1 parent 0ed6a7093d
commit 322b7cd688
3 files changed
+61 -33

No files matched your search

+23 -32
View File
@@ -1,12 +1,3 @@
from __future__ import annotations
import logging
import socket
from typing import Any
from napalm.base.exceptions import ConnectionException
from napalm_device_types import FingerprintRule, MediaDriver, PortSpec
"""NAPALM driver for Sonos speakers.
Provides network-automation-style access to Sonos speakers via the
@@ -14,6 +5,15 @@ SoCo library, exposing NAPALM standard methods alongside Sonos-specific
media-playback controls.
"""
from __future__ import annotations
import logging
import socket
from typing import Any
from napalm.base.exceptions import ConnectionClosedException, ConnectionException
from napalm_device_types import FingerprintRule, MediaDriver, PortSpec
logger = logging.getLogger(__name__)
@@ -72,6 +72,12 @@ class SonosDriver(MediaDriver):
self._device = None
self._speaker_info = None
def _connected_device(self) -> Any:
"""Return the SoCo device; raise if :meth:`open` has not connected it."""
if self._device is None:
raise ConnectionClosedException(f"Sonos at {self.hostname} is not connected")
return self._device
def is_alive(self) -> dict[str, bool]:
"""Check whether the speaker is reachable on the network."""
if self._device is None:
@@ -170,7 +176,7 @@ class SonosDriver(MediaDriver):
interfaces["wlan0"] = {"ipv4": ipv4_entry, "ipv6": {}}
return interfaces
def get_arp_table(self) -> list[dict[str, Any]]:
def get_arp_table(self, vrf: str = "") -> list[dict[str, Any]]:
"""Return the ARP table (not supported on Sonos)."""
return []
@@ -340,10 +346,7 @@ class SonosDriver(MediaDriver):
def set_volume(self, volume: int) -> None:
"""Set the volume (0-100)."""
if self._device is None:
return
volume = max(0, min(100, volume))
self._device.volume = volume
self._connected_device().volume = max(0, min(100, volume))
def get_mute(self) -> bool:
"""Return whether the speaker is muted."""
@@ -357,39 +360,27 @@ class SonosDriver(MediaDriver):
def set_mute(self, muted: bool) -> None:
"""Mute or unmute the speaker."""
if self._device is None:
return
self._device.mute = muted
self._connected_device().mute = muted
def play(self) -> None:
"""Start playback."""
if self._device is None:
return
self._device.play()
self._connected_device().play()
def pause(self) -> None:
"""Pause playback."""
if self._device is None:
return
self._device.pause()
self._connected_device().pause()
def stop(self) -> None:
"""Stop playback."""
if self._device is None:
return
self._device.stop()
self._connected_device().stop()
def next_track(self) -> None:
"""Skip to the next track."""
if self._device is None:
return
self._device.next()
self._connected_device().next()
def previous_track(self) -> None:
"""Go back to the previous track."""
if self._device is None:
return
self._device.previous()
self._connected_device().previous()
def get_group_info(self) -> dict[str, Any]:
"""Return information about the Sonos group this speaker belongs to."""