Conversation list command to support match-all languages agents (#91844)

* Conversation list command to support match-all languages agnets

* Reuse var
pull/91867/head
Paulus Schoutsen 2023-04-22 12:43:09 -04:00 committed by GitHub
parent 95d44e100b
commit d4fb2cdcf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 8 deletions

View File

@ -266,15 +266,18 @@ async def websocket_list_agents(
for agent_info in manager.async_get_agent_info():
agent = await manager.async_get_agent(agent_info.id)
supported_languages = agent.supported_languages
if language and supported_languages != MATCH_ALL:
supported_languages = language_util.matches(
language, supported_languages, country
)
agent_dict: dict[str, Any] = {
"id": agent_info.id,
"name": agent_info.name,
"supported_languages": agent.supported_languages,
"supported_languages": supported_languages,
}
if language:
agent_dict["supported_languages"] = language_util.matches(
language, agent.supported_languages, country
)
agents.append(agent_dict)
connection.send_message(websocket_api.result_message(msg["id"], {"agents": agents}))

View File

@ -1,6 +1,8 @@
"""Tests for the conversation component."""
from __future__ import annotations
from typing import Literal
from homeassistant.components import conversation
from homeassistant.components.homeassistant.exposed_entities import (
DATA_EXPOSED_ENTITIES,
@ -12,11 +14,14 @@ from homeassistant.helpers import intent
class MockAgent(conversation.AbstractConversationAgent):
"""Test Agent."""
def __init__(self, agent_id: str) -> None:
def __init__(
self, agent_id: str, supported_languages: list[str] | Literal["*"]
) -> None:
"""Initialize the agent."""
self.agent_id = agent_id
self.calls = []
self.response = "Test response"
self._supported_languages = supported_languages
@property
def attribution(self) -> conversation.Attribution | None:
@ -26,7 +31,7 @@ class MockAgent(conversation.AbstractConversationAgent):
@property
def supported_languages(self) -> list[str]:
"""Return a list of supported languages."""
return ["smurfish"]
return self._supported_languages
async def async_process(
self, user_input: conversation.ConversationInput

View File

@ -3,6 +3,7 @@
import pytest
from homeassistant.components import conversation
from homeassistant.const import MATCH_ALL
from . import MockAgent
@ -14,6 +15,16 @@ def mock_agent(hass):
"""Mock agent."""
entry = MockConfigEntry(entry_id="mock-entry")
entry.add_to_hass(hass)
agent = MockAgent(entry.entry_id)
agent = MockAgent(entry.entry_id, ["smurfish"])
conversation.async_set_agent(hass, entry, agent)
return agent
@pytest.fixture
def mock_agent_support_all(hass):
"""Mock agent that supports all languages."""
entry = MockConfigEntry(entry_id="mock-entry-support-all")
entry.add_to_hass(hass)
agent = MockAgent(entry.entry_id, MATCH_ALL)
conversation.async_set_agent(hass, entry, agent)
return agent

View File

@ -85,6 +85,11 @@
'smurfish',
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---
@ -104,6 +109,11 @@
'smurfish',
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---
@ -123,6 +133,11 @@
'supported_languages': list([
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---
@ -142,6 +157,11 @@
'supported_languages': list([
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---
@ -162,6 +182,11 @@
'supported_languages': list([
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---
@ -182,6 +207,11 @@
'supported_languages': list([
]),
}),
dict({
'id': 'mock-entry-support-all',
'name': 'Mock Title',
'supported_languages': '*',
}),
]),
})
# ---

View File

@ -1576,6 +1576,7 @@ async def test_get_agent_list(
hass: HomeAssistant,
init_components,
mock_agent,
mock_agent_support_all,
hass_ws_client: WebSocketGenerator,
snapshot: SnapshotAssertion,
) -> None: