Home Assistant Unofficial Reference 2024.12.1
text.py
Go to the documentation of this file.
1 """Support for esphome texts."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from aioesphomeapi import EntityInfo, TextInfo, TextMode as EsphomeTextMode, TextState
8 
9 from homeassistant.components.text import TextEntity, TextMode
10 from homeassistant.core import callback
11 
12 from .entity import (
13  EsphomeEntity,
14  convert_api_error_ha_error,
15  esphome_state_property,
16  platform_async_setup_entry,
17 )
18 from .enum_mapper import EsphomeEnumMapper
19 
20 TEXT_MODES: EsphomeEnumMapper[EsphomeTextMode, TextMode] = EsphomeEnumMapper(
21  {
22  EsphomeTextMode.TEXT: TextMode.TEXT,
23  EsphomeTextMode.PASSWORD: TextMode.PASSWORD,
24  }
25 )
26 
27 
28 class EsphomeText(EsphomeEntity[TextInfo, TextState], TextEntity):
29  """A text implementation for esphome."""
30 
31  @callback
32  def _on_static_info_update(self, static_info: EntityInfo) -> None:
33  """Set attrs from static info."""
34  super()._on_static_info_update(static_info)
35  static_info = self._static_info_static_info
36  self._attr_native_min_attr_native_min = static_info.min_length
37  self._attr_native_max_attr_native_max = static_info.max_length
38  self._attr_pattern_attr_pattern = static_info.pattern
39  self._attr_mode_attr_mode = TEXT_MODES.from_esphome(static_info.mode) or TextMode.TEXT
40 
41  @property
42  @esphome_state_property
43  def native_value(self) -> str | None:
44  """Return the state of the entity."""
45  state = self._state_state
46  return None if state.missing_state else state.state
47 
48  @convert_api_error_ha_error
49  async def async_set_value(self, value: str) -> None:
50  """Update the current value."""
51  self._client_client.text_command(self._key_key, value)
52 
53 
54 async_setup_entry = partial(
55  platform_async_setup_entry,
56  info_type=TextInfo,
57  entity_type=EsphomeText,
58  state_type=TextState,
59 )
None _on_static_info_update(self, EntityInfo static_info)
Definition: text.py:32