Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity for conversation integration."""
2 
3 from abc import abstractmethod
4 from typing import Literal, final
5 
6 from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
7 from homeassistant.helpers.restore_state import RestoreEntity
8 from homeassistant.util import dt as dt_util
9 
10 from .const import ConversationEntityFeature
11 from .models import ConversationInput, ConversationResult
12 
13 
15  """Entity that supports conversations."""
16 
17  _attr_should_poll = False
18  _attr_supported_features = ConversationEntityFeature(0)
19  __last_activity: str | None = None
20 
21  @property
22  @final
23  def state(self) -> str | None:
24  """Return the state of the entity."""
25  if self.__last_activity__last_activity is None:
26  return None
27  return self.__last_activity__last_activity
28 
29  async def async_internal_added_to_hass(self) -> None:
30  """Call when the entity is added to hass."""
31  await super().async_internal_added_to_hass()
32  state = await self.async_get_last_stateasync_get_last_state()
33  if (
34  state is not None
35  and state.state is not None
36  and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
37  ):
38  self.__last_activity__last_activity = state.state
39 
40  @final
42  self, user_input: ConversationInput
43  ) -> ConversationResult:
44  """Process a sentence."""
45  self.__last_activity__last_activity = dt_util.utcnow().isoformat()
46  self.async_write_ha_stateasync_write_ha_state()
47  return await self.async_processasync_process(user_input)
48 
49  @property
50  @abstractmethod
51  def supported_languages(self) -> list[str] | Literal["*"]:
52  """Return a list of supported languages."""
53 
54  @abstractmethod
55  async def async_process(self, user_input: ConversationInput) -> ConversationResult:
56  """Process a sentence."""
57 
58  async def async_prepare(self, language: str | None = None) -> None:
59  """Load intents for a language."""
ConversationResult internal_async_process(self, ConversationInput user_input)
Definition: entity.py:43
None async_prepare(self, str|None language=None)
Definition: entity.py:58
ConversationResult async_process(self, ConversationInput user_input)
Definition: entity.py:55