2019-02-06 03:31:15 +00:00
|
|
|
"""Tests for the conversation component."""
|
2023-01-19 18:59:02 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-04-22 16:43:09 +00:00
|
|
|
from typing import Literal
|
|
|
|
|
2023-01-19 18:59:02 +00:00
|
|
|
from homeassistant.components import conversation
|
2023-04-12 02:39:40 +00:00
|
|
|
from homeassistant.components.homeassistant.exposed_entities import (
|
|
|
|
DATA_EXPOSED_ENTITIES,
|
|
|
|
ExposedEntities,
|
|
|
|
)
|
2023-01-19 18:59:02 +00:00
|
|
|
from homeassistant.helpers import intent
|
|
|
|
|
|
|
|
|
|
|
|
class MockAgent(conversation.AbstractConversationAgent):
|
|
|
|
"""Test Agent."""
|
|
|
|
|
2023-04-22 16:43:09 +00:00
|
|
|
def __init__(
|
|
|
|
self, agent_id: str, supported_languages: list[str] | Literal["*"]
|
|
|
|
) -> None:
|
2023-01-19 18:59:02 +00:00
|
|
|
"""Initialize the agent."""
|
2023-02-04 04:35:29 +00:00
|
|
|
self.agent_id = agent_id
|
2023-01-19 18:59:02 +00:00
|
|
|
self.calls = []
|
|
|
|
self.response = "Test response"
|
2023-04-22 16:43:09 +00:00
|
|
|
self._supported_languages = supported_languages
|
2023-01-19 18:59:02 +00:00
|
|
|
|
2023-04-19 15:15:21 +00:00
|
|
|
@property
|
|
|
|
def attribution(self) -> conversation.Attribution | None:
|
|
|
|
"""Return the attribution."""
|
|
|
|
return {"name": "Mock assistant", "url": "https://assist.me"}
|
|
|
|
|
2023-04-18 20:11:04 +00:00
|
|
|
@property
|
|
|
|
def supported_languages(self) -> list[str]:
|
|
|
|
"""Return a list of supported languages."""
|
2023-04-22 16:43:09 +00:00
|
|
|
return self._supported_languages
|
2023-04-18 20:11:04 +00:00
|
|
|
|
2023-01-19 18:59:02 +00:00
|
|
|
async def async_process(
|
2023-01-25 03:47:49 +00:00
|
|
|
self, user_input: conversation.ConversationInput
|
2023-01-24 03:38:41 +00:00
|
|
|
) -> conversation.ConversationResult:
|
2023-01-19 18:59:02 +00:00
|
|
|
"""Process some text."""
|
2023-01-25 03:47:49 +00:00
|
|
|
self.calls.append(user_input)
|
|
|
|
response = intent.IntentResponse(language=user_input.language)
|
2023-01-19 18:59:02 +00:00
|
|
|
response.async_set_speech(self.response)
|
|
|
|
return conversation.ConversationResult(
|
2023-01-25 03:47:49 +00:00
|
|
|
response=response, conversation_id=user_input.conversation_id
|
2023-01-19 18:59:02 +00:00
|
|
|
)
|
2023-04-12 02:39:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def expose_new(hass, expose_new):
|
|
|
|
"""Enable exposing new entities to the default agent."""
|
|
|
|
exposed_entities: ExposedEntities = hass.data[DATA_EXPOSED_ENTITIES]
|
|
|
|
exposed_entities.async_set_expose_new_entities(conversation.DOMAIN, expose_new)
|
|
|
|
|
|
|
|
|
2023-05-02 20:08:09 +00:00
|
|
|
async def expose_entity(hass, entity_id, should_expose):
|
2023-04-12 02:39:40 +00:00
|
|
|
"""Expose an entity to the default agent."""
|
|
|
|
exposed_entities: ExposedEntities = hass.data[DATA_EXPOSED_ENTITIES]
|
2023-05-02 20:08:09 +00:00
|
|
|
await exposed_entities.async_expose_entity(
|
|
|
|
conversation.DOMAIN, entity_id, should_expose
|
|
|
|
)
|