Spotify Playlist Player

I updated the code to work in Home Assistant 0.59.2. I implemented the code as a “package” (see code below). I did this before I realized I’d be foiled by not having a premium account to use the API controls though heh. So … it’s untested (I get the same error as switched). Thank you to GigabitGuy!

\\HASSIO\config\configuration.yaml: (relevant excerpts)

homeassistant:
#...
  # Extra code is stored in these package.  Look for more variable names here
  packages: 
    pack_1: !include custom_components/spotify_playlist_player.yaml

#...
http:
  # Secrets are defined in the file secrets.yaml
  # api_password: !secret http_password
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  # base_url: example.xxxxxxxxxx.org:8123
  base_url: xxxxxxxxxx.duckdns.org
      #base_url needed for Spotify component
  api_password: xxxxxxxxxxxx
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

media_player:
#...
# Spotify media player only displays artwork of currently playing songs.  It leaves the artwork up after Spotify is off.  Also, no controls work (play/pause/stop/forward/back).  Chromecast Audio works for displaying the same artwork and stopping play.
  - platform: spotify
    client_id:  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    client_secret:  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    aliases:
          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: 'Web Player (Chrome)'
         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: 'All Home Speakers'

.

\\HASSIO\config\custom_components\spotify_playlist_player.yaml:

group:
    music_spotify:
      name: Music 
      entities:
      - input_select.spotify_playlist
      - input_select.spotify_source
      - script.spotify_music
      - input_number.spotify_volume

#Use this to find aliases for your spotify media_player component (in your configuration.yaml file).  Look at home-assistant.log to find the aliases of your components.  Enter these aliases into that component in configuration.yaml.
# https://home-assistant.io/components/media_player.spotify/
logger:
    default: warning
    logs:
        homeassistant.components.media_player.spotify: info
      
homeassistant:
  customize:
    script.spotify_music:
      friendly_name: 'Play'
      icon: mdi:play

input_number:
  spotify_volume:
    name: Volume
    icon: mdi:volume-high
    min: 0
    max: 1
    step: 0.05

input_select:
  spotify_playlist:
    name: 'Playlist:'
    options:
      - Your Favorite Coffehouse
      - Morning Commute
      - Cinematic Chill-Out
      - FUTURESYNTH
      - From a Smokey Bar
      - Piano in the Background
    initial: Your Favorite Coffehouse
    icon: mdi:spotify
  spotify_source:
    name: 'Source:'
    options:
      - All Home Speakers
      - Bedroom Speakers
      - Livingroom Speakers
      - Kitchen Speakers
      - Bathroom Speakers
      - PS4-353
    initial: All Home Speakers
    icon: mdi:speaker-wireless

script:
    spotify_music:
    #alias: Play selected spotify playlist
        sequence:
          - service: media_player.volume_set
            data:
              entity_id: media_player.spotify
              volume_level: '0.40'
          -  service: media_player.select_source
             data_template: 
               entity_id: media_player.spotify
               source: >
                {% if is_state("input_select.spotify_source", "All Home Speakers") %} All Home Speakers
                {% elif is_state("input_select.spotify_source", "Bedroom Speakers") %} Bedroom Speakers
                {% elif is_state("input_select.spotify_source", "Livingroom Speakers") %} Livingroom Speakers
                {% elif is_state("input_select.spotify_source", "Kitchen Speakers") %} Kitchen Speakers
                {% elif is_state("input_select.spotify_source", "Bathroom Speakers") %} Bathroom Speakers
                {% elif is_state("input_select.spotify_source", "PS4-353") %} PS4-353
                {% endif %}
          -  service: media_player.play_media
             data_template:
               entity_id: media_player.spotify
               media_content_type: playlist
               media_content_id: >
                {% if is_state("input_select.spotify_playlist", "Morning Commute") %} spotify:user:spotify:playlist:37i9dQZF1DX2MyUCsl25eb
                {% elif is_state("input_select.spotify_playlist", "Cinematic Chill-Out") %} spotify:user:spotify:playlist:37i9dQZF1DWVFJtzvDHN4L
                {% elif is_state("input_select.spotify_playlist", "FUTURESYNTH") %} spotify:user:12131571557:playlist:2JgjNysZhksrtRjuGbbWns
                {% elif is_state("input_select.spotify_playlist", "From a Smokey Bar") %} spotify:user:spotify:playlist:37i9dQZF1DWTlzJXLRpd4a 
                {% elif is_state("input_select.spotify_playlist", "Piano in the Background") %} spotify:user:spotify:playlist:37i9dQZF1DX7K31D69s4M1
                {% elif is_state("input_select.spotify_playlist", "Your Favorite Coffehouse") %} spotify:user:spotify:playlist:37i9dQZF1DX6ziVCJnEm59
                {% endif %}
            
automation:
  - alias: 'Set Spotify Volume'
    trigger:
        platform: state
        entity_id: input_number.spotify_volume
    action:
        service: media_player.volume_set
        data_template:
          entity_id: media_player.spotify
          volume_level: '{{  states.input_number.spotify_volume.state  }}'
3 Likes

Great work @Wheezy, I didn’t know about the new package feature, as I have been away from HA a while. I’m having huge issues with automations in general after trying to split it up/being away for a couple of months, maybe this approach can fix my issues as well.

1 Like

I’m going nuts over this! I’ve must have tried a hundred different combinations of base_urls and redirect URI’s to no avail, I get either an Error 500 or a Invalid redirect URI no matter what I do.

The firewall ports are open, I can access HASS from outside my LAN, so my Duckdns redirect is working. I’ve tried using the local server IP as URI, I’ve tried my Duckdns address, with and without trailing slashes or port numbers. Still I get an error all the time. What am I doing wrong?

configuration.yaml:

http:
  # Uncomment this to add a password (recommended!)
  api_password: xXxXxXxXxXx
  # Uncomment this if you are using SSL or running in Docker etc
  base_url: XxXxXxXxXxX.duckdns.org
media_player:
  - platform: cast
    host: 192.168.1.152 #Kitchen 
  - platform: spotify
    client_id: XxXxXxXxXxXXxXxXxXxXxXXxXxXxXxXxX
    client_secret: xXxXxXxXxXxxXxXxXxXxXxxXxXxXxXxXx

Screenshot from 2017-12-16 22-54-27
(Yes, I have saved them)

From the log:
2017-12-16 22:57:47 INFO (Thread-5) [homeassistant.components.media_player.spotify] no token; requesting authorization

The last times I ran the authorization before writing this post, I got an INVALID_CLIENT: Invalid redirect URI in my browser, it doesn’t matter if I use my internal IP:port or my Duckdns address. Neither the HASS log or the journalctl output will give me any more details on whats wrong.

Edit: after changing my base_url to my local IP for the umptieth time it suddenly works.

http:
  # Uncomment this to add a password (recommended!)
  api_password: xXxXxXxXxXx
  # Uncomment this if you are using SSL or running in Docker etc
  base_url: http://192.168.x.xx:8123 # 

Spotify whitelist as above.

Now that the authorization is done I just have to get it working too, that seems easier said than done…

With all respect @bonterra, but I don’t think this is the thread to be doing component troubleshooting in, and you might get better help in a dedicated thread. :wink:

Yeah, you’re probably right. Eventually I did get the authorization to succeed though, I added my details to my post above. I’ll give @Wheezy’s package a try once I find some time to sit down.

Easy workaround: Just automatically skip to the next track once shuffle is enabled.

      - service: media_player.media_next_track
        data:
          entity_id: media_player.spotify
2 Likes

Hey Home Assistant Community,

I figured out a way to accomplish casting Spotify Playlists to Chromecast Audio’s through Home Assistant. I use this project in tandem with a writeup on Mopidy and Icecast2, can be found here: https://www.vittoriomonaco.de/home-automation-part-7.html

You can use my adaptation here:
https://github.com/robwolff3/homeassistant-config/blob/master/packages/audiocontrol.yaml

From my testing so far it works pretty well.

4 Likes

dude this is exactly what i need, basically commenting to remember for later so i can adjust mine. I thought that w should ahve ability to set the text and value on input_selects so we don’t have to do anything weird.

Nice @robwolff3, I tried to play with Mopidy earlier on, but newer really got it going - that tutorial was just what I needed… Maybe its time to revisit and update this when I have the time.

Commenting to setup myself soon. Thanks for all the code :slight_smile:

Great trick!
How did you set up your ‘All Home Speakers’ ? I can play only one device with spotify…

In gh, you can create groups of speakers containing chromecast audio and gh devices. Name it and it will show up as a new device that you can cast to.

I have this configuration, the playlist select works fine, but the source not at all. Any hints?
Other thing, do I need the below automation, for?

script:
  spotify_music:
    alias: Play selected spotify playlist
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.spotify
          volume_level: ‘{{ states.input_number.spotify_volume.state }}’
      -  service: media_player.select_source
         data_template: 
           entity_id: media_player.spotify
           source: >
            {% if is_state("input_select.spotify_source", "Portable speaker") %} Portable speaker
            {% elif is_state("input_select.spotify_source", "Living room speaker") %} Living room speaker
            {% elif is_state("input_select.spotify_source", "Dining room speaker") %} Dining room speaker
            {% elif is_state("input_select.spotify_source", "Guest room speaker") %} Guest room speaker
            {% elif is_state("input_select.spotify_source", "Master bedroom speaker") %} Master bedroom speaker
            {% elif is_state("input_select.spotify_source", "Irma room speaker") %} Irma room speaker
            {% elif is_state("input_select.spotify_source", "Portable and dining room") %} Portable and dining room
            {% elif is_state("input_select.spotify_source", "All speakers") %} All speakers
            {% elif is_state("input_select.spotify_source", "First floor speakers") %} First floor speakers
            {% elif is_state("input_select.spotify_source", "SHIELD") %} SHIELD
            {% endif %}
      -  service: media_player.play_media
         data_template:
           entity_id: media_player.spotify
           media_content_type: playlist
           media_content_id: >
            {% if is_state("input_select.spotify_playlist", "Morning Commute") %} spotify:user:spotify:playlist:37i9dQZF1DX2MyUCsl25eb
            {% elif is_state("input_select.spotify_playlist", "Cinematic Chill-Out") %} spotify:user:spotify:playlist:37i9dQZF1DWVFJtzvDHN4L
            {% elif is_state("input_select.spotify_playlist", "FUTURESYNTH") %} spotify:user:12131571557:playlist:2JgjNysZhksrtRjuGbbWns
            {% elif is_state("input_select.spotify_playlist", "From a Smokey Bar") %} spotify:user:spotify:playlist:37i9dQZF1DWTlzJXLRpd4a 
            {% elif is_state("input_select.spotify_playlist", "Piano in the Background") %} spotify:user:spotify:playlist:37i9dQZF1DX7K31D69s4M1
            {% elif is_state("input_select.spotify_playlist", "Romance") %} spotify:user:claudioita:playlist:1xntGadjxisaQNyuqh5Zyn
            {% elif is_state("input_select.spotify_playlist", "Sleeping") %} spotify:user:claudioita:playlist:3obCZpU1YpzZbzTr45B7gM
            {% endif %}


automation:
 - alias: Set Spotify Source
    trigger:
      - platform: state
        entity_id: input_select.spotify_source
    condition:
      - condition: template
        value_template: "{{ not is_state_attr('media_player.spotify', 'source', states('input_select.spotify_source')) }}"
    action:
      - service: media_player.select_source
        entity_id: media_player.spotify
        data_template:
          source: "{{ states('input_select.spotify_source') }}"
 - alias: Set Spotify Playlist
    trigger:
      - platform: state
        entity_id: input_select.spotify_playlist
    condition:
      - condition: template
        value_template: "{{ not is_state_attr('media_player.spotify', 'source', states('input_select.spotify_playlist')) }}"
    action:
      - service: media_player.media_play
        entity_id: media_player.spotify
        data_template:
          source: "{{ states('input_select.spotify_playlist') }}"
1 Like

Shouldn’t all the entity_id’s created by the package have audiocontrol_ infront of them? Isn’t that what a package does?

I’ve just started using packages and I had to make a few amendments to the last one I tried because of this…

Edit: Also, your package has an entity_id “script.playlist_remote” in a group, but doesn’t actually exist in the package.

Edit2: Nevermind, got it all working, found the script elsewhere in your config. THANKS!

Hi, I also could not find that script: script.playlist_remote
Any hint about where to look for?

Thanks in advance.

hi im on the latest hassio 0.63.3 do i have to add that?

how can you put the images of the playlists?

same for me it ignors my sonos and just plays it on my echo dot

I’ve gotten everything to work but the volume. I originally tried:

volume_level: ‘{{ states.input_slider.spotify_volume.state }}’

But I got this error:

Invalid service data for media_player.volume_set: expected float for dictionary value @ data['volume_level']. Got "'{{ states.input_slider.spotify_volume.state }}"

it appears you may have used data: instead of data_template: