I’m sure most of you clicking on this are familiar with the great work @Danielhiversen has done creating Google Music in HA. While this (as of today) continues to work with minor tweaks here and there the project itself is no longer being developed. With current changes happening in HA I fear it’s only a matter of time before it stops working all together.
@ajfriesen Created a feature request for an official component Please go vote!.
Update 06/01/19:
Please now consider using gmusic as a media player instead
My intention here is NOT creating an “official component” although that is something I hope to contribute toward in the future. For now I have found AppDaemon to be an easier approach to start learning python and the unofficial Google Music api. Please keep in mind, I am not a programmer and just starting out, I have only learned some basics of AppDaemon and Python. Using the original gmusic in HA code as a reference I’ve managed this, not perfect, but useable alternative to the original Google Music in HA.
I’ve also slightly furthered functionality by adding a few things I greatly wished for in the original
- Listen to “I’m Feeling Lucky” and other stations saved in your GM library
- Sync your playlists and stations from GM library without restarting HA
This app requires installing gmusicapi
If your running appdaemon in a virtualenv
Click Here
This is not horrible but does require using the terminal to install gmusicapi
and (optionally) create the oauth-credentials file. Here I assume both Home Assistant and AppDaemon are installed and running in separate virtualenv on the same host. I have installed gmusicapi
inside the same virtualenv as AppDaemon. Your paths maybe different but here’s how I installed gmusicapi
and got my oauth-file. In most cases you will only need to create the oauth once.
Install gmusicapi
In my setup, HomeAssistant and AppDaemon both run by user hass
so I’ll start by changing to that user, activating the appdaemon virtualenv and installing gmusicapi
su - hass
source /srv/appdaemon/bin/activate
pip3 install --upgrade gmusicapi
Now before exiting the virtualenv we’ll use gmusicapi
to create the oauth file.
Creating oauth credentials for gmusicapi
Again, still inside the appdaemon virtualenv, begin by starting an interactive python shell
python
Now from the python
shell we can create the oauth file in several simple steps. I’ve named my oauth file GoogleMusic.oauth
but you can pick any name. I think it’s easiest to save this file in the appdaemon config dir. For example: /home/hass/appdaemon/conf/GoogleMusic.oauth
but really it can be saved anywhere accessible by appdaemon.
from gmusicapi import Mobileclient
mc = Mobileclient()
mc.perform_oauth(storage_filepath="/home/hass/appdaemon/conf/GoogleMusic.oauth", open_browser=False)
Follow the on screen instructions and you will create your oauth file.
Basically the last command will give you a link. Go to the url and login with the (google) account you want to use with google music. This will intern give a long app password to paste back in the terminal. That’s it for getting the oauth file!
Testing oauth login
Next we’ll quickly test the new log in. If you don’t know your device_id
don’t worry. Just use something like 000
. This will fail to connect to Google Music. However if your login is valid, this will return a list of valid device_id
s. Using one from the list, test the login again
mc.oauth_login(device_id='21150e2000000456', oauth_credentials='/home/hass/appdaemon/conf/GoogleMusic.oauth', locale='en_US')
This must return True
in order for oauth login to work correctly.
Finish up in the terminal
Exit the python
shell and deactivate the appdaemon virtualenv
exit()
deactivate
Click Here to see how that looked in my terminal
### url = https://unofficial-google-music-api.readthedocs.io/en/latest/reference/mobileclient.html#mobileclient-interface
### REQUIRES: gmusicapi
### Since it's for appdaemon: Install this in the appdaemon virtualenv
$ su - hass
$ source /srv/appdaemon/bin/activate
$ pip3 install --upgrade gmusicapi
$ python
------------------------------------------------------------------------------------------
Python 3.6.7 (default, Mar 30 2019, 01:09:45)
[GCC 4.2.1 Compatible FreeBSD Clang 6.0.0 (tags/RELEASE_600/final 326565)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from gmusicapi import Mobileclient
>>> mc = Mobileclient()
>>> mc.perform_oauth(storage_filepath="/home/hass/appdaemon/conf/GoogleMusic.oauth", open_browser=False)
Visit the following url:
https://accounts.google.SOME-RANDOMLY-LONG-URL
Follow the prompts, then paste the auth code here and hit enter:
### To Get A Valid Device ID try to log in without one:
>>> mc.oauth_login(device_id='000', oauth_credentials='/home/hass/appdaemon/conf/gmusic.cred', locale='en_US')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/srv/appdaemon/lib/python3.6/site-packages/gmusicapi/clients/mobileclient.py", line 183, in oauth_login
return self._login(session_login, device_id, locale)
File "/srv/appdaemon/lib/python3.6/site-packages/gmusicapi/clients/mobileclient.py", line 143, in _login
self.android_id = self._validate_device_id(device_id, is_mac=is_mac)
File "/srv/appdaemon/lib/python3.6/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 000.Your valid device IDs are:
* 11150e2000000123
* 21150e2000000456
* 31150e2000000789
------------------------------------------------------------------------------------------
### Test your login
>>> mc.oauth_login(device_id='21150e2000000456', oauth_credentials='/home/hass/appdaemon/conf/gmusic.cred', locale='en_US')
True
>>>
>>> mc.logout()
True
>>>
>>> exit()
$
$ deactivate
### Make file read-only for hass
$ chmod 400 /home/hass/appdaemon/conf/GoogleMusic.oauth
If your using the hassio addon
Click Here
Well this does work but seriously consider using gmusic as a media player instead. Don’t even think about this on a PI device. Maybe if you have a decent Intel NUC but be warned the issue is during installation of gmusicapi, building wheel for lxml can take as long as 10 minutes to install using an older i7. And this delay is going to happen on every reboot.
Add the following to your appdaemon config. It can take several minutes for these packages to be installed during appdaemon startup.
{
"disable_auto_token": false,
"system_packages": [
"build-base",
"libxslt-dev",
"libxml2-dev",
"gcc"
],
"python_packages": [
"setuptools",
"gmusicapi",
"lxml"
]
}
Add entities to Home Assistant.
The original GM in HA is built on a custom switch component. This app simply relies mainly on the input_boolean
and input_select
entities which can be easily added and configured using a Home Assistant package . I name this gmaac_package.yaml
but the name does not matter, it can be any_name.yaml
You will need to add your own media players under gmaac_player
. For example to add media_player.bedroom_stereo
you would add bedroom_stereo
. Sorry you still can not use friendly names here.
homeassistant/packages/gmaac_package.yaml
https://github.com/tprelog/GoogleMusic_HomeAssistant/blob/master/homeassistant/packages/gmaac_package.yaml
Adding GMAAC (Google Music All Access Control) to AppDaemon
Create a directory inside your appdaemon apps called gmaac
that will contain two files
gmaac.yaml
and gmaac.py
gmaac.yaml
contains the configuration . You will need to edit the device_id
and set the desired login_type
your using for the gmusicapi.
It would be preferred to use the oauth
login. Set oauth_credentials
to the location of your GoogleMusic.oauth
The legacy
login is available if you are unable to create GoogleMusic.oauth
. With the legacy
login you should be able to use the same user
and password
that you would use for the original GM custom switch.
appdaemon/apps/gmaac/gmaac.yaml
https://github.com/tprelog/GoogleMusic_HomeAssistant/blob/master/homeassistant/appdaemon/apps/gmaac/gmaac.yaml
appdaemon/apps/gmaac/gmaac.py
https://github.com/tprelog/GoogleMusic_HomeAssistant/blob/master/homeassistant/appdaemon/apps/gmaac/gmaac.py