Feeding Google Home alarms into Home Assistant

Feeding Google Home alarms into Home Assistant

The problem

IMG_20201206_155451_1.jpg

I have a Lenovo Smart Clock in my bedroom which I use for setting alarms and interacting with Google Assistant and Home Assistant (HA) trough Nabu Casa. Great device for the money, but it has a problem.

I wanted to have a wake up routine that triggers 15min before the alarm so that I can gently increase the brightness of my IKEA Smart bulbs until the alarm starts but had no way of feeding that alarm information into HA. Now I have!

My setup

I'm currently using a Raspberry Pi 4 4GB running Raspbian lite 64-bit from a Crucial SSD as my host machine. A linux host is required for what I'm about to describe. Sadly, if you are using Hass.io this method won't work for you.

Inside the pi I'm using docker with the help of IOT Stack, a great quick starter to have some of most used IOT docker containers up and running very fast. Home Assistant is running on top of docker with HA Supervised. Some of my important containers (NodeRed, deCONZ, Mosquitto, InfluxDB) are running outside of HA Supervised since I like to have the control over my most important applications. Additionally, it makes HA restarting faster as these containers keep running in the background. Maybe more about this in a future blog post!

Solution overview

Note: This tutorial is based on Cas van Cooten Github.

To fetch the information from your Google Home devices, we will rely on Google Home Local API by Rithvik Vibhu. This API will require local authorization tokens to get the information from your devices over HTTPS on port 8443 of your device.

Cas van Cooten (github.com/chvancooten/homeassistant-google..) created a handy script which handles the creation of the tokens and pushes it to your HA instance. Let's check how to make it running.

Software dependencies

To have the script running you will need

  1. gpsoauth python library
  2. latest release of grpcurl
  3. Google Foyer proto files
  4. python script to get tokens

1. Python dependencies

Install gpsoauth python library by running

pip3 install gpsoauth

If you don't have pip installed yet, install it by running

sudo apt-get install python3-pip

Reference: https://gist.github.com/rithvikvibhu/952f...

2. Install grpcurl

To run the scripts you will need a library called grpcurl. To install it, golang tool is required. There's a great tutorial here on how to get it.

Once you have go commands available, install grpcurl with

go get github.com/fullstorydev/grpcurl/...
go install github.com/fullstorydev/grpcurl/cmd/grpcurl

In my case, here's my final folder structure after the last commands. The path to grpcurl will be /home/pi/go/bin

/home/pi
└──go
│   └──bin
│       │   grpcurl
│   └──pkg
│   └──src

Reference: https://github.com/fullstorydev/grpcurl/releases

3. Google Foyer proto files

Save to any directory you want the proto file and it's folders. Preserve the folder structure otherwise it won't work. In my case I saved the folders to the directory /home/pi/googlehome/.

4. Tokens script

Get the get_tokens.py from here and save it to the directory you want. In my case it will be /home/pi/googlehome/.

Once saved, configure the script with your Google username and password. I've used Google App Passwords to generate a password because I have 2FA enabled in my Google account.

At this point you can run the script python3 get_tokens.py and check if it runs successfully.

Configuring the script

Now that all the dependencies are set, grab the latest get_tokens.sh script from here and configure the required variables. This bash script will create an authentication token using the python script and push it to HA.

Here's an example of how the configuration file looks like for me

# Configure these variables
hassApi="http://localhost:8123/api"
hassApiToken=your-long-key-from-homeassistant
getTokenScriptPath="/home/pi/googlehome/get_tokens.py"
grpCurlPath="/home/pi/go/bin/grpcurl"
protoPath="/home/pi/googlehome"
targetDevices=("Clock")
healthCheck=true
healthCheckUrl=https://hc-ping.com/your-ping-will-explain-later

If you need to check your target device name, just go to Google Home app. If you want to fetch the alarms from more than one device you can use targetDevices=("Bedroom clock 1" "Bedroom clock 2") for example.

Run the script ./get_tokens.sh to check if it's working correctly. After it runs you should already see input_text.google_tokens entity created in HA.

Schedule a timer

The tokens expire after 1h. If you want to always have a working token, create a cron job to run the script at a given schedule. I only want to fetch the alarm data before my morning starts so I've set a cron job for 4am every day.

Configure HA

Now that the tokens are being generated, we can use the Google Home Local API to get the information we want using the authentication code.

I've tried two approaches: using a command-line entity in HA or by using Node-Red. The first method is documented in Cas van Cooten Github here so I'll show how I did it with Node-Red.

node-red.PNG

The flow waits for a change in the token. When the token changes, it builds the header, runs curl command and it parses the data to be sent as a HA entity.

The flow code can be found on my Github here.

Extra lines

If you want, you can have a service called healthchecks.io associated to the script to monitor if it's running. Just include both the ping id and the boolean in bash script variables. There's a healthchecksio custom component which you can use within Home Assistant.

Special thanks to: