Spotcast - custom component to start playback on an idle chromecast device

I don’t know what to say. transfer_playback was removed in favour of force_playback in one the the larer releases.

Its boolean so it is either true or false. The text you are referring to was what is repeated, just informational

Ooh ok, now it all makes sense. Thanks!

Hi,

I’ve been trying to setup spotcast for a day without success.

Home Assistant 0.108.4
Spotcast v2.9.1 (HACS)

Here is my config

spotcast:
  username: !secret spotify_primary_username
  password: !secret spotify_primary_password
  accounts:
    klang:
      username: !secret spotify_klang_username
      password: !secret spotify_klang_password
    jake:
      username: !secret spotify_jake_username
      password: !secret spotify_jake_password

and my scritps.yaml

test_spotcast:
  alias: 'Test Spotcast Track'
  sequence:
    - service: spotcast.start
      data:
        device_name: "Office Speaker"
        uri: 'spotify:track:1Zyd6zQnC6XIIzmg3hP7Ot'

Here is my log:

2020-04-15 22:12:45 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 177, in start_casting
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/__init__.py", line 266, in startSpotifyController
    sp.launch_app()
  File "/usr/local/lib/python3.7/site-packages/pychromecast/controllers/spotify.py", line 85, in launch_app
    "Timeout when waiting for status response from Spotify app"
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app

Run script again and got this:

2020-04-15 22:17:03 INFO (MainThread) [homeassistant.components.script] Test Spotcast Track: Running script
2020-04-15 22:17:03 INFO (MainThread) [homeassistant.components.script] Test Spotcast Track: Executing step call service
2020-04-15 22:17:04 DEBUG (SyncWorker_18) [custom_components.spotcast] cast info: ChromecastInfo(host='192.168.88.14', port=8009, service='JBL-Link-300-ac3c324bf7dfef410f276cd2f18fd1ae._googlecast._tcp.local.', uuid='ac3c324b-f7df-ef41-0f27-6cd2f18fd1ae', manufacturer='JBL', model_name='JBL Link 300', friendly_name='Office Speaker', is_dynamic_group=None)
2020-04-15 22:17:04 INFO (SyncWorker_18) [pychromecast] Querying device status
2020-04-15 22:17:04 DEBUG (SyncWorker_18) [custom_components.spotcast] Found cast device: Chromecast('192.168.88.14', port=8009, device=DeviceStatus(friendly_name='Office Speaker', model_name='JBL Link 300', manufacturer='JBL', uuid='ac3c324b-f7df-ef41-0f27-6cd2f18fd1ae', cast_type='cast'))
2020-04-15 22:17:04 INFO (SyncWorker_18) [pychromecast.controllers] Not launching app CC32E753 - already running
2020-04-15 22:17:07 ERROR (MainThread) [homeassistant.components.google_assistant.http] Request for https://homegraph.googleapis.com/v1/devices:reportStateAndNotification failed: 400
2020-04-15 22:17:14 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 177, in start_casting
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/__init__.py", line 266, in startSpotifyController
    sp.launch_app()
  File "/usr/local/lib/python3.7/site-packages/pychromecast/controllers/spotify.py", line 85, in launch_app
    "Timeout when waiting for status response from Spotify app"
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.chromecast_devices is taking over 10 seconds
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 INFO (SyncWorker_7) [pychromecast] Querying device status
2020-04-15 22:17:27 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.chromecast_devices fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 476, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  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/spotcast/sensor.py", line 44, in update
    self._chromecast_devices = pychromecast.get_chromecasts()
  File "/usr/local/lib/python3.7/site-packages/pychromecast/__init__.py", line 122, in get_chromecasts
    blocking=blocking,
  File "/usr/local/lib/python3.7/site-packages/pychromecast/__init__.py", line 41, in _get_chromecast_from_host
    cast_type = CAST_TYPES.get(model_name.lower(), CAST_TYPE_CHROMECAST)
AttributeError: 'NoneType' object has no attribute 'lower'

Run script again and got this:

2020-04-15 22:19:58 ERROR (MainThread) [homeassistant.components.script] Test Spotcast Track: Error executing script. Unexpected error for call_service at pos 1: Failed to get device id from Spotify
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 154, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 178, in start_casting
    spotify_device_id = spotify_cast_device.getSpotifyDeviceId(client)
  File "/config/custom_components/spotcast/__init__.py", line 290, in getSpotifyDeviceId
    raise HomeAssistantError('Failed to get device id from Spotify')
homeassistant.exceptions.HomeAssistantError: Failed to get device id from Spotify
2020-04-15 22:19:58 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall script.test_spotcast (c:95851f51f8d848efb94a647572c8cc9b)>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1240, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/src/homeassistant/homeassistant/core.py", line 1255, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 207, in service_handler
    await script.async_turn_on(variables=service.data, context=service.context)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 284, in async_turn_on
    await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 802, in async_run
    await run.async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 523, in async_run
    await self._async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 540, in _async_run
    await self._async_step(log_exceptions=not propagate_exceptions)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 154, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 178, in start_casting
    spotify_device_id = spotify_cast_device.getSpotifyDeviceId(client)
  File "/config/custom_components/spotcast/__init__.py", line 290, in getSpotifyDeviceId
    raise HomeAssistantError('Failed to get device id from Spotify')

and some lucky time (5%), it works.

Please help me.

Thanks,

  • Klang

Spotcast is using a Spotify token library which requires the Spotify web player (på open.spotify.com) to be available. Seems you have problems accessing that from your hass instance

What should I do?

The graph called “Number of Requests/Endpoint” shows steady lines (looks like one line) with about 2805 requests per day each.
These lines are for “/v1/me”, “/v1/me/player” and “/v1/me/player/devices/”. So I guess only the last one is relevant.
Is it possible to find out exactly what device this is? :slight_smile:

Hi all,

this is a great component, and I feel like a newb asking around here, but how does this sensor configuration works? I’d love to have my chromecast to be available but the “normal” spotify card, won’t show the inactive chromecast devices as sources, and this should be a solution but I can’t get it to work. Can you help me starting up?

Thanks! Mart

Hi,
see documentation on Github: https://github.com/fondberg/spotcast
Everything you need should be there.

Hi @fondberg, thanks for building this. I am trying to use Spotcast to play a playlist via the Raspberry Pi 4 that I’m running home assistant on, and via the Spotify Connect Plugin, I have that part at least working. However, when I write a script to use Spotcast to play the playlist as part of an automation, I get a 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login. I believe I have all the redirect URI set up correctly with Spotify, and I get no Configuration errors for Spotcast, so I think that is all correct. I have the latest versions of HA and Spotcast installed. Any suggestions you might have would be appreciated.

Thanks.

I have the same issue, 400 error. Worked great last months… Are there more people with this issue? Did they changed the endpoint?

I have this issue suddenly starting today. Perhaps something to do with tokens @fondberg? I removed the spotcast component and installed again. Also removed password and re-entered and restarted HA. I never had this issue before, so can’t wrap my head around as to why this happens now suddenly… Haven’t touched anything within HA as well for the last few days.

The plugin worked fine before I went to bed about 12 hours ago, but now I get the 400 error that everyone describes above.
My initial thought is that spotify changed something with how the tokens work or something.
Enabled HTTP debugging in init.py by adding:

import http.client as http_client
http_client.HTTPConnection.debuglevel = 1

Not sure if that actually did anything, because I can’t find any HTTP debug lines, but oh well.

My logs output this:

2020-04-27 18:06:26 ERROR (MainThread) [homeassistant.components.script] Start classical music: Error executing script. Unexpected error for call_service at pos 2: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 154, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 168, in start_casting
    access_token, expires = get_spotify_token(username=user, password=pwd)
  File "/config/custom_components/spotcast/__init__.py", line 87, in get_spotify_token
    data = st.start_session(username, password)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 75, in start_session
    _login(session, cookies, username, password, token)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 33, in _login
    response.raise_for_status()
  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: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login
2020-04-27 18:06:26 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.139946646597584] 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login
2020-04-27 18:06:26 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.139946646597584] 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 134, in handle_call_service
    connection.context(msg),
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1255, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 207, in service_handler
    await script.async_turn_on(variables=service.data, context=service.context)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 284, in async_turn_on
    await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 802, in async_run
    await run.async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 523, in async_run
    await self._async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 540, in _async_run
    await self._async_step(log_exceptions=not propagate_exceptions)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 154, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 168, in start_casting
    access_token, expires = get_spotify_token(username=user, password=pwd)
  File "/config/custom_components/spotcast/__init__.py", line 87, in get_spotify_token
    data = st.start_session(username, password)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 75, in start_session
    _login(session, cookies, username, password, token)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 33, in _login
    response.raise_for_status()
  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: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login

Seems to me like it’s unable to log in, as this raises an exception:

File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 75, in start_session
    _login(session, cookies, username, password, token)

Username and password worked yesterday and haven’t been changed, so it’s got to have something to do with the token. I’ll try to investigate further.

@fondberg any idea?

I enabled all debugging and got some more out of the log.

2020-04-27 18:28:28 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.139857978908048] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=script.start_classical_music, old_state=<state script.start_classical_music=off; last_triggered=None, friendly_name=Start classical music @ 2020-04-27T18:26:45.141387+02:00>, new_state=<state script.start_classical_music=off; last_triggered=2020-04-27T18:28:25.621200+02:00, friendly_name=Start classical music @ 2020-04-27T18:26:45.141387+02:00>>}
2020-04-27 18:28:28 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.139857856915408] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=script.start_classical_music, old_state=<state script.start_classical_music=off; last_triggered=None, friendly_name=Start classical music @ 2020-04-27T18:26:45.141387+02:00>, new_state=<state script.start_classical_music=off; last_triggered=2020-04-27T18:28:25.621200+02:00, friendly_name=Start classical music @ 2020-04-27T18:26:45.141387+02:00>>}
2020-04-27 18:28:28 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.139857856915408] Sending {'id': 20, 'type': 'result', 'success': False, 'error': {'code': 'unknown_error', 'message': '400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login'}}
2020-04-27 18:28:29 DEBUG (MainThread) [homeassistant.components.http.auth] Authenticated 192.168.0.114 for /api/error/all using bearer token
2020-04-27 18:28:29 DEBUG (MainThread) [homeassistant.components.http.view] Serving /api/error/all to 192.168.0.114 (auth: True)
2020-04-27 18:28:30 DEBUG (MainThread) [homeassistant.components.http.view] Serving /api/webhook/82abfec00707c91dfbfb615505ca476da8e63303d874d36b0a38056f6cb71903 to 192.168.0.113 (auth: False)
2020-04-27 18:28:30 DEBUG (MainThread) [homeassistant.components.webhook] Handling webhook POST payload for 82abfec00707c91dfbfb615505ca476da8e63303d874d36b0a38056f6cb71903

I can’t really make much sense of it, the spotify server seems to deny the connection but I can’t figure out why.

I haven’t tested for a couple if days. I’ve been sick again and only have my cell phone.

It could be that they shut down the way to get the browser token again. If this is the case it is going to take a while to reverse engineer it.

Does anyone get it to work?

Glad I made it to this thread. Will be monitoring for updates and possible resolution. I am getting the same error.


Log Details (ERROR)

Logger: homeassistant.components.websocket_api.http.connection.22771971542224
Source: custom_components/spotcast/__init__.py:84
Integration: websocket_api (documentation, issues)
First occurred: 1:16:04 PM (1 occurrences)
Last logged: 1:16:04 PM
400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 134, in handle_call_service
    connection.context(msg),
  File "/usr/src/homeassistant/homeassistant/core.py", line 1232, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1259, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  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/spotcast/__init__.py", line 165, in start_casting
    access_token, expires = get_spotify_token(username=user, password=pwd)
  File "/config/custom_components/spotcast/__init__.py", line 84, in get_spotify_token
    data = st.start_session(username, password)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 75, in start_session
    _login(session, cookies, username, password, token)
  File "/usr/local/lib/python3.7/site-packages/spotify_token.py", line 33, in _login
    response.raise_for_status()
  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: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login


Hi,

I installed “Spotify Card” today as well as the main Spotify integration and both log in and work perfectly. When trying to select a Chromecast device I got the error “Failed to call service spotcast/start. Service not found.” so I installed “Start Spotify on chromecast” and followed the instructions but I get “Failed to call service spotcast/start. 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/login” now.

Any ideas as to where the error is coming from?

This is a single user instance and I have premium.

Unfortunately also found it not working today. While i am glad it isn’t just me, sad that it may be a more complicated server side problem.

Same here, same error…