Roon Control via Siri, SSH Shortcuts, and Roon API

Several users have posted various ways to use Siri to control Roon. These usually take the form of a fake device in HomeBridge that then communicates with HomeKit and Siri. This seems cool but maybe overkill. I was able to get Siri voice control of Roon working with simple SSH shortcuts that execute Python scripts which utilize the Roon API to control Roon. People don’t seem to know about Apple’s SSH shortcuts. They can be used to execute commands on systems that allow SSH access. I used an Ubuntu 20.10 system recently installed to install the Python Roon API project (pip install roonapi) and quickly cobbled together a Python script based on one of the examples in that project. The Python script accepts an argument specifying an artist name in my Roon library. It then uses the Roon API to play music from my library by that artist. Currently the zone is hard coded in the Python script.

On my iPhone I then created a couple of shortcuts which use the “Run script over SSH” option for Apple Scripting shortcuts. The shortcuts execute a Bash command which is a shell script that executes the Python script with appropriate arguments. It all seems to work. I did run into a few gotchas. It seems with current Python/Pip installs you need to specify an environment variable telling Python where the Pip modules are located. So I needed a login shell to pick that up but I could not figure out how to tell the Apple shortcut to give me an SSH login shell. The Python env variable is set in the shell script the SSH session executes.

Anyway, I easily overcame these small difficulties and have an initial couple of shortcuts that play Jethro Tull and Pink Floyd triggered by Siri voice commands. I have a lot to learn about the Roon API but it seemed fairly easy to get started. Thanks!

Here’s the details.

Example iOS SSH Shortcut command:

bash -l -c “play_artist ‘Pink Floyd’”

Example play_artist shell script (needs to be in your PATH):

#!/bin/bash

ROON=$HOME/Roon
PLAY=play_artist.py
ARTIST=“Deep Purple”

export PYTHONUSERBASE=/home/doctor/Python3

[ -d $ROON ] || exit 1

cd $ROON

[ -f $PLAY ] || exit 2

[ “$1” ] && ARTIST="$1"

python $PLAY -a “$ARTIST”

Example play_artist.py Python script to call Roon API:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", “–artist”, help=“artist selection”)
args = parser.parse_args()
if args.artist:
artist = args.artist
else:
artist = “Jethro Tull”

from roonapi import RoonApi
appinfo = {
“extension_id”: “python_roon_test”,
“display_name”: “Python library for Roon”,
“display_version”: “1.0.0”,
“publisher”: “gregd”,
“email”: “mygreat@emailaddress.com”,
}

server = “xxx.xx.xx.xxx” # Set this to your Roon Core server IP
target_zone = “Living Room” # Set this to the name of the zone you want
# Can be None if you don’t yet have a token
token = open(“mytokenfile”).read()

roonapi = RoonApi(appinfo, token, server)
# get target zone output_id
zones = roonapi.zones
output_id = [
output[“zone_id”]
for output in zones.values()
if output[“display_name”] == target_zone
][0]
print(“OUTPUT ID”, output_id)
# Play artist from Library
print(“SINGLE ARTIST”)
items = roonapi.play_media(output_id, [“Library”, “Artists”, artist])
# save the token for next time
with open(“mytokenfile”, “w”) as f:
f.write(roonapi.token)

That’s it. I hope to improve this over time but for now I’m pretty happy to get something working in my free time on a slow Thursday. Hope the code formats ok, I’m new here and haven’t yet figured out how to format these postings properly. Let me know how I should proceed or if there is an easier way to do the same thing.

2 Likes

I’ve added these initial versions of Bash and Python scripts to my Scripts repository on Gitlab. These can be downloaded in Zip format by visiting:

https://gitlab.com/doctorfree/Scripts/-/archive/master/Scripts-master.zip?path=Roon

Or visit my Scripts repository at Ron Record / Scripts · GitLab and click on the Roon folder to download in other formats.

Let me know if you see anything incorrect or if you have suggestions for improvement.

Cool! I got my basic control commands for Roon set up in homebridge. Works pretty reliably.

I‘ve also played with various methods getting artists parsed into Siri for playback. The problem with all of those is that they are pretty static, ie. you have to write one script for each artist. which gets pretty tedious if you have a library of 2500 artists :joy: So, I gave up.

Would be great if anyone could figure how to parse variables from Siri into those scripts. I dont think its poss atm.

Very cool, I’m not an Apple/Sri user but this still has my attention as that’s just the trigger. The scripting approach looks v. Interesting

HomeBridge seems like a good solution if you are using it to connect multiple devices not yet supported in Apple HomeKit. I implemented this SSH Shortcut solution as an alternative to HomeBridge as it seemed simpler and all I wanted was this one use case - controlling Roon via Siri. Not sure if HomeBridge still would be a better solution, depends on which is more stable and flexible and easy to use.

You bring up an issue that I was going to try and address next - my Roon control shortcuts are currently statically configured to play specific artists, one shortcut per artist. Same for zones and it would be similar for albums. I am looking at Shortcut Inputs as a possible way to add a variable to the shortcut which would get its value from the spoken command. I haven’t used Shortcut Inputs before so I am just learning. Yes, having 2500 shortcuts for 2500 artists in my library would be unwieldy. Maybe it’s not possible. Did you try using a Shortcut Input?

The use of a command line script to invoke a Python script to send commands to the Roon API, all of that back-end scripting is not necessarily tied to Apple or Siri. I’m doing the Apple Shortcuts and Siri activation bits just because that is what I have. One could implement a similar solution using any device and speech input system that could be configured to execute an SSH command. The Bash and Python scripts as well as the Roon API are all independent of front-end device and voice assistant.

I tried python scripts but didnt progress with passing variables. Hope you do in one shape or another :grinning:

As I have a homebridge running anyway and know my way around there a bit I used that road for the main Roon commands

The latest versions of my Python scripts accept arguments and parse them. Right now I can pass in an artist name and a Roon zone name as arguments. The argument processing on the Python side is easy, it’s getting the name of an artist or zone passed in to the SSH Shortcut that I am trying to figure out. I mean without hard coding it in the shortcut.

Here is a snippet of Python code that does the initial argument processing:

import argparse

# Set to IP address of your Roon Core

server = “x.x.x.x”
# Name of the file that holds a Roon API token
tokenfile = “roontoken.txt”

parser = argparse.ArgumentParser()
parser.add_argument("-a", “–artist”, help=“artist selection”)
parser.add_argument("-z", “–zone”, help=“zone selection”)
args = parser.parse_args()

if args.artist:
artist = args.artist
else:
artist = “Jethro Tull”
if args.zone:
target_zone = args.zone
else:
target_zone = “Living Room”

1 Like

Setup guides for voice control of Roon using Siri and/or Google Assistant are now available. See:

Hi @Ronald_Record

@Klaus_Engel and me currently have rooWatch (an Apple Watch App as part of https://rooExtend.com) in betatest with some interested community members.

It supports Roon Control by Siri via an Apple Watch App. Will be published next week :+1:

Maybe it makes sense if @Klaus_Engel and you get in contact to get a common view to the voice control.

Best DrCWO

1 Like

@DrCWO sounds interesting. I look forward to learning more about rooWatch. Currently I use the HomeRun App on my Apple Watch to run HomeKit Scenes from my watch. All of these different methods work with some effort in setup and configuration but I wish they were more seamlessly integrated and user friendly. Hopefully things will improve over time but for now it sure is fun tinkering!