Move text service registration to services module (#182824)

This commit is contained in:
epenet
2026-09-21 18:43:01 +02:00
committed by GitHub
parent 7fe792f1b9
commit a0338b5a82
4 changed files with 58 additions and 32 deletions
+4 -30
View File
@@ -7,32 +7,31 @@ import logging
import re
from typing import Any, final, override
import probatio
from propcache.api import cached_property
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_MODE, MAX_LENGTH_STATE_STATE # noqa: F401
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import ExtraStoredData, RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.hass_dict import HassKey
from .const import ( # noqa: F401
ATTR_MAX,
ATTR_MIN,
ATTR_PATTERN,
ATTR_VALUE,
DATA_COMPONENT,
DOMAIN,
SERVICE_SET_VALUE,
TextEntityCapabilityAttribute,
)
from .services import async_setup_services
_LOGGER = logging.getLogger(__name__)
DATA_COMPONENT: HassKey[EntityComponent[TextEntity]] = HassKey(DOMAIN)
ENTITY_ID_FORMAT = DOMAIN + ".{}"
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
@@ -57,36 +56,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
)
await component.async_setup(config)
component.async_register_entity_service(
SERVICE_SET_VALUE,
{probatio.Required(ATTR_VALUE): cv.string},
_async_set_value,
)
async_setup_services(hass)
return True
async def _async_set_value(entity: TextEntity, service_call: ServiceCall) -> None:
"""Service call wrapper to set a new value."""
value = service_call.data[ATTR_VALUE]
if len(value) < entity.min:
raise ValueError(
f"Value {value} for {entity.entity_id} is too short (minimum length"
f" {entity.min})"
)
if len(value) > entity.max:
raise ValueError(
f"Value {value} for {entity.entity_id}"
f" is too long (maximum length {entity.max})"
)
if entity.pattern_cmp and not entity.pattern_cmp.match(value):
raise ValueError(
f"Value {value} for {entity.entity_id}"
f" doesn't match pattern {entity.pattern}"
)
await entity.async_set_value(value)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry."""
return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
+9 -1
View File
@@ -1,9 +1,17 @@
"""Provides the constants needed for the component."""
from enum import StrEnum
from typing import Final
from typing import TYPE_CHECKING, Final
from homeassistant.util.hass_dict import HassKey
if TYPE_CHECKING:
from homeassistant.helpers.entity_component import EntityComponent
from . import TextEntity
DOMAIN: Final = "text"
DATA_COMPONENT: HassKey[EntityComponent[TextEntity]] = HassKey(DOMAIN)
class TextEntityCapabilityAttribute(StrEnum):
+44
View File
@@ -0,0 +1,44 @@
"""Services for the Text integration."""
from typing import TYPE_CHECKING
import probatio
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv
from .const import ATTR_VALUE, DATA_COMPONENT, SERVICE_SET_VALUE
if TYPE_CHECKING:
from . import TextEntity
async def _async_set_value(entity: TextEntity, service_call: ServiceCall) -> None:
"""Service call wrapper to set a new value."""
value = service_call.data[ATTR_VALUE]
if len(value) < entity.min:
raise ValueError(
f"Value {value} for {entity.entity_id} is too short (minimum length"
f" {entity.min})"
)
if len(value) > entity.max:
raise ValueError(
f"Value {value} for {entity.entity_id}"
f" is too long (maximum length {entity.max})"
)
if entity.pattern_cmp and not entity.pattern_cmp.match(value):
raise ValueError(
f"Value {value} for {entity.entity_id}"
f" doesn't match pattern {entity.pattern}"
)
await entity.async_set_value(value)
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Register the text services."""
hass.data[DATA_COMPONENT].async_register_entity_service(
SERVICE_SET_VALUE,
{probatio.Required(ATTR_VALUE): cv.string},
_async_set_value,
)
+1 -1
View File
@@ -13,8 +13,8 @@ from homeassistant.components.text import (
DOMAIN,
SERVICE_SET_VALUE,
TextMode,
_async_set_value,
)
from homeassistant.components.text.services import _async_set_value
from homeassistant.const import MAX_LENGTH_STATE_STATE
from homeassistant.core import HomeAssistant, ServiceCall, State
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY