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
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:
1 parent
0ed6a7093d
commit
322b7cd688
3 files changed
+61
-33
No files matched your search
@@ -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
|
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
|
||||||
3.12 venv no longer has.
|
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
|
## [0.3.0] - 2026-10-23
|
||||||
|
|
||||||
The first release on PyPI.
|
The first release on PyPI.
|
||||||
|
|||||||
@@ -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.
|
"""NAPALM driver for Sonos speakers.
|
||||||
|
|
||||||
Provides network-automation-style access to Sonos speakers via the
|
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.
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -72,6 +72,12 @@ class SonosDriver(MediaDriver):
|
|||||||
self._device = None
|
self._device = None
|
||||||
self._speaker_info = 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]:
|
def is_alive(self) -> dict[str, bool]:
|
||||||
"""Check whether the speaker is reachable on the network."""
|
"""Check whether the speaker is reachable on the network."""
|
||||||
if self._device is None:
|
if self._device is None:
|
||||||
@@ -170,7 +176,7 @@ class SonosDriver(MediaDriver):
|
|||||||
interfaces["wlan0"] = {"ipv4": ipv4_entry, "ipv6": {}}
|
interfaces["wlan0"] = {"ipv4": ipv4_entry, "ipv6": {}}
|
||||||
return interfaces
|
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 the ARP table (not supported on Sonos)."""
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -340,10 +346,7 @@ class SonosDriver(MediaDriver):
|
|||||||
|
|
||||||
def set_volume(self, volume: int) -> None:
|
def set_volume(self, volume: int) -> None:
|
||||||
"""Set the volume (0-100)."""
|
"""Set the volume (0-100)."""
|
||||||
if self._device is None:
|
self._connected_device().volume = max(0, min(100, volume))
|
||||||
return
|
|
||||||
volume = max(0, min(100, volume))
|
|
||||||
self._device.volume = volume
|
|
||||||
|
|
||||||
def get_mute(self) -> bool:
|
def get_mute(self) -> bool:
|
||||||
"""Return whether the speaker is muted."""
|
"""Return whether the speaker is muted."""
|
||||||
@@ -357,39 +360,27 @@ class SonosDriver(MediaDriver):
|
|||||||
|
|
||||||
def set_mute(self, muted: bool) -> None:
|
def set_mute(self, muted: bool) -> None:
|
||||||
"""Mute or unmute the speaker."""
|
"""Mute or unmute the speaker."""
|
||||||
if self._device is None:
|
self._connected_device().mute = muted
|
||||||
return
|
|
||||||
self._device.mute = muted
|
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
"""Start playback."""
|
"""Start playback."""
|
||||||
if self._device is None:
|
self._connected_device().play()
|
||||||
return
|
|
||||||
self._device.play()
|
|
||||||
|
|
||||||
def pause(self) -> None:
|
def pause(self) -> None:
|
||||||
"""Pause playback."""
|
"""Pause playback."""
|
||||||
if self._device is None:
|
self._connected_device().pause()
|
||||||
return
|
|
||||||
self._device.pause()
|
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
"""Stop playback."""
|
"""Stop playback."""
|
||||||
if self._device is None:
|
self._connected_device().stop()
|
||||||
return
|
|
||||||
self._device.stop()
|
|
||||||
|
|
||||||
def next_track(self) -> None:
|
def next_track(self) -> None:
|
||||||
"""Skip to the next track."""
|
"""Skip to the next track."""
|
||||||
if self._device is None:
|
self._connected_device().next()
|
||||||
return
|
|
||||||
self._device.next()
|
|
||||||
|
|
||||||
def previous_track(self) -> None:
|
def previous_track(self) -> None:
|
||||||
"""Go back to the previous track."""
|
"""Go back to the previous track."""
|
||||||
if self._device is None:
|
self._connected_device().previous()
|
||||||
return
|
|
||||||
self._device.previous()
|
|
||||||
|
|
||||||
def get_group_info(self) -> dict[str, Any]:
|
def get_group_info(self) -> dict[str, Any]:
|
||||||
"""Return information about the Sonos group this speaker belongs to."""
|
"""Return information about the Sonos group this speaker belongs to."""
|
||||||
|
|||||||
+31
-1
@@ -1,6 +1,7 @@
|
|||||||
from unittest.mock import MagicMock, patch
|
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
|
from napalm_sonos import SonosDriver
|
||||||
|
|
||||||
@@ -179,3 +180,32 @@ class TestSonosDriver:
|
|||||||
self.driver.set_volume(42)
|
self.driver.set_volume(42)
|
||||||
self.mock_device.play.assert_called_once()
|
self.mock_device.play.assert_called_once()
|
||||||
assert self.mock_device.volume == 42
|
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"))
|
||||||
Reference in new issue
Block a user