处理意图
任何组件都可以注册来处理意图。这使得单个组件能够处理从多个语音助手触发的意图。
一个组件必须为它想要处理的每种意图类型注册一个意图处理程序。意图处理程序必须扩展homeassistant.helpers.intent.IntentHandler。
import asyncio
from homeassistant.helpers import intent
DATA_KEY = 'example_key'
@asyncio.coroutine
def async_setup(hass, config):
hass.data[DATA_KEY] = 0
intent.async_register(hass, CountInvocationIntent())
class CountInvocationIntent(intent.IntentHandler):
"""处理CountInvocationIntent意图。"""
# 要处理的意图类型
intent_type = 'CountInvocationIntent'
# 可选。槽位的验证模式
# slot_schema = {
# 'item': cv.string
# }
@asyncio.coroutine
def async_handle(self, intent_obj):
"""处理意图。"""
intent_obj.hass.data[DATA_KEY] += 1
response = intent_obj.create_response()
response.async_set_speech(
"此意图已被调用 {} 次".format(
intent_obj.hass.data[DATA_KEY]))
return response
总结
说明了在Home Assistant中任何组件都可处理意图,且需为不同意图类型注册处理程序,处理程序要继承特定类。给出了示例代码,在async_setup函数中初始化数据并注册意图处理类,处理类中定义意图类型、可选的槽位验证模式及处理意图的异步方法,在方法中更新数据并创建响应,设置语音内容,为开发者实现意图处理功能提供了具体范例和操作流程,有助于实现多语音助手的意图整合处理,增强系统的交互性和灵活性。
948

被折叠的 条评论
为什么被折叠?



