Spotify shuffle mode

Good job on the spotify module @happyleaves! Nice to see it included in 0.43.

Are there any plans to add support for toggling shuffle mode on/off to it? I don’t see any of the services that fits in the media player docs (which is surprising), so perhaps it’s not possible atm?

According to the documentation there is support for that in the API: https://developer.spotify.com/web-api/toggle-shuffle-for-users-playback/ so it should probably be possible to integrate it.

As you point out, it is possible (along with some other modes) via the Spotify Web API. But Home Assistant doesn’t have any UI controls for such a thing. In the interim it would be implemented as a service, I guess.

I personally don’t have plans in the foreseeable future to add that, but it wouldn’t be hard. My fork of the spotipy lib already supports it.

Alright, thanks, I’ll see if I can figure out how to use the spotipy lib to do it via a python script then. :slight_smile:

@chrio please let me know if you have success! It’s right, shuffle is just possible with Kodi atm I think.
Since yesterday there is a feature request whether it’s possible to change that. In case you are interested.

Thanks, I hadn’t seen that, added my vote. I hope more settings such as shuffle and repeat modes will end up in the standard media_player feature sets later on.

As for spotify, I will give it a try to set it via a python script using the .spotify-token-cache + spotipy lib if possible, but I have limited python experience and time, so well see if I can come up with anything useful.

Would love to hear if you get it working!

Would it be possible to somehow scrape the current access token from .spotify-token-cache and then use a shell script action for the API?

curl -X PUT "https://api.spotify.com/v1/me/player/shuffle?state=true" -H "Authorization: Bearer {{ access token }}"

You can do it like that, but you can’t be sure that the token is still valid.

Here is a quick and dirty way to do it via a shell script:

#!/bin/bash
AUTH=`cat /home/homeassistant/.homeassistant/.spotify-token-cache | cut -d , -f 3 | cut -d \" -f 4`
A='curl -X PUT "https://api.spotify.com/v1/me/player/shuffle?state=true" -H "Accept: application/json" -H "Authorization: Bearer '
C=${A}${AUTH}\"

eval $C

As long as it’s run shortly after you run the spotify service from homeassistant it should work though. A better way would be to build a small python app that uses the library, but that takes a bit more time.

1 Like

I’ve spent the last hour trying to figure this out :joy:

Thank you @chrio, that’s excellent! I’m going to be using it after starting a playlist (and then have a next track command after) so that playlists don’t just start on the same track.

Edit: I had to change it to this to get it to work (between quotes, not a comma and a quote)

#!/bin/bash
AUTH=`cat /home/homeassistant/.homeassistant/.spotify-token-cache | cut -d \" -f 4 | cut -d \" -f 5`
echo $AUTH
A='curl -X PUT "https://api.spotify.com/v1/me/player/shuffle?state=true" -H "Accept: application/json" -H "Authorization: Bearer '
C=${A}${AUTH}\"

eval $C
1 Like

[quote=“daniel-stoneuk, post:9, topic:16541”]
and then have a next track command after
[/quote]Ah, good idea, I hadn’t thought of that.

You could add it to the script in the same way, using a second curl request. According to https://developer.spotify.com/web-api/console/post-next/ the format should be like this:

curl -X POST “https://api.spotify.com/v1/me/player/next” -H “Accept: application/json” -H “Authorization: Bearer TOKEN”

@chrio @daniel-stoneuk

I have no idea how to implement that. I probably Don’t I just need to copy the shell script after shell command do I?!?

@daniel-stoneuk can you share your configuered automation/script here?

Short version:
Create a file, paste the content, save and make it executable. Then call the script via a shell command in home assistant.

  1. cat > spotifyrandom.sh
  • Paste (or type) the text.
  • Press ctrl+d
  • Check the new file with cat spotifyrandom.sh
  • If it looks ok you can make it executable with: chmod 755 spotifyrandom.sh, OR you can just call it with bash [the path to]/spotifyrandom.sh
2 Likes

What @chrio said and then to add it to an automation add this to your configuration file

shell_command: 
  shuffle_spotify: bash /home/homeassistant/.homeassistant/spotifyrandom.sh

and then you can use it in an automation such as here:

action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.spotify
        volume_level: 0
    - service: media_player.select_source
      data:
        entity_id: media_player.spotify
        source: Echo Dot
    - service: media_player.play_media
      data: 
        entity_id: media_player.spotify
        media_content_type: playlist
        media_content_id: <myplaylistid>

This is the shell script:

    - service: shell_command.shuffle_spotify

and then the rest…

    - service: media_player.media_next_track
      data:
        entity_id: media_player.spotify
    - service: media_player.volume_set
      data:
        entity_id: media_player.spotify
        volume_level: 1
    - service: light.turn_on
      data:
        entity_id: light.main
        transition: 600
        brightness: 255
1 Like

Thank you @chrio @daniel-stoneuk for the detailed help!
As I run in docker I changed the path of the script and the config.yaml accordingly:

#!/bin/bash
AUTH=`cat /home/pi/home-assistant/configuration/.spotify-token-cache | cut -d \" -f 4 | cut -d \" -f 5`
echo $AUTH
A='curl -X PUT "https://api.spotify.com/v1/me/player/shuffle?state=true" -H "Accept: application/json" -H "Authorization: Bearer '
C=${A}${AUTH}\"

eval $C

and config.yaml

shell_command: 
  shuffle_spotify: bash /home/pi/home-assistant/configuration/spotifyrandom.sh

my script starts spotify playlist and skips to next but the shell_command does not work.
when I activate it directly in putty it says

osmc@KODI:/home/pi/home-assistant/configuration$ bash spotifyrandom.sh
AQClRZSpyO-jcu0k_Cg6z0rjtLM1gdKctA6bX2LLxGrd9_xoCftGFNoeuGJY76bNDHfbuBkQI-lhAzKPvD3XtxuVulR5PJ3cCgzECZv-Pv4AA7r2p2ndGwHCmDCWPk1zUfo
spotifyrandom.sh: line 6: $'\r': command not found
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
osmc@KODI:/home/pi/home-assistant/configuration$ bash spotifyrandom.sh
AQClRZSpyO-jcu0k_Cg6z0rjtLM1gdKctA6bX2LLxGrd9_xoCftGFNoeuGJY76bNDHfbuBkQI-lhAzKPvD3XtxuVulR5PJ3cCgzECZv-Pv4AA7r2p2ndGwHCmDCWPk1zUfo
spotifyrandom.sh: line 6: $'\r': command not found
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

OK my first error was creating a new file in microsoft explorer this is where the
spotifyrandom.sh: line 6: $’\r’: command not found
came from.
Created a new file with sudo nano and now I can start with

bash spotifyrandom.sh in putty
but in the script it is still not running

OK for the completion of what I found out.
I could execute a shell script from my docker file via

sudo docker exec -it hass bash /config/spotifyrandom.sh

however next error “curl is not installed” so I’ll call it a day
Better to wait :slight_smile:

I submitted a pull request for enabling shuffle in Spotify and it was merged recently: https://github.com/home-assistant/home-assistant/pull/7339

Should be available on the next HA version!

2 Likes

Nice then I will wait :wink: and watch a movie instead
thanks you for your work! :slight_smile:

1 Like

Brilliant! It works great. Thanks @abmantis!

Just created a pull request for documentation.

 - service: media_player.shuffle_set
      data: 
        entity_id: media_player.spotify
        shuffle: 'true'
1 Like

Hi all, I would really love to have this shuffle option. Because I automatically start a playlist when I arrive home, and I don’t always want to hear the same songs. :slight_smile:

I noticed that there is a service now in HASS. But I have trouble adding the parameters in the frontend, I don’t know ther right syntax and I haven’t been able to find the right one yet. Can somebody help me with that?

I did get it working like this:

shufflespotify:
alias: ‘Shuffle Spotify’
sequence:
- service: media_player.shuffle_set
data:
entity_id: media_player.spotify
shuffle: true

After all that is what I was intending, but I still have no idea how to add the parameters in the servic frontend.

Hey Everyone,

Just joined and figured I’d pass along what I’ve learned. I tried the “shuffle_set” feature listed above and it didn’t seem to work so I reverted to trying the shell script. That worked for a while until the structure of the .spotify_token_cache file changed and broke the cut. So after some googling I came up with the follow. Please provide any feedback you have.

#!/bin/bash
AUTH=`grep -Po '(?<="access_token": ")[^"]*' /home/homeassistant/.homeassistant/.spotify-token-cache`
echo $AUTH
A='curl -X PUT "https://api.spotify.com/v1/me/player/shuffle?state=true" -H "Accept: application/json" -H "Authorization: Bearer '
C=${A}${AUTH}\"
echo
echo $C
eval $C

Using grep instead of cut seems a bit cleaner and easier to understand. It also continues to work if the contents of the file change order.