64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""Support for esphome texts."""
|
|
from __future__ import annotations
|
|
|
|
from aioesphomeapi import EntityInfo, TextInfo, TextMode as EsphomeTextMode, TextState
|
|
|
|
from homeassistant.components.text import TextEntity, TextMode
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .entity import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
|
from .enum_mapper import EsphomeEnumMapper
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up esphome texts based on a config entry."""
|
|
await platform_async_setup_entry(
|
|
hass,
|
|
entry,
|
|
async_add_entities,
|
|
info_type=TextInfo,
|
|
entity_type=EsphomeText,
|
|
state_type=TextState,
|
|
)
|
|
|
|
|
|
TEXT_MODES: EsphomeEnumMapper[EsphomeTextMode, TextMode] = EsphomeEnumMapper(
|
|
{
|
|
EsphomeTextMode.TEXT: TextMode.TEXT,
|
|
EsphomeTextMode.PASSWORD: TextMode.PASSWORD,
|
|
}
|
|
)
|
|
|
|
|
|
class EsphomeText(EsphomeEntity[TextInfo, TextState], TextEntity):
|
|
"""A text implementation for esphome."""
|
|
|
|
@callback
|
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
|
"""Set attrs from static info."""
|
|
super()._on_static_info_update(static_info)
|
|
static_info = self._static_info
|
|
self._attr_native_min = static_info.min_length
|
|
self._attr_native_max = static_info.max_length
|
|
self._attr_pattern = static_info.pattern
|
|
self._attr_mode = TEXT_MODES.from_esphome(static_info.mode) or TextMode.TEXT
|
|
|
|
@property
|
|
@esphome_state_property
|
|
def native_value(self) -> str | None:
|
|
"""Return the state of the entity."""
|
|
state = self._state
|
|
if state.missing_state:
|
|
return None
|
|
return state.state
|
|
|
|
async def async_set_value(self, value: str) -> None:
|
|
"""Update the current value."""
|
|
await self._client.text_command(self._key, value)
|