chore: adopt the NAPALM driver standard #6

Merged
christianmanivong merged 4 commits from chore/driver-standard into main 2026-10-23 14:42:36 +00:00
2 changed files with 9 additions and 4 deletions
Showing only changes of commit a6d1d1e597 - Show all commits

No files matched your search

+8 -3
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import logging
import socket
from typing import Any
@@ -13,6 +14,8 @@ SoCo library, exposing NAPALM standard methods alongside Sonos-specific
media-playback controls.
"""
logger = logging.getLogger(__name__)
class SonosDriver(MediaDriver):
"""NAPALM driver for controlling Sonos speakers.
@@ -33,7 +36,9 @@ class SonosDriver(MediaDriver):
FingerprintRule("rincon", weight=5.0),
]
def __init__(
# A Sonos speaker takes no login: username and password exist only for NAPALM's
# constructor signature, and the empty default is no credential.
def __init__( # nosec B107
self,
hostname: str,
username: str = "",
@@ -60,7 +65,7 @@ class SonosDriver(MediaDriver):
self._speaker_info = self._device.get_speaker_info()
except Exception as e:
# Broad catch: SoCo can raise a variety of transport/XML errors
raise ConnectionException(f"Cannot connect to Sonos at {self.hostname}: {e}")
raise ConnectionException(f"Cannot connect to Sonos at {self.hostname}: {e}") from e
def close(self) -> None:
"""Close the connection to the Sonos speaker."""
@@ -112,7 +117,7 @@ class SonosDriver(MediaDriver):
return wifimode in ("0", "", None)
except Exception:
# Broad catch: SoCo device_properties may fail for many reasons
pass
logger.debug("Sonos device properties: ignored", exc_info=True)
return False
def get_interfaces(self) -> dict[str, dict[str, Any]]:
+1 -1
View File
@@ -52,7 +52,7 @@ class TestSonosDriver:
mock_soco.side_effect = Exception("Connection refused")
try:
driver.open()
assert False, "Expected ConnectionException"
raise AssertionError("Expected ConnectionException")
except ConnectionException:
pass