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-pushtotalk

Press the Enter key and try the following query:

Turn on.

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

  INFO 
 : 
 root 
 : 
 Recording 
 audio 
 request 
 . 
 INFO 
 : 
 root 
 : 
 End 
 of 
 audio 
 request 
 detected 
 INFO 
 : 
 root 
 : 
 Transcript 
 of 
 user 
 request 
 : 
 "turn on" 
 . 
 INFO 
 : 
 root 
 : 
 Playing 
 assistant 
 response 
 . 
 INFO 
 : 
 root 
 : 
 Turning 
 device 
 on 
 INFO 
 : 
 root 
 : 
 Waiting 
 for 
 device 
 executions 
 to 
 complete 
 . 
 INFO 
 : 
 root 
 : 
 Finished 
 playing 
 assistant 
 response 
 . 
 

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 pushtotalk.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/grpc
nano pushtotalk.py

Search for the following handler definition:

  device_handler 
 = 
 device_helpers 
 . 
 DeviceRequestHandler 
 ( 
 device_id 
 ) 
 @device_handler 
 . 
 command 
 ( 
 'action.devices.commands.OnOff' 
 ) 
 def 
  
 onoff 
 ( 
 on 
 ): 
 if 
 on 
 : 
 logging 
 . 
 info 
 ( 
 'Turning device on' 
 ) 
 else 
 : 
 logging 
 . 
 info 
 ( 
 'Turning device off' 
 ) 
 

The code in onoff() handles the command action.devices.commands.OnOff . This command is part of the OnOff trait schema.

Currently, the onoff() definition logs output to the console. It uses helper functions in device_helpers.py to parse the response JSON and store the command parameters in variables for ease of use. You can modify this code to do whatever you want on your project.

If you have an LED kit, keep reading to learn how to light the LED in response to the OnOff command. If you do 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 pushtotalk.py file.

cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc
nano pushtotalk.py

In the pushtotalk.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. 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.

  device_handler 
 = 
 device_helpers 
 . 
 DeviceRequestHandler 
 ( 
 device_id 
 ) 
 GPIO 
 . 
 setmode 
 ( 
 GPIO 
 . 
 BCM 
 ) 
 GPIO 
 . 
 setup 
 ( 
 25 
 , 
 GPIO 
 . 
 OUT 
 , 
 initial 
 = 
 GPIO 
 . 
 LOW 
 ) 
 @device_handler 
 . 
 command 
 ( 
 'action.devices.commands.OnOff' 
 ) 
 def 
  
 onoff 
 ( 
 on 
 ): 
 if 
 on 
 : 
 logging 
 . 
 info 
 ( 
 'Turning device on' 
 ) 
 GPIO 
 . 
 output 
 ( 
 25 
 , 
 1 
 ) 
 else 
 : 
 logging 
 . 
 info 
 ( 
 'Turning device off' 
 ) 
 GPIO 
 . 
 output 
 ( 
 25 
 , 
 0 
 ) 
 

Save your changes and close the file.

Run the sample

Run the modified sample code.

python pushtotalk.py

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

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

Design a Mobile Site
View Site in Mobile | Classic
Share by: