با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
شما می توانید هر تعداد ویژگی که می خواهید به مدل دستگاه خود اضافه کنید. این ویژگیها تنها به یک نوع دستگاه مرتبط نیستند، میتوانید هر طور که میخواهید از آنها استفاده کنید.
این فرآیند برای افزودن هر صفت و مدیریت دستور ورودی است:
اطلاعات مورد نیاز برای هر متغیر را در بلوک کد بالا پیدا کنید.
command-name
از مرحله 1 به صفحه ویژگی خاص بروید (به عنوان مثال، ColorTemperature ). از یک دستور از جدول Device COMMANDS استفاده کنید.
parameter-name
دوباره به جدول Device COMMANDS در صفحه ویژگی نگاه کنید. هر دستور دارای یک یا چند پارامتر مرتبط با آن است. اینها در زیر "params" در JSON درخواست EXECUTE فهرست شده اند. از نام پارامتر دقیق استفاده کنید. توجه داشته باشید که برخی از این پارامترها اشیایی هستند که حاوی پارامترهای دیگری هستند - فقط از شی سطح بالا استفاده کنید.
conditional
شما به شدت نیازی به استفاده از یک شرطی در کد کنترل کننده خود ندارید، اما ممکن است به تمایز نحوه اجرای دستور در دستگاه کمک کند.
در اینجا چند مثال برای صفات روشنایی و دمای رنگ آورده شده است:
ifcommand=="action.devices.commands.BrightnessAbsolute":ifparams['brightness']:ifparams['brightness']>50:print('brightness > 50')else:print('brightness <= 50')ifcommand=="action.devices.commands.ColorAbsolute":ifparams['color']:ifparams['color'].get('name')=="blue":print('The color is blue.')else:print('The color is not blue.')
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThe Google Assistant Library for Python is deprecated; use the Google Assistant Service instead.\u003c/p\u003e\n"],["\u003cp\u003eYou can add traits, like Brightness or ColorTemperature, to your device model to extend its functionality.\u003c/p\u003e\n"],["\u003cp\u003eTo add a trait, modify the \u003ccode\u003ehotword.py\u003c/code\u003e file to handle the incoming commands associated with that trait.\u003c/p\u003e\n"],["\u003cp\u003eUpdate the device model with the added trait and run the modified code to test it.\u003c/p\u003e\n"]]],[],null,["# Add More Traits and Handlers\n\n| **Warning:** The Google Assistant Library for Python is deprecated as of June 28th, 2019. Use the [Google Assistant Service](/assistant/sdk/guides/service/python) instead.\n\n\u003cbr /\u003e\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 `hotword.py` file.\n\n ```\n cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library\n ``` \n\n ```\n nano hotword.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 ```python\n if command == \"action.devices.commands.\u003cvar translate=\"no\"\u003ecommand-name\u003c/var\u003e\":\n if params['\u003cvar translate=\"no\"\u003eparameter-name\u003c/var\u003e']:\n if conditional:\n print('Something happened.')\n else:\n print('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 | `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 if command == \"action.devices.commands.BrightnessAbsolute\":\n if params['brightness']:\n if params['brightness'] \u003e 50:\n print('brightness \u003e 50')\n else:\n print('brightness \u003c= 50')\n\n if command == \"action.devices.commands.ColorAbsolute\":\n if params['color']:\n if params['color'].get('name') == \"blue\":\n print('The color is blue.')\n else:\n print('The color is not blue.')\n ```\n5. [Update the device model](/assistant/sdk/guides/library/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/library\n ``` \n\n ```\n python hotword.py --device-model-id my-model\n ```\n7. Try a query.\n\n *Hey Google, set brightness to 65%.*\n\n *Ok Google, make it blue.*\n\nNext step\n---------\n\n[Register Custom Device Actions](/assistant/sdk/guides/library/python/extend/custom-actions)"]]