How I hacked our SONOS music player and control it through email using Python

Robert Canare
4 min readOct 25, 2020

I didn’t hack the SONOS speaker, I just found a way to control it through email or to play Christmas and mellow songs just to have fun.

actual photo

Based on the SONOS official website, you should download the SONOS app to manage the device, then after installing it I was not able to add a SONOS device that it’s on a different network. For example, if your current IP is on the 192.168.200.0/24 network and the device is on a different network you wouldn’t control it using the software.

Then the challenge was how can I control the SONOS speaker on the different VLANs or control it remotely.

SONOS software

So I came up with the idea of controlling it through IMAP.

Enabling IMAP setting on GMAIL

Before I started coding I enabled IMAP on my GMAIL account, so I can retrieve all the emails from my inbox using Python.

Gmail IMAP settings

Goal
My goal was so simple for now, I just want to play Christmas and mellow songs, for now.

And we can add more features to the code in the future.

Creating a playlist on SONOS using the software

Before I changed back my VLAN to work VLAN, I created a playlist first on the SONOS. I created Christmas and mellow songs playlist.

So the playlist IDs are 8 and 1.

Command patterns would look like

sonos play christmas = Very straight forward it will play a lovely christmas songs
sonos play mellow = And it will play English old songs

SoCo module

After Googling, I found this SoCO Python module the documentation is very straight forward and easy to understand.

SoCo Python Module

Modules and variables needed

All the modules and variables need are very straight forward.

On the actual source code, module, and libraries

Read IMAP function

This is the very hard part of the code, I’m having a hard time to extract the subject or content of the email. This function will just return the last 2 emails from the inbox.

On the actual source code, reading IMAP

Output

Extracting the commands and put it on variable

For further validation, I set the email subject into 3 parts and put it in a variable.

sonos_validation sonos_command sonos_value
On the actual source code, extracting data from IMAP

Output

Validate the email if it’s for our SONOS

On this part, I make sure all the emails that dedicated to SONOS will execute and disregard other emails then return a boolean and the command.

On the actual source code, check if it’s an email for the SONOS

Output

Verify the command if it’s executed

Verifying if the command was executed and if not, it will add it to the command text file so it won’t execute it again.

On the actual source code, check if it’s executed already

Output

Fading out the volume and clearing the current queue songs

On this part, it will lessen the volume by 5 then clearing all queues and increment the volume up by 5.

On the actual source code, fading and clearing the queue

Christmas songs function

On the actual source code, play Christmas songs

Mellow songs function

On the actual source code, play mellow songs

And wait for the email

If all the validations are met it will execute it.

On the actual source code, just wait and check for the new email

Current inbox

My current inbox contains just 2 emails for SONOS, the script will execute the most recent one.

LOL

Source code

https://github.com/robertcanare/So-Bot-X-/blob/main/sobot-x.py

--

--