添加更多特征和处理程序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以根据需要向设备模型添加任意数量的特征。这些特征是
并非只能局限于一种设备类型,您可以根据自己的喜好来使用这些设备。
以下是添加任何特征并处理传入命令的过程:
确定您需要的特征
添加。
打开 pushtotalk.py
文件。
cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc
nano pushtotalk.py
将以下代码块添加到
会处理 action.devices.commands.OnOff
命令(不要删除现有的
代码块中)。
@device_handler.command('action.devices.commands.command-name')
def my-function(parameter-name):
if conditional:
logging.info('Something happened.')
else:
logging.info('Something else happened.')
在上述代码块中找到每个变量所需的信息。
command-name | 从以下位置前往特定特征页面:
第 1 步(例如 ColorTemperature)。
使用设备命令表中的命令。 |
my-function | 根据需要为处理程序函数命名。 |
parameter-name | 再次查看设备命令
表格。每个命令都有一个或多个参数
及其关联。这些项列在 EXECUTE 中的 "params" 下
请求 JSON。请使用确切的参数名称。请注意,其中一些
参数是指包含其他参数的对象 - 只需使用
顶级对象。
|
conditional | 您并不一定要使用条件语句
,但可能有助于您区分
命令。 |
下面是一些特征亮度特征的示例
和 ColorTemperature:
@device_handler.command('action.devices.commands.BrightnessAbsolute')
def brightnessCheck(brightness):
if brightness > 50:
logging.info('brightness > 50')
else:
logging.info('brightness <= 50')
@device_handler.command('action.devices.commands.ColorAbsolute')
def color(color):
if color.get('name') == "blue":
logging.info('color is blue')
else:
logging.info('color is not blue')
更新设备模型
替换成您在第 1 步中添加的特征。
运行修改后的源代码。
cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc
python pushtotalk.py
按 Enter 键,然后尝试查询。
例如:
将亮度设为 65%。
设为蓝色。
下一步
注册自定义设备操作
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eYou can freely add numerous traits to your device model, independent of device type.\u003c/p\u003e\n"],["\u003cp\u003eThe process to add a trait involves updating the \u003ccode\u003epushtotalk.py\u003c/code\u003e file with a new code block to handle the incoming command and then updating the device model.\u003c/p\u003e\n"],["\u003cp\u003eEach added trait requires specific command details that can be found on the relevant trait documentation page.\u003c/p\u003e\n"],["\u003cp\u003eOnce implemented, the code can be tested by running \u003ccode\u003epushtotalk.py\u003c/code\u003e and using voice commands corresponding to the added trait.\u003c/p\u003e\n"]]],[],null,["# Add More Traits and Handlers\n\nYou can add as many traits to your device model as you wish. These traits are\nnot tied to just one device type, you can use them as you choose.\n\nThis is the process to add any trait and handle the incoming command:\n\n1. Determine which [trait](/assistant/sdk/reference/traits) you want\n to add.\n\n2. Open the `pushtotalk.py` file.\n\n ```\n cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc\n ``` \n\n ```\n nano pushtotalk.py\n ```\n3. Add the following code block under the existing one that\n handles the `action.devices.commands.OnOff` command (don't delete the existing\n code block).\n\n ```\n @device_handler.command('action.devices.commands.command-name')\n def my-function(parameter-name):\n if conditional:\n logging.info('Something happened.')\n else:\n logging.info('Something else happened.')\n ```\n4. Find the information you need for each variable in the above code block.\n\n |------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n | `command-name` | Go to the specific trait page from Step 1 (for example, [ColorTemperature](/assistant/sdk/reference/traits/colortemperature)). Use a command from the **Device COMMANDS** table. |\n | `my-function` | Name your handler function whatever you want. |\n | `parameter-name` | Look again at the **Device COMMANDS** table on the trait page. Each command has one or more parameters associated with it. These are listed under `\"params\"` in the EXECUTE request JSON. Use the exact parameter name. Note that some of these parameters are objects that contain other parameters - just use the top-level object. |\n | `conditional` | You don't strictly need to use a conditional in your handler code, but it may help to differentiate how you execute the command on the device. |\n\n Here are some examples for traits [Brightness](/assistant/sdk/reference/traits/brightness)\n and [ColorTemperature](/assistant/sdk/reference/traits/colortemperature): \n\n ```python\n @device_handler.command('action.devices.commands.BrightnessAbsolute')\n def brightnessCheck(brightness):\n if brightness \u003e 50:\n logging.info('brightness \u003e 50')\n else:\n logging.info('brightness \u003c= 50')\n\n @device_handler.command('action.devices.commands.ColorAbsolute')\n def color(color):\n if color.get('name') == \"blue\":\n logging.info('color is blue')\n else:\n logging.info('color is not blue')\n ```\n5. [Update the device model](/assistant/sdk/guides/service/python/extend/register-device-traits)\n with the trait you added in Step 1.\n\n6. Run the modified source code.\n\n ```\n cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/grpc\n ``` \n\n ```\n python pushtotalk.py\n ```\n7. Press the Enter key and try a query.\n\n For example:\n\n *Set brightness to 65%.*\n\n *Make it blue.*\n\nNext step\n---------\n\n[Register Custom Device Actions](/assistant/sdk/guides/service/python/extend/custom-actions)"]]