From 082ba4b922c55eebaa3f34548be337344551a173 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 21 Sep 2026 18:45:32 +0200 Subject: [PATCH] Move time service registration to services module (#182823) --- homeassistant/components/time/__init__.py | 19 +++++----------- homeassistant/components/time/const.py | 10 ++++++++- homeassistant/components/time/services.py | 27 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 homeassistant/components/time/services.py diff --git a/homeassistant/components/time/__init__.py b/homeassistant/components/time/__init__.py index 56a9e9e81c78..fa7fb8c292dc 100644 --- a/homeassistant/components/time/__init__.py +++ b/homeassistant/components/time/__init__.py @@ -4,23 +4,21 @@ from datetime import time, timedelta import logging from typing import final, override -import probatio from propcache.api import cached_property from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_TIME -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.const import ATTR_TIME # noqa: F401 +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.typing import ConfigType -from homeassistant.util.hass_dict import HassKey -from .const import DOMAIN, SERVICE_SET_VALUE +from .const import DATA_COMPONENT, DOMAIN, SERVICE_SET_VALUE # noqa: F401 +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[TimeEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE @@ -30,11 +28,6 @@ SCAN_INTERVAL = timedelta(seconds=30) __all__ = ["DOMAIN", "TimeEntity", "TimeEntityDescription"] -async def _async_set_value(entity: TimeEntity, service_call: ServiceCall) -> None: - """Service call wrapper to set a new date.""" - return await entity.async_set_value(service_call.data[ATTR_TIME]) - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Time entities.""" component = hass.data[DATA_COMPONENT] = EntityComponent[TimeEntity]( @@ -42,9 +35,7 @@ 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_TIME): cv.time}, _async_set_value - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/time/const.py b/homeassistant/components/time/const.py index 441f87a914a9..d1fba65fea64 100644 --- a/homeassistant/components/time/const.py +++ b/homeassistant/components/time/const.py @@ -1,7 +1,15 @@ """Provides the constants needed for the component.""" -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 TimeEntity DOMAIN: Final = "time" +DATA_COMPONENT: HassKey[EntityComponent[TimeEntity]] = HassKey(DOMAIN) SERVICE_SET_VALUE = "set_value" diff --git a/homeassistant/components/time/services.py b/homeassistant/components/time/services.py new file mode 100644 index 000000000000..bf9dc6b0b4fc --- /dev/null +++ b/homeassistant/components/time/services.py @@ -0,0 +1,27 @@ +"""Services for the Time integration.""" + +from typing import TYPE_CHECKING + +import probatio + +from homeassistant.const import ATTR_TIME +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.helpers import config_validation as cv + +from .const import DATA_COMPONENT, SERVICE_SET_VALUE + +if TYPE_CHECKING: + from . import TimeEntity + + +async def _async_set_value(entity: TimeEntity, service_call: ServiceCall) -> None: + """Service call wrapper to set a new date.""" + return await entity.async_set_value(service_call.data[ATTR_TIME]) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the time services.""" + hass.data[DATA_COMPONENT].async_register_entity_service( + SERVICE_SET_VALUE, {probatio.Required(ATTR_TIME): cv.time}, _async_set_value + )