fix: what bandit and the remaining lint rules found
- open() raises ConnectionException from the SoCo error (B904), so the cause stays attached. - _check_wired_connection: the except Exception: pass (B110) now logs at debug level, so a swallowed failure leaves a trace. A module logger is new. - nosec for the empty password default (B107): a Sonos speaker takes no login, the argument exists for NAPALM's constructor signature. - tests: raise AssertionError instead of assert False (B011).
This commit is contained in:
1 parent
ad69c0e643
commit
a6d1d1e597
2 files changed
+9
-4
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
import socket
|
import socket
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -13,6 +14,8 @@ SoCo library, exposing NAPALM standard methods alongside Sonos-specific
|
|||||||
media-playback controls.
|
media-playback controls.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class SonosDriver(MediaDriver):
|
class SonosDriver(MediaDriver):
|
||||||
"""NAPALM driver for controlling Sonos speakers.
|
"""NAPALM driver for controlling Sonos speakers.
|
||||||
@@ -33,7 +36,9 @@ class SonosDriver(MediaDriver):
|
|||||||
FingerprintRule("rincon", weight=5.0),
|
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,
|
self,
|
||||||
hostname: str,
|
hostname: str,
|
||||||
username: str = "",
|
username: str = "",
|
||||||
@@ -60,7 +65,7 @@ class SonosDriver(MediaDriver):
|
|||||||
self._speaker_info = self._device.get_speaker_info()
|
self._speaker_info = self._device.get_speaker_info()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Broad catch: SoCo can raise a variety of transport/XML errors
|
# 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:
|
def close(self) -> None:
|
||||||
"""Close the connection to the Sonos speaker."""
|
"""Close the connection to the Sonos speaker."""
|
||||||
@@ -112,7 +117,7 @@ class SonosDriver(MediaDriver):
|
|||||||
return wifimode in ("0", "", None)
|
return wifimode in ("0", "", None)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Broad catch: SoCo device_properties may fail for many reasons
|
# Broad catch: SoCo device_properties may fail for many reasons
|
||||||
pass
|
logger.debug("Sonos device properties: ignored", exc_info=True)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_interfaces(self) -> dict[str, dict[str, Any]]:
|
def get_interfaces(self) -> dict[str, dict[str, Any]]:
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ class TestSonosDriver:
|
|||||||
mock_soco.side_effect = Exception("Connection refused")
|
mock_soco.side_effect = Exception("Connection refused")
|
||||||
try:
|
try:
|
||||||
driver.open()
|
driver.open()
|
||||||
assert False, "Expected ConnectionException"
|
raise AssertionError("Expected ConnectionException")
|
||||||
except ConnectionException:
|
except ConnectionException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user