Google Music in HA -- Now as a media player!

I’ve made a little addition to the player component. It will let you add an input_boolean to your card to switch easy between shuffle on and off. just add the entitiy to your card. Be aware i have no idea what I’m doing, but it’s working fine so far.

https://github.com/mason-xx/GoogleMusic_HomeAssistant/blob/daa81da869d0cec8459f6cea70e67ae6cf199c22/homeassistant/packages/gmusic_player.yaml

cards:
  - entities:
      - artwork: cover
        entity: media_player.gmusic_player
        group: true
        hide:
          progress: true
        replace_mute: stop
        source: icon
        type: 'custom:mini-media-player'
        volume_stateless: true
      - entity: input_boolean.gmusic_player_shuffle
      - entity: input_select.gmusic_player_source
      - entity: input_select.gmusic_player_speakers
      - entity: input_select.gmusic_player_playlist
      - entity: input_select.gmusic_player_station
    type: entities
type: vertical-stack

gmusic

2 Likes

Hi @mason

It’s not hard if you want to show all stations that are not in your library. But for now you have to make this change in custom_components/gmusic_player/media_player.py

On line 389 you should find:
library = station.get('inLibrary')

Simply change this to library = True

And don’t forget you’ll need to restart HA for changes to take effect.

3 Likes

thanks for the info, I guess I have to dig a little bit deepter into this…

strange thing though. It looks like the component is skipping tracks. The auto_advanced automation is fireing even though the tracks isn’t finished. Not sure why this is happening…

For the last few days, my installation has been skipping tracks before they have finished, too. Unfortunately, I’m yet to find the culprit…

@mason @smccoy

I’m having this issue myself. I’m not sure what’s changed. I think it’s something with GM servers. I’ve seen this reported in various issues with other things. Basically if the client takes too long to reach the end of a long song or if the download speed is too slow the connection is often reset during playback. The solution I think would be to download the entire song at once to/and play it from ram. That’s a lot over my head though.

The best I could do, would be to try using gmusicproxy for playback instead.

[gmusicproxy] project is based on the great Unofficial Google Play Music API of Simon Weber: it already permits to create URLs to stream the tracks as regular MP3 but they expire in 1 minute! Keeping this proxy running, it can generate persistent local URLs that never expire and that can be used in any media-player.

@mason @smccoy

How are you guys running homeassistant? I mean are you using hass.io or running in a virtualenv on Linux?

@troy @smccoy

would be sad, that integration was lean and slick… with some gmusicproxy i need a lot more moving parts. well we see…

currently im moving from an old virtualenv homeassistant installation to a dockerised supervisor install of hassio on my linux box. to minimise the maintanance and my old install was stuck at some 0.8x pre lovelace release since i feared the switch.

So I run HA using a virtualenv (in an iocage jail on FreeNAS) I agree it is another moving part but I don’t think it has to change much. Really gmusic proxy can just be installed and running separately. For example I have just installed gmusicproxy in a virtualenv on my laptop to quickly test.

I have made one change in custom_components/gmusic_player/media_player.py

On line 521 is where it gets the stream url:

_url = self._api.get_stream_url(uid)

I have just changed this to get the stream from gmusicproxy instead.

  • 192.168.1.19 is the ip of my laptop running gmusicproxy
_url = "http://192.168.1.19:9999/get_song?id=" + uid

I do realize this may be less than ideal but I have not had a song skip since I started to test this. It can at least be a temporary workaround until this can be solved properly. I found this commit where this is handled in gmproxy but I’m too much a python noob to understand how to implement something similar here.

1 Like

@mason @troy

I am running dockerized hassio on an Ubuntu box here. I will throw gmusicproxy on a system tonight to test. Even though it is one more piece, it is a pretty simple workaround to get music working again.

@smccoy @mason

I think I can just add an option so you can switch between direct gmusicapi url or gmusicproxy url.

Sorry I can’t just fix this properly yet, this is just my first attempt at learning both python and how to make custom components. I really wish I was just a bit smarter, it’s slow going for me trying to learn python from the internet :rofl: ( and I took the summer off ) - I have a lot of ideas for this and at the same time sooo many questions even regarding just the basics of python programming. I haven’t had that “Oh, I get it!” moment where it it all starts to just make sense.

@mason @troy

Well, Gmusicproxy fixed the problem! For anybody running Docker, there is a Gmusicproxy Docker package available here: https://hub.docker.com/r/digitallyseamless/gmusicproxy that works great. Just create a config file, change the compose to point to your config and go. I did have to use the IP of my host instead of localhost to get it connected, but it is working great.

An option would be handy, but it is not a big deal to manually edit the file, either. I just commented out the original line so I can switch back without too much hassle.

No need to apologize! I’m still trying to ‘get’ Python, too. I’m just excited to have music working again and I can’t wait to see what else you will dream up!

@troy @smccoy thanks for the input, I’ll have a look when I find the time. still have a lot other stuff t setup and restructure. and as said, every little bit is welcome, no need to apologize.

edit: okay i was to curious and just added another container, looking good so far :slight_smile:
Now, if I only could find a way to scrobble the music to last.fm this would be the cherry on top. Or a like dislike button for your player if you need some more challenges learning python :wink:

thanks guys

Update – 09/23/2019

To help with future updates I have made some changes.

  • Moved user config to a separate package file
  • Now you can install and update using HACS

hacs_badge
In HACS Settings → Custom Repositories, add the following:

https://github.com/tprelog/homeassistant-gmusic_player

Use Category: Integration

Press Install or reinstall
You will still need to add both new package files

Manual upgrade

Download and replace custom_components/gmusic_player/media_player.py
You will still need to add both new package files



Change for oauth related login options

  • This still requires an existing oauth_credentials file

I have eliminated the login_type and oauth_cred setting.
These are now set using username and password options.

Key Type Required Description
username string YES Username must be set to oauth
password string YES Path to existing oauth_credentials
device_id string YES Set your valid device_id here.
token_path none NO Not used by oauth!

Example oauth

    username: "oauth"
    password: "/home/hass/homeassistant/oauth.file"

@smccoy @mason and I have noticed songs cutting off before they should end. You can use gmusicproxy as a workaround until this is fixed more correctly. If set gmusic_player can stream from your local gmusicproxy server. Enable this by entering the proxy url as shown the below.

Example url

    gmusicproxy: "http://192.168.1.100:9999"
4 Likes

Would love to see this set as the default option or to be able to toggle from the front end.

Hi @troy, thanks for the welcome! While it’d be nice if there was a simple copy/paste solution, I wasn’t expecting there to be. :slight_smile: I only just got this up and running, and haven’t had time to dig into the internals too much due to life being life. I was hoping you’d at least be able to point me in a direction to look, which you have, so thank you! I will look into setting up an automation that writes the data from the gmusic_player when it’s active to the default media receiver instance. Even if it’s not easily possible, thanks for work on this plugin. It’s super handy (and fun to watch people’s reactions when you say “Alexa, it’s time to chill” and the mood lighting sets while some background music starts automatically)

I have the same issue. Did you manage to find a solution? It seems that the media player component Is the problem. Set image in media player

@troy

I updated last night and so far, everything is working great. Thank you for your hard work!

1 Like

Cannot get legacy app password login to work. Tried with deviceid 00 to get a list of valid ones. Reconfigured with valid id but then I get this permission error. Installed using HACS.

ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform gmusic_player
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 150, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/gmusic_player/media_player.py", line 87, in setup_platform
    add_devices([GmusicComponent(hass, config)])
  File "/home/homeassistant/.homeassistant/custom_components/gmusic_player/media_player.py", line 141, in __init__
    with open(_authtoken, 'wb') as f:
PermissionError: [Errno 13] Permission denied: './.gmusic_authtoken'

EDIT: specifying a token_path fixed this

Looks like it is trying to use the token and the directory permissions are incorrect. Make sure it is RW

I’ll add an option to set this in the config. I think from that you can use an input_boolean to trigger an automation that changes the setting.

There is a way to do this but it’s a bit of a hack. I’ll try to get together a working example but it’s not perfect. Basically you set up some kind of automation to sync the gmusic_player attributes to the Default Media Receiver.

1 Like

Hi, I have set up everything, the card shows what it should ok, but when I try to play anything, anywhere it gives me an error:
Failed to get URL for track: (Tn2r6kg5wvhfbfdtiuis47hcbua)
Any ideas? tks

By chance, do you have gmusicproxy anywhere in your gmusic_config.yaml?