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

Have to restart a couple of times to get the input_select populated right. my hassio is already a little to big and got his performance issues.
The update for the oauth login Threw me up a little - I used one of the alpha versions until just now. :rofl:

The fields function is sexy as F.
And an optional input_select is ok as long as it isnt away for good - you need an way to get your stupid assinged playlist names somehow :stuck_out_tongue:

Does on your side the automation for the next track also triggers often in the middle of an song?

No it will not go away but to better explain, I’m thinking of moving the actual commands that populates the input_select out of the gmusic_player component and into separate scripts for each source Each script would fire a custom event similar to example above. Then we could use an automation to trigger the script to fire when HA starts. In the end this would give the same function provided now. Additionally for example, let’s say you do not use stations - you could just turn-off the automation that fires the script to “set_options for the input_select containing stations” – Even more you would be able to completely remove the input_select for stations and even the script and automation as well. Since the event to populate that input_select would never be fired gmusic_player would no longer crap out if you don’t have it or if the list is empty or whatever.

This might seem silly with only two sources but I’ve been playing around and more is on the way.
I cherry picked these changes already and will be included in gmusic_player shortly.

As it stands now @miikkajo’s fork has removed the playlist functionality. So this has got me thinking maybe some people don’t want every source so rather than have to remove it completely, you could just disable it’s related automation. I think this could effectively allow you to enable or disable different sources right from the HA gui by turning off or on the related automation.

Yes this happens but I can’t control it. gmusic_player itself is not actually playing music. (which is why the “speakers” are required) The songs get cut off because the playback url return by gmusicapi is only valid for a minute. Under certain situations the connection can be reset but since the link is no longer valid playback the song can not continue streaming so it just cuts off. The automation see the “default media receiver” go to an idle state so it triggers the next song. I have always noticed this happen once in a while (like maybe one or two song a month), even back using the original gmusic_switch. I’m not sure why it has gotten so much worse. Since late August it has become so bad that only a few songs ever make it to the end. The quick and easiest workaround to use gmusicproxy. (Needs installed separately) but then you can just set the url in gmusic_config.yaml and gmusic_player will get playback url from gmusicproxy that does not expire.

Ive set up gmusic_proxy using the docker image.
In packages I have gmusic_config.yaml with:

media_player:
  - platform: gmusic_player
    username: !secret gmusic_username
    password: !secret gmusic_password
    device_id: !secret gmusic_device_id
    gmusicproxy: 192.168.1.73:9999

0.0.0.0:9999 seems to be the ip:port that gmusicproxy is listening on but I cant see anything in the gmusicproxy log when I try to play anything. If I use vlc on a different machine with:

vlc 'http://192.168.1.73:9999/get_by_search?type=album&artist=Rolling%20Stones&title=tattoo&exact=no'

I can see in gmusicproxy log that it gets a track id for that album

192.168.1.43 - - [25/Oct/2019 19:02:28] "GET /get_by_search?type=album&artist=Rolling%20Stones&title=tattoo&exact=no HTTP/1.1" 200 -


Searching for album with query: Rolling Stones tattoo


Selected album: The Rolling Stones - Tattoo You (Buzub7yrmswkll7t7d7kancjtle)


Getting the tracks of the album with id 'Buzub7yrmswkll7t7d7kancjtle': Tattoo You - The Rolling Stones

edit I feel stupid I needed this in my gmusic_config.yaml instead

    gmusicproxy: http:192.168.1.73:9999

You need to http:// to the url in your config.

    gmusicproxy: 192.168.1.73:9999

should be.

    gmusicproxy: http://192.168.1.73:9999

Ya my edit tells the story :confounded::hammer: learning docker too lol .I wonder why the docker image page wants to map out port 80 when the built image actually listens on 9999

Ha I just saw your edit. No need to feel stupid. I’m clawing my through all this stuff as well. Every step of the way I’ve been learning as I go. Figure out one thing and then it’s something else.

I wish I was smart enough to somehow fix this track skipping issue inside the gmusic_player without relying on something else but at this point, that seems a long way off so relying on gmusicproxy will likely be around for awhile. Would love to see a gmusicproxy add-on for Hass io. Sure would make this much easier. Maybe we could furnish the add-on master @frenck with lavish gifts , beer and monetary bribes. I would give it a shot myself but I don’t use hass io or even docker for that matter. I’d have much to learn and not enough time for me at this point. I could on the other hand create a FreeNAS plugin for gmusicproxy if anyone wanted. I would be surprised to see how many people run HA on FreeNAS and use gmusic_player

There is a gmusicproxy hassio addon https://github.com/miikkajo/hassio_addons

1 Like

Perfect! Thanks @miikkajo I’ll add a link to the first post

What have people done with scripts or automations with this? I’m curious. I set up a small script that I call with a google home but the problem is to get it to play more than one song I have to turn on the google player on and off and on and maybe advance a few songs,and its a little buggy with a script at least that’s my experience. Whats everybody’s work around for this?

@dasbooter
If start gmusic_player just from HA gui (not using a script or anything) does it function correctly. I mean will it advance to the next track like it should?

If yes would you mind to share the script you created for you G Home

EDIT:

I’m not sure exactly what your asking… What are you trying to do?

Sure. Just trying to start up Gmusic with a voice command:

googlemusic_next_song:
  alias: Next song in G Music
  sequence:
    service: media_player.media_next_track
    entity_id: all
googlemusic_previous_song:
  alias: Previous song in G Music
  sequence:
    service: media_player.media_previous_track
    entity_id: all
googlemusic_play_some_music:
  alias: Start playing G Music
  sequence:
    service: media_player.media_play
    entity_id: all

Ive added a - delay: 00:00:03 then started the music again simulating the on off button to get it to play more than one song.

Manually it works but as somebody else mentioned you have to click the on button then turn it off and back on then usually advance to the next song then it continually plays instead of stopping after the first song. My method of doing this in the script seems akward

Man I can imagine that is very awkward. But more to the point, you should not have to do all this to begin with. I have never seen this myself or heard anyone mention this issue. Something is not right. Can we walk through this?

Just using HA – Please check these things in order

  • Check the state of both automation.gmusic_player_auto_advance and automation.gmusic_player_auto_play = on

  • Besure your media player and gmusic_player are off

  • When you press the power button on gmusic_player does the first song begin to play?

  • What is the state of sensor.gmusic_receiver while the first song is playing?

  • Now before the song ends press the next track button on gmusic_player

  • Did the track advance? If not what happened?

  • What is the state of sensor.gmusic_receiver now?

Got me curious, so I tried this myself. Turns out I didn’t need to do anything at all.

With a subscription to Nabu Casa (HA Cloud) and using Google Assistant I simple said

  • “Hey Google, sync my devices”
  • “Hey Google, turn on Google Music Player”

Other “Hey Google” things that worked without any further configuration

  • “Pause”
  • “Unpause”
  • “Play” (Use this to unpause)
  • “Stop” (This will turn-off Google Music Player as well)
  • “Set volume to 3” (Any number 0 - 100)
  • “Change volume to 10”
  • “Mute”
  • “Unmute”
  • “Turn off Google Music Player”

To skip a song I did need a script.

  • “Hey Google, turn on next track”
script:
  gmusic_player_next_track:
    alias: Next Track
    sequence:
    - service: media_player.media_next_track
      data:
        entity_id: media_player.gmusic_player
Error while setting up platform gmusic_player
Traceback (most recent call last):
  File "/usr/src/homeassistant/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.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  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/gmusic_player/media_player.py", line 87, in setup_platform
    add_devices([GmusicComponent(hass, config)])
  File "/config/custom_components/gmusic_player/media_player.py", line 137, in __init__
    logged_in = self._api.login(_username, _password, _device_id, authtoken)
  File "</usr/local/lib/python3.7/site-packages/decorator.py:decorator-gen-76>", line 2, in login
  File "/usr/local/lib/python3.7/site-packages/gmusicapi/utils/utils.py", line 133, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/gmusicapi/clients/mobileclient.py", line 214, in login
    return self._login(session_login, android_id, locale)
  File "/usr/local/lib/python3.7/site-packages/gmusicapi/clients/mobileclient.py", line 143, in _login
    self.android_id = self._validate_device_id(device_id, is_mac=is_mac)
  File "/usr/local/lib/python3.7/site-packages/gmusicapi/clients/mobileclient.py", line 80, in _validate_device_id
    raise InvalidDeviceId('Invalid device_id %s.' % device_id, device_ids)
gmusicapi.exceptions.InvalidDeviceId: Invalid device_id 584.Your valid device IDs are:
* a1b502ea63ef3681b3257c987a03f1dc12c26ecc5d0453487d02d9d12ce9029a

Hi, I tried to recover the ID, but it only gives me the long one, I can’t find the short one (android?)

this the log when i hit play:

2019-10-31 17:42:46 ERROR (SyncWorker_9) [custom_components.gmusic_player.media_player] Failed to get URL for track: (0f50f56f-8ae8-39a5-bdea-3a690823b703)

2019-10-31 17:42:47 ERROR (SyncWorker_9) [custom_components.gmusic_player.media_player] Failed to get URL for track: (2e67b6d3-aac1-3271-a33c-41047bcf18f7)

2019-10-31 17:42:47 ERROR (SyncWorker_9) [custom_components.gmusic_player.media_player] Failed to get URL for track: (09f50bab-2cbf-34ca-9b87-ee86b4327e97)

2019-10-31 17:42:47 ERROR (SyncWorker_9) [custom_components.gmusic_player.media_player] Failed to get URL for track: (36b2a8ac-8bd3-3894-ac9a-8c0ece109748)

It’s not gonna work with that long ID. It has to be the shorter android ID. Do you have an android device resisted to the same account your trying to use here? If so, install Google Music and login from the android device. This should register it’s device ID.

Also from the Google music web page, If you log in and go to setting you can see what devices are registered to your Google Music account. You must first see an android device in this list before gmusicapi will return it’s ID in the gmusic_player logs

Cool your supporting them with nabu casa. Ive set it up with the google projects never publish your project hack. Do you think maybe your just using googles music service that works with google music and not going through your google music player? Ive noticed that it starts playing not in your google music player and instead on the google home I’m standing next too. Or was that your intention. In any case ,weirdly, activating your player with voice command and script only seems to get one song to play no matter what I’m doing. I have to hit the button manually in the front end than everything works? Ill try some of the things you suggested but Im messing around in docker and I only get so much technology time…my girl says I’m addicted lol

  • Check the state of both automation.gmusic_player_auto_advance and automation.gmusic_player_auto_play = on
    yes both are on

  • Besure your media player and gmusic_player are off
    ok did that

  • When you press the power button on gmusic_player does the first song begin to play?
    yes it does

  • What is the state of sensor.gmusic_receiver while the first song is playing?
    sensor.gmusic_receiver playing

  • Now before the song ends press the next track button on gmusic_player

  • Did the track advance? If not what happened?
    yes it does advance

  • What is the state of sensor.gmusic_receiver now?
    says playing
    couldnt help myself want this to work

:thinking: So based on the questions you answered, everything should be working (At least as I intended.)

Do you think maybe your just using googles music service that works with google music and not going through your google music player?

Possibly but not exactly. I think it is sort of google though. I am suspecting that voice may only control gmusic_player with a single command. That is “Hey Google, turn on Google Music Player” ( If you have not figured, “Google Music Player” is the friendly name for gmusic_player") but from now forward, Google sees you chromecast device playing music so all voice commands are controlling cast device directly, no longer gmusic_player.

Ive noticed that it starts playing not in your google music player and instead on the google home I’m standing next too. Or was that your intention.

But I do feel that first “Hey, turn on…” command is gmusic_player because it does turn on and play the selected music on the selected speakers according to what I have set using the corresponding gmusic_player_input_select(s). To this regard you should see both media_players turn on (selected speakers and gmusic_player) gmusic_player only handles using the gmusicapi to lookup music from your google music library. when it finally gets the playback url it sends the playback command to the selected speakers (based on speakers_input_select) So in other words, if “google home I’m standing next too” is the selected option in “speakers” then that is where you should here music begin to play.

Honestly, I have not done anything by intention with regard voice commands. If any of it works, it is by no intention or design of my own. At the same time I also see no reason this should not work. I mean I still do not understand much of the dev docs but I never seen anything specific mentioned to enable voice control for a custom component.

Ill try some of the things you suggested

If you go through those questions again, except replace this third question.

  • When you press the power button on gmusic_player does the first song begin to play?
  • When you say “hey google, turn on google music player” does the first song begin to play?

Do you get the same answers for all the questions when started with voice control?

Thanks, now everything works perfectly

I seem to be having trouble getting this working…

I’ve followed the guides… installed via HACS, added packages: !include_dir_named packages, created a packages folder in config, uploaded the two yaml files. Added gmusic_username, gmusic_password, gmusic_device_id to secrets. added my media players.

but I seem to get the following error;

Log Details (ERROR)
Sun Dec 15 2019 20:05:41 GMT+0000 (Greenwich Mean Time)
Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  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/gmusic_player/media_player.py", line 355, in _update_sources
    self._update_playlists()
  File "/config/custom_components/gmusic_player/media_player.py", line 377, in _update_playlists
    self.hass.services.call(input_select.DOMAIN, input_select.SERVICE_SET_OPTIONS, data)
  File "/usr/src/homeassistant/homeassistant/core.py", line 1173, in call
    self._hass.loop,
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 428, in result
    return self.__get_result()
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/src/homeassistant/homeassistant/core.py", line 1213, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 208, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 287, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 283, in _exec
    v = func(v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: length of value must be at least 1 for dictionary value @ data['options']