diff --git a/homeassistant/components/text/__init__.py b/homeassistant/components/text/__init__.py index cee4d3c1e907..fe654986e553 100644 --- a/homeassistant/components/text/__init__.py +++ b/homeassistant/components/text/__init__.py @@ -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) diff --git a/homeassistant/components/text/const.py b/homeassistant/components/text/const.py index b0f52a9b6983..8056df4eb7b6 100644 --- a/homeassistant/components/text/const.py +++ b/homeassistant/components/text/const.py @@ -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): diff --git a/homeassistant/components/text/services.py b/homeassistant/components/text/services.py new file mode 100644 index 000000000000..ff0a62562fc8 --- /dev/null +++ b/homeassistant/components/text/services.py @@ -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, + ) diff --git a/tests/components/text/test_init.py b/tests/components/text/test_init.py index 1dda5c3fb771..f1e809b5e20b 100644 --- a/tests/components/text/test_init.py +++ b/tests/components/text/test_init.py @@ -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