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

Weird. Can you see the chromecast devices in the sensor.chromecasts or it is empty?

The sensor is empty:

devices_json: []
last_update: 2019-05-19T13:36:57.103061+02:00
friendly_name: Chromecast Devices

but the media_player works just fine:

media_player.koksradion	playing	
{ 
volume_level: 0.25
is_volume_muted: false
media_content_id: spotify:track:56lhDZNQ5J47aog6mGKeGk
media_content_type: music
media_duration: 288.744
media_position: 5.533
media_position_updated_at: 2019-05-19T11:39:19.528833+00:00
media_title: Thunder Road
media_artist: Bruce Springsteen
media_album_name: Born To Run
app_id: CC32E753
app_name: Spotify
friendly_name: Köksradion
entity_picture: https://i.scdn.co/image/a1ab684baeee9102220549945c6e11b89cda7de7
supported_features: 21439 
}

I’m running HASS v0.93.1 in Docker with network_mode: host.

Really weird
 they both use pychromecast.
Can you see some errors in the log for spotcast?

Nope, I set the component logger to debug

logger:
  default: error
  logs:
    homeassistant.components.media_player: debug
    custom_components.spotcast: debug

and all I get is:

2019-05-20 07:26:35 DEBUG (SyncWorker_2) [custom_components.spotcast.sensor] updating

2019-05-20 07:26:40 DEBUG (SyncWorker_2) [custom_components.spotcast.sensor] Found chromecast devices: []

I do get an error in the Home Assistant log about the Spotify player not being able to update either:

  File "/usr/src/app/homeassistant/components/spotify/media_player.py", line 169, in update

This line refers to the available device's section in the code:

# Available devices
        player_devices = self._player.devices()
        if player_devices is not None:
            devices = player_devices.get('devices')
[...]

When checking the logs more thoroughly, I realized I had quite a few network related errors, such as Network unreachable. I googled a bit and then found this forum post just now: Discovery: Network is unreachable (0.86 and up) and after running the suggested command, my spotcast sensor is being correctly updated. I take it my server is a bit overloaded on the network side, no?

(I do run quite a few Docker containers separately and they all create their own sub-networks, possibly bogging down the host network stack. I’ve had a few issues with this before.)

2019-05-20 08:16:22 DEBUG (SyncWorker_5) [custom_components.spotcast.sensor] updating
2019-05-20 08:16:30 DEBUG (SyncWorker_5) [custom_components.spotcast.sensor] Found chromecast devices: [Chromecast('192.168.1.66', port=8009, device=DeviceStatus(friendly_name='Vardagsrummet', model_name='Chromecast Audio', manufacturer='Google Inc.', uuid=UUID('602724f8-c569-62aa-354e-80f90faf46c4'), cast_type='audio')), Chromecast('192.168.1.66', port=42807, device=DeviceStatus(friendly_name='Hemgrupp', model_name='Google Cast Group', manufacturer='Google Inc.', uuid=UUID('793c0e22-0e2b-4608-8d97-36cfe438da78'), cast_type='group')), Chromecast('192.168.1.190', port=8009, device=DeviceStatus(friendly_name='TV', model_name='Chromecast', manufacturer='Google Inc.', uuid=UUID('6f693d76-a8f4-4c9f-065b-cb42c409c413'), cast_type='cast')), Chromecast('192.168.1.171', port=8009, device=DeviceStatus(friendly_name='Mini', model_name='Google Home Mini', manufacturer='Google Inc.', uuid=UUID('14d20608-1d87-eecb-6748-d3103bbca314'), cast_type='audio')), Chromecast('192.168.1.152', port=8009, device=DeviceStatus(friendly_name='Köksradion', model_name='Chromecast Audio', manufacturer='Google Inc.', uuid=UUID('2dba9482-fec8-4e35-11c5-8c5aed64367f'), cast_type='audio'))]

I see. Let me know if you encounter something related to spotcast

For people ignoring the above comment (like me) thinking it only affects updates of the component files itself, this also fixed the issue I had with adding non default accounts to the configuration after testing with only the primary account added. Also remember to restart after folder is deleted
 :slight_smile:

Problem I had was that if I added accounts to configuration, nothing would start playing when I used the spotcast service. The Google Home would chime with that cast connection sound, but no playback would start.

New release out with these new features

  • transfer of current playback
  • use entity_id as an alternative to chromecast device name
1 Like

Nice! But what does ‘transfer of current playback’ mean?

If something is playing on any device (eg your phone) it will start spotify on the chromecast device or speaker group and transfer (continue the playback from the other device) to the chromecast device

2 Likes

Wow, this seems to solve something I have been struggling with for a very long time!

Unfortunately, I cannot get it to work on my hassio (hass OS - raspberry pi 3) installation :frowning: I’m running Home Assistant 0.93.2

In this folder:
config/custom_components/spotcast/

I have put the following files:

  • __init__.py
  • manifest.json
  • sensor.py
  • services.yaml

I’ve also added this configuration in configuration.yaml:

spotcast:
  username: my_username
  password: my_password

Unfortunately when I start home assistant, the following log message appears:
Setup failed for spotcast: No setup function defined.

Any idea what I’m missing here?

Works great, was hoping for something like this. But eeehhhmmm dombo here.

How to stop the music ??? When I press the play icon before a second time it restarts the playlist. Or do I have to add the “normal” spotify player to ?

Nice to hear. Stopping is not implemented. Neither is pause
 pull requests are welcome :wink:

I know many people have the spotify media player side by side this card.

The version of pychromecast this component requires is different to the one that hass requires, and this is causing a conflict at launch. If I update pychromecast to 3.2.2 as per spotcast, hass tries to install 3.2.1, fails and gets stuck on startup. If I do nothing (i.e. leave pychromecast at 3.2.1) this component fails to load. If I edit the manifest.json for this plugin to pychromecast==3.2.1, hass and the component load but the functionality doesn’t seem to work at all. Any ideas?

What you say is true. Unfortunately the only ways are:

  1. Use an older version of the component. See commit history for when the component used 3.2.1 of pychromecast
  2. Wait for next release of HA as the version of pychromecast was bumped in a PR like a week ago

OK thankyou!

This request has been mentioned already but I am going to throw my support.

I rarely listen to a playlist I created in order. I use the shuffle play functionality from spotify when starting a playlist in their application.

I have tried to mimic this behavior using the spotify component and the shuffle command but there are a couple of problems with it.

The spotcast start service is asynchronous. What I have seen that after I call the start command, a shuffle command from the spotify component will usually not perform correctly. I have added wait templates to look at available updates in the spotify component but the component itself updates what seems to be around 10-15 seconds. This makes having these waits be really painful.

In the time that I did get something to work, the playlist always started with the first song and then followed by a shuffled song. This is not quite the behavior I wanted. I have tried the shuffle first and then followed by a media start but that never seems to work.

The spotify API seems to have support for starting a playlist at an offset. I think having this to a random number between 0 and the number of songs in a playlist will handle part of always the playlist with the same song.

I tried modifying part of spotcast using the available spotipy routines but found some issues:
The spotify API allows the track information from a playlist to be obtained through

GET https://api.spotify.com/v1/playlists/{playlist_id}/tracks

Part of this information is the number of tracks. Unfortunately, spotipy doesn’t seem at first glance to provide this same api. The closest thing I could find was:

user_playlist_tracks

This seems to require an actual user id which I could not immediately figure out how to do.

 return self._get("users/%s/playlists/%s/tracks" % (user, plid),
                         limit=limit, offset=offset, fields=fields,
                         market=market)  
  1. Even if I could figure out how to obtain the number of tracks, I didnt like the idea of having to have another component to handle the shuffle. I tried doing shuffling using the spotify API but I also ran into the problem of having to wait for a state change which I wasn’t sure what API to use.

Nice investigation! A question, do you want shuffle play or start playback at a random position or both?

I’ve added shuffle support in a PR. can you test it? https://github.com/fondberg/spotcast/pull/18

I do not personally see a use case for having playback at a random position if ‘shuffle’ is not active after the song. I would personally just want something that mimics the shuffle play functionality.

To be clear ‘shuffle play’ is starting the playlist at a random position and then continuing with shuffle.

In the PR link you provided, it seems that shuffle is enabled after the playlist starts. I was hoping to have a single option that did both the random position and the shuffle enable. However, I do not mind specifying both that the playlist start at a random position and then shuffle.