Autostart/resume radiostream with Google Chromecast Audio on powering up

Hello everyone.
I am new to HA and though searching through related posts and reviewing user configurations in the cookbook i couldn’t find an answer so far.

For the setting i’ trying to implement right now: i do have an Chromecast Audio device up and and running and it integrates nicely with HA. The Device should run in my Kitchen. I am not there the whole day (yup, normally i do not work at home) i’d like to power my device of. It should not run (and stream) the whole day. To also be approved by my girlfriend it should turn on and automatically start a radio stream of my choice, just like the old device did. i don’t want to turn on the device and then have to actively tell it to stream, wether with my cellphone or tablet or some computer here. No it should start the stream intself.

Since Google did not implement any autostart or resume function the idea was to do it via a homeautomation software.

Up to now i was able to build a trigger that starts a stream, when the device gets the state ‘off’.

automation:
  - alias: Starte Radio Stream, wenn Chromecast Audio eingeschaltet wird
    trigger:
      platform: state
      entity_id: media_player.kuche_chromecast_audio
      state: 'off'
    action:
      service: media_player.play_media
      data:
        entity_id: media_player.kuche_chromecast_audio
        media_content_id: http://mp3channels.webradio.rockantenne.de/heavy-metal
        media_content_type: audio/mp3`

If the HA Server is freshly fired up this works nicely. But when i am powering of my GCA HA seems to have problems recognizing the new state or whatever and just reports GCA beeing playing…

Is there anyway to check for GCA beeing ‘online’ for let’s say every 20 seconds and then updating the status? Or am i seeing this from a totally wrong dierection here?

So thanks

Update:

Ok, right now i got a kind of ugly way to realize the setting mentioned above. With my very limited programming skills i haven’t been able to figure out, wether the problem could be solved wit pychromecast itself, so this is my workaround with HA. Suggestions on how to do it more elegant are appreciated :wink:

  1. Automation to load a stream upon discovery

    alias: “CCA platform_discovered”
    trigger:

    • platform: event
      event_type: platform_discovered
      event_data:
      platform: cast
      action:
      service: script.s_cca_discovered
  2. The script for starting the stream and a delay of some seconds, to fully load the module

    sequence:

    • delay:
      seconds: 15
    • condition: state
      entity_id: media_player.kuche_chromecast_audio
      state: ‘off’
    • service: media_player.play_media
      data:
      entity_id: media_player.kuche_chromecast_audio
      media_content_id: http://mp3channels.webradio.rockantenne.de/heavy-metal
      media_content_type: audio/mp3

So far so good. The main problem is to kind of “unload” the module in order to start the automation again after turning the device off and on again. the only way i could find is to restart the server.

  1. Automation to restart the server depending on device_tracker

    alias: “CCA off --> restart HA”
    trigger:
    platform: state
    entity_id: device_tracker.cca_kueche
    to: “not_home”
    action:
    service: script.s_ha_restart

  2. Script to restart with a delay (for testing purposes)

    sequence:

    • delay:
      seconds: 5
    • service: homeassistant.restart

In my opinion this works but seems kind of clunky. And i don’t like to restart the server all the time for it my interfere with other components / task later on.

Maybe someone got an idea on how to unload the cast module a clean way? Or an whole different approach on this matter?