Sonos Command Line Controller Available (Python soco-cli)


Userlevel 7
Badge +20
  • Virtuoso
  • 1725 replies

I’ve created a simple command line control program for Sonos, implemented in Python. I find it very useful for creating a variety of Sonos control scripts (e.g., using cron to run scripts at specific times), and sometimes just for quickly controlling my Sonos systems when I don’t want to start the app.

It works with S1, S2, and split Sonos systems, and it should run on any OS that supports Python 3.

If you’re comfortable using Python and the command line, you may find it useful, too. It’s quite powerful, with a wide range of control actions, yet simple to install and use.

GitHubhttps://github.com/avantrec/soco-cli

PyPihttps://pypi.org/project/soco-cli

This is an early release, so expect some bugs and limitations. I’d welcome your feedback and suggestions.


13 replies

Userlevel 3
Badge +9

Hi @pwt, thanks for sharing that with us and greatly appreciate all your efforts. If you need anything just let us know. Thanks!

Userlevel 7
Badge +20

Hi @pwt, thanks for sharing that with us and greatly appreciate all your efforts. If you need anything just let us know. Thanks!

Thanks. I’ve done substantial additional work on the utility, and it’s in quite good shape now, with several Improvements and new features. I’m finding it fun to use, and useful. There’s good documentation on the GitHub page.

if anyone has any questions or suggestions, please feel free to ask them here.

I knew you were active with Soco…. this is a very useful extension / alternative method of a quickly executing commands, thank you.

 

Now, just need to think how I can use this beyond what I already do in SoCo

Userlevel 7
Badge +20

Now, just need to think how I can use this beyond what I already do in SoCo

Great! Please do report any problems or suggest ideas for enhancements.

 

Here’s a handful of quick examples to whet the appetites of others.

1. Group the Bathroom with the Bedroom, play a favourite radio station until a specific time, then stop the stream and ungroup:

C:> sonos Bathroom group Bedroom : Bedroom play_fav “Radio 4” : wait_until 09:00 : Bedroom stop : Bathroom ungroup

2. Set the sleep timer for a specific time:

c:> sonos Bedroom sleep_at 23:45

3. Reset speaker volumes overnight:

C:> sonos wait_until 03:00 : Bedroom vol 25 : Bathroom vol 25

4. Set a speaker to play another’s Line-In, and play for an hour:

C:> sonos Bedroom line_in Lounge : Bedroom play : wait 01:00 : Bedroom stop

 

Badge

Great work. I’ve been using soco for a while now, but this makes life a lot easier - thanks!

Only thing I’ve found so far that doesn’t seem to work, and is probably more of a documentation issue, is that the github page suggests you can run:

sonos SPEAKER version

This gives ‘Error: Action 'version' not found’.

Of course sonos --version works fine

Userlevel 7
Badge +20

Only thing I’ve found so far that doesn’t seem to work, and is probably more of a documentation issue, is that the github page suggests you can run:
  sonos SPEAKER version
  This gives ‘Error: Action 'version' not found’.

Thank you very much for finding and reporting this. I’ve corrected the documentation now.

Badge

I noticed there have been a couple of updates over the last few days, inlcuding the addition of ‘list_playlist_tracks’. This works great, except I’ve got a couple of playlists that include one of more spaces (e.g. ‘Saturday Afternoon Mix’), and I can’t find a way for the sonos command to accept this as a valid playlist name. No matter what quotes I use, or escaping the spaces it errors with:

Error: Action 'list_playlist_tracks' takes 1 parameter(s)

Also, it only seems to show me the first 100 songs in the playlist. Most of my playlists have considerably more than this. I seem to remember that soco displays 100 by default, but it can be increased I think?

Userlevel 7
Badge +20

Thanks for testing!

I noticed there have been a couple of updates over the last few days, inlcuding the addition of ‘list_playlist_tracks’. This works great, except I’ve got a couple of playlists that include one of more spaces (e.g. ‘Saturday Afternoon Mix’), and I can’t find a way for the sonos command to accept this as a valid playlist name. No matter what quotes I use, or escaping the spaces it errors with:

Error: Action 'list_playlist_tracks' takes 1 parameter(s)

It should be possible to enclose the playlist name in double quotes, i.e., “Saturday Afternoon Mix”. On Linux and macOS either single or double quotes can be used, but Windows requires doubles.

Also, it’s possible to use fuzzy matching with partial playlist names (without spaces), provided they’re not ambiguous.

Also, it only seems to show me the first 100 songs in the playlist. Most of my playlists have considerably more than this. I seem to remember that soco displays 100 by default, but it can be increased I think?

I’ll include a fix for this (along with some other goodies) in a release later today.

Badge

 

It should be possible to enclose the playlist name in double quotes, i.e., “Saturday Afternoon Mix”. On Linux and macOS either single or double quotes can be used, but Windows requires doubles.

Also, it’s possible to use fuzzy matching with partial playlist names (without spaces), provided they’re not ambiguous.

 

 

My mistake - quoting does work, and thanks for the tip regarding fuzzy matching.

Userlevel 7
Badge +20

Also, it only seems to show me the first 100 songs in the playlist. Most of my playlists have considerably more than this. I seem to remember that soco displays 100 by default, but it can be increased I think?

Just FYI, v0.1.22_1 is released now, with a fix for the issue above.

There’s also a new ‘wait_stopped_for’ action, and the addition of a simple looping  primitive.

Userlevel 7
Badge +20

FYI: Plenty of new features added over the last few days:

https://github.com/avantrec/soco-cli/blob/master/CHANGELOG.txt

Here’s a little automation usage example: my wife wants the speakers in our bedroom to revert to a specific radio channel after use, so she can just walk in and press the speaker button to start playback. Here’s the script I run:

sonos Bedroom wait_start : Bedroom wait_stopped_for 2m : Bedroom mute on : Bedroom play_fav "Radio 4" : Bedroom stop : Bedroom mute off : loop

I just leave this running on one of my Raspberry Pis. It detects when playback has stopped for a couple of minutes, tees up the default radio station, then loops back.

Userlevel 4
Badge +1

I like this very much.  Now just need to whack a web server on it and a few web pages to call the python.  There goes my weekend....

Userlevel 7
Badge +20

I like this very much.  Now just need to whack a web server on it and a few web pages to call the python.  There goes my weekend....

Great. Please let me know of any issues, and any extra features you’d like to see.

Reply