fix(driver): follow the media contract's shapes, raise on failed reads
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 4m21s
driver / test (3.13) (pull_request) Successful in 4m16s
driver / lowest (pull_request) Successful in 3m46s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m14s
CI / driver (pull_request) Successful in 6m10s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 4m21s
driver / test (3.13) (pull_request) Successful in 4m16s
driver / lowest (pull_request) Successful in 3m46s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m14s
CI / driver (pull_request) Successful in 6m10s
driver / publish (pull_request) Skipped
get_playback_state returned SoCo's transport string and get_zone_info
the speaker's details, where napalm-device-types' MediaDriver declares
{"state", "source"} and a list of {"name", "coordinator", "members"}.
Both now return those shapes; the speaker's details stay in get_facts.
get_group_info and get_zone_info caught every exception and answered
{}, so a failed read looked like a speaker in no group. They now raise
like the other getters (#9); {} and [] mean no group.
Closes #11
This commit is contained in:
1 parent
53ea5f051f
commit
218e2c7a7b
4 files changed
+176
-72
No files matched your search
@@ -27,6 +27,16 @@ logger = logging.getLogger(__name__)
|
||||
#: The speaker's one network connection. Wired or wireless is not known (#2).
|
||||
_INTERFACE = "wlan0"
|
||||
|
||||
#: UPnP AVTransport states, as napalm-device-types' media contract names them.
|
||||
#: NO_MEDIA_PRESENT is a speaker with nothing queued: stopped.
|
||||
_PLAYBACK_STATES = {
|
||||
"PLAYING": "playing",
|
||||
"PAUSED_PLAYBACK": "paused",
|
||||
"STOPPED": "stopped",
|
||||
"TRANSITIONING": "transitioning",
|
||||
"NO_MEDIA_PRESENT": "stopped",
|
||||
}
|
||||
|
||||
|
||||
class SonosDriver(MediaDriver):
|
||||
"""NAPALM driver for controlling Sonos speakers.
|
||||
@@ -314,10 +324,25 @@ class SonosDriver(MediaDriver):
|
||||
f"Sonos at {self.hostname} refused to report {what}: {exc}"
|
||||
) from exc
|
||||
|
||||
def get_playback_state(self) -> str:
|
||||
"""Return the transport state: PLAYING, PAUSED_PLAYBACK, STOPPED or TRANSITIONING."""
|
||||
def get_playback_state(self) -> dict[str, str]:
|
||||
"""Return the transport state and the source, as the media contract shapes it.
|
||||
|
||||
* state (string) - ``"playing"``, ``"paused"``, ``"stopped"`` or
|
||||
``"transitioning"``
|
||||
* source (string) - SoCo's music source in lower case: ``"library"``,
|
||||
``"radio"``, ``"line_in"``, ``"tv"``, ``"airplay"``, ``"none"``, …
|
||||
|
||||
:raises CommandErrorException: for a transport state outside these four
|
||||
(UPnP's recording states), or when the speaker refuses the read.
|
||||
"""
|
||||
info = self._read("the playback state", lambda d: d.get_current_transport_info())
|
||||
return str(info["current_transport_state"])
|
||||
transport = str(info["current_transport_state"])
|
||||
if transport not in _PLAYBACK_STATES:
|
||||
raise CommandErrorException(
|
||||
f"Sonos at {self.hostname} reports transport state {transport!r}"
|
||||
)
|
||||
source = str(self._read("the music source", lambda d: d.music_source))
|
||||
return {"state": _PLAYBACK_STATES[transport], "source": source.lower()}
|
||||
|
||||
def get_current_track(self) -> dict[str, Any]:
|
||||
"""Return information about the currently playing track."""
|
||||
@@ -360,46 +385,46 @@ class SonosDriver(MediaDriver):
|
||||
self._connected_device().previous()
|
||||
|
||||
def get_group_info(self) -> dict[str, Any]:
|
||||
"""Return information about the Sonos group this speaker belongs to."""
|
||||
if self._device is None:
|
||||
return {}
|
||||
try:
|
||||
group = self._device.group
|
||||
coordinator = group.coordinator
|
||||
members = group.members
|
||||
return {
|
||||
"coordinator": coordinator.player_name if coordinator else "",
|
||||
"coordinator_uid": coordinator.uid if coordinator else "",
|
||||
"members": [
|
||||
{
|
||||
"name": m.player_name,
|
||||
"uid": m.uid,
|
||||
"ip_address": m.ip_address,
|
||||
}
|
||||
for m in members
|
||||
],
|
||||
}
|
||||
except Exception:
|
||||
# Broad catch: SoCo group/network operations can fail in many ways
|
||||
return {}
|
||||
"""Return the Sonos group this speaker belongs to: its coordinator and members.
|
||||
|
||||
def get_zone_info(self) -> dict[str, Any]:
|
||||
"""Return detailed zone/speaker information."""
|
||||
if self._device is None:
|
||||
return {}
|
||||
try:
|
||||
info = self._speaker_info or {}
|
||||
return {
|
||||
"player_name": info.get("player_name", ""),
|
||||
"zone_name": info.get("zone_name", ""),
|
||||
"uid": info.get("uid", ""),
|
||||
"model_name": info.get("model_name", ""),
|
||||
"software_version": info.get("software_version", ""),
|
||||
"hardware_version": info.get("hardware_version", ""),
|
||||
"serial_number": info.get("serial_number", ""),
|
||||
"mac_address": info.get("mac_address", ""),
|
||||
"ip_address": info.get("ip_address", self.hostname),
|
||||
}
|
||||
except Exception:
|
||||
# Broad catch: speaker_info dict access is safe, but keep for safety
|
||||
return {}
|
||||
``{}`` for a speaker in no group.
|
||||
"""
|
||||
return self._read("the group", _group_info)
|
||||
|
||||
def get_zone_info(self) -> list[dict[str, Any]]:
|
||||
"""Return the zone this speaker plays in, as the media contract shapes it.
|
||||
|
||||
One entry: ``name`` (the speaker's room), ``coordinator`` (whether it
|
||||
leads its group) and ``members`` (the names of the other speakers in the
|
||||
group). ``[]`` for a speaker in no group.
|
||||
"""
|
||||
return self._read("the zone", _zone_info)
|
||||
|
||||
|
||||
def _group_info(device: Any) -> dict[str, Any]:
|
||||
group = device.group
|
||||
if group is None:
|
||||
return {}
|
||||
coordinator = group.coordinator
|
||||
return {
|
||||
"coordinator": coordinator.player_name if coordinator else "",
|
||||
"coordinator_uid": coordinator.uid if coordinator else "",
|
||||
"members": [
|
||||
{"name": m.player_name, "uid": m.uid, "ip_address": m.ip_address} for m in group.members
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _zone_info(device: Any) -> list[dict[str, Any]]:
|
||||
group = device.group
|
||||
if group is None:
|
||||
return []
|
||||
uid = device.uid
|
||||
coordinator = group.coordinator
|
||||
return [
|
||||
{
|
||||
"name": device.player_name,
|
||||
"coordinator": coordinator is not None and coordinator.uid == uid,
|
||||
"members": sorted(m.player_name for m in group.members if m.uid != uid),
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user