home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.72k stars 30.45k forks source link

Spotify Integration and Media Player Card - No Active Source #93157

Closed supersebbo closed 9 months ago

supersebbo commented 1 year ago

The problem

When the Spotify integration goes into 'Idle' state it forgets the last 'Source' device meaning there are extra steps required to be able to get it to play music from Home Assistant. This is especially tedious when using the Lovelace Media Control card - to the point it feels like a bug? Perhaps this is intended and this is a feature request, but the current flow contains an unhandled error which I think deserves better treatment.

To reproduce:

  1. Assign a source to the Spotify integration and play some music.

  2. Stop playing the music and wait approx 5 mins for the Spotify integration to go into 'Idle' state.

  3. In this state the Media Control Card reverts to it's default 'blank' state.

  4. Now attempt you play music by either hitting Play on the card or going into the media browser and selecting a Playlist. It will give a Service error as the Spotify integration has 'forgotten' the Source device even though the source is still available. Spotify1

  5. In order to assign a source again requires 3 further UI actions. 1) open the card options modal 2) hit the drop-down to select the source again (even if there is only one source, it does not get automatically selected), 3) Click out of the modal. Spotify3

  6. Now the Media Control card comes back to life and you can select media to play.

I would expect that the Spotify integration would store the ID of the last played Source device and, if no other source is active, automatically select this the next time a Play action is requested, rather than giving the Service error. OR there is an option to select a 'default' source for the Spotify integration that would be used if no other source were active when a Play action is requested.

What version of Home Assistant Core has the issue?

core-2023.5.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Spotify

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @frenck, mind taking a look at this issue as it has been labeled with an integration (spotify) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `spotify` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign spotify` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


spotify documentation spotify source (message by IssueLinks)

skynet01 commented 1 year ago

Same here, +1 to get this fixed :)

JohnnyTwoFingers commented 1 year ago

Same here, same issue

1liminal1 commented 1 year ago

Spotify seems completely broken now

mekeppler commented 1 year ago

I am experiencing the same issue: Failed to call service media_player/media_pause. Spotify error: None

When I open the media card and try and select a source, virtually everything is gone...I have 10+ speakers (Sonos, Google, Alexa, etc.) in my house and none of them are displayed.

image

AzzMG commented 1 year ago

Same here, +1

joostlek commented 1 year ago

Right, I am going to respond on the original issue, for others that have a similar problem but not quite, please open a new issue.

This is an issue from Spotify itself. If spotify has an active playback session (maybe doing it via the app) it allows requests to play pause etc. If it doesn't, we get this error. This isn't something HA itself can solve. It would be something bad to always select the single source that's available. For example what if the device you wanted was offline and you opened spotify on mobile, it would blast out on mobile.

But HA can help you with this, create an automation to select a source and then start blasting your favorite tunes.

@mekeppler Spotify has something called restricted devices, devices from which you can't control the playback. My sonos roam is one of them. In a recent update I removed the supported features from them, as they did not work anyway. If all those speakers you have are resitrcted devices, we can't do much. If you think this is wrong, please open a new issue and I will be happy to help you check it out.

skynet01 commented 1 year ago

Can you give an example of automation that would mitigate this issue?

joostlek commented 1 year ago

https://github.com/home-assistant/core/issues/98739#issuecomment-1685947549 Here is a good example. You see the media_player select source, wait a bit, then turn on the music.

jjprz commented 1 year ago

If you need to play Spotify on mobile for this to work, the Spotify integration in Home Assistant is no longer useful.

dusopn commented 11 months ago

Hello. I am at ends here too. Shortly after music was played, source list starts to lose all previous sources. So I thought I will define the source as is stated in documentation, in my thinking maybe that will stay available later, but when I try to do this, I get error:

service: media_player.select_source
entity_id: media_player.spotify_myusername
data:
  source: "Bathroom speaker"

If dissapearing sources can't be solved, I am willing to select source in the automation code before playing, but as per the error above I don't know how to?

Thanks for any clarification

jjprz commented 11 months ago

The only solution to make Spotify always work is the custom Spotcast component

joostlek commented 11 months ago

When filling in the service in the developer tools, my service call looks like

service: media_player.select_source
data:
  source: Bathroom speaker
target:
  entity_id: media_player.spotify_joost_lekkerkerker
dusopn commented 11 months ago

When filling in the service in the developer tools, my service call looks like

service: media_player.select_source
data:
  source: Bathroom speaker
target:
  entity_id: media_player.spotify_joost_lekkerkerker

Thank you, I used syntax from official documentation, should that be changed? I would do it myself just I am not sure if I understand this correctly:

https://www.home-assistant.io/integrations/spotify/#selecting-output-source

The service now seems to activate, however this will still not show the source as available in the drop-down list or service state attributes. Isn't that what should happen?

Thank you again. D

dusopn commented 11 months ago

The only solution to make Spotify always work is the custom Spotcast component

Thanks I'll look into that.

joostlek commented 11 months ago

The source list directly reflect Spotifys API. If they don't return a value, we don't know where to connect to

jtauscher commented 9 months ago

Same problem with Spotify/HA/Alexa. It only works when Alexa is already playing spotify.

joostlek commented 9 months ago

I have a PR to fix the docs. I am going to close this issue since this is expected behaviour. Spotify from itself can't start a session on a device. Maybe there are ways using cast or something like that, but that's outside of the scope of this integration.

jtauscher commented 9 months ago

I have a PR to fix the docs. I am going to close this issue since this is expected behaviour. Spotify from itself can't start a session on a device. Maybe there are ways using cast or something like that, but that's outside of the scope of this integration.

I think thats not exactly true unless i misunderstood. I can start even group session with spotify app on mobile phone.

joostlek commented 9 months ago

Yes but that works via your local network, your phone can discover networks signals that there is a spotify supported device in the network it can play music on. Home assistant only connects to the Spotify API, and they can't scan your local network

jtauscher commented 9 months ago

Hm I see your point, but it also works while not being connected to wifi. That means I can also start eg multiroom sessions on alexa speakers being connected to mobile network anywhere in the world.

joostlek commented 9 months ago

I am not sure how that works, but if you turn on debug logging and reload the integration you will see that the returned devices probably is empty.

jtauscher commented 9 months ago

Thats what appears in the logs after that command:

service: media_player.select_source
entity_id: media_player.spotify_xxx
data:
  source: Wohnbereich klein

log:

`2024-01-09 13:53:03.356 ERROR (SyncWorker_20) [spotipy.client] Max Retries reached 2024-01-09 13:53:03.360 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.spotify_xxx fails Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 486, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 894, in urlopen return self.urlopen( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 894, in urlopen return self.urlopen( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 894, in urlopen return self.urlopen( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 884, in urlopen retries = retries.increment(method, url, response=response, _pool=self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/util/retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.spotify.com', port=443): Max retries exceeded with url: /v1/me/player?additional_types=episode (Caused by ResponseError('too many 500 error responses'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/spotipy/client.py", line 266, in _internal_call response = self._session.request( ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request resp = self.send(prep, send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send r = adapter.send(request, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 510, in send raise RetryError(e, request=request) requests.exceptions.RetryError: HTTPSConnectionPool(host='api.spotify.com', port=443): Max retries exceeded with url: /v1/me/player?additional_types=episode (Caused by ResponseError('too many 500 error responses'))`

joostlek commented 9 months ago

That's not what I asked - I asked for debug logging after you reloaded the integration

jtauscher commented 9 months ago

My bad. Don't know how to read that stuff:

`2024-01-09 14:08:40.360 DEBUG (SyncWorker_3) [spotipy.client] Sending GET to https://api.spotify.com/v1/me/ with Params: {} Headers: {'Authorization': 'Bearer BQDqZqAeBfy0fVBi8ouW5yLuX9w', 'Content-Type': 'application/json'} and Body: None 2024-01-09 14:08:40.569 DEBUG (SyncWorker_3) [spotipy.client] RESULTS: {'display_name': 'xxx', 'external_urls': {'spotify': 'https://open.spotify.com/user/sihd7pp4xke'}, 'href': 'https://api.spotify.com/v1/users/sihd7ip4xke', 'id': 'sihdp4xke', 'images': [], 'type': 'user', 'uri': 'spotify:user:sihd7i0pp4xke', 'followers': {'href': None, 'total': 0}, 'country': 'PH', 'product': 'premium', 'explicit_content': {'filter_enabled': False, 'filter_locked': False}} 2024-01-09 14:08:40.570 DEBUG (SyncWorker_4) [spotipy.client] Sending GET to https://api.spotify.com/v1/me/player/devices with Params: {} Headers: {'Authorization': 'Bearer BQDqZuuX9w', 'Content-Type': 'application/json'} and Body: None 2024-01-09 14:08:40.643 DEBUG (SyncWorker_4) [spotipy.client] RESULTS: {'devices': [{'id': 'b98fcb2c3_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Echo Dot Flur', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 23}, {'id': '09595b87-fe69-4cca-98f7-14c9f28fb6a0_amzn_3', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Ansage', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 26}, {'id': 'f042d6ca5_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Jxxxs 5. Echo Dot', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 34}, {'id': '10ef204f-e4d1-4373-aa02-0cca_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'WC', 'supports_volume': False, 'type': 'Speaker', 'volume_percent': 23}, {'id': 'e6ff7d562_amzn_5', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Wohnbereich', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 8}, {'id': '09595b87-fe69-4cca-98f7-14cb6a0_amzn_4', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Flur', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 26}, {'id': '095956a0_amzn_2', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Multiroom', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 26}, {'id': '8b135a98_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Wohnzimmer', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 26}, {'id': -c198-4c4c-bd50-'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Wohnbereich klein', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 24}, {'id': -fe69-4cca-98f7-14c9f28fb6a0_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Bad', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 26}, {'id': '594a3f3_amzn_1', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Küche', 'supports_volume': True, 'type': 'Speaker', 'volume_percent': 24}]} 2024-01-09 14:08:40.649 DEBUG (MainThread) [homeassistant.components.spotify] Finished fetching xxx Devices data in 0.079 seconds (success: True) 2024-01-09 14:08:40.654 DEBUG (SyncWorker_4) [spotipy.client] Sending GET to https://api.spotify.com/v1/me/player with Params: {'market': None, 'additionaltypes': [<MediaType.EPISODE: 'episode'>]} Headers: {'Authorization': 'Bearer BQDJ41u7j8DUap6qDhQKvEJ-5XmLjQ46zw8ggC8ouW5yLuX9w', 'Content-Type': 'application/json'} and Body: None 2024-01-09 14:08:40.735 DEBUG (SyncWorker_4) [spotipy.client] RESULTS: None

`

joostlek commented 9 months ago

And what do you see in the select devices dropdown?

jtauscher commented 9 months ago

It seems to be all there..

image

joostlek commented 9 months ago

You can select one right?

jtauscher commented 9 months ago

Some work some dont:

Fehler beim Aufrufen des Diensts (error during calling service) media_player/select_source. Spotify error: None

joostlek commented 9 months ago

Can you maybe open a separate issue? I think this is different from what this issue is about, but this seems interesting