대화형 작업이 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 | 다음에서 특정 trait 페이지로 이동
1단계 (예: ColorTemperature)
기기 명령어 표의 명령어를 사용합니다. |
my-function | 핸들러 함수의 이름을 원하는 대로 지정합니다. |
parameter-name | 기기 명령어를 다시 확인합니다.
트레잇 페이지의 테이블을 참조하세요. 각 명령어에는 하나 이상의 매개변수가 있습니다.
관련이 있습니다. 실행의 "params" 아래에 나열됩니다.
요청할 수 있습니다 정확한 매개변수 이름을 사용합니다. 이 중 일부는
매개변수는 다른 매개변수를 포함하는 객체입니다.
최상위 객체입니다.
|
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%로 설정합니다.
파란색으로 만듭니다.
다음 단계
맞춤 기기 작업 등록
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)"]]