Spotify Metafy (Spotify playlists as media player entities)

I created a backend integration and a very simple frontend plugin to allow Spotify playlist to be integrated into home assistant.
The backend integration turns given playlists into media players that will play to a given Spotify source.

Example Config:

media_player:
- platform: spotify_metafy
  users:
  - user_prefix: "john "
    spotify_entity_id: media_player.spotify_john
    destination: Denon AVR
    playlists:
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
  - user_prefix: "larry "
    spotify_entity_id: media_player.spotify_larry
    destination: Denon AVR
    playlists:
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa
    - uri: spotify:playlist:1sr8gOmSXcWBU4MOql6KCa

NOTE: this requires a Spotify premium account and this requires the Spotify integration to already be set up and running https://www.home-assistant.io/integrations/spotify/

The project can be found here: https://github.com/rako77/spotify_metafy

More Info about the front end can be found here: Spotify Metafy Frontend (Spotify playlists as media player entities)

the user_prefix is an optional variable I use it because I have multiple users and the users both want to use playlists with the same name e.g. “Daily Mix 1” so with the user prefix it will become “john Daily Mix 1” and “larry Daily Mix 1” for the friendly names you would see in home assistant.

5 Likes

This looks great, but I’m having trouble getting it configured. I’ve installed it via HACS by adding it as a custom repository. It looks like configuration is supposed to be done via the UI (Integrations tab), but the process keeps failing. Here is the error message:

Error occurred loading configuration flow for integration spotify_metafy: No module named 'custom_components.spotify_metafy.config_flow'

I’m getting the same error.
Could you explain what is the “user_prefix:”

Sorry, this is only configurable from yaml for now. I just pushed a fix so you shouldn’t be able to even try and configure it via the front end.

the user_prefix is an optional variable I use it because I have multiple users and the users both want to use playlists with the same name e.g. “Daily Mix 1” so with the user prefix it will become “john Daily Mix 1” and “larry Daily Mix 1” for the friendly names you would see in home assistant.

I’m guessing this only works with Spotify Connect sources right? So basically the sources that the Spotify integration supports right?

@ASNNetworks Correct. Only sources that the Spotify integration supports. If you are looking for support for Chromecast devices you can always try https://github.com/fondberg/spotcast

Yes, I use that integration for a while now. Unfortunately it stopped working as of two days ago for everyone, probably due to changes on the token backend of Spotify. So I was hoping an alternative came to rise :wink:

It sucks Spotify Connect doesn’t support group speakers, otherwise I would have used Spotify Connect on all my speakers :frowning:

Is my config ok ?

  - platform: spotify_metafy
    spotify_entity_id: media_player.spotify_stephane
    destination: Couloir
    playlists:
    - uri: spotify:playlist:37i9dQZF1E3a8I6QtsCIuA

Can you give me more infos about how and where to install the frontend?

@stban1983 you should try something like:

media_player:
- platform: spotify_metafy
  users:
  - spotify_entity_id: media_player.spotify_stephane
    destination: Couloir
    playlists:
    - uri: spotify:playlist:37i9dQZF1E3a8I6QtsCIuA

Hope this helps.

Sorry I designed this for multiple users, I’ll see what I can do to make it simpler for configuring a single user in a future update.

Thanks a lot, it’s working!

I posted an update to this integration, please update as there was a pretty major bug in my code.

I asked the same in the other thread related to the frontend…

If you want feature requests :slight_smile:

  • Possibility to chose the playing device dynamically
  • Possibility to play shuffle
  • Start playing even if the player is idle

Regards!

2 Likes

I already asked this to the developer… hope ti will be available soon…

Possibility to chose the playing device dynamically

I just updated the back end and front end, now you can double tap or hold a playlist icon to change source.

Possibility to play shuffle

I don’t know how to make UI for this I feel like it should be part of the hold/double tap flow but I don’t have control over that UI

Start playing even if the player is idle

this might be a bug, can you give me more info about your setup? I aim to select the source and start playing music on click/tap, which should get the target device to turn on.

Yes, it works!
And what about support also for chromecast and google devices?

All is working :slight_smile:
Thank you @xqueenslander !

Suddenly i get these errors after a restart:

2020-05-31 16:41:00 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up spotify_metafy platform for media_player
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/spotipy/client.py", line 172, in _internal_call
    except requests.exceptions.HTTPError:
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.spotify.com/v1/playlists/37i9dQZF1E36buj8U8F3MT?additional_types=track

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotify_metafy/media_player.py", line 80, in setup_platform
    spotify_playlist_info = spotify_media_player._spotify.playlist(uri)
  File "/usr/local/lib/python3.7/site-packages/spotipy/client.py", line 494, in playlist
  File "/usr/local/lib/python3.7/site-packages/spotipy/client.py", line 207, in _get
    def _post(self, url, args=None, payload=None, **kwargs):
  File "/usr/local/lib/python3.7/site-packages/spotipy/client.py", line 187, in _internal_call
    except requests.exceptions.RetryError:
spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/playlists/37i9dQZF1E36buj8U8F3MT?additional_types=track:
 The access token expired

So all my sensors are gone while other integrations of Spotify are still working good.
How to fix?