1 """Component to allow setting text as platforms."""
3 from __future__
import annotations
5 from dataclasses
import asdict, dataclass
6 from datetime
import timedelta
7 from enum
import StrEnum
10 from typing
import Any, final
12 from propcache
import cached_property
13 import voluptuous
as vol
35 _LOGGER = logging.getLogger(__name__)
37 DATA_COMPONENT: HassKey[EntityComponent[TextEntity]] =
HassKey(DOMAIN)
38 ENTITY_ID_FORMAT = DOMAIN +
".{}"
39 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
40 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
46 __all__ = [
"DOMAIN",
"TextEntity",
"TextEntityDescription",
"TextMode"]
49 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
50 """Set up Text entities."""
51 component = hass.data[DATA_COMPONENT] = EntityComponent[TextEntity](
52 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
54 await component.async_setup(config)
56 component.async_register_entity_service(
58 {vol.Required(ATTR_VALUE): cv.string},
66 """Service call wrapper to set a new value."""
67 value = service_call.data[ATTR_VALUE]
68 if len(value) < entity.min:
70 f
"Value {value} for {entity.entity_id} is too short (minimum length"
73 if len(value) > entity.max:
75 f
"Value {value} for {entity.entity_id} is too long (maximum length {entity.max})"
77 if entity.pattern_cmp
and not entity.pattern_cmp.match(value):
79 f
"Value {value} for {entity.entity_id} doesn't match pattern {entity.pattern}"
81 await entity.async_set_value(value)
85 """Set up a config entry."""
90 """Unload a config entry."""
95 """Modes for text entities."""
102 """A class that describes text entities."""
105 native_max: int = MAX_LENGTH_STATE_STATE
106 mode: TextMode = TextMode.TEXT
107 pattern: str |
None =
None
110 CACHED_PROPERTIES_WITH_ATTR_ = {
120 """Representation of a Text entity."""
122 _entity_component_unrecorded_attributes = frozenset(
123 {ATTR_MAX, ATTR_MIN, ATTR_MODE, ATTR_PATTERN}
126 entity_description: TextEntityDescription
128 _attr_native_value: str |
None
129 _attr_native_min: int
130 _attr_native_max: int
131 _attr_pattern: str |
None
132 _attr_state:
None =
None
133 __pattern_cmp: re.Pattern |
None =
None
137 """Return capability attributes."""
139 ATTR_MODE: self.
modemode,
140 ATTR_MIN: self.
minmin,
141 ATTR_MAX: self.
maxmax,
142 ATTR_PATTERN: self.
patternpattern,
148 """Return the entity state."""
153 f
"Entity {self.entity_id} provides state {self.native_value} which is "
154 f
"too short (minimum length {self.min})"
158 f
"Entity {self.entity_id} provides state {self.native_value} which is "
159 f
"too long (maximum length {self.max})"
163 f
"Entity {self.entity_id} provides state {self.native_value} which "
164 f
"does not match expected pattern {self.pattern}"
170 """Return the mode of the entity."""
171 if hasattr(self,
"_attr_mode"):
172 return self._attr_mode
173 if hasattr(self,
"entity_description"):
174 return self.entity_description.mode
179 """Return the minimum length of the value."""
180 if hasattr(self,
"_attr_native_min"):
181 return self._attr_native_min
182 if hasattr(self,
"entity_description"):
183 return self.entity_description.native_min
189 """Return the minimum length of the value."""
194 """Return the maximum length of the value."""
195 if hasattr(self,
"_attr_native_max"):
196 return self._attr_native_max
197 if hasattr(self,
"entity_description"):
198 return self.entity_description.native_max
199 return MAX_LENGTH_STATE_STATE
204 """Return the maximum length of the value."""
205 return min(self.
native_maxnative_max, MAX_LENGTH_STATE_STATE)
210 """Return a compiled pattern."""
211 if self.
patternpattern
is None:
220 """Return the regex pattern that the value must match."""
221 if hasattr(self,
"_attr_pattern"):
222 return self._attr_pattern
223 if hasattr(self,
"entity_description"):
224 return self.entity_description.pattern
229 """Return the value reported by the text."""
230 return self._attr_native_value
233 """Change the value."""
234 raise NotImplementedError
237 """Change the value."""
238 await self.
hasshass.async_add_executor_job(self.
set_valueset_value, value)
243 """Object to hold extra stored data."""
245 native_value: str |
None
250 """Return a dict representation of the text data."""
254 def from_dict(cls, restored: dict[str, Any]) -> TextExtraStoredData |
None:
255 """Initialize a stored text state from a dict."""
258 restored[
"native_value"],
259 restored[
"native_min"],
260 restored[
"native_max"],
267 """Mixin class for restoring previous text state."""
271 """Return text specific state data to be restored."""
279 """Restore attributes."""
282 return TextExtraStoredData.from_dict(restored_last_extra_data.as_dict())
TextExtraStoredData extra_restore_state_data(self)
TextExtraStoredData|None async_get_last_text_data(self)
re.Pattern|None pattern_cmp(self)
str|None native_value(self)
None async_set_value(self, str value)
None set_value(self, str value)
dict[str, Any] capability_attributes(self)
ExtraStoredData|None async_get_last_extra_data(self)
list[_T] match(self, BluetoothServiceInfoBleak service_info)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
None _async_set_value(TextEntity entity, ServiceCall service_call)