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

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

Incidentally, there’s now a simple Python SoCo-CLI API that avoids the overhead of starting the program for each command (or command sequence). For example:

$ sonos Kitchen volume 30

becomes:

exit_code, output, error = api.run_command("Kitchen", "volume", "30")

The API call returns a three-tuple of exit code, output string, and error string. Assuming success (exit code 0), the output string contains exactly what would have been printed by the standalone command.

The command handles all the speaker discovery machinations behind the scenes, and caches discovery results. It also traps all exceptions, with the exception description appearing in the error string.

Docs at: https://github.com/avantrec/soco-cli#using-soco-cli-as-a-python-library

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
March 24, 2021

Hi there! I have never used python and am a complete and absolute beginner. 

Is there a step by step instruction manual for a total idiot anywhere? 

pwt
pwtAuthor
Virtuoso
March 25, 2021

Hi there! I have never used python and am a complete and absolute beginner. 

Is there a step by step instruction manual for a total idiot anywhere? 

This is a pretty comprehensive guide to installing Python on various platforms:

https://realpython.com/installing-python/

Once you’ve done this, you can install SoCo-CLI and its dependencies using pip install -U soco-cli from a command window (terminal), then run it using all of the various sonos commands.

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 I
March 25, 2021

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 is awesome and thanks for sharing! Is there a command to force download and install firmware updates to Sonos speakers?

pwt
pwtAuthor
Virtuoso
March 25, 2021

Is there a command to force download and install firmware updates to Sonos speakers?

No, there isn’t, sorry. I think updates are done outside the UPnP control mechanisms that are used by SoCo.

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
March 29, 2021

How do you group more then 2 speakers? 

sonos -l "family room" group "media room"

combines 2 but I need 2 more rooms grouped.

pwt
pwtAuthor
Virtuoso
March 30, 2021

How do you group more then 2 speakers? 

sonos -l "family room" group "media room"

combines 2 but I need 2 more rooms grouped.

The following will group sp2, sp3 and sp4 with sp1 as the group coordinator:

sonos -l sp2 group sp1 : sp3 group sp1 : sp4 group sp1

(The ‘ : ’ allows multiple sonos actions to be invoked as part of the same command, but it’s equivalent to issuing multiple separate sonos commands.)

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

SoCo-CLI v0.3.31 introduces additional control over Sonos alarms.

In addition to being able to list alarms, it’s now possible to create alarms, delete alarms, and enable or disable alarms, using their alarm IDs. The new actions are: ‘create_alarm’, ‘remove_alarm’, and ‘enable_alarm’.

As always, feedback is welcome.

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

Just wanted to say thanks for this!  I played around with it this morning expecting a moderately useful utility.  WOW was I surprised about how much work you have put into this - very comprehensive functionality.

Anyone with Sonos speakers who spends time at the CLI should grab this.

pwt
pwtAuthor
Virtuoso
April 10, 2021

SoCo-CLI v0.3.31 introduces additional control over Sonos alarms.

In addition to being able to list alarms, it’s now possible to create alarms, delete alarms, and enable or disable alarms, using their alarm IDs. The new actions are: ‘create_alarm’, ‘remove_alarm’, and ‘enable_alarm’.

As always, feedback is welcome.

Version 0.3.34 adds the ability to copy and modify alarms, making it very quick and convenient to copy an alarm from one speaker to another, and then modify it if required. (I’ll also add a move_alarm function -- which I just thought of! -- in the next release.)

Example: sonos Kitchen copy_alarm 123 will copy the alarm with ID ‘123’ to the Kitchen speaker. (Alarm IDs are discovered using ‘list_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