spotcast: Spotify connect devices not found with multiple accounts
I’m using spotcast with multiple spotify accounts and on different spotify connect devices. It works nicely if the spotify account I’m using in spotcast is the last one that played anything on the spotify connect device that I’m using. However, if the other spotify account was recently using that device, it fails with a “Device not found” or “Could not find device” error, see log below.
I’m not sure but this might be a restriction of spotify itself, so maybe it cannot be fixed in spotcast. If not, maybe a workaround to use the account that last played on the device could be used. I’m currently using such a workaround in my script:
spotify_play:
alias: Spotify Play
description: 'Play playlist in Spotify'
sequence:
- variables:
# For debugging, use fixed media otherwise it comes from MQTT
# media: spotify:playlist:718z1xDP4P2qzJzy8lidGc
# Get the target_device_name from the input select. These names have
# to match the names that are shown in the spotify accounts
# source_list.
target_device_name: "{{ states('input_select.musicbox_target_device') }}"
# Check which account was playing last on the target device as only
# this account can be used:
spotify_account: >
{% if target_device_name in (state_attr('media_player.spotify_thomas', 'source_list'))|trim %} thomas
{% elif target_device_name in (state_attr('media_player.spotify_anna', 'source_list'))|trim %} anna
{% else %} error in the automation
{% endif %}
- service: logbook.log
data:
name: MusicBox
message: >
starts playing spotify on '{{ target_device_name }}' with account '{{ spotify_account }}' media '{{ media }}'
- service: spotcast.start
data:
device_name: '{{ target_device_name }}'
random_song: false
repeat: "off"
start_volume: 22
account: '{{ spotify_account }}'
uri: '{{ media }}'
Log with spotify_device_id:
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.100 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Running script sequence
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.101 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step setting variables
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.103 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step call service
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.106 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step call service
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.415 ERROR (SyncWorker_3) [spotipy.client] HTTP Error for PUT to https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28 with Params: {} returned 404 due to Device not found
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.420 ERROR (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Error executing script. Unexpected error for call_service at pos 3: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28:
Oct 22 19:31:43 server-pi python[142640]: Device not found, reason: None
Oct 22 19:31:43 server-pi python[142640]: Traceback (most recent call last):
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 245, in _internal_call
Oct 22 19:31:43 server-pi python[142640]: response.raise_for_status()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
Oct 22 19:31:43 server-pi python[142640]: raise HTTPError(http_error_msg, response=self)
Oct 22 19:31:43 server-pi python[142640]: requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28
Oct 22 19:31:43 server-pi python[142640]: During handling of the above exception, another exception occurred:
Oct 22 19:31:43 server-pi python[142640]: Traceback (most recent call last):
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 451, in _async_step
Oct 22 19:31:43 server-pi python[142640]: await getattr(self, handler)()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 684, in _async_call_service_step
Oct 22 19:31:43 server-pi python[142640]: await service_task
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1738, in async_call
Oct 22 19:31:43 server-pi python[142640]: task.result()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1781, in _execute_service
Oct 22 19:31:43 server-pi python[142640]: await self._hass.async_add_executor_job(
Oct 22 19:31:43 server-pi python[142640]: File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
Oct 22 19:31:43 server-pi python[142640]: result = self.fn(*self.args, **self.kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/__init__.py", line 225, in start_casting
Oct 22 19:31:43 server-pi python[142640]: spotcast_controller.play(
Oct 22 19:31:43 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/spotcast_controller.py", line 312, in play
Oct 22 19:31:43 server-pi python[142640]: client.start_playback(**kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 1767, in start_playback
Oct 22 19:31:43 server-pi python[142640]: return self._put(
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 312, in _put
Oct 22 19:31:43 server-pi python[142640]: return self._internal_call("PUT", url, payload, kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 267, in _internal_call
Oct 22 19:31:43 server-pi python[142640]: raise SpotifyException(
Oct 22 19:31:43 server-pi python[142640]: spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28:
Oct 22 19:31:43 server-pi python[142640]: Device not found, reason: None
Oct 22 19:31:43 server-pi python[142640]: 2022-10-22 19:31:43.434 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [546740151824] http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28:
Oct 22 19:31:43 server-pi python[142640]: Device not found, reason: None
Oct 22 19:31:43 server-pi python[142640]: Traceback (most recent call last):
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 245, in _internal_call
Oct 22 19:31:43 server-pi python[142640]: response.raise_for_status()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
Oct 22 19:31:43 server-pi python[142640]: raise HTTPError(http_error_msg, response=self)
Oct 22 19:31:43 server-pi python[142640]: requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28
Oct 22 19:31:43 server-pi python[142640]: During handling of the above exception, another exception occurred:
Oct 22 19:31:43 server-pi python[142640]: Traceback (most recent call last):
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/websocket_api/commands.py", line 202, in handle_call_service
Oct 22 19:31:43 server-pi python[142640]: await hass.services.async_call(
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1738, in async_call
Oct 22 19:31:43 server-pi python[142640]: task.result()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1775, in _execute_service
Oct 22 19:31:43 server-pi python[142640]: await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 410, in _service_handler
Oct 22 19:31:43 server-pi python[142640]: await self.async_turn_on(variables=service.data, context=service.context)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 369, in async_turn_on
Oct 22 19:31:43 server-pi python[142640]: await coro
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 399, in _async_run
Oct 22 19:31:43 server-pi python[142640]: return await self.script.async_run(script_vars, context)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1524, in async_run
Oct 22 19:31:43 server-pi python[142640]: await asyncio.shield(run.async_run())
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 409, in async_run
Oct 22 19:31:43 server-pi python[142640]: await self._async_step(log_exceptions=False)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 453, in _async_step
Oct 22 19:31:43 server-pi python[142640]: self._handle_exception(
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 476, in _handle_exception
Oct 22 19:31:43 server-pi python[142640]: raise exception
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 451, in _async_step
Oct 22 19:31:43 server-pi python[142640]: await getattr(self, handler)()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 684, in _async_call_service_step
Oct 22 19:31:43 server-pi python[142640]: await service_task
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1738, in async_call
Oct 22 19:31:43 server-pi python[142640]: task.result()
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1781, in _execute_service
Oct 22 19:31:43 server-pi python[142640]: await self._hass.async_add_executor_job(
Oct 22 19:31:43 server-pi python[142640]: File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
Oct 22 19:31:43 server-pi python[142640]: result = self.fn(*self.args, **self.kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/__init__.py", line 225, in start_casting
Oct 22 19:31:43 server-pi python[142640]: spotcast_controller.play(
Oct 22 19:31:43 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/spotcast_controller.py", line 312, in play
Oct 22 19:31:43 server-pi python[142640]: client.start_playback(**kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 1767, in start_playback
Oct 22 19:31:43 server-pi python[142640]: return self._put(
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 312, in _put
Oct 22 19:31:43 server-pi python[142640]: return self._internal_call("PUT", url, payload, kwargs)
Oct 22 19:31:43 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/spotipy/client.py", line 267, in _internal_call
Oct 22 19:31:43 server-pi python[142640]: raise SpotifyException(
Oct 22 19:31:43 server-pi python[142640]: spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=055a55a152a33ae1fec9bc657e83091d2ab31c28:
Oct 22 19:31:43 server-pi python[142640]: Device not found, reason: None
Log with friendly name:
Oct 22 19:33:49 server-pi python[142640]: 2022-10-22 19:33:49.857 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Running script sequence
Oct 22 19:33:49 server-pi python[142640]: 2022-10-22 19:33:49.858 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step setting variables
Oct 22 19:33:49 server-pi python[142640]: 2022-10-22 19:33:49.861 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step call service
Oct 22 19:33:49 server-pi python[142640]: 2022-10-22 19:33:49.866 INFO (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Executing step call service
Oct 22 19:33:50 server-pi python[142640]: 2022-10-22 19:33:50.507 ERROR (SyncWorker_0) [custom_components.spotcast.spotcast_controller] Could not find device Esszimmer from hass.data
Oct 22 19:33:50 server-pi python[142640]: 2022-10-22 19:33:50.510 ERROR (MainThread) [homeassistant.components.script.spotify_play_debug] Debug Spotify Play: Error executing script. Error for call_service at pos 3: Could not find device with name Esszimmer
Oct 22 19:33:50 server-pi python[142640]: 2022-10-22 19:33:50.516 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [546740151824] Could not find device with name Esszimmer
Oct 22 19:33:50 server-pi python[142640]: Traceback (most recent call last):
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/websocket_api/commands.py", line 202, in handle_call_service
Oct 22 19:33:50 server-pi python[142640]: await hass.services.async_call(
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1738, in async_call
Oct 22 19:33:50 server-pi python[142640]: task.result()
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1775, in _execute_service
Oct 22 19:33:50 server-pi python[142640]: await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 410, in _service_handler
Oct 22 19:33:50 server-pi python[142640]: await self.async_turn_on(variables=service.data, context=service.context)
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 369, in async_turn_on
Oct 22 19:33:50 server-pi python[142640]: await coro
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 399, in _async_run
Oct 22 19:33:50 server-pi python[142640]: return await self.script.async_run(script_vars, context)
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1524, in async_run
Oct 22 19:33:50 server-pi python[142640]: await asyncio.shield(run.async_run())
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 409, in async_run
Oct 22 19:33:50 server-pi python[142640]: await self._async_step(log_exceptions=False)
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 453, in _async_step
Oct 22 19:33:50 server-pi python[142640]: self._handle_exception(
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 476, in _handle_exception
Oct 22 19:33:50 server-pi python[142640]: raise exception
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 451, in _async_step
Oct 22 19:33:50 server-pi python[142640]: await getattr(self, handler)()
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 684, in _async_call_service_step
Oct 22 19:33:50 server-pi python[142640]: await service_task
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1738, in async_call
Oct 22 19:33:50 server-pi python[142640]: task.result()
Oct 22 19:33:50 server-pi python[142640]: File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1781, in _execute_service
Oct 22 19:33:50 server-pi python[142640]: await self._hass.async_add_executor_job(
Oct 22 19:33:50 server-pi python[142640]: File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
Oct 22 19:33:50 server-pi python[142640]: result = self.fn(*self.args, **self.kwargs)
Oct 22 19:33:50 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/__init__.py", line 190, in start_casting
Oct 22 19:33:50 server-pi python[142640]: spotify_device_id = spotcast_controller.get_spotify_device_id(
Oct 22 19:33:50 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/spotcast_controller.py", line 234, in get_spotify_device_id
Oct 22 19:33:50 server-pi python[142640]: spotify_cast_device = SpotifyCastDevice(
Oct 22 19:33:50 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/spotcast_controller.py", line 62, in __init__
Oct 22 19:33:50 server-pi python[142640]: self.castDevice = self.getChromecastDevice(device_name)
Oct 22 19:33:50 server-pi python[142640]: File "/home/homeassistant/.homeassistant/custom_components/spotcast/spotcast_controller.py", line 88, in getChromecastDevice
Oct 22 19:33:50 server-pi python[142640]: raise HomeAssistantError(
Oct 22 19:33:50 server-pi python[142640]: homeassistant.exceptions.HomeAssistantError: Could not find device with name Esszimmer
- Using latest version of spotcast
- Using latest stable version of Home Assistant
- I have setup the Spotify integration in Home Assistant
- I have Spotify Premium
- I am using multiple accounts
- I have renewed my
sp_dcandsp_keyvalues and restarted Home Assistant (see README) - I’m attaching relevant logs with level debug for component spotcast (see README)
- I’m using entity_id in the service call and have tried device_name but the issue remains
Environment
- Installation type: [HA_OS|Container|Supervised|Core]
- HA version: [ 2022.10.5]
- spotcast version: [3.6.30 ]
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 18
This renders the spotify integration and spotcast pretty useless for multi-user use…
working on it for v4. Its going to be part of the standard workflow. But I will need help from developpers with Alexa devices that will be able to work on deregistering an application to be able to register on a second account after.
Indeed it looks like the Spotify API does not return the spotify connect device anymore when another user is already connected to that device. The only way to make it show up in the API is by joining a group session or by taking over the device in another (desktop/mobile) client. Seems the spotify web client also uses the same API as it has the same problem.
I too have the same problem… I have a HEOS speaker that is usually controlled from HA with a spotify account I made specifically for HA, and it works really well… but occasionally someone in the house uses their spotify phone app to directly cast to that speaker from their phone and their spotify account. From what I can tell, the Spotify API will stop returning the device when called from the HA account, when a different account was the last to access/use it. I’m assuming that is what causes the ‘device not found’ errors with SpotCast. It seems that the official Spotify apps, on phones, or even the Windows app, will detect speakers on the local network (not just from the API) and reassign/add them back to the active account… It seems SpotCast doesn’t have this power (yet?).
My only solution thus far, is when this problem is discovered/reported, I log into the HA spotify account on my windows app and start a song to that speaker, that re-associates it to the HA account and then all my automatons work fine again. (until somebody else uses the speaker again, fortunately for me, this is rare)