Smartthings API for Sonos

Hi! I have been dabbling a little bit with the Smartthings API ans was able to successfully adapt @Dinis_Carvalho amazing dimmer tile !

Now I am trying to use the same Smartthings API to control my Sonos speakers. For starters, I would like to play a preset (saved favorites). I was able to read the list of presets and display them in a list. I was not successful in sending the right command to Smartthings to play the desired preset. This is mainly since I am not sure where to find the appropriate documentation.

Where do I find the documentation for available Sonos commands using the Smartthings API? I am looking for the correct instructions in the ‘data’ variable below. The values in there are complete guesses because I am not sure where to find the correct parameters.

function setPreset(value) {
      var data = '{"commands": [{"component": "main", "capability": "presets", "command": "playPreset", "arguments": ['+value+']}]}';
          axios.post(baseUrl + '/' + device + '/commands', data, getAxiosConfig()).then(function(response) {
              console.log(response.status);
          });
    }

P.S.: Please don’t judge. I am no developer and have been only just started dabbling with this API.

1 Like

It’s not particularly well documented, but it seems like you figured out most of it! Unfortunately, sometimes it’s a matter of trial-and-error to find the right combination (the Jukebox thread has some helpful tips). The SmartThings capability documentation is often a good starting point as well:

:information_source: Sometimes you have to visit the ‘Proposed Capabilities’ section from the left side menu like in the case of playPreset()

playPreset() is part of the mediaPresets capability, so I suspect that’s the main change you would need to make.

You’ve already figured out how to find the presets and identify the preset ID, which is great progress. And it looks like you’ve already got a feel for the command and argument format, so great work there as well. I suspect that making the capability tweak should get you pointed in the right direction. :slight_smile:

1 Like

Thank you for that! I updated my code to this based on your feedback and the documentation:
var data = '{"commands": [{"component": "main", "capability": "mediaPresets", "command": "playPreset", "arguments": '+value+'}]}';

Still no luck. Receiving a
(Unprocessable Entity) error.
Any help is appreciated.

You’re missing the “” around the presetId value.

{
    "commands":[
        {
            "component":"main",
            "capability":"mediaPresets",
            "command":"playPreset",
            "arguments":["188"]
        }
    ]
}

FWIW, the playPreset command is incorporated into the Sonos Edge (Beta) drivers, so it’s not necessary to use the API, but I understand there are reasons to do so because I do it myself.

ETA: Two notes of caution:

  • The mediaGroup commands in the Sonos beta driver are currently scrambled and don’t work properly via the API. I’ve had a pull request in for three weeks to get those six lines of code straightened out, but there’s been no action so far.

  • Also, if the preset you request is a playlist (as opposed to a station), and there are songs from a previous playlist already in the queue, the songs in your new preset are added to the existing queue rather than replacing it. There’s an existing PR on that one as well.

2 Likes

Thank you so much! I could swear I had tried that same syntax before. Probably had a typo. Anyway, this works!

Thanks for all your help! This is neat since now at least I can dynamically leverage my presets. One step closer to a proper sonos client!

@IslandSoul , since you seem very well versed with the Sonos API, do you know if you can assign and remove devices to and from a group via API? That is something that would really add a lot of value to the integration. It is one of the few things I still need to use the sonos app for.

With the Sonos Control API, yes. With the SmartThings API, no.

The Sonos Control API is much more powerful, but a bit trickier in that you have to work through the authorization maze to get a proper Refresh Token, which you then use to request a new bearer token every 24 hours. The SmartThings PATs are good a bit longer, something like 50 years.

I have an absurd number of Sonos automations, but for the majority I use node-sonos-http-API, which I run on a RPi. It uses UPnP and has a group presets capability that makes group management a breeze.

2 Likes