Combined music player

Not so much my project, but a combination of two projects that I’ve packaged together and thought people might be interested in.

I’ve taken @Bob_NL 's chromecast radio idea from here - Chromecast Radio with station and player selection

…and @Danielhiversen 's google music player idea from here - Google music in HA

Then I worked a bit of magic to reduce the code right down, and to instigate some logic so you can’t try and play two streams at the same time, and it came out like this:

music

5 Likes

The package…

#########################################################
#                                                       #
# This package creates a device which provides a radio  #
# player of sorts in the front end, allowing to select  #
# a radio station, or a google music playlist and play  #
# them across the chromecasts, like a Sonos system      #
#                                                       #
#########################################################


input_boolean:
  play_radio:
    name: Radio
    initial: off
    icon: mdi:play

    
input_select:
  radio_station:
    name: 'Select Radio Station:'
    options:
      - Free Radio CW
      - Free Radio 80s
      - Absolute Radio
      - Absolute 80s
      - Absolute 90s
      - Heart
      - Heart 80s
      - Sleep Radio
    icon: mdi:radio

  google_music:
    name: Select Playlist
    options:
      - " "
    icon: mdi:music

  media_player:
    name: Speakers
    options:
     - "kitchencca"
     - "livingroomcca" 
     - "bedroomcca" 
     - "downstairs_group" 
     - "everywhere group" 
    icon: mdi:speaker-wireless


input_number:
  volume_radio:
    name: Volume
    icon: mdi:volume-high
    min: 0
    max: 1
    step: 0.05
    initial: '0.2'


switch:
 - platform: gmusic
   user: !secret google_music_user
   password: !secret google_music_pass
   device_id: !secret google_music_device_id
   media_player: media_player
   playlist: google_music


group:
  Music:
    control: hidden
    name: Music
    entities:
      - input_number.volume_radio
      - input_select.media_player
      - input_boolean.play_radio
      - input_select.radio_station
      - switch.google_music
      - input_select.google_music


automation:
  - alias: 'Play Radio'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_boolean.play_radio
      to: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.google_music
      -  service: media_player.play_media
         data_template:
           entity_id: >
            {% if is_state("input_select.media_player", "kitchencca") %} media_player.kitchencca
            {% elif is_state("input_select.media_player", "livingroomcca") %} media_player.livingroomcca
            {% elif is_state("input_select.media_player", "bedroomcca") %} media_player.bedroomcca
            {% elif is_state("input_select.media_player", "downstairs_group") %} media_player.downstairs_group
            {% elif is_state("input_select.media_player", "everywhere_group") %} media_player.everywhere_group
            {% endif %}
           media_content_id: >
            {% if is_state("input_select.radio_station", "Free Radio CW") %}  http://tx.whatson.com/http_live_bauer.php?i=freecoventry.aac&awparams=loggedin:false&amsparams=playerid:BMUK_tunein
            {% elif is_state("input_select.radio_station", "Free Radio 80s") %} http://tx.whatson.com/http_live_bauer.php?i=free80s.aac&awparams=loggedin:false&amsparams=playerid:BMUK_tunein
            {% elif is_state("input_select.radio_station", "Absolute Radio") %} http://player.absoluteradio.co.uk/tunein.php?i=ar24.aac
            {% elif is_state("input_select.radio_station", "Absolute 80s") %} http://player.absoluteradio.co.uk/tunein.php?i=a824.aac
            {% elif is_state("input_select.radio_station", "Absolute 90s") %} http://player.absoluteradio.co.uk/tunein.php?i=a924.aac
            {% elif is_state("input_select.radio_station", "Heart") %} http://media-ice.musicradio.com/HeartWestMids
            {% elif is_state("input_select.radio_station", "Heart 80s") %} http://media-ice.musicradio.com/Heart80s
            {% elif is_state("input_select.radio_station", "Sleep Radio") %} http://37.59.28.208:8453/stream
            {% endif %}
           media_content_type: 'audio/mp4'

  - alias: 'Stop Radio if Google Music selected'
    initial_state: on
    trigger:
      platform: state
      entity_id: switch.google_music
      to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.play_radio
      state: 'on'
    action:
      service: homeassistant.turn_off
      entity_id: input_boolean.play_radio

  - alias: 'Set Music Volume'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_number.volume_radio
    action:
      service: media_player.volume_set
      data_template:
        entity_id: >
          {% if is_state("input_select.media_player", "kitchencca") %} media_player.kitchencca
          {% elif is_state("input_select.media_player", "livingroomcca") %} media_player.livingroomcca
          {% elif is_state("input_select.media_player", "bedroomcca") %} media_player.bedroomcca
          {% elif is_state("input_select.media_player", "downstairs_group") %} media_player.downstairs_group
          {% elif is_state("input_select.media_player", "everywhere_group") %} media_player.everywhere_group
          {% endif %}
        volume_level: '{{  states.input_number.volume_radio.state  }}'

  - alias: "Stop music"
    initial_state: on
    trigger:
      - platform: state
        entity_id: input_boolean.play_radio
        to: 'off'
      - platform: state
        entity_id: switch.google_music
        to: 'off'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.play_radio
          state: 'off'
        - condition: state
          entity_id: switch.google_music
          state: 'off'
    action:
      - service: media_player.turn_off
        data:
          entity_id:
            - media_player.kitchencca
            - media_player.livingroomcca
            - media_player.bedroomcca
            - media_player.downstairs_group
            - media_player.everywhere_group
      - service: input_number.set_value
        data_template:
          entity_id: input_number.volume_radio
          value: 0.20

Basically - switching on the Radio switch will play the selected radio station over the selected speakers, switching on the google music switch will play the playlist, if the one is on and the other is selected the first one will stop first, and when they both get switched off the volume gets returned to 20% ready for next time.

Hope this is of some use to somebody.

4 Likes

Nice (Y).
How do you configure the Google Music playlists?

1 Like

It’s all done on Google music itself (I use the desktop uploader on a Windows laptop), then you use @Danielhiversen 's custom_component (link in first post) for the gmusic switch and it automatically populates the input_select :+1:

1 Like

Very interesting, but i still don’t know where to get my device_id… can you please help me to understand how to have this? Maybe is the MAC address of one of my mobile devices with google play music installed? Tablet? Smartphone?

The way I did it:

Put a random string in the device_id bit, restart homeassistant.

Check the logs for the error, it will say something like “device id must be X characters”

Change random string to have correct number of characters. Restart homeassistant.

Check error message, it will now say something like “invalid device id for this account, valid device ids are…”

Copy and paste one of the valid ones from the list in to the configuration.

Sorted.

… and this in line 76 of gmusic.py ? So the device is 16 chars chosen by me?

No, in your yaml.

and in gmusic.py must i add some things like my username and password or must i leave it as i downloaded?

You don’t edit gmusic.py at all, just put the configuration options in the gmusic switch in your yaml file.

ok i’ll try again with these changes…

Thank you for all useful informations in this thread. Got mine working fine and am compiling all those entities in a simpler card (see screenshot below). I’m then asking myself if there’s any way to get GMusic song metadatas as I got it working with radio streams, any idea?

My experience of making combined media player with convenient storing of radio stations list: Development Tips and Tricks