1 """Agent foundation for conversation integration."""
3 from __future__
import annotations
9 import voluptuous
as vol
18 OLD_HOME_ASSISTANT_AGENT,
20 from .entity
import ConversationEntity
22 AbstractConversationAgent,
28 ConversationTraceEvent,
29 ConversationTraceEventType,
30 async_conversation_trace,
33 _LOGGER = logging.getLogger(__name__)
36 @singleton.singleton("conversation_agent")
39 """Get the active agent."""
44 """Validate agent ID."""
47 raise vol.Invalid(
"invalid agent ID")
53 hass: HomeAssistant, agent_id: str |
None =
None
54 ) -> AbstractConversationAgent | ConversationEntity |
None:
55 """Get specified agent."""
56 if agent_id
is None or agent_id
in (HOME_ASSISTANT_AGENT, OLD_HOME_ASSISTANT_AGENT):
57 return hass.data[DATA_DEFAULT_ENTITY]
60 return hass.data[DATA_COMPONENT].
get_entity(agent_id)
64 if not manager.async_is_valid_agent_id(agent_id):
67 return manager.async_get_agent(agent_id)
73 conversation_id: str |
None,
75 language: str |
None =
None,
76 agent_id: str |
None =
None,
77 device_id: str |
None =
None,
78 ) -> ConversationResult:
79 """Process text and get intent."""
83 raise ValueError(f
"Agent {agent_id} not found")
85 if isinstance(agent, ConversationEntity):
86 agent.async_set_context(context)
87 method = agent.internal_async_process
89 method = agent.async_process
92 language = hass.config.language
94 _LOGGER.debug(
"Processing in %s: %s", language, text)
98 conversation_id=conversation_id,
106 ConversationTraceEventType.ASYNC_PROCESS,
107 dataclasses.asdict(conversation_input),
110 result = await method(conversation_input)
111 trace.set_result(**result.as_dict())
116 """Class to manage conversation agents."""
119 """Initialize the conversation agents."""
121 self._agents: dict[str, AbstractConversationAgent] = {}
126 if agent_id
not in self._agents:
127 raise ValueError(f
"Agent {agent_id} not found")
129 return self._agents[agent_id]
133 """List all agents."""
134 agents: list[AgentInfo] = []
135 for agent_id, agent
in self._agents.items():
136 config_entry = self.
hasshass.config_entries.async_get_entry(agent_id)
140 if config_entry
is None:
142 "Conversation agent %s is still loaded after config entry removal",
150 name=config_entry.title
or config_entry.domain,
157 """Check if the agent id is valid."""
158 return agent_id
in self._agents
163 self._agents[agent_id] = agent
167 """Unset the agent."""
168 self._agents.pop(agent_id,
None)
None async_unset_agent(self, str agent_id)
bool async_is_valid_agent_id(self, str agent_id)
None async_set_agent(self, str agent_id, AbstractConversationAgent agent)
None __init__(self, HomeAssistant hass)
list[AgentInfo] async_get_agent_info(self)
AbstractConversationAgent|None async_get_agent(self, str agent_id)
CalendarEntity get_entity(HomeAssistant hass, str entity_id)
AgentManager get_agent_manager(HomeAssistant hass)
ConversationResult async_converse(HomeAssistant hass, str text, str|None conversation_id, Context context, str|None language=None, str|None agent_id=None, str|None device_id=None)
AbstractConversationAgent|ConversationEntity|None async_get_agent(HomeAssistant hass, str|None agent_id=None)
str agent_id_validator(Any value)
Generator[ConversationTrace] async_conversation_trace()
HomeAssistant async_get_hass()