對話動作已於 2023 年 6 月 13 日淘汰。詳情請參閱「
對話動作已淘汰」。
新增更多托盤和處理常式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以視需要為裝置型號新增多個特徵。這些特徵
不限於單一裝置類型,您可以自由選擇使用。
就是新增任何特徵並處理傳入指令的程序:
決定您想要的特徵。
。
開啟 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%。
設為藍色。
下一步
註冊自訂裝置動作
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間: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)"]]