Home Assistant Unofficial Reference 2024.12.1
text.py
Go to the documentation of this file.
1 """Demo platform that offers a fake text entity."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.text import TextEntity, TextMode
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from . import DOMAIN
12 
13 
15  hass: HomeAssistant,
16  config_entry: ConfigEntry,
17  async_add_entities: AddEntitiesCallback,
18 ) -> None:
19  """Set up the Demo text platform."""
21  [
22  DemoText(
23  unique_id="text",
24  device_name="Text",
25  native_value="Hello world",
26  ),
27  DemoText(
28  unique_id="password",
29  device_name="Password",
30  native_value="Hello world",
31  mode=TextMode.PASSWORD,
32  ),
33  DemoText(
34  unique_id="text_1_to_5_char",
35  device_name="Text with 1 to 5 characters",
36  native_value="Hello",
37  native_min=1,
38  native_max=5,
39  ),
40  DemoText(
41  unique_id="text_lowercase",
42  device_name="Text with only lower case characters",
43  native_value="world",
44  pattern=r"[a-z]+",
45  ),
46  ]
47  )
48 
49 
51  """Representation of a demo text entity."""
52 
53  _attr_has_entity_name = True
54  _attr_name = None
55  _attr_should_poll = False
56 
57  def __init__(
58  self,
59  unique_id: str,
60  device_name: str,
61  native_value: str | None,
62  mode: TextMode = TextMode.TEXT,
63  native_max: int | None = None,
64  native_min: int | None = None,
65  pattern: str | None = None,
66  ) -> None:
67  """Initialize the Demo text entity."""
68  self._attr_unique_id_attr_unique_id = unique_id
69  self._attr_native_value_attr_native_value = native_value
70  self._attr_mode_attr_mode = mode
71  if native_max is not None:
72  self._attr_native_max_attr_native_max = native_max
73  if native_min is not None:
74  self._attr_native_min_attr_native_min = native_min
75  if pattern is not None:
76  self._attr_pattern_attr_pattern = pattern
77  self._attr_device_info_attr_device_info = DeviceInfo(
78  identifiers={(DOMAIN, unique_id)},
79  name=device_name,
80  )
81 
82  async def async_set_value(self, value: str) -> None:
83  """Update the value."""
84  self._attr_native_value_attr_native_value = value
85  self.async_write_ha_stateasync_write_ha_state()
None async_set_value(self, str value)
Definition: text.py:82
None __init__(self, str unique_id, str device_name, str|None native_value, TextMode mode=TextMode.TEXT, int|None native_max=None, int|None native_min=None, str|None pattern=None)
Definition: text.py:66
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: text.py:18