您可以根据需要向设备模型添加任意数量的特征。这些特征是 并非只能局限于一种设备类型,您可以根据自己的喜好来使用这些设备。
以下是添加任何特征并处理传入命令的过程:
- 确定您需要的特征 添加。 
- 打开 - 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%。 - 设为蓝色。 
