mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
ESPHome Text entities (#102742)
This commit is contained in:
parent
789a00043a
commit
ad692f3341
@ -29,6 +29,7 @@ from aioesphomeapi import (
|
||||
SensorInfo,
|
||||
SensorState,
|
||||
SwitchInfo,
|
||||
TextInfo,
|
||||
TextSensorInfo,
|
||||
UserService,
|
||||
build_unique_id,
|
||||
@ -68,6 +69,7 @@ INFO_TYPE_TO_PLATFORM: dict[type[EntityInfo], Platform] = {
|
||||
SelectInfo: Platform.SELECT,
|
||||
SensorInfo: Platform.SENSOR,
|
||||
SwitchInfo: Platform.SWITCH,
|
||||
TextInfo: Platform.TEXT,
|
||||
TextSensorInfo: Platform.SENSOR,
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
"loggers": ["aioesphomeapi", "noiseprotocol"],
|
||||
"requirements": [
|
||||
"async-interrupt==1.1.1",
|
||||
"aioesphomeapi==18.0.12",
|
||||
"aioesphomeapi==18.1.0",
|
||||
"bluetooth-data-tools==1.13.0",
|
||||
"esphome-dashboard-api==1.2.3"
|
||||
],
|
||||
|
63
homeassistant/components/esphome/text.py
Normal file
63
homeassistant/components/esphome/text.py
Normal file
@ -0,0 +1,63 @@
|
||||
"""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)
|
@ -237,7 +237,7 @@ aioecowitt==2023.5.0
|
||||
aioemonitor==1.0.5
|
||||
|
||||
# homeassistant.components.esphome
|
||||
aioesphomeapi==18.0.12
|
||||
aioesphomeapi==18.1.0
|
||||
|
||||
# homeassistant.components.flo
|
||||
aioflo==2021.11.0
|
||||
|
@ -218,7 +218,7 @@ aioecowitt==2023.5.0
|
||||
aioemonitor==1.0.5
|
||||
|
||||
# homeassistant.components.esphome
|
||||
aioesphomeapi==18.0.12
|
||||
aioesphomeapi==18.1.0
|
||||
|
||||
# homeassistant.components.flo
|
||||
aioflo==2021.11.0
|
||||
|
115
tests/components/esphome/test_text.py
Normal file
115
tests/components/esphome/test_text.py
Normal file
@ -0,0 +1,115 @@
|
||||
"""Test ESPHome texts."""
|
||||
|
||||
from unittest.mock import call
|
||||
|
||||
from aioesphomeapi import APIClient, TextInfo, TextMode as ESPHomeTextMode, TextState
|
||||
|
||||
from homeassistant.components.text import (
|
||||
ATTR_VALUE,
|
||||
DOMAIN as TEXT_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def test_generic_text_entity(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_generic_device_entry,
|
||||
) -> None:
|
||||
"""Test a generic text entity."""
|
||||
entity_info = [
|
||||
TextInfo(
|
||||
object_id="mytext",
|
||||
key=1,
|
||||
name="my text",
|
||||
unique_id="my_text",
|
||||
max_length=100,
|
||||
min_length=0,
|
||||
pattern=None,
|
||||
mode=ESPHomeTextMode.TEXT,
|
||||
)
|
||||
]
|
||||
states = [TextState(key=1, state="hello world")]
|
||||
user_service = []
|
||||
await mock_generic_device_entry(
|
||||
mock_client=mock_client,
|
||||
entity_info=entity_info,
|
||||
user_service=user_service,
|
||||
states=states,
|
||||
)
|
||||
state = hass.states.get("text.test_mytext")
|
||||
assert state is not None
|
||||
assert state.state == "hello world"
|
||||
|
||||
await hass.services.async_call(
|
||||
TEXT_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: "text.test_mytext", ATTR_VALUE: "goodbye"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_client.text_command.assert_has_calls([call(1, "goodbye")])
|
||||
mock_client.text_command.reset_mock()
|
||||
|
||||
|
||||
async def test_generic_text_entity_no_state(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_generic_device_entry,
|
||||
) -> None:
|
||||
"""Test a generic text entity that has no state."""
|
||||
entity_info = [
|
||||
TextInfo(
|
||||
object_id="mytext",
|
||||
key=1,
|
||||
name="my text",
|
||||
unique_id="my_text",
|
||||
max_length=100,
|
||||
min_length=0,
|
||||
pattern=None,
|
||||
mode=ESPHomeTextMode.TEXT,
|
||||
)
|
||||
]
|
||||
states = []
|
||||
user_service = []
|
||||
await mock_generic_device_entry(
|
||||
mock_client=mock_client,
|
||||
entity_info=entity_info,
|
||||
user_service=user_service,
|
||||
states=states,
|
||||
)
|
||||
state = hass.states.get("text.test_mytext")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_generic_text_entity_missing_state(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_generic_device_entry,
|
||||
) -> None:
|
||||
"""Test a generic text entity that has no state."""
|
||||
entity_info = [
|
||||
TextInfo(
|
||||
object_id="mytext",
|
||||
key=1,
|
||||
name="my text",
|
||||
unique_id="my_text",
|
||||
max_length=100,
|
||||
min_length=0,
|
||||
pattern=None,
|
||||
mode=ESPHomeTextMode.TEXT,
|
||||
)
|
||||
]
|
||||
states = [TextState(key=1, state="", missing_state=True)]
|
||||
user_service = []
|
||||
await mock_generic_device_entry(
|
||||
mock_client=mock_client,
|
||||
entity_info=entity_info,
|
||||
user_service=user_service,
|
||||
states=states,
|
||||
)
|
||||
state = hass.states.get("text.test_mytext")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNKNOWN
|
Loading…
x
Reference in New Issue
Block a user