Chromecast Radio with station and player selection

I’ll give it a try. Thanks for your replies and help, it’s really appreciated.

It works amazing on my Xiaomi MiBox (android Tv) with built in chromecast.
But how do i get this radio on the lovelace screen?:thinking:

Found the simple solution.
Run Lovelace Migration app!

Hi All, I have this all up and running (thank you so much @wills106 ). Just wondering if any one knows how I would call this as a service/ action in order to make it part of an automation. Any help greatly appreciated.

Just realised the script you already wrote works well as a call service. Cheers

1 Like

Hey all,
also from my side, thanks a lot for running my home radio for the last year :slight_smile:
I found myself doing a lot of errors while working with the configuration, so after iterations on the interface and a complete rewrite using Lovelace Custom UI, I would love to share my derivate of the famous chromecast player. I opened up a thread for it

I’d love to get some of your insights in how you’ve been using home-assistant to play your media.

1 Like

Hi Rob,

This worked perfect for me until the 0.84.1 update. Somehow the radio does not play anymore. Do you know if something is changed?

I got this error:

Log Details (ERROR)
Wed Dec 12 2018 22:15:02 GMT+0100 (CET)

Error executing service <ServiceCall script.radio538 (c:71405165e6764e7ead8b7ca762c3ed03)>
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/core.py”, line 1130, in _safe_execute
await self._execute_service(handler, service_call)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/core.py”, line 1143, in _execute_service
await handler.func(service_call)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/components/script.py”, line 123, in service_handler
context=service.context)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/components/script.py”, line 181, in async_turn_on
kwargs.get(ATTR_VARIABLES), context)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 130, in async_run
await self._handle_action(action, variables, context)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 172, in _handle_action
action, variables, context)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 261, in _async_call_service
context=context
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/helpers/service.py”, line 81, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/core.py”, line 1101, in async_call
processed_data = handler.schema(service_data)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled([], data)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 589, in validate_dict
return base_validate(path, iteritems(data), out)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 427, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: required key not provided @ data[‘volume_level’]

I found out it has something to do with the volume set. If I disable that part in the script, it works fine.

Great stuff @wills106

Can you have a look at my repo here and try these 2 files in your setup?

https://raw.githubusercontent.com/pkozul/ha-input-select/master/custom_components/input_select.py

https://raw.githubusercontent.com/pkozul/ha-input-select/master/packages/radio.yaml

As you can see, I’ve created an enhanced version of input_select which allows you to add display names for each option. So, in the radio.yaml, you end up with this:

input_select:

  chromecast_radio_station:
    name: 'Select Radio Station'
    icon: mdi:radio
    options:
      - Absolute Radio Classic Rock
      - RealXS Manchester
      - Custom Station
    values:
      - http://network.absoluteradio.co.uk/core/audio/mp3/live.pls?service=vc
      - http://media-ice.musicradio.com/RealXSManchesterMP3
      - http://custom

As you can see, each option is represented in both the options: section (its display name), as well as in the values: section (its underlying value, which could be its entity ID, radio station URL, etc.). The only important thing is that the position of each item should be consistent in both lists.

Then, you have a corresponding template sensor that keeps in sync with the input_select. It updates whenever the input_select option is changed, and it it stores the options’s ‘value’ (i.e. the corresponding item from the values: section).

sensor:
  - platform: template
    sensors:
      chromecast_radio_station:
        entity_id: input_select.chromecast_radio_station
        friendly_name: chromecast Radio Station
        value_template: >
          {% for option in state_attr("input_select.chromecast_radio_station", "options") -%}
            {% if is_state("input_select.chromecast_radio_station", option) -%}
              {{ state_attr("input_select.chromecast_radio_station", 'values')[loop.index - 1] }}
            {%- endif %}
          {%- endfor %}

The end result is that you no longer need those if / elif / endif statements in your scripts. The scripts are nice and simple, as they use the template sensor which holds the real underlying value.

script:
  play_chromecast_radio:
    alias: Cast Selected Radio on Chromecast Speakers
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: sensor.chromecast_radio_speakers
          volume_level: '{{ states.input_number.volume_radio.state }}'
      - service: media_player.play_media
        data_template:
          entity_id: sensor.chromecast_radio_speakers
          media_content_id: sensor.chromecast_radio_station
          media_content_type: 'audio/mp4'

  stop_chromecast_radio:
    alias: Stop Playing Radio on ChromeCast
    sequence:
      - service: media_player.turn_off
        data_template:
          entity_id: sensor.chromecast_radio_speakers

Good thing about all this is that the enhanced input_select is backwards compatible and can be used as normal in your HA setup. The values: section is totally optional, and is purely an opt-in feature.

Anyway, all of this is in the 2 files I posted above.

Try it out and let me know how it goes.

Cheers,
Pete

6 Likes

hassos 0.84.1 now Radio Deejay streaming works like a charm!

1 Like

hm… that didn’t work for me. I am receiving this error:

Invalid config for [input_select]: [values] is an invalid option for [input_select]. Check: input_select->input_select->chromecast_radio_station->values. (See /config/configuration.yaml, line 215). Please check the docs at Input Select - Home Assistant

@dziugasp Did you copy input_select.py from my repo to your HA custom_components folder?

1 Like

To be honest, I didn’t notice custom component. Thanks!

I had to change script’s templates to


script:
  play_radio:
    alias: Play Radio
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ states.sensor.radio_speakers.state }}"
          volume_level: '{{ states.input_number.volume_radio.state }}'
      - service: media_player.play_media
        data_template:
          entity_id: "{{ states.sensor.radio_speakers.state }}"
          media_content_id: '{{ states.sensor.radio_station.state }}'
          media_content_type: 'music'

  stop_radio:
    alias: Stop Playing Radio
    sequence:
      - service: media_player.turn_off
        data_template:
          entity_id: "{{ states.sensor.radio_speakers.state }}"

to make this work. (btw i removed “chromecast” references)

Other than that, works nicely and I like the custom input. Thanks! :metal:

1 Like

Hello to all. I am new to Hassio. After 3 days of trial and error I managed to get a version of Chromecast Radio working :grinning: Thanks to everyone involved with this project. I have tried to add just one radio station to the radio.yaml, Capital UK. It appears to be formatted correctly but the Config checker shows an error. Please can anyone can point out where I am going wrong with adding a new station.Thanks in advance for any help.
Here is a snippet of the radio. yaml
{% elif is_state("input_select.chromecast_radio_station", "RealXS Manchester") %} http://media-ice.musicradio.com:80/RealXSManchesterMP3 {% if is_state("input_select.chromecast_radio_station", "Capital UK") %} http://media-ice.musicradio.com:80/CapitalUKMP3
Sorry I can not post any more but new users are allowed to post 2 links.

This worked great for me. Thanks

@jiksey I have created a custom package for you showing how to add an extra Radio Station.

Follow the instructions in my earlier post for adding the packages folder to your setup.

But go to my GitHub look in the experimental folder and put that file (radio-capitaluk.yaml) in your packages folder. Either keep it the same name or rename just to radio.yaml

If you compare that to my original radio.yaml you will see what I added and where.

wills106

Thanks for taking the time help me with this. I should be able to work it out now. Much Appreciated.

If you need anymore help just ask, once you get your head around it it’s pretty easy to add either new Radio Stations or other Cast devices. Just copy and paste really.

1 Like

Just spotted it thanks. I was not adding the station to the options: menu

Thanks again

1 Like

Nice!
Thanks for the config, sometimes you’re so stuck in trying to make tricky things work, you overlook the simplest things, which have more impact!