2019-11-27 10:25:43 +00:00
|
|
|
"""The Intent integration."""
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import http
|
|
|
|
from homeassistant.components.http.data_validator import RequestDataValidator
|
2020-02-08 12:10:59 +00:00
|
|
|
from homeassistant.const import SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON
|
|
|
|
from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant
|
2019-12-13 14:38:41 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, integration_platform, intent
|
2019-11-27 10:25:43 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the Intent component."""
|
|
|
|
hass.http.register_view(IntentHandleView())
|
2019-12-01 22:12:57 +00:00
|
|
|
|
2019-12-13 14:38:41 +00:00
|
|
|
await integration_platform.async_process_integration_platforms(
|
|
|
|
hass, DOMAIN, _async_process_intent
|
|
|
|
)
|
2019-12-01 22:12:57 +00:00
|
|
|
|
2020-02-08 12:10:59 +00:00
|
|
|
hass.helpers.intent.async_register(
|
|
|
|
intent.ServiceIntentHandler(
|
|
|
|
intent.INTENT_TURN_ON, HA_DOMAIN, SERVICE_TURN_ON, "Turned {} on"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
hass.helpers.intent.async_register(
|
|
|
|
intent.ServiceIntentHandler(
|
|
|
|
intent.INTENT_TURN_OFF, HA_DOMAIN, SERVICE_TURN_OFF, "Turned {} off"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
hass.helpers.intent.async_register(
|
|
|
|
intent.ServiceIntentHandler(
|
|
|
|
intent.INTENT_TOGGLE, HA_DOMAIN, SERVICE_TOGGLE, "Toggled {}"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-11-27 10:25:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-12-13 14:38:41 +00:00
|
|
|
async def _async_process_intent(hass: HomeAssistant, domain: str, platform):
|
|
|
|
"""Process the intents of an integration."""
|
|
|
|
await platform.async_setup_intents(hass)
|
2019-12-01 22:12:57 +00:00
|
|
|
|
|
|
|
|
2019-11-27 10:25:43 +00:00
|
|
|
class IntentHandleView(http.HomeAssistantView):
|
|
|
|
"""View to handle intents from JSON."""
|
|
|
|
|
|
|
|
url = "/api/intent/handle"
|
|
|
|
name = "api:intent:handle"
|
|
|
|
|
|
|
|
@RequestDataValidator(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required("name"): cv.string,
|
|
|
|
vol.Optional("data"): vol.Schema({cv.string: object}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def post(self, request, data):
|
|
|
|
"""Handle intent with name/data."""
|
|
|
|
hass = request.app["hass"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
intent_name = data["name"]
|
|
|
|
slots = {
|
|
|
|
key: {"value": value} for key, value in data.get("data", {}).items()
|
|
|
|
}
|
|
|
|
intent_result = await intent.async_handle(
|
|
|
|
hass, DOMAIN, intent_name, slots, "", self.context(request)
|
|
|
|
)
|
|
|
|
except intent.IntentHandleError as err:
|
|
|
|
intent_result = intent.IntentResponse()
|
|
|
|
intent_result.async_set_speech(str(err))
|
|
|
|
|
|
|
|
if intent_result is None:
|
|
|
|
intent_result = intent.IntentResponse()
|
|
|
|
intent_result.async_set_speech("Sorry, I couldn't handle that")
|
|
|
|
|
|
|
|
return self.json(intent_result)
|