Move template service registration to services module (#184319)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
epenet
2026-10-05 15:10:06 +02:00
committed by GitHub
co-authored by Claude Opus 5
parent d402b23d29
commit 91fabb4644
4 changed files with 58 additions and 47 deletions
+6 -44
View File
@@ -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
@@ -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)
+2 -2
View File
@@ -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
+2 -1
View File
@@ -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,