From 5944ccffaa718c078d4fd938394706ab87642883 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 5 Oct 2026 09:52:38 +0200 Subject: [PATCH] Move input_text service registration to services module (#184279) Co-authored-by: Claude Opus 5 --- .../components/input_text/__init__.py | 47 +++++++++---------- homeassistant/components/input_text/const.py | 14 ++++++ .../components/input_text/services.py | 38 +++++++++++++++ 3 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 homeassistant/components/input_text/services.py diff --git a/homeassistant/components/input_text/__init__.py b/homeassistant/components/input_text/__init__.py index 93ba65de8905..1a8b2092b975 100644 --- a/homeassistant/components/input_text/__init__.py +++ b/homeassistant/components/input_text/__init__.py @@ -1,5 +1,6 @@ """Support to enter a value into a text box.""" +from dataclasses import dataclass import logging from typing import Any, Self, override @@ -16,19 +17,25 @@ from homeassistant.const import ( # noqa: F401 MAX_LENGTH_STATE_STATE, SERVICE_RELOAD, ) -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import collection, config_validation as cv from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.restore_state import RestoreEntity -import homeassistant.helpers.service from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType, VolDictType -from .const import InputTextEntityStateAttribute +from .const import ( # noqa: F401 + ATTR_VALUE, + CONF_VALUE, + DATA_INPUT_TEXT, + DOMAIN, + SERVICE_SET_VALUE, + InputTextEntityStateAttribute, +) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DOMAIN = "input_text" CONF_INITIAL = "initial" CONF_MIN = "min" @@ -36,17 +43,14 @@ CONF_MIN_VALUE = 0 CONF_MAX = "max" CONF_MAX_VALUE = 100 CONF_PATTERN = "pattern" -CONF_VALUE = "value" MODE_TEXT = "text" MODE_PASSWORD = "password" -ATTR_VALUE = CONF_VALUE ATTR_MIN = "min" ATTR_MAX = "max" ATTR_PATTERN = CONF_PATTERN -SERVICE_SET_VALUE = "set_value" STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 @@ -111,7 +115,14 @@ CONFIG_SCHEMA = probatio.Schema( }, extra=probatio.ALLOW_EXTRA, ) -RELOAD_SERVICE_SCHEMA = probatio.Schema({}) + + +@dataclass(slots=True) +class InputTextData: + """Runtime data for the input_text integration.""" + + component: EntityComponent[InputText] + yaml_collection: collection.YamlCollection async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -144,25 +155,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS ).async_setup(hass) - async def reload_service_handler(service_call: ServiceCall) -> None: - """Reload yaml entities.""" - conf = await component.async_prepare_reload(skip_reset=True) - await yaml_collection.async_load( - [{CONF_ID: id_, **(cfg or {})} for id_, cfg in conf.get(DOMAIN, {}).items()] - ) - - homeassistant.helpers.service.async_register_admin_service( - hass, - DOMAIN, - SERVICE_RELOAD, - reload_service_handler, - schema=RELOAD_SERVICE_SCHEMA, - ) - - component.async_register_entity_service( - SERVICE_SET_VALUE, {probatio.Required(ATTR_VALUE): cv.string}, "async_set_value" - ) + hass.data[DATA_INPUT_TEXT] = InputTextData(component, yaml_collection) + async_setup_services(hass) return True diff --git a/homeassistant/components/input_text/const.py b/homeassistant/components/input_text/const.py index 25760de29eab..a51656cf2b61 100644 --- a/homeassistant/components/input_text/const.py +++ b/homeassistant/components/input_text/const.py @@ -1,6 +1,20 @@ """Constants for the input_text integration.""" from enum import StrEnum +from typing import TYPE_CHECKING, Final + +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from . import InputTextData + +DOMAIN: Final = "input_text" + +DATA_INPUT_TEXT: HassKey[InputTextData] = HassKey(DOMAIN) + +CONF_VALUE: Final = "value" +ATTR_VALUE: Final = CONF_VALUE +SERVICE_SET_VALUE: Final = "set_value" class InputTextEntityStateAttribute(StrEnum): diff --git a/homeassistant/components/input_text/services.py b/homeassistant/components/input_text/services.py new file mode 100644 index 000000000000..d4b57c4c9cfe --- /dev/null +++ b/homeassistant/components/input_text/services.py @@ -0,0 +1,38 @@ +"""Services for the input_text integration.""" + +import probatio + +from homeassistant.const import CONF_ID, SERVICE_RELOAD +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.service import async_register_admin_service + +from .const import ATTR_VALUE, DATA_INPUT_TEXT, DOMAIN, SERVICE_SET_VALUE + +RELOAD_SERVICE_SCHEMA = probatio.Schema({}) + + +async def _async_reload_service(service_call: ServiceCall) -> None: + """Reload yaml entities.""" + hass = service_call.hass + data = hass.data[DATA_INPUT_TEXT] + conf = await data.component.async_prepare_reload(skip_reset=True) + await data.yaml_collection.async_load( + [{CONF_ID: id_, **(cfg or {})} for id_, cfg in conf.get(DOMAIN, {}).items()] + ) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the input_text services.""" + async_register_admin_service( + hass, + DOMAIN, + SERVICE_RELOAD, + _async_reload_service, + schema=RELOAD_SERVICE_SCHEMA, + ) + + hass.data[DATA_INPUT_TEXT].component.async_register_entity_service( + SERVICE_SET_VALUE, {probatio.Required(ATTR_VALUE): cv.string}, "async_set_value" + )