您可以視需求在裝置型號中新增多個特徵。這些特徵並非只與一種裝置類型相關聯,可以視需要使用。
這個流程可新增特徵及處理傳入的指令:
決定要新增的特徵。
開啟
hotword.py
檔案。cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library
nano hotword.py
在處理
action.devices.commands.OnOff
指令的現有程式碼區塊底下新增下列程式碼區塊 (請勿刪除現有的程式碼區塊)。if command == "action.devices.commands.command-name": if params['parameter-name']: if conditional: print('Something happened.') else: print('Something else happened.')
在上方的程式碼區塊中,找出每個變數需要的資訊。
command-name
前往步驟 1 的特定特徵頁面 (例如「ColorTemperature」ColorTemperature)。使用 Device COMMANDS 表格中的指令。 parameter-name
再次查看特徵頁面的「裝置指令」表格。每個指令都有一或多個相關聯的參數。這些程式碼列在 EXECUTE 要求 JSON 的 "params"
下。請使用確切的參數名稱。請注意,部分參數是包含其他參數的物件,只要使用頂層物件即可。conditional
您不一定要在處理常式程式碼中使用條件式,但這可能會有助於區分您在裝置上執行指令的方式。 以下是一些亮度和 ColorTemperature 特徵的範例:
if command == "action.devices.commands.BrightnessAbsolute": if params['brightness']: if params['brightness'] > 50: print('brightness > 50') else: print('brightness <= 50') if command == "action.devices.commands.ColorAbsolute": if params['color']: if params['color'].get('name') == "blue": print('The color is blue.') else: print('The color is not blue.')
使用您在步驟 1 新增的特徵更新裝置型號。
執行經過修改的原始碼。
cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library
python hotword.py --device-model-id my-model
嘗試查詢。
Ok Google,將亮度設為 65%。
Ok Google,變成藍色。