Handle Commands

Follow these instructions to execute custom code on your device in response to commands from the Google Assistant.

Run the sample

Now that you defined a trait and updated the model, check to make sure the Google Assistant is sending back an On/Off command for the appropriate query.

googlesamples-assistant-hotword --device-model-id my-model

Try the following query:

Ok Google, turn on.

You should see the following statements in the console output. If you don't, see the troubleshooting instructions.

ON_RECOGNIZING_SPEECH_FINISHED:
  {'text': 'turn on'}
ON_DEVICE_ACTION:
  {'inputs': [{'payload': {'commands': [{'execution': [{'command': 'action.devices.commands.OnOff',
  'params': {'on': True}}], 'devices': [{'id': 'E56D39D894C2704108758EA748C71255'}]}]},
  'intent': 'action.devices.EXECUTE'}], 'requestId': '4785538375947649081'}
Do command action.devices.commands.OnOff with params {'on': True}

You will find where these statements are printed in the source code.

Get the source code

You are now ready to start your own project:

git clone https://github.com/googlesamples/assistant-sdk-python

Find the command handler

The hotword.py file in the sample code uses the SDK to send requests and receive responses from the Google Assistant.

cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library
nano hotword.py

Search for the following handler definition:

def process_event(event):

Currently, this function prints out every Device Action event name and any parameters with the following line:

print('Do command', command, 'with params', str(params))

This code handles the command action.devices.commands.OnOff. This command is part of the OnOff trait schema. Currently, this code just prints output to the console. You can modify this code to do whatever you want on your project. Add the following block under the print command in process_event().

print('Do command', command, 'with params', str(params)) # Add the following:
if command == "action.devices.commands.OnOff":
    if params['on']:
        print('Turning the LED on.')
    else:
        print('Turning the LED off.')

Run your modified source code directly to see the output.

python hotword.py --device-model-id my-model

Use the same query as before:

Ok Google, turn on.

If you connected an LED to the Raspberry Pi, keep reading to learn how to light the LED in response to the OnOff command. If you did not, skip the next section to learn how to add more traits and handlers.

Next steps - Raspberry Pi

Now that you know how to handle the incoming command, modify the sample code to light an LED. This will require some additional hardware if you are using the Raspberry Pi.

Import the GPIO package

To simplify software access to the General Purpose Input/Output (GPIO) pins on the Raspberry Pi, install the RPi.GPIO package in the virtual environment.

pip install RPi.GPIO

Modify the sample

Open the hotword.py file.

nano hotword.py

In the hotword.py file, import the RPi.GPIO module to control the GPIO pins on the Pi. Place the following statement near the other import statements:

import RPi.GPIO as GPIO

Modify the code to set the output pin initially to the low logic state. Do this in the main() function, before processing events:

with Assistant(credentials, device_model_id) as assistant:
    events = assistant.start()

    device_id = assistant.device_id
    print('device_model_id:', device_model_id)
    print('device_id:', device_id + '\n')
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)
        ...

Modify the code you added in process_event(). When the on command is received, set the pin to the high logic state. When the off command is received, set the pin to the low logic state.

if command == "action.devices.commands.OnOff":
    if params['on']:
        print('Turning the LED on.')
        GPIO.output(25, 1)
    else:
        print('Turning the LED off.')
        GPIO.output(25, 0)

Save your changes and close the file.

Run the sample

Run the modified sample code.

python hotword.py --device-model-id my-model

Use the same query as before. The LED should turn on.

That's just the beginning. Learn how to add more traits and handlers.