From 91fabb4644947b35946d2acb4812781156180472 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:10:06 +0200 Subject: [PATCH] Move template service registration to services module (#184319) Co-authored-by: Claude Opus 5 --- homeassistant/components/template/__init__.py | 50 +++---------------- homeassistant/components/template/services.py | 48 ++++++++++++++++++ tests/components/template/test_blueprint.py | 4 +- tests/components/template/test_config.py | 3 +- 4 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 homeassistant/components/template/services.py diff --git a/homeassistant/components/template/__init__.py b/homeassistant/components/template/__init__.py index 5a1b2e3c65a0..4c958bec5d83 100644 --- a/homeassistant/components/template/__init__.py +++ b/homeassistant/components/template/__init__.py @@ -2,27 +2,17 @@ import logging -from homeassistant import config as conf_util from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_DEVICE_ID, CONF_NAME, SERVICE_RELOAD -from homeassistant.core import Event, HomeAssistant, ServiceCall -from homeassistant.exceptions import ConfigEntryError, HomeAssistantError +from homeassistant.const import CONF_DEVICE_ID, CONF_NAME +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryError from homeassistant.helpers import device_registry as dr, issue_registry as ir from homeassistant.helpers.helper_integration import async_remove_helper_devices -from homeassistant.helpers.reload import async_reload_integration_platforms -from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.typing import ConfigType -from homeassistant.loader import async_get_integration -from .const import ( - CONF_ADDITIONAL_OPTIONS, - CONF_MAX, - CONF_MIN, - CONF_STEP, - DOMAIN, - PLATFORMS, -) +from .const import CONF_ADDITIONAL_OPTIONS, CONF_MAX, CONF_MIN, CONF_STEP, DOMAIN from .helpers import async_get_blueprints, process_config +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -43,35 +33,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if DOMAIN in config: await process_config(hass, config) - async def _reload_config(call: Event | ServiceCall) -> None: - """Reload top-level + platforms.""" - - await async_get_blueprints(hass).async_reset_cache() - try: - unprocessed_conf = await conf_util.async_hass_config_yaml(hass) - except HomeAssistantError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="failed_to_reload_template_entities", - translation_placeholders={"error": str(err)}, - ) from err - - integration = await async_get_integration(hass, DOMAIN) - conf = await conf_util.async_process_component_and_handle_errors( - hass, unprocessed_conf, integration - ) - - if conf is None: - return - - await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS) - - if DOMAIN in conf: - await process_config(hass, conf) - - hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) - - async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config) + async_setup_services(hass) return True diff --git a/homeassistant/components/template/services.py b/homeassistant/components/template/services.py new file mode 100644 index 000000000000..ab4707099776 --- /dev/null +++ b/homeassistant/components/template/services.py @@ -0,0 +1,48 @@ +"""Services for the template integration.""" + +from homeassistant import config as conf_util +from homeassistant.const import SERVICE_RELOAD +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.reload import async_reload_integration_platforms +from homeassistant.helpers.service import async_register_admin_service +from homeassistant.loader import async_get_integration + +from .const import DOMAIN, PLATFORMS +from .helpers import async_get_blueprints, process_config + + +async def _async_reload_config(call: ServiceCall) -> None: + """Reload top-level + platforms.""" + hass = call.hass + + await async_get_blueprints(hass).async_reset_cache() + try: + unprocessed_conf = await conf_util.async_hass_config_yaml(hass) + except HomeAssistantError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="failed_to_reload_template_entities", + translation_placeholders={"error": str(err)}, + ) from err + + integration = await async_get_integration(hass, DOMAIN) + conf = await conf_util.async_process_component_and_handle_errors( + hass, unprocessed_conf, integration + ) + + if conf is None: + return + + await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS) + + if DOMAIN in conf: + await process_config(hass, conf) + + hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the template services.""" + async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _async_reload_config) diff --git a/tests/components/template/test_blueprint.py b/tests/components/template/test_blueprint.py index 79e563202f4e..b075641c743a 100644 --- a/tests/components/template/test_blueprint.py +++ b/tests/components/template/test_blueprint.py @@ -15,7 +15,7 @@ from homeassistant.components.blueprint import ( BlueprintInUse, DomainBlueprints, ) -from homeassistant.components.template import DOMAIN, SERVICE_RELOAD +from homeassistant.components.template import DOMAIN from homeassistant.components.template.config import ( ALARM_CONTROL_PANEL_DOMAIN, BINARY_SENSOR_DOMAIN, @@ -31,7 +31,7 @@ from homeassistant.components.template.config import ( VACUUM_DOMAIN, WEATHER_DOMAIN, ) -from homeassistant.const import STATE_ON +from homeassistant.const import SERVICE_RELOAD, STATE_ON from homeassistant.core import Context, HomeAssistant, callback from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component diff --git a/tests/components/template/test_config.py b/tests/components/template/test_config.py index d0a7945925ea..e245d0fb6691 100644 --- a/tests/components/template/test_config.py +++ b/tests/components/template/test_config.py @@ -3,11 +3,12 @@ import probatio import pytest -from homeassistant.components.template import DOMAIN, PLATFORMS +from homeassistant.components.template import DOMAIN from homeassistant.components.template.config import ( CONFIG_SECTION_SCHEMA, async_validate_config_section, ) +from homeassistant.components.template.const import PLATFORMS from homeassistant.core import HomeAssistant from homeassistant.helpers import ( device_registry as dr,