fix(driver): refuse media controls when not connected (#2) #8

Merged
christianmanivong merged 1 commits from fix/bugs-2026-11 into main 2026-11-01 22:00:44 +00:00
3 changed files with 61 additions and 33 deletions

No files matched your search

+7
View File
@@ -12,6 +12,13 @@ All notable changes to this driver. The format follows
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
3.12 venv no longer has.
### Fixed
- `play()`, `pause()`, `stop()`, `next_track()`, `previous_track()`, `set_volume()`
and `set_mute()` raise `ConnectionClosedException` when the driver is not connected,
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).
## [0.3.0] - 2026-10-23
The first release on PyPI.
+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."""
+31 -1
View File
@@ -1,6 +1,7 @@
from unittest.mock import MagicMock, patch
from napalm.base.exceptions import ConnectionException
import pytest
from napalm.base.exceptions import ConnectionClosedException, ConnectionException
from napalm_sonos import SonosDriver
@@ -179,3 +180,32 @@ class TestSonosDriver:
self.driver.set_volume(42)
self.mock_device.play.assert_called_once()
assert self.mock_device.volume == 42
def test_the_driver_module_has_its_docstring():
import napalm_sonos.sonos_driver as module
assert module.__doc__ and "Sonos" in module.__doc__
def test_get_arp_table_takes_napalms_vrf_argument():
assert SonosDriver("192.168.1.100").get_arp_table(vrf="") == []
@pytest.mark.parametrize(
"control",
[
lambda d: d.play(),
lambda d: d.pause(),
lambda d: d.stop(),
lambda d: d.next_track(),
lambda d: d.previous_track(),
lambda d: d.set_volume(10),
lambda d: d.set_mute(True),
],
ids=["play", "pause", "stop", "next_track", "previous_track", "set_volume", "set_mute"],
)
def test_a_media_control_refuses_when_not_connected(control):
"""Returning silently made a control that did nothing look like it had worked."""
with pytest.raises(ConnectionClosedException):
control(SonosDriver("192.168.1.100"))