Control Philips Hue with Google Assistant SDK on Ubuntu

Introduction

I’ve a Philips Hue bridge and a set of bulbs. On the other hand, Google has an SDK for their Google Assistant. By combining these two, I was able to create a simple Linux application that identifies your custom voice command and map those commands to control the bridge.

In this guide, I will go through step by step on creating this project so that you can also control your Philips Hue bridge from your Linux desktop.  As a matter of fact, one you go through the steps, you will also learn how to define custom voice commands and execute custom code blocks.

However please note that I’ve wrote this after prototyping and getting things working. So some of the approaches I have used might not be conventional.

Things you need

  • A gmail account
  • Philips Hue Bridge and a philips hue light
  • A computer with:
    • Ubuntu(better to be a newer version)
    • A microphone
    • A Speaker
    • Stable internet connection

Part 1 – Architecture Setup

Create a Google developer project

First you need to create a Google Developer project. We will use this project to test “Actions on Google”. Actions on Google is a program that runs via Google Assistant. It enables us to create custom commands(actions) and execute them on Google Assistant API.

Go to https://console.cloud.google.com/projectcreate

For project Name give ‘test’ (assuming you already don’t have a project called test) and click create. Then you will be navigated to the Dashboard page for the project. Bookmark this page as we might come back to this.

Enable Google Assistant API

Then we need to enable Google Assistant API under the created project. This is an API that enables voice commands that is captured from a voice input device to be recognized.

To enable this go to ‘API and Services -> Dashboard’ From the left menu. Then click enable APIs and services Button. From the search Google assistant API. Then enable the service.

We also need to enable OAuth grant access from the device to the API. For this, go to credentials from the left menu and select OAuth consent screen. Then under select your email address for the Email Address drop down and click save.

Register the PC as a device To connect with Google assistant and the PC we need to register the PC as a device in Google actions.

for that go to: https://console.actions.google.com/

Then click add/ import Project.

For project name search the previous project name and select from the drop down. Then click import project.

Now you should be able to see getting started Project page. In this page, click skip button. Once done, you should see a page similar to below.

In this page click “Device registration” and click “register model”. Now you should see the register model form.

For this, type a product name (e.g. My PC) and something similar for the Manufacturer name. Then register the model. After that, download the credentials file. We will need this to be located in our PC. For Traits options, do not select any and click ‘Skip’.

Part 2 – System setup

Now we can dive into the coding part. We are going to use python3 that comes with Ubuntu package manager. We will install our dependencies in a Python virtual environment.

First lets ensure we have the correct packages.

Then create a directory for the project and a virtual python environment called ‘env’ for the project. The directory we create here will be used to store all the files related to the project.

Now you should source to use the virtual Python environment.

Then install pip packages

 

Generate Credentials

The next step is to generate credentials so that we can run our code and use the Assistant SDK. For client-secret option, pass the location of the previously downloaded credential file.

Once you run that, you will see a link in the terminal. Follow that link and grant permission. Then copy the verification code and paste in the terminal when it asks.

If authorization was successful, you will see a response similar to the following:

Part 3 – Coding

First we need to define our custom action and send it to Google Actions. Lets create a json file . touch actions.json

In this tutorial the intention is to turn off/on a bulb in Philips Hue system. So we are going to create an action that handles a voice command like “Turn off table lamp” where “off” is a state and “table lamp” is the bulb name in our network.

So let’s create a file called actions.json and put the following.

Here is the basic structure of the actions.json

actions.intent.name : This is the identifier for our custom action.

actions.intent.parameters : Here we define a list of parameters that are populated by the voice command

actions.intent.trigger.queryPatterns : Here we define the custom voice command. As you can also see we also used the defined parameters so that SDK will populate the users given value.

types : Here we define the values for custom parameter types. Actions also support some Schema.org types by default.

Let’s see how this all map. Suppose you tell “Hey google, turn ‘off’ my ‘table lamp'”. Assistant SDK will send the voice command to Actions. Actions will map ‘off’ value to lightState and ‘table lamp’ value to lightName. We will get those mapping back from the API.

To learn more about Actions read the docs at https://developers.google.com/actions/extending-the-assistant.

Now you need to upload the fiule. For this you need to download “gactions” tool. Please downland it from https://developers.google.com/actions/tools/gactions-cli and put in the current “hue-test” directory. Then make it executable. After that you can upload it to your project.

When you are using it for the first time for the project, you have to authenticate. Click the generated URL in the terminal and grant permissions. Then paste the verification back on the terminal.

Then we need to enable test mode for the actions.

Next lets create a file called user_config.json to store the configuration such as Google project ID and device ID.

 

Now we need to create the python script to use these voice command and turn off/on the bulb. So lets create a main.py file in the current directory (hue-test). Notice that we also use Phue python library to give commands to the Philips bridge.

Now you can simply run python main.py and say “Hey Google, turn off my table lamp”.  The command should be identified by Google and send with the correct identified command name with parameters.  If everything works correctly, the command that connect to bridge and turn on/off the lights should be executed.

This guide is simply a prototype for connecting Google SDK and Philips Hue as a smart device. Hopefully, now you should be able to go with your requirements and build something fancy. 🙂

I hope my guide helped you at least for some extent.

Troubleshooting

If it complains about activity permission error, make sure you have granted corrected Activity controls. See “Set activity controls for your account” on

https://developers.google.com/assistant/sdk/guides/library/python/embed/config-dev-project-and-account

 

Leave a Reply

Your email address will not be published. Required fields are marked *