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

Lyricist II
April 15, 2021

Amazing utility! I’ve incorporated it into the Apple Home app via Homebridge with buttons to play a certain radio station and another for power nap - pause music, set a once only alarm, resume. A cron’ed job groups certain rooms. Command line rules!

Lyricist II
April 16, 2021

What is the best way to find if a speaker is playing something already? I tried sonos -l office info and then grep’ing for PLAYING but grouped speakers return PLAYING even if they are not (?)

pwt
pwtAuthor
Virtuoso
April 16, 2021

What is the best way to find if a speaker is playing something already? I tried sonos -l office info and then grep’ing for PLAYING but grouped speakers return PLAYING even if they are not (?)

The most concise way to find a speaker’s current state is to use the state action, i.e.:

sonos -l office state

This will return a short description of the player state, and if directed at a slave speaker in a group it will be redirected to report the state of the group coordinator. The track action also reports playback state, along with details of what’s playing.

The info action isn’t redirected to the group coordinator, so it just dumps the data for the target speaker. When a speaker is a slave in a group, Sonos doesn’t bother to keep the playback state correct.

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
April 16, 2021

The info action isn’t redirected to the group coordinator, so it just dumps the data for the target speaker. When a speaker is a slave in a group, Sonos doesn’t bother to keep the playback state correct.

In the next SoCo-CLI release, I’ll modify the info and sysinfo actions so they correctly report the state of slave speakers.

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
April 17, 2021

The most concise way to find a speaker’s current state is to use the state action, i.e.:

sonos -l office state

I’ve now moved the details of the state action to a more sensible section of the documentation:

https://github.com/avantrec/soco-cli/blob/v0.3.37/README.md#speaker-and-sonos-system-information

  • state (or status, playback): Returns the current playback state for the speaker, one of: PAUSED_PLAYBACK, PLAYING, STOPPED, or TRANSITIONING.

In the next SoCo-CLI release, I’ll modify the info and sysinfo actions so they correctly report the state of slave speakers.

This change is now released in SoCo-CLI v0.3.37.

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
Lyricist II
April 22, 2021

I am getting a 

Error: 1: list index out of range | 2: list index out of range 

when I run

sonos -l office play_favourite_number 28

The number is valid - I run 

sonos -l office list_favs

to verify and I get back 40 items.

 

 

 

pwt
pwtAuthor
Virtuoso
April 22, 2021

I am getting a 

Error: 1: list index out of range | 2: list index out of range 

when I run

sonos -l office play_favourite_number 28

The number is valid - I run 

sonos -l office list_favs

to verify and I get back 40 items.

You may want to take this discussion over to GitHub (https://github.com/avantrec/soco-cli/issues), where it’s generally easier to work on issues.

Either way, (A) could you provide the output of the following:

  1. sonos -v
  2. sonos -l office play_favourite number 28 --log=info

(B) Could you try ‘play_favourite’ using the favourite name instead of its number, and report whether this works.

(C) What type of favourite are you trying to play?

(D) Is it only #28 that fails, or is it a problem across all favourite numbers.

(It may be worth noting that not all Sonos Favourites are playable using SoCo-CLI; e.g., single tracks from Spotify will work, but not albums. That’s not the problem you’re seeing, however.)

Thanks!

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
May 3, 2021

FYI, folks: SoCo-CLI version 0.3.40 introduces the ability to snooze playing alarms, e.g.:

sonos bedroom snooze_alarm 10       <-- Snoozes for 10 mins
sonos bedroom snooze alarm 30m <-- Snoozes for 30 mins
sonos bedroom snooze_alarm 01:20:00 <-- Snoozes for 1hr 20 mins

Support for alarms management is now quite comprehensive: see the documentation at:

https://github.com/avantrec/soco-cli#alarms

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
May 28, 2021

SoCo-CLI version 0.3.43 introduces support for imported local library playlists.

There are new actions to list library playlists, list the tracks in a library playlist, and to add a library playlist to the queue:

  • list_library_playlists or llp
  • list_library_playlist_tracks <playlist_name> or llpt
  • add_library_playlist_to_queue <playlist_name> or alpq

The actions operate in the same way as the existing ones for Sonos playlists.

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
June 21, 2021

SoCo-CLI versions 0.3.45+ introduce support for the track_follow action. This will print out the currently playing track, and each subsequent track when the track changes. e.g.:

(soco-cli) pwt@pwt-mba ~ % sonos study track_follow     

Album: Takk...
Artist: Sigur Rós
Duration: 0:04:30
Position: 30
Title: Hoppípolla

Album: OK Computer
Artist: Radiohead
Duration: 0:04:27
Position: 31
Title: Exit Music (For A Film)

Album: Heaven or Las Vegas
Artist: Cocteau Twins
Duration: 0:04:58
Position: 32
Title: Heaven or Las Vegas

Playback is stopped or paused

Album: Heaven or Las Vegas
Artist: Cocteau Twins
Duration: 0:04:58
Position: 32
Title: Heaven or Las Vegas

...

track_follow is available in both command line and interactive modes.

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