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

Yep, HA Core 2022.6.0b2. OK, no problem, will wait for another update from you.

Update

Version 2.5.0 - 28 May 2022

:star2: Improvements

  • Combined all helper scripts in one script (script.google_home_resume_helper)

:bug: Bug fixes

  • Fix for uppercase characters in context.id causing errors in creating groups
  • Fixed resume for the Ytube Music custom integration
1 Like

Should be fixed now in 2.5.0
I noticed the same today, so thatā€™s why I assumed it was caused by the beta :slight_smile:

2.5.0 - working again smoothly :slight_smile:

Great, thanks for the feedback!

Hi,

Just bumped on this and it seems to be exactly what I was looking for. Canā€™t wait to try it!!!
Starting now at 11:50PM on a sunday evening is probably a bad idea, but still tempting ā€¦

So I still need to test and report back, just wanted to say a big thank you already!

Kind regards
Y.

1 Like

I found the problem!

I have the group defined with its elements in the script. I have also de same group as a media_player entity, so the script connects one time with the speakers because it connects with the group entity (first chime) and the once again to each speaker individually when running through the elements of the group defined in the script. I deleted the group definition in the script and now it chimes only once.

Hi,

I donā€™t think you found the problem.

First, why it chimes twice:
When your speakers are off in Home Assistant, the volume can not be retrieved by the script. So speakers which are off are turned on in the script (first chime). Then the volume is stored. After that the TTS is sent to the speakers (second chime). You probably didnā€™t get the chime after you removed the group details because the speakers were already in an ā€œactiveā€ state like idle or playing.

Then why the group settings are important:
Home Assistant has seperate media_players for the group itself an the members, however (unlike for Home Assistant native speaker groups) it does not know which group members belong to the group.
So without the setting for the speaker groups, the script wonā€™t be able to:

  • resume a speaker group which was playing when you send a TTS to a single member of the group
  • resume a single member of the group when you send a TTS to the entire group
  • set the volume of the individual speakers to the original volume after it has been adjusted for a TTS to the group

In April the settings for Google Home Speakers was shortly adjusted, and they would never turn off. That removed the need to turn them on and would also removed the first chime. However this was reverted. There is still an open architectural discussion on that topic, but it is a bit silent now. It is only a minor change, but I tried running the cast integration as a custom component, but that would break spotcast.

Hello,
I use your script without a problem but when I try to service call for scrypt the music did not resume and no trace show up .
I dont know what is happening. pls help
Thanks.

Please download the json of the trace of both scripts and share that using a code sharing website like dpaste.org.
I canā€™t do anything with these screenshots.

This is the one that work

This is the one that not

service: script.google_home_resume
data:
action:
- service: script.doorbell_press_data
data: {}

The Helper Script does not even run

Here are the Trace of script.doorbell_press_data

It will not work with that service call as you are calling another script and you are not providing which entities are affected by that script.
So the resume script has no information on which entities are targeted, and will therefore resume nothing.

Try like this:

service: script.google_home_resume
data:
  action:
    - service: script.your_script
  target:
    entity_id:
      - media_player.foo
      - media_player.bar

I see. So itā€™s mean I have to specific which speaker to resume right ? but what if I play music from different group at different time ? how can I know which target to be resume?

Or Instead using the scrypt I should call service like the one that work? Is this way are better for my need?

You have to specify which are targeted by your doorbell script, it doesnā€™t matter if they are playing or not, that will be determined by the resume script.

But the resume script needs to know which of your Google Home speakers will potentially be interrupted by your action.

If you use a service call like tts.google_translate or media_player.play_media your also need to provide the target in the service call, so the resume script will use that. If you use a script, you donā€™t provide the target in the service call, so there is no way for the resume script to know the targets. Thatā€™s why you have to specify them separately.

OH. ok I just specify target and itā€™s work.
my fault,I understand that target is the device that will resume music but clearly it is not.

Thanks

@TheFes today I found your script, thanks for creating it!

Just FYI, when I copy over the code into my script.yaml file, via VSC, I see two errors:
image

and

I thought I would let you know :slight_smile:

Also, the script is not (yet) working, I will need to do some more tests (tomorrow) to find out what is the reason.

You can ignore these errors, you are allowed to template the entire data section of a service call, apparently the VSCode extension isnā€™t aware of this.

Iā€™ve checked it for my self now as well, and I donā€™t see these errors if I place the code in scripts.yaml.
Iā€™m using the latest version of the Studio Code Server add-on.

If you canā€™t get the script working, please send me a trace a described in the start post.

1 Like

Firstly, thank you for this awesome script. I started building my own when I came across it and it surpasses anything I would have put together.

I started testing it, and it works when playing a streaming URL (e.g. a Radio station), but it fails when playing content from Spotify. I have double check that the names are indeed the same, this was the first error, which was replaced by the one below, but I am not sure how to resolve it. Any suggestions will be much appreciated.

My HA instance:
Home Assistant Core 2022.6.6
Home Assistant Supervisor 2022.05.3

Logger: homeassistant.components.script.google_home_resume_helper
Source: custom_components/spotcast/spotcast_controller.py:174
Integration: Scripts (documentation, issues)
First occurred: 20:52:59 (16 occurrences)
Last logged: 21:14:33

00 šŸ”Š Google Home Resume - Helper Script: If at step 1: Resume needed?: Resume playing: Spotify?: Error executing script. Unexpected error for call_service at pos 2: 'NoneType' object has no attribute 'get'
00 šŸ”Š Google Home Resume - Helper Script: If at step 1: Resume needed?: Error executing script. Unexpected error for choose at pos 2: 'NoneType' object has no attribute 'get'
00 šŸ”Š Google Home Resume - Helper Script: If at step 1: Error executing script. Unexpected error for if at pos 7: 'NoneType' object has no attribute 'get'
00 šŸ”Š Google Home Resume - Helper Script: Error executing script. Unexpected error for if at pos 1: 'NoneType' object has no attribute 'get'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 447, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 680, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1704, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1747, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 170, in start_casting
    client = spotcast_controller.get_spotify_client(account)
  File "/config/custom_components/spotcast/spotcast_controller.py", line 183, in get_spotify_client
    return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
  File "/config/custom_components/spotcast/spotcast_controller.py", line 174, in get_token_instance
    dc = self.accounts.get(account).get(CONF_SP_DC)
AttributeError: 'NoneType' object has no attribute 'get'

I usually see this error when the script uses the spotcast.start service call with a user account which is not defined in the spotcast configuration.

Could you post a trace of the script?

Thank you for the quick answer - Here is the trace.