Hi @thlucas,
first of all, I’m posting here because it seems to be the most active channel for support requests. If opening a bug report on GitHub is more appropriate, please let me know. Thank you!
Now to the point — this is most likely an issue on my end rather than a bug in the integration, but I’d appreciate any advice on how to handle it.
Sometimes in the evening, when I put my daughter to bed, I run a script that includes several actions using SpotifyPlus, such as:
- alias: Transfer playback to specified device
action: spotifyplus.player_transfer_playback
data:
entity_id: media_player.spotifyplus_paintervale
device_id: Camera Linda
play: false
- alias: Disable Shuffle Mode on the specified device
action: spotifyplus.player_set_shuffle_mode
data:
entity_id: media_player.spotifyplus_paintervale
device_id: Camera Linda
state: false
- alias: Start the playlist on the specified device
action: spotifyplus.player_media_play_context
data:
entity_id: media_player.spotifyplus_paintervale
device_id: Camera Linda
context_uri: >-
https://open.spotify.com/playlist/4ufyQwaLq1MR49KvGRCNAo?si=AbYrmWorRKeW7G20B4c44A&pi=kT5FCDevRke6T
mode: single
icon: mdi:bed
Occasionally, something goes wrong and it doesn’t work. I’ve noticed that sometimes a Home Assistant restart solves the issue. Yesterday I found the following error in the logs, and I thought it might be related — is that the case?
Is there something I could automate or handle differently to prevent this?
It’s a bit frustrating because it’s often my wife who triggers the script, and I’d rather not give her more reasons to say “this home automation causes more trouble than it solves.” You know what I mean, right? 
Thanks in advance!
This error originated from a custom integration.
Logger: custom_components.spotifyplus
Source: custom_components/spotifyplus/media_player.py:1246
integration: SpotifyPlus (documentation, issues)
First occurred: 08:29:28 (1 occurrence)
Last logged: 08:29:28
SpotifyApiError: Spotify Connect Directory Task - Could not retrieve dynamic device list: SAM1001E - Spotify Web API returned an error status while processing the "GetPlayerDevices" method. Status: 401 - Unauthorized Message: "The access token expired"
Traceback (most recent call last):
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyconnect/spotifyconnectdirectorytask.py", line 1246, in RefreshDynamicDevices
self.UpdatePlayerDevices()
~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyconnect/spotifyconnectdirectorytask.py", line 1395, in UpdatePlayerDevices
playerDevices:list[Device] = self._SpotifyClientInstance.GetPlayerDevices(refresh=True)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 7703, in GetPlayerDevices
self.MakeRequest('GET', msg)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 1393, in MakeRequest
msg.ResponseData = self._CheckResponseForErrors(msg, response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 796, in _CheckResponseForErrors
raise SpotifyWebApiError(errCode, errMessage, msg.MethodName, response.reason, _logsi)
spotifywebapipython.spotifywebapierror.SpotifyWebApiError: SAM1001E - Spotify Web API returned an error status while processing the "GetPlayerDevices" method.
Status: 401 - Unauthorized
Message: "The access token expired"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/config/custom_components/spotifyplus/media_player.py", line 1246, in update
self._playerState = self.data.spotifyClient.GetDevicePlaybackState(deviceId=self._attr_source)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 6483, in GetDevicePlaybackState
scDevice:SpotifyConnectDevice = self._ResolveDeviceObject(deviceId, activateDevice=False)
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 940, in _ResolveDeviceObject
scDevice = self.GetSpotifyConnectDevice(
device,
refreshDeviceList=True,
activateDevice=activateDevice)
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyclient.py", line 10118, in GetSpotifyConnectDevice
scDevice = self._SpotifyConnectDirectory.GetDevice(deviceValue, refreshDynamicDevices=True, raiseExceptionIfNotFound=False)
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyconnect/spotifyconnectdirectorytask.py", line 913, in GetDevice
scActiveDevice = self.RefreshDynamicDevices()
File "/usr/local/lib/python3.13/site-packages/spotifywebapipython/spotifyconnect/spotifyconnectdirectorytask.py", line 1266, in RefreshDynamicDevices
raise SpotifyApiError("%s - Could not retrieve dynamic device list: %s" % (self.name, str(ex)), ex, logsi=_logsi)
spotifywebapipython.spotifyapierror.SpotifyApiError: SpotifyApiError: Spotify Connect Directory Task - Could not retrieve dynamic device list: SAM1001E - Spotify Web API returned an error status while processing the "GetPlayerDevices" method.
Status: 401 - Unauthorized
Message: "The access token expired"