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

Hmmm. Your log seems to suggest that pychromecast cant connect yo any of the devices. Pychromecast yses zeroconf which relies on mDNS (the .local host names) so I would say your HA instance has some issues at the time of the log.

Can you send a log of what spotcast logs in debug mode during the failure?

Hello. I’m getting the following error. Any ideas?

2020-09-01 22:47:09 ERROR (MainThread) [homeassistant.components.homeassistant] Component error: spotcast - No module named ‘pychromecast’

I tried installing pychromecast via pip but that didn’t seem to help any.

How have you installed HA?

I am running Home Assistant version 0.111.4. That’s where the log above came from. I installed spotcast using HACS.

I installed HA via pip like in these instructions: Installing Home Assistant on Raspberry pi with touchscreen kiosk mode

That seems like an unusual question so perhaps I don’t understand what’s you are asking.

Reason for the question iw that HA automatically installls pychromecast without spotcast so i assumed you had an unusual setup.
Normal cast wont work for u either without pychromecast so my suggestion is to head over to ask for help from the HA community discord channel

In case someone else has a similar issue, I solved it by copying pychromecast, google and casttube from /home/pi/.local/lib/python3.7/site-packages/ to /srv/homeassistant/lib/python3.7/site-packages/ and changing owner to homeassistant user. I probably could have made this a symbolic link so that PIP will update accordingly.

Likely the result of the installation method… making a virtual package into a native raspberry install causing two python installations. I’ll fix this some other time.

Installing Home Assistant on Raspberry pi with touchscreen kiosk mode

Hi, can you explain how I can view this in debug mode?

It happens again now (after 4 days). Same behaviour as before, same log, same speaker group that doesn’t work (while others do work that are listed in the log).

Also: as soon as I restarted HA server, that speaker group started to play. Could there somehow be a hang in my network or HA holding a service call and release it after restart? Very strange this part.

Just to clarify: I noticed the speaker group not playing. Then did nothing for around 8 hours. Then tried again, still didn’t play (hoped it fixed itself). Then I restarted HA and immediately the service call of just a minute ago started when HA server went offline.

Hi all - I have Spotcast installed and I’m trying to get it working. I’m having a problem when I add random_song: true to my script. It gives an error saying ‘Total’. Any ideas?

My Code:

aa_play_song:
  alias: 'aaa Play a Spotify track on Chromecast'
  sequence:
     - service: spotcast.start
       data:
         random_song: true
         shuffle: true
         device_name: 'Kitchen max'
         uri: 'spotify:playlist:37i9dQZF1DX5hHfOi73rY3?si=RHL7DWhXS1G9L8e-olfnoQ'

And the error log:

Log Details (ERROR)
Logger: homeassistant.components.websocket_api.http.connection.140464490510464
Source: custom_components/spotcast/__init__.py:247
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 11:51:10 (1 occurrences)
Last logged: 11:51:10

'total'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 125, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1308, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1347, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 313, in start_casting
    play(client, spotify_device_id, uri, random_song, repeat, shuffle, position)
  File "/config/custom_components/spotcast/__init__.py", line 247, in play
    position = random.randint(0, results["total"] - 1)
KeyError: 'total'

Just installed Spotcast on my Pi4. Brilliant. Exactly what I was looking for. =)
Using it in my flows in node red.

does it work with another playlist?

I’ve started having issues with Spotcast and my tokens expiring within hours of updating them, i have tried both in incognito mode and normally and they seem to only last a few hours

2020-09-11 02:13:46 ERROR (SyncWorker_35) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/player/devices returned 401 due to The access token expired

I tried it with a couple of different ones. I’ll have another play as soon as I get a chance over the next couple of days.

If you did not figure that out, the logger config needs to look like this to work for spotcast.

logger:
  default: info
  logs:
    custom_components.spotcast: debug
1 Like

Did you follow the steps in the readme exactly?

I did, yes

Somehow the addon stopped working yesterday… Tried a lot but cant find anything. Getting the message:
Failed to call service spotcast/start. empty range for randrange() (0, 0, 0)

2020-09-14 13:27:54 DEBUG (SyncWorker_50) [custom_components.spotcast] websocket_handle_devices msg: {'type': 'spotcast/devices', 'id': 84}
2020-09-14 13:27:54 DEBUG (SyncWorker_50) [custom_components.spotcast] setting up with  account default
2020-09-14 13:27:54 DEBUG (SyncWorker_50) [custom_components.spotcast] expires: 1600086471 time: 1600082874.2053912
2020-09-14 13:27:51 DEBUG (SyncWorker_23) [custom_components.spotcast] setting up with  account default
2020-09-14 13:27:51 DEBUG (SyncWorker_23) [custom_components.spotcast] Version: 3.4.2, playing URI: spotify:playlist:2WoOqqMVjSL0nLBHeVPpgF on device-id: 16a3f3ad93f100cfca20555072d10cee182e22a6
2020-09-14 13:27:51 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.2774958192] empty range for randrange() (0, 0, 0)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 125, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1308, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1347, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 313, in start_casting
    play(client, spotify_device_id, uri, random_song, repeat, shuffle, position)
  File "/config/custom_components/spotcast/__init__.py", line 247, in play
    position = random.randint(0, results["total"] - 1)
  File "/usr/local/lib/python3.8/random.py", line 248, in randint
    return self.randrange(a, b+1)
  File "/usr/local/lib/python3.8/random.py", line 226, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

Try remove random song

Just a FYI - I managed to solve this as I was using the wrong code for the playlists. rather than clicking on share on a playlist and copying the while string from that, I just now copy the long code part of the URL on the spotify web player.

Setting a default device name helped. Remove random song just caused another error.

Anyone know how I would convert this script to run from nodered?

spotify_disco:
  alias: 'Spotify Disco'
  sequence:
     - service: spotcast.start
       data:
         random_song: true
         shuffle: true
         device_name: 'Kitchen max'
         uri: 'spotify:playlist:37i9dQZF1DX4kJGZhtnOIh'