This commit is contained in:
+22
-1
@@ -52,6 +52,9 @@ class TargetError(ValueError):
|
||||
|
||||
|
||||
class Controller:
|
||||
# HEOS's source id for Spotify, in get_now_playing_media's payload.
|
||||
SPOTIFY_SID = "4"
|
||||
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.heos = HeosClient(cfg.HEOS_HOST, cfg.HEOS_PORT)
|
||||
@@ -375,6 +378,21 @@ class Controller:
|
||||
reply = self.heos.command("player/get_play_state", pid=self.playback_pid(key))
|
||||
return parse_message(reply["heos"]["message"]).get("state", "stop")
|
||||
|
||||
def on_spotify(self, key: str) -> bool:
|
||||
"""Whether a room's now-playing is a Spotify stream, playing or
|
||||
paused -- the only thing its play/pause and next buttons make sense
|
||||
for. An idle player can refuse the query outright, which is "not
|
||||
Spotify" too."""
|
||||
with self._lock:
|
||||
self._fresh()
|
||||
try:
|
||||
reply = self.heos.command("player/get_now_playing_media", pid=self.playback_pid(key))
|
||||
except HeosError:
|
||||
return False
|
||||
payload = reply.get("payload") or {}
|
||||
return (str(payload.get("sid")) == self.SPOTIFY_SID
|
||||
or str(payload.get("mid", "")).startswith("spotify:"))
|
||||
|
||||
def toggle_play(self, key: str, state: str = None) -> str:
|
||||
"""Start or stop a room. With no state, flips whatever it is doing
|
||||
now -- read from the speakers rather than trusted from the phone,
|
||||
@@ -487,12 +505,14 @@ class Controller:
|
||||
"grouped": key in joined,
|
||||
"volume": None,
|
||||
"play_state": None,
|
||||
"spotify": False,
|
||||
"error": None,
|
||||
}
|
||||
try:
|
||||
scope, obj_id = self._volume_handles(key)[0]
|
||||
room["volume"] = self._read_volume(scope, obj_id)
|
||||
room["play_state"] = self.get_play_state(key)
|
||||
room["spotify"] = self.on_spotify(key)
|
||||
except (TargetError, HeosError, KeyError) as exc:
|
||||
room["available"] = False
|
||||
room["error"] = str(exc)
|
||||
@@ -502,7 +522,8 @@ class Controller:
|
||||
snapshot["errors"].append(str(exc))
|
||||
snapshot["rooms"] = [
|
||||
{"key": key, "label": self.cfg.TARGETS[key]["label"], "available": False,
|
||||
"grouped": False, "volume": None, "play_state": None, "error": str(exc)}
|
||||
"grouped": False, "volume": None, "play_state": None, "spotify": False,
|
||||
"error": str(exc)}
|
||||
for key in self.cfg.ROOM_KEYS
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user