[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eFollow the provided instructions to execute custom code on your device in response to Google Assistant commands.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003epushtotalk.py\u003c/code\u003e file manages communication with the Google Assistant using the SDK and includes a handler for the OnOff command.\u003c/p\u003e\n"],["\u003cp\u003eYou can modify the provided sample code to control hardware, such as an LED, based on received commands.\u003c/p\u003e\n"],["\u003cp\u003eFor Raspberry Pi users, instructions are included to integrate with GPIO pins for hardware control.\u003c/p\u003e\n"],["\u003cp\u003eExpand functionality by adding more traits and handlers to your project.\u003c/p\u003e\n"]]],["The document outlines how to enable custom code execution on a device in response to Google Assistant commands. It demonstrates handling the `OnOff` command by running a sample, cloning the source code, and navigating to the `pushtotalk.py` file. The `onoff()` function within this file logs whether the device is turned on or off, and can be modified for custom actions. It then guides the user to use Raspberry Pi GPIO controls to light an LED based on received commands.\n"],null,["# Handle Commands\n\nFollow these instructions to execute custom code on your device in\nresponse to commands from the Google Assistant.\n\nRun the sample\n--------------\n\nNow that you defined a trait and updated the model, check to make\nsure the Google Assistant is sending back an On/Off command for the appropriate\nquery. \n\n```\ngooglesamples-assistant-pushtotalk\n```\n\nPress the Enter key and try the following query:\n\n*Turn on.*\n\nYou should see the following statements in the console output. If you don't, see\nthe [troubleshooting instructions](/assistant/sdk/guides/service/troubleshooting#traits). \n\n INFO:root:Recording audio request.\n INFO:root:End of audio request detected\n INFO:root:Transcript of user request: \"turn on\".\n INFO:root:Playing assistant response.\n INFO:root:Turning device on\n INFO:root:Waiting for device executions to complete.\n INFO:root:Finished playing assistant response.\n\nYou will find where these statements are printed in the source code.\n\nGet the source code\n-------------------\n\nYou are now ready to start your own project: \n\n```\ngit clone https://github.com/googlesamples/assistant-sdk-python\n```\n\nFind the command handler\n------------------------\n\nThe `pushtotalk.py` file in the sample code uses the SDK to send requests and\nreceive responses from the Google Assistant. \n\n```\ncd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc\n``` \n\n```\nnano pushtotalk.py\n```\n\nSearch for the following handler definition: \n\n device_handler = device_helpers.DeviceRequestHandler(device_id)\n\n @device_handler.command('action.devices.commands.OnOff')\n def onoff(on):\n if on:\n logging.info('Turning device on')\n else:\n logging.info('Turning device off')\n\nThe code in `onoff()` handles the command `action.devices.commands.OnOff`. This\ncommand is part of the [OnOff](/assistant/sdk/reference/traits/onoff)\ntrait schema.\n\nCurrently, the `onoff()` definition logs output to the console. It uses helper\nfunctions in `device_helpers.py` to parse the response JSON and store the command\nparameters in variables for ease of use. You can modify this code to do whatever\nyou want on your project.\n\nIf you have an LED kit, keep reading to learn how to light the LED in response\nto the OnOff command. If you do not, skip the next section to learn how to\nadd more [traits and handlers](/assistant/sdk/guides/service/python/extend/add-trait-and-handler).\n\nNext steps - Raspberry Pi\n-------------------------\n\nNow that you know how to handle the incoming command, modify the sample code\nto light an LED. This will require some additional hardware if you are using the\nRaspberry Pi.\n\n### Import the GPIO package\n\nTo simplify software access to the General Purpose Input/Output (GPIO) pins on\nthe Raspberry Pi, install the [RPi.GPIO](https://pypi.python.org/pypi/RPi.GPIO)\npackage in the virtual environment. \n\n```\npip install RPi.GPIO\n```\n\n### Modify the sample\n\nOpen the `pushtotalk.py` file. \n\n```\ncd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc\n``` \n\n```\nnano pushtotalk.py\n```\n\nIn the `pushtotalk.py` file, import the [RPi.GPIO](https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage)\nmodule to control the GPIO pins on the Pi. Place the following statement near the\nother `import` statements: \n\n import RPi.GPIO as GPIO\n\nModify the code to set the output pin initially to the low logic state. When the\non command is received, set the pin to the high logic state. When the\noff command is received, set the pin to the low logic state.\n**Note:** This sample uses the `GPIO.BCM` option, which refers to pins by their Broadcom [numbering](https://pinout.xyz). BCM 25 refers to physical pin 22 on the Raspberry Pi 3. \n\n device_handler = device_helpers.DeviceRequestHandler(device_id)\n GPIO.setmode(GPIO.BCM)\n GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)\n\n @device_handler.command('action.devices.commands.OnOff')\n def onoff(on):\n if on:\n logging.info('Turning device on')\n GPIO.output(25, 1)\n else:\n logging.info('Turning device off')\n GPIO.output(25, 0)\n\nSave your changes and close the file.\n\n### Run the sample\n\nRun the modified sample code. \n\n```\npython pushtotalk.py\n```\n\nUse the same query as before. The LED should turn on.\n\nThat's just the beginning. Learn how to add more [traits and handlers](/assistant/sdk/guides/service/python/extend/add-trait-and-handler)."]]