Home Assistant Unofficial Reference 2024.12.1
text.py
Go to the documentation of this file.
1 """Support for KNX/IP text."""
2 
3 from __future__ import annotations
4 
5 from xknx import XKNX
6 from xknx.devices import Notification as XknxNotification
7 from xknx.dpt import DPTLatin1
8 
9 from homeassistant import config_entries
10 from homeassistant.components.text import TextEntity
11 from homeassistant.const import (
12  CONF_ENTITY_CATEGORY,
13  CONF_MODE,
14  CONF_NAME,
15  CONF_TYPE,
16  STATE_UNAVAILABLE,
17  STATE_UNKNOWN,
18  Platform,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.restore_state import RestoreEntity
23 from homeassistant.helpers.typing import ConfigType
24 
25 from . import KNXModule
26 from .const import CONF_RESPOND_TO_READ, CONF_STATE_ADDRESS, KNX_ADDRESS, KNX_MODULE_KEY
27 from .entity import KnxYamlEntity
28 
29 
31  hass: HomeAssistant,
32  config_entry: config_entries.ConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up sensor(s) for KNX platform."""
36  knx_module = hass.data[KNX_MODULE_KEY]
37  config: list[ConfigType] = knx_module.config_yaml[Platform.TEXT]
38 
39  async_add_entities(KNXText(knx_module, entity_config) for entity_config in config)
40 
41 
42 def _create_notification(xknx: XKNX, config: ConfigType) -> XknxNotification:
43  """Return a KNX Notification to be used within XKNX."""
44  return XknxNotification(
45  xknx,
46  name=config[CONF_NAME],
47  group_address=config[KNX_ADDRESS],
48  group_address_state=config.get(CONF_STATE_ADDRESS),
49  respond_to_read=config[CONF_RESPOND_TO_READ],
50  value_type=config[CONF_TYPE],
51  )
52 
53 
55  """Representation of a KNX text."""
56 
57  _device: XknxNotification
58  _attr_native_max = 14
59 
60  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
61  """Initialize a KNX text."""
62  super().__init__(
63  knx_module=knx_module,
64  device=_create_notification(knx_module.xknx, config),
65  )
66  self._attr_mode_attr_mode = config[CONF_MODE]
67  self._attr_pattern_attr_pattern = (
68  r"[\u0000-\u00ff]*" # Latin-1
69  if issubclass(self._device_device.remote_value.dpt_class, DPTLatin1)
70  else r"[\u0000-\u007f]*" # ASCII
71  )
72  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
73  self._attr_unique_id_attr_unique_id = str(self._device_device.remote_value.group_address)
74 
75  async def async_added_to_hass(self) -> None:
76  """Restore last state."""
77  await super().async_added_to_hass()
78  if not self._device_device.remote_value.readable and (
79  last_state := await self.async_get_last_stateasync_get_last_state()
80  ):
81  if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
82  self._device_device.remote_value.value = last_state.state
83 
84  @property
85  def native_value(self) -> str | None:
86  """Return the value reported by the text."""
87  return self._device_device.message
88 
89  async def async_set_value(self, value: str) -> None:
90  """Change the value."""
91  await self._device_device.set(value)
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: text.py:60
None async_set_value(self, str value)
Definition: text.py:89
XknxNotification _create_notification(XKNX xknx, ConfigType config)
Definition: text.py:42
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: text.py:34