Script to resume Google Cast devices after they have been interrupted by any action

Update

2023.2

Only a small update with a small bugfix right now :slight_smile:

:recycle: RESUME SCRIPT

:bug: Bug fixes

  • Fix error in resume helper when no volume was stored

Thanks for the great work, this is a game changer! (wife acceptance factor went up!!)

One small thing that does not work perfectly for me:
I use triggered automations to make certain announcements on my Google Minis:

  • increase volume to 0.7
  • play local media (mp3) with announce: true
  • as automation_enabled is set, the “00 - Google Home Automatic Resume” will auto-trigger

The resume script works fine with multiple Spotify accounts (yay!), however:

  1. after the announcement, the Spotify music continues at the increased volume level for ~2 seconds and stops again
  2. after a 2-3 second period of silence, it will continue to play at the previous volume level

I assume 1. is the buffer from the previous stream and 2. is the “real” resumption.
Can I avoid 1. entirely? It is too loud and as it gets interrupted again, it’s of no use. I would rather jump straight into 2. (I’m fine with the 2-3 seconds of silence)

Thanks

1 Like

It’s better to let the script increase the volume, there is a setting for the automation for a default volume for announcements.

For the Spotify thing, the service call to resume it, is issued twice, I think that’s causing it. I will have a look if I can remove it, or only issue it a second time if the first one failed

1 Like

Yes, using the global volume solves one part of the issue, it does no longer continue to play the music too loudly after the announcement. Thanks for that.
Not ideal to having to stick to a single volume value for all announcements. But then again, I can just use the global one for everything that needs to resume music afterwards.
And for the critical ones, where I need volume=1.0 (fire alarm, water leakage), I probably don’t need to resume music anyways
 :slight_smile:

Yes, if you are able to fix the double service call, that would be perfect!

I read and understood why you would not want to make it a blueprint. But how about a HACS addon? (Does that work for packages?) That would at least allow users to see available updates easily and to update with a simple click on a button.

With HACS it needs to be an integration written in python, which I can’t

If you want to use different volume you can call the script yourself and don’t use the automation

1 Like

Ok got it, thanks.

What was the reason to implement the repeat and count: 2 for resuming Spotify?

I can try to make it a single call in my setup and see how reliable that works.

Because in the previous version of Spotcast it would sometimes fail the first time.
But I’ll improve it somewhere next week

1 Like

Can you please advise me what the problem is. When I run this my automation:

alias: Warning at 7 p.m
description: ""
trigger:
  - platform: time
    at: "19:00:00"
condition: []
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.indoor
    data:
      media_content_id: >-
        media-source://tts/google_translate?message=it+will+be+7+clock
      media_content_type: provider
    metadata:
      title: It will be 7 clock
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: >-
            media-source://tts/google_translate?message=it+will+be+7+clock
mode: single

so your script doesn’t work. Your automation 00 - Google Home Automatic Resume will not start at all.

Thank you

You didn’t add announce: true

Thanks, don’t be offended, I’m a beginner. Where exactly should I put it announce: true?

Under data. But why are you sending a cached TTS anyway, and not using the TTS service?

So the GUI generated the automation for me. So please send how it should be right. But I need it to speak the Czech language. Google NEST does not speak Czech.

It doesn’t really matter which language Google Nest speaks, as long as the TTS integration can speak the language.

You are now sending a cached TTS which you’ve sent earlier, and which will be gone at some point. You should just use the TTS integration (use call a service > tts.google_translate_say) and enter the right message there. In that case it will also automatically be sent as an announcement

Thanks, it seems to be working. I don’t see announce: true anywhere, but it works.

action:
  - service: tts.google_translate_say
    data:
      cache: false
      entity_id: media_player.indoor
      message: Za pĆŻl hodiny bude 7 hodin.
mode: single

No, that’s because after HA receives the TTS message (an mp3) from the integration, it will do a similar service call as you did before, but with announce: true

That’s right with it announce: true ?

service: media_player.play_media
target:
  entity_id: media_player.indoor
data:
  media_content_id: media-source://media_source/local/zvon.wav
  media_content_type: audio/x-wav
  announce: true
metadata:
  title: zvon.wav
  thumbnail: null
  media_class: music
  children_media_class: null
  navigateIds:
    - {}
    - media_content_type: app
      media_content_id: media-source://media_source

Yes, looks fine

Unfortunately, sometimes there is a problem. For some unknown reason the music won’t play back. However, it only does it sometimes and I don’t know why. However, the automation “00 - Google Home Automatic Resume” starts.
But I have this error in the log:

Logger: spotipy.client
Source: custom_components/spotcast/__init__.py:276
Integration: Spotcast ([documentation](https://github.com/fondberg/spotcast), [issues](https://github.com/fondberg/spotcast/issues))
First occurred: 22. Ășnora 2023 v 17:19:49 (2 occurrences)
Last logged: 09:07:10

* HTTP Error for PUT to https://api.spotify.com/v1/me/player/repeat?state=off&device_id=0becea5bee4ba4d68173b82cd6ff9942 with Params: {} returned 404 due to Device not found
* HTTP Error for PUT to https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc with Params: {} returned 404 due to Device not found

and



Logger: homeassistant.components.script.google_home_resume_helper
Source: custom_components/spotcast/__init__.py:276
Integration: Skript (documentation, issues)
First occurred: 22. Ășnora 2023 v 17:19:49 (12 occurrences)
Last logged: 09:07:10

    00 - Google Home Resume - Helper Script: If at step 2: Parallel action at step 1: parallel 1: Resume needed?: Resume playing: Spotify?: Error executing script. Unexpected error for repeat at pos 2: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc: Device not found, reason: None
    00 - Google Home Resume - Helper Script: If at step 2: Parallel action at step 1: parallel 1: Resume needed?: Error executing script. Unexpected error for choose at pos 2: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc: Device not found, reason: None
    00 - Google Home Resume - Helper Script: If at step 2: Parallel action at step 1: parallel 1: Error executing script. Unexpected error for if at pos 9: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc: Device not found, reason: None
    00 - Google Home Resume - Helper Script: If at step 2: Error executing script. Unexpected error for parallel at pos 1: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc: Device not found, reason: None
    00 - Google Home Resume - Helper Script: Error executing script. Unexpected error for if at pos 2: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=1574201b8d8f066213f217203d9567fc: Device not found, reason: None

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/spotipy/client.py", line 269, in _internal_call
    response.raise_for_status()
  File "/usr/local/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/repeat?state=off&device_id=0becea5bee4ba4d68173b82cd6ff9942

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1787, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1830, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 276, in start_casting
    client.repeat(state=repeat, device_id=spotify_device_id)
  File "/usr/local/lib/python3.10/site-packages/spotipy/client.py", line 1875, in repeat
    self._put(
  File "/usr/local/lib/python3.10/site-packages/spotipy/client.py", line 336, in _put
    return self._internal_call("PUT", url, payload, kwargs)
  File "/usr/local/lib/python3.10/site-packages/spotipy/client.py", line 291, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/repeat?state=off&device_id=0becea5bee4ba4d68173b82cd6ff9942:
 Device not found, reason: None


Update

Version 2023.2.1 - 27 February 2023

:star2: Improvements

  • Only perform spotcast.start once, unless Spotify is not playing after 10 seconds
1 Like

Please how do I update correctly if I installed from the package?