Nouvelle page
"""Fallback Conversation Agent"""
from __future__ import annotations
import logging
import requests
from homeassistant.components import conversation
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.util import ulid
from home_assistant_intents import get_languages
from homeassistant.helpers import (
config_validation as cv,
intent,
)
from .const import (
CONF_DEBUG_LEVEL,
CONF_PRIMARY_AGENT,
CONF_FALLBACK_AGENT,
DEBUG_LEVEL_NO_DEBUG,
DEBUG_LEVEL_LOW_DEBUG,
DEBUG_LEVEL_VERBOSE_DEBUG,
DOMAIN,
)
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
# hass.data key for agent.
DATA_AGENT = "agent"
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Fallback Conversation Agent from a config entry."""
agent = FallbackConversationAgent(hass, entry)
conversation.async_set_agent(hass, entry, agent)
return True
def post_to_api(url, json):
return requests.post(url, json=json)
class FallbackConversationAgent(conversation.AbstractConversationAgent):
"""Fallback Conversation Agent."""
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Initialize the agent."""
self.hass = hass
self.entry = entry
@property
def supported_languages(self) -> list[str]:
"""Return a list of supported languages."""
return get_languages()
async def async_process(
self, user_input: conversation.ConversationInput
) -> conversation.ConversationResult:
url = 'http://192.168.122.1:10500'
data = {'text': user_input.text}
response_from_api = await self.hass.async_add_executor_job(post_to_api, url, data) #task.executor(requests.post, )
response = intent.IntentResponse(language=user_input.language)
response.async_set_speech(response_from_api.text)
return conversation.ConversationResult(
conversation_id=None,
response=response
)