Sonos Command Line Controller Available (Python soco-cli)


Userlevel 7
Badge +20
  • Virtuoso
  • 1732 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.


18 replies

Userlevel 5
Badge +13

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.

Userlevel 7
Badge +20

Just to note that I’ve added a cue_favourite action in soco-cli v0.1.28.

This silently cues up one of your Sonos Favourites so that it plays next time you press ‘play’ on the speaker. It’s convenient shorthand for the sequence: mute speaker, play_favourite, stop playback, unmute speaker. I found myself repeating this action sequence in several scripts, so decided to encode it in a single action.

Example: If I like to listen to Radio 4 sometime after 7am in the Kitchen. I can cue up the station ready to respond to hitting the play button, with the following script:

sonos wait_until 0700 : Kitchen cue_fav “Radio 4”

 

Userlevel 7
Badge +20

Another quick update: I’ve now added direct shuffle and repeat actions to inspect and set your speakers’ shuffle and repeat modes, as follows:

sonos <speaker_name> shuffle <on|off>
sonos <speaker_name> repeat <off|one|all>

Using the actions without parameters will return the current state.

These actions are more convenient than the previous method, which combined the shuffle and repeat states (because this is how SoCo and the native Sonos API work).

The new actions are available in soco-cli v0.1.32 and later. Please see the (extensive) documentation for more details:

https://github.com/avantrec/soco-cli/blob/master/README.md 

Userlevel 7
Badge +20

Just to summarise recent changes: enhancements to queue management, selection from searches, favourite radio stations, and Line In control. Change log shown below. Feedback and enhancement requests are welcome.

v0.1.38   - Add search caching and indexed playback for 'tracks_in_album'
- Add search caching and indexed playback for 'list_albums'
- Add search caching and indexed playback for 'search_artists'
- Add '--docs' option to print URL to online documentation
- Add 'soco-discover' synonym for 'sonos-discover'
- Add line_in 'right_input' parameter for stereo paired P:5/Fives
- Behaviour change: Line-In starts playback after being selected
v0.1.37 - Fix 'play_favourite_radio_station'
- Improve output from 'track' action for non-queue items
- Add 'cue_favourite_radio_station' action
v0.1.36 - Add 'last_search' action to cache track and album searches
- Add 'queue_last_search_number' action
v0.1.35 - Add 'queue_position' action
- Add 'play_next' option to 'queue_track' and 'queue_album'
- Add 'play_next' option for 'add_playlist_to_queue'
- Add 'play_next' option for 'add_favourite_to_queue'
v0.1.34 - Add 'fade' synonym for 'cross_fade'
- Add 'remove_current_track_from_queue' action
- Add 'remove_last_track_from_queue' action
v0.1.33 - Add 'none' as a synonym for 'off', in the 'repeat' action
- Add the ability to use sequences and ranges with 'remove_from_queue'

 Docs: https://github.com/avantrec/soco-cli/blob/v0.1.38/README.md

Userlevel 7
Badge +20

With yesterday’s release of SoCo v0.20, I’m pleased to report that soco-cli now supports stereo pairing. Any pair of Sonos speakers can be stereo-paired, and it’s fast and simple to use.

Pairing:

sonos <left_speaker> pair <right_speaker>

Unpairing:

sonos <left_speaker> unpair

This is obviously mostly applicable to the case of pairing a Sonos One or One SL with a Play:1, but there may be other corner cases where it proves useful.

Userlevel 7
Badge +20

Version 0.1.40 of soco-cli improves seek functionality by adding new seek_forward and seek_back actions, alongside the existing seek (seek_to) option.

The seek_forward (sf) and seek_back (sb) actions allow one to skip ahead or skip backwards within a track: useful for jumping through podcast ads, or re-listening to a section of audio. Skipping can be by an arbitrary duration, within the length of the audio track being played.

Examples:

sonos Study skip_forward 30s
sonos Study skip_back 2m
sonos Study seek_to 00:03:32

As always, let me know if there are features you’d like to see, or if you encounter any problems.

Reply