Skip to main content
pwt
Virtuoso
July 10, 2020

Sonos Command Line Controller Available (Python soco-cli)

  • July 10, 2020
  • 55 replies
  • 6970 views

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.

GitHub: https://github.com/avantrec/soco-cli

PyPi: https://pypi.org/project/soco-cli

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

This topic has been closed for further comments. You can use the search bar to find a similar topic, or create a new one by clicking Create Topic at the top of the page.

55 replies

pwt
pwtAuthor
Virtuoso
July 30, 2020

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.

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
Enthusiast I
July 30, 2020

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....

pwt
pwtAuthor
Virtuoso
July 30, 2020

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.

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
August 19, 2020

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”

 

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
September 29, 2020

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 

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
October 9, 2020

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

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
October 20, 2020

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.

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
October 27, 2020

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.

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
December 30, 2020

SoCo-CLI v0.1.46 now supports direct Sonos playback of local audio files, using the play_file action. I’ve included the documentation below:

Local Audio Files

It's possible to play local audio files in MP3, M4A, MP4, FLAC, OGG, and WAV formats directly on your Sonos speakers using the play_file (or play_local_file) action.

This sets up a temporary local web server on the local host, from which Sonos can access the audio file and play it. The action will not terminate until playback has concluded. Example:

sonos Lounge play_file mozart.mp3

The host running soco-cli must remain switched on and connected to the network during playback. The web server is only active for the duration of the play_file action, and will only serve the specified audio file.

 

Full documentation can be found at: https://github.com/avantrec/soco-cli/blob/master/README.md 

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300
pwt
pwtAuthor
Virtuoso
January 1, 2021

Version 0.1.47 of SoCo-CLI adds the ability to play local M3U playlists, comprised of audio files stored on the local filesystem.

Example: sonos Kitchen play_m3u “Morning Mix.m3u”

The m3u filename can be followed by the `s` option to shuffle the playlist.

Example: sonos Kitchen play_m3u chilled.mp3 s

4 x P:5 | Sub | 4 x P:1 | 3 x Connect | 2 x Boost | Connect:Amp | 2 x One | Amp | Move | Port | Era 300