How I hacked our SONOS music player and control it through email using Python
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.
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.
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.
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.
Modules and variables needed
All the modules and variables need are very straight forward.
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.
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
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.
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.
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.
Christmas songs function
Mellow songs function
And wait for the email
If all the validations are met it will execute it.
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