Sonos Cloud API - Better alerts & TTS

wow. I feel dumb now. THANK YOU. I somehow cross named them. ( Was aware they are different, and named them differently intentionally - but somehow mixed up my naming convention :frowning: )

1 Like

For anyone using the great Google Cloud TTS with this, this is how it works, even for selecting language, voices, ā€¦:

service: media_player.play_media
data:
entity_id: media_player.yourplayer
media_content_id: >-
media-source://tts/google_cloud?language=de-DE&voice=de-DE-Wavenet-B&message=Your_text_here
media_content_type: music
extra:
volume: 30
play_on_bonded: true or false

Replace de-DE-Wavenet-B with your Google voice of choice.

You can say the content of an entity with this:
media-source://tts/google_cloud?language=de-DE&voice=de-DE-Wavenet-B&message=ā€™{{
states.sensor.yoursensorhere.state }}ā€™

1 Like

Since upgrading to HA 2022.6 and Sonos Cloud 0.3.0, Iā€™ve had a few times when the integration suddenly stops working with error message ā€œinvalid access tokenā€.

The first time it happened, I noticed log message ā€œā€¦Application Credentials have been imported and can be removed from configuration.yamlā€ - so I removed them. This didnt fix the issue so I deleted the integration (and restarted HA just in case). When I added the integration again, I wasnt prompted to enter a client ID or key (even after deleting from configuration.yaml and deleting the integration). But it remember them and the problem was fixed.

Today it failed again, after working fine for about 2 weeks.

2022-08-04 17:54:59.500 ERROR (MainThread) [custom_components.sonos_cloud] Household request failed (401): {"fault":{"faultstring":"Invalid Access Token","detail":{"errorcode":"keymanagement.service.invalid_access_token"}}}

This time I deleted the integration, then created new credentials in Sonos Dev account (even though the old ones hadnā€™t changed). When adding the integration again, it STILL didnā€™t ask for client credentials so I was forced to enter the new client ID and key in configuration.yaml. Now its working again.

  • Has anyone else had this problem after upgrading from configuration.yaml version?
  • Where are the client ID and key stored now and is there any way to delete them?

Thanks.

Iā€™ve had that a few times now as well, no particular pattern identified. On each occasion I have removed the Integration, rebooted and then re-added it. This has always prompted to re-authentication which I have done with the original details. Interesting Iā€™m being prompted to re-authenticate and your notā€¦

One difference is I have the client credentials commented out in my configuration.yaml

Itā€™s definitely not obvious, but theyā€™re on the Integrations page hidden behind the top-right ā€œā€¦ā€ button as ā€œApplication Credentialsā€:

This will hold the credentials imported from configuration.yaml or ones you add during the new onboarding flow.

I havenā€™t hit these 401 errors myself and Iā€™m wondering if theyā€™re transient. If you leave the credentials as-is and wait some time, does login with the same credentials later succeed?

1 Like

I have this issue too, starting with the same upgrade to Sonos Cloud 0.3.0.

It does not start working again if I wait, at least after 24 hours.

Edit: I created a fresh Sonos Developer API key and deleted/reinstalled the integration. Weā€™ll see how long it lastsā€¦

Thanks @jjlawren. I wasnā€™t aware HA stored OAuth credentials there. Makes perfect sense now.

To answer your question, both times when it stopped working I left it for about 24 hours and it continued to fail. So it doesnt appear to be transient and has been 100% reliable for many days leading up to those moments.

The first time I used the same OAuth credentials to get it working again, so Iā€™m not sure where the 401 is coming from. Maybe HA itself? Let me know if you want me to enable verbose logging or check anything if it happens again. Happy to help because this is the best Sonos integration by far.

Interesting. To clarify, after re-adding I was not prompted for the Sonos Dev Application credentials, but I was prompted for my main Sonos Account credentials. Next time I can try deleting the OAuth credentials from the Application Credentials page. Then Iā€™ll see how long it lasts if the credentials arenā€™t imported from config.yaml.

Thatā€™s pretty crappy. The native TTS is awesome and volume control would be a pretty dang useful feature when trying to use Sonos speakers for system announcements. Boo.

Thatā€™s a very old comment youā€™re replying to. Check out the examples for how to accomplish this today.

This integration rocks, thanks for your hard work!

We like to have parties around sporting events and Iā€™m using this to play music during commercial breaks/after scores/etc. It really ups the atmosphere! I basically created a ā€œSoundboardā€ tab and can pick from what Iā€™d like to hear.

One question though, is there a way to make an ā€œinterruptā€ button? Sometimes Iā€™ll start a song and the game will quickly come back from commercial and I donā€™t have a way to stop the playing media and return to the TV audio.

The API states that currently playing alerts can be interrupted by followup calls. Perhaps try to play an ā€œemptyā€ alert to cancel whatā€™s playing?

1 Like

@jjlawren Iā€™m also seeing the ā€œInvalid Access Tokenā€ error. I canā€™t pinpoint when it happens, but the error does not repeat and the integration works on subsequent attempts to use SONOS cloud. Seems to be occurring after a long (~8 hours) of no use of the integration.
Your comment mentions the use of the Application Credentials in the configuration.yaml fileā€¦ I donā€™t have the credentials there. Just in the integration setup section of HA. Should the credentials be referenced in the configuration.yaml file?

Not sure I can make any guesses besides it being a transient issue with the cloud service.

No, thatā€™s just old info from before the HA credentials store existed.

I just loaded this integration, but canā€™t get it to work via a service call. this is what i am using:

service: media_player.play_media
data:
  entity_id: media_player.living_room_sonos_cloud
  media_content_id: media-source://google_cloud_say?lanaguage=en-GB&voice=en-GB-Standard-F&message=Testing Sonos text to speech
  media_content_type: music
  extra:
    volume: 50

I can call this service, and i get no errors in the log, but i also donā€™t get any voice on the sonos. If i go to the media tab and select google cloud TTS using the same media player as an output i do get playback on the sonos. I have tried the message with and without single quotes. anyone have any ideas?

Try something like this:

service: tts.google_cloud_say
data:
  entity_id: media_player.living_room_sonos_cloud
  message: Testing Sonos text to speech
  language: en-GB

thanks, that works, butā€¦ it doesnā€™t give me the options to set volume like in the play_media call. basically i was following the third example at https://github.com/jjlawren/sonos_cloud#examples

This isnā€™t what your looking for but will work, go to the media_player.living_room_sonos_cloud entity and set the volume there, once set on the entity any time itā€™s called it will use that volume.

1 Like

The format looks wrong. Try media-source://tts/google?... as the media_content_id.

that i didnā€™t know, thank you!