Google Smarthome Switch
Minimal example implementation of Google Assistant Smarthome Actions to control a switch
Here is an stripped down example of adding a switch in Google Smarthome so you can use your voice to control something.
Create and set up project in Actions Console
- Use the Actions on Google Console to add a new project with a name of your choosing and click Create Project.
- Select Home Control, then click Smart Home.
Get your JWT token
- Navigate to the Google Cloud Console API Manager for your project id.
- Enable the HomeGraph API.
- Navigate to the Google Cloud Console API & Services page
- Select Create Credentials and create a Service account key
- Create a new Service account
- Use the role Service Account > Service Account Token Creator
- Create the account and download a JSON file. Save this as src/smart-home-key.json.
npm install
npm run build
npm run start
Once you have this local server running, in another terminal, start ngrok to expose it to the outside world.
# install ngrok if you don't have it
npm install ngrok -g
# run it
ngrok http 3000
You will see some output, including
Forwarding https://{random-id}.ngrok.io -> http://localhost:3000
(Hint: You can use the "Web Interface" link to study the payloads Google is sending to your service)
- Navigate back to the Actions on Google Console.
- From the top menu under Develop, click on Actions (left nav). Click on Add your first action and choose your app's language(s).
- Enter the URL https://{random-id}.ngrok.io/smarthome for fulfillment and click Done.
- On the left navigation menu, click on Account Linking.
- Select No, I only want to allow account creation on my website. Click Next.
- For Linking Type, select OAuth.
- For Grant Type, select 'Authorization Code' for Grant Type.
- Under Client Information, enter the client ID and secret as defined src/config-provider.ts:
- Client Id: sampleClientId
- Client Secret: sampleClientSecret
- The Authorization URL is the hosted URL of your app with 'https://{random-id}.ngrok.io/fakeauth' as the path
- The Token URL is the hosted URL of your app with 'https://{random-id}.ngrok.io/faketoken' as the path
- Enter any remaining necessary information you might need for authentication your app. Click Save.
Now let's test it:
- On a device with the Google Assistant logged into the same account used to create the project in the Actions Console, enter your Assistant settings.
- Click Home Control.
- Click the '+' sign to add a device.
- Find your app in the list of providers.
- After clicking your app, a new "Smart Switch" will show up in your device list
- Start using the Google Assistant in the Actions Console to control your devices. Try saying 'turn the switch on'.
What's with the python file (screen.py)?
I used it to control another process (maybe some lights via USB). The call to that is in index.ts: swState()
Here's an interesting project you might find useful: GPIO for a Linux PC using a $3 board
This is a simplified version of Google's official sample code.
It should take you minutes instead of hours.