เพิ่มลักษณะเฉพาะและเครื่องจัดการ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณเพิ่มลักษณะเฉพาะให้กับรุ่นอุปกรณ์ได้มากเท่าที่ต้องการ ลักษณะเหล่านี้
ไม่ได้เชื่อมโยงกับอุปกรณ์เพียงประเภทเดียว คุณสามารถใช้อุปกรณ์เหล่านั้นได้ตามต้องการ
กระบวนการนี้เป็นกระบวนการเพิ่มลักษณะและจัดการคําสั่งที่เข้ามาใหม่
กำหนดลักษณะที่คุณต้องการ
ที่จะเพิ่ม
เปิดไฟล์ 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 | ดูอีกครั้งที่คำสั่งอุปกรณ์
ในหน้าลักษณะเฉพาะ แต่ละคำสั่งมีพารามิเตอร์อย่างน้อย 1 รายการ
ที่เกี่ยวข้อง รายชื่อเหล่านี้แสดงภายใต้ "params" ใน EXECUTE
ส่งคำขอ 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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[[["\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)"]]