Sonos Cloud API - Better alerts & TTS

The local UPnP-based API used in the built-in Sonos integration does not (seem to) have access to all features developed by Sonos. One example of this is their audioClip API. This API allows you to play a short audio clip over the currently playing music without needing to go through the snapshot/restore dance. The music volume is lowered to allow the clip to be heard, and then automatically restored when the clip is finished. It’s a much simpler and nicer experience.

Unfortunately this is only available via their cloud-based API which requires registration of an app through their developer website, can only use publicly-accessible audio files, etc. It’s also likely not supported on every device, but all of mine on S2 currently work.

It’s still in the (very) early development stages, but I wanted to share some testable code since the feature has been so useful to me already.

Note: this is not intended to replace the built-in Sonos integration, but to work in tandem. As of now it creates duplicate media_player entities which can accept media_player.play_media service calls. That’s it. If I’m able to figure out how to call this using the local API then this project will be abandoned basically immediately. :laughing:

If you’d like to try it out, here it is in custom_component form: https://github.com/jjlawren/sonos_cloud/.

Edit: New features in 0.1.2 and 0.2.0 described in this post.

18 Likes

looks nice :slight_smile:

Edit:
but i can get it work - under Integrations is only Sonos not Sonos cloud.
NVM im dumb - Ctrl F5 solved it^^

is the " volume parameter" also work for tts or only mp3?

Right now the TTS integration doesn’t pass extra parameters through. Should be possible to add that feature with a core PR, though.

1 Like

PR? :slight_smile:
Not sure if you mean on my site or on your addon

I mean a pull request on HA core. Supporting the volume param via TTS requires a new feature in the existing HA TTS integration(s).

Edit: Pass through extra dict from TTS to media_player.play_media by jjlawren · Pull Request #53630 · home-assistant/core · GitHub

1 Like

It looks like it’s working great so far.

the only question is is there support somehow for getting the tts/mp3 synchronized across all speakers if playing to multiple?

Right now it’s just slightly off.

If it could be synchronized like the sonos groups it would be pretty much perfect!

:grinning:

Unfortunately their API only allows you to target a single speaker (unless I’ve missed something). Faking it by calling it multiple times is the “solution” for now.

I’m not quite clear on what you mean by “calling it multiple times” but this works pretty well and there is only a slightly out of synch result (at least in the limited quick testing I did):

  notification_sonos_cloud_tts_all:
    alias: Sonos Cloud TTS 
    sequence:
      service: tts.cloud_say
      data:
        entity_id:
          - media_player.sonos_cloud_diningroom
          - media_player.sonos_cloud_kitchen
          - media_player.sonos_cloud_living_room
        message: "Hello there"

Unless you were meaning something different?

No, that’s exactly what I meant. That service call will actually make three separate calls to the Sonos API, one for each speaker. It’s almost in sync because it’s being called quickly in succession.

It would be nice (but is not currently possible) to be able to target the group to play across all speakers in sync with a single native call.

1 Like

OK, thanks for confirming it’s the best workaround.

And thanks for creating this.

I saw both yours and the other one at about the same time after recently reading the other thread after just getting my brandy-new Sonos One SL’s :slightly_smiling_face:.

i wasn’t sure which was going to work better but after looking a bit deeper at both of them yours seems WAY easier to set up (since I don’t use add-ons) and is actually maintained.

I’m really surprised this thread hasn’t gotten more attention, TBH. :man_shrugging:

Thanks again.

3 Likes

So am I but I just found it so that’s one more :slight_smile:

However…

I seem to have failed at the first hurdle as I don’t have a ‘Sonos Cloud’ integration available to add

image

I have the copied the files and added the entry to config (and restarted).

image

sonos_cloud:
  client_id: !secret sonos_cloud_client_id
  client_secret: !secret sonos_cloud_client_secret

You should see a message like this if the custom component is found:

We found a custom integration sonos_cloud which has not been tested by Home Assistant.

If not, are there any other references to sonos_cloud in your logs?

Edit: Since there’s a __pycache__ directory in there, it implies to me that it’s been loaded at least once.

Yes, it is loaded, I have that message in my logs.

2021-09-16 16:12:26 WARNING (SyncWorker_7) [homeassistant.loader] We found a custom integration sonos_cloud which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant

Any chance forcing a reload of the frontend in your browser fixes it?

Doh…
That did it.

How many times will that one catch me out?

Thanks, now to experiment…

Well that went well!

Amazingly simple.

I’m really surprised this thread hasn’t gotten more attention, TBH. :man_shrugging:
:wink:

2 Likes

Does this mean you think it might be at a later date?

And also, I just tried playing a TTS to a speaker in a group playing music. It seems to have faded the music on the whole group before playing the TTS on the single targeted speaker.

Is this intended or just a (current?) limitation to fade the whole group?

Both appear to be current limitations (or expected behavior) of the Sonos audioClip API.

As mentioned here it’s possible to send the same request to multiple speakers, but they will not be perfectly synced.

1 Like

I have no idea why I can’t get this working…I must be missing something obvious to everyone but me!

All files are in place, have put necessary sections in configuration.yaml and secrets.yaml:

sonos_cloud_dir

sonos_cloud_config

But I can’t restart, my config is invalid:

invalid

I do have my config broken up into packages, but even if I try to put the relevant section directly into the configuration.yaml file, it still can’t find the integration.

I can’t add the integration on the config page because it isn’t there. I’ve cleared my cache multiple times.

I would appreciate it very much if another set of eyes can see what I am missing - thank you!