Controlling Sonos via a Google Home and Google Assistant

I just bought a Google Home Mini to play with while they’re on a Black Friday offer.  They don’t connect to Sonos by default yet, so I thought I’d try to do something about it …

It wasn’t an extremely quick job, but pretty painless nevertheless (on my Fedora 26 box).

Job 1:  command-line control of the Sonos

There’s a Python library (soco) and command line (socos) which I’ve tinkered with in the past to do this.  (appropriate sudo prefixes presumed!)

pip install soco
pip install socos

Test it (note, socos won’t take anything but IP addresses [see the Python script later]):

echo -e "set SONOS_IP\nplay" | socos

You can see the commands with socos help.

Step 2:  CGI wrapper

Here’s my script (put into /path/to/ifttt/, see later), you can add custom amendments to the things that socos accepts, especially changing Sonos names to IP addresses.

It’s a little over-complicated as I first thought it had to run multiple commands to socos to (e.g.) set the device and then pass a command, but most commands take an IP address as a final argument.  I’ve left it that way it is in case I need the flexibility in the future.

#!/usr/bin/env python
import cgi
import fileinput
import re
import subprocess
import sys
# Send a proper HTML response back to the client
print "Content-Type: text/plain\r\n"
print "\r\n"
# Read the commands and pass them to socos
commands = 0
proc = subprocess.Popen( ['socos'], shell=True,
  stdin=subprocess.PIPE, stdout=subprocess.PIPE )
for line in fileinput.input():
  commands = commands+1
  for segment in line.strip().split('|'):
    # Custom amendments to what is said
    cmd = re.sub( " *\\bplease\\b *", "", segment \
      .replace('lounge','LOUNGE_IP') \
      .replace('play3','PLAY3_IP') \
      .replace('volume up','volume +25') \
      .replace('volume down','volume -25') \
      + "\n" )
    proc.stdin.write( cmd )
# Flush output, removing socos prompts
for line in iter( proc.stdout.readline, '' ):
  output = re.sub( "\\bsocos.*?>", "", line.rstrip() )
  if len(output):
    print output
# Done
if commands == 0:
  print( "No commands received" )

Test it:

echo -e "set play3|status" | /path/to/ifttt/

Step 3:  Web accessible

I’m not gonna help you get Apache running!  Once you have, this is the config I used:

# /etc/httpd/conf.d/ifttt.conf
Alias /ifttt /path/to/ifttt
<directory "="" path="" to="" ifttt"="">
 Options +FollowSymLinks +ExecCGI -Indexes
 AddHandler cgi-script .cgi .pl .py
 AuthType Basic
 AuthName "IFTTT"
 AuthUserFile /path/to/auth/iftt.auth
 Require valid-user
 AllowOverride All
 Order allow,deny
 Allow from all

Set up a password:

htpasswd -c -b /path/to/auth/ifttt.auth USERNAME PASSWORD

“ifttt”?  You can see where I’m going ?.

And test the Python script again, but via Apache:

echo -e "set play3\nstate" | curl -X POST --data-binary @- --basic \

Step 4:  IFTTT

Sign up to If This Then That (I used my Google login to make things easier).  Create an applet that uses Google Assistant (you’ll have to link your IFTTT account with your Google one), and start with:

Say a phrase with a text ingredient
What do you want to say?  sonos three $
What do you want the Assistant to say in response?  OK, telling Sonos the command $
Link to Webhooks

Make a web request
Method  POST
Content Type  text/plain
Body  {{TextField}} play3

NB: alternative for more complex commands (and my original setting) — use the pipe to separate multiple socos commands:

Body  set play3|{{TextField}}

Step 5:  Profit!

Or not.  I think that’s all I did (aside from wads of trial and error and diagnosing network issues, like I tried using Digest auth. not Basic, and that didn’t work, or failing to POST multi-line commands, so having to add the fake pipe (|) separator.

You ought now to be able to say something like “Hey Google, Sonos Three pause” or  “Hey Google, Sonos Three volume up”.

I have a Sonos Play 3 (the above examples) called “play 3” and a Sonos Connect (called “lounge”) — I’ll have to add another near identical IFTTT applet to control the lounge one.

Not a bad morning’s work!


About fnx

PSN: the_phoenix
This entry was posted in Software, Tech and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *