Deprecated native solution has been integrated.
Had some time to come up with and code a solution to the extra start calls. the script will no longer produce any unnecessary calls to change the output device between tracks.
import requests
import sys
import time
#replace with the hostname or IP of your raspberry pi.
#if your tautulli install is a docker container you'll probably have to use an IP address
rpi_host = "172.16.0.34" #replace with host name or ip of your raspberry pi
rpi_port = "32500"
rpi_path = "/settings"
rpi_url = "http://" + rpi_host + ":" + rpi_port + rpi_path
rpi_player_name = "rpi audio desk" #<--- this is the name of my player change for yours
#information for tautulli requests
tautulli_host = "172.16.0.32" #replace with the host name or IP of your tautulli install
tautulli_port = "8181"
tautulli_path = "/api/v2"
api_key = "c996af6732dc45578fbf52dc8d58ccae" #found in settings -> Web Interface
tautulli_url = "http://" + tautulli_host + ":" + tautulli_port + tautulli_path
device = "0,0" #default device
if len(sys.argv) > 1:
device = sys.argv[1]
make_request = True
if len(sys.argv) > 2 and sys.argv[2] == "start":
response = requests.get(rpi_url)
if response.status_code == 200:
current_device = response.json()["audioDeviceUuid"]
make_request = device not in current_device #if device is not a sub string of current device set to True
#wait 1 second then make a request to see if playback has actually stopped
elif len(sys.argv) > 2 and sys.argv[2] == "stop":
time.sleep(1)
params = {
"apikey": api_key,
"cmd": "get_activity"
}
response = requests.get(tautulli_url, params=params)
if response.status_code == 200:
sessions = response.json()["response"]["data"]["sessions"]
#if the session still exists break if it doesn't set make_requet to false
for session in sessions:
if session["player"] == rpi_player_name and session["state"] == "playing":
make_request = False
break
if make_request:
params = {
"name": "audioDeviceUuid",
"value": "hw:" + device
}
requests.put(rpi_url, params=params)
in addition to the change to the playback stop argument change from the previous point add a space and the word "start the playback start argument.
I’d like to get to the point where the script can get the current playing status from the rpi directly instead of having to make a get request to tautulli but I’ll leave that for another day.
