Spotify device ID's keep changing

Hi,

I’ve made a wake up routine that includes Spotify. It worked fine for about a week but it stoped working after that week. Turned out that the device ID had changed from what I had in the alias property in media_player. I’ve looked up the Spotify API documentation and it turns out device id’s are not static. They can change from time to time.

I’ve tried to remove the aliases all together and directly use the name of the device in my script but that resulted in the following error:

2019-02-09 16:16:34 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved

Traceback (most recent call last):

  File "/usr/src/app/homeassistant/helpers/service.py", line 286, in _handle_service_platform_call

    await getattr(entity, func)(**data)

  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run

    result = self.fn(*self.args, **self.kwargs)

  File "/usr/src/app/homeassistant/components/media_player/spotify.py", line 245, in select_source

    self._player.transfer_playback(self._devices[source],

KeyError: 'STR-DN1080 47016D'

I figured from this error that the device id is mandatory and that it is not possible to use devices names. (Or is it possible?).

Any idea how I could resolve this issue without changing the ID in the configuration from time to time. Is there some auto alias setting I’m not seeing?

Bump on a old post, I ran into this exact issue with dynamic Spotify device id’s.

Luckily HA recognizes device names as well, please see below

service: spotcast.start
data:
  device_name: Echo Show
  uri: spotify:playlist:37i9dQZF1E36xhmfSuyjJz
  random_song: true

Below is my node red flow selecting which device using a input selection helper

[{"id":"73f724c46d293274","type":"server-state-changed","z":"e23ef64855980fae","name":"Devices","server":"91b2c05b.a0cf7","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"input_select.devices","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":70,"y":140,"wires":[["8b4a4426ce061a3e"]]},{"id":"8b4a4426ce061a3e","type":"switch","z":"e23ef64855980fae","name":"Options Payload","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Everywhere","vt":"str"},{"t":"eq","v":"Bedroom","vt":"str"},{"t":"eq","v":"Echo Show","vt":"str"},{"t":"eq","v":"Indoors","vt":"str"},{"t":"eq","v":"Music Echo","vt":"str"},{"t":"eq","v":"Office Echo","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":260,"y":140,"wires":[["f931cfe898cab778"],["f9e93f08e7bff401"],["251cf3e5cc24138f"],["d99c3ae73f6a23f4"],["30b7b9eebf2eb052"],["8e547ad0c463b398"]]},{"id":"f931cfe898cab778","type":"api-call-service","z":"e23ef64855980fae","name":"Everywhere","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.spotify_johnathan_norman"],"data":"{\t   \"device_name\":\"Everywhere\",\t   \"random_song\":true\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":450,"y":20,"wires":[[]]},{"id":"f9e93f08e7bff401","type":"api-call-service","z":"e23ef64855980fae","name":"Bedroom","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.everywhere"],"data":"{\t   \"device_name\": \"Bedroom Echo\",\t   \"random_song\":true \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":460,"y":80,"wires":[[]]},{"id":"251cf3e5cc24138f","type":"api-call-service","z":"e23ef64855980fae","name":"Echo Show","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.everywhere"],"data":"{\t   \"device_name\": \"Echo Show\",\t   \"random_song\":true \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":470,"y":140,"wires":[[]]},{"id":"d99c3ae73f6a23f4","type":"api-call-service","z":"e23ef64855980fae","name":"Indoors","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.everywhere"],"data":"{\t   \"device_name\": \"Indoors\",\t   \"random_song\":true \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":480,"y":200,"wires":[[]]},{"id":"30b7b9eebf2eb052","type":"api-call-service","z":"e23ef64855980fae","name":"Music Echo","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.everywhere"],"data":"{\t   \"device_name\": \"Music_Echo\",\t   \"random_song\":true \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":470,"y":260,"wires":[[]]},{"id":"8e547ad0c463b398","type":"api-call-service","z":"e23ef64855980fae","name":"Office Echo","server":"91b2c05b.a0cf7","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["media_player.everywhere"],"data":"{\t   \"device_name\": \"Office Echo\",\t   \"random_song\":true \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":410,"y":320,"wires":[[]]},{"id":"91b2c05b.a0cf7","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]