From ed63699a4b024c519dba23364c4b10cbe4654557 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 21 Sep 2026 18:43:30 +0200 Subject: [PATCH] Move date service registration to services module (#182821) --- homeassistant/components/date/__init__.py | 19 +++++----------- homeassistant/components/date/const.py | 10 ++++++++- homeassistant/components/date/services.py | 27 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 homeassistant/components/date/services.py diff --git a/homeassistant/components/date/__init__.py b/homeassistant/components/date/__init__.py index a1a2e8cb0f48..ab675f2960bf 100644 --- a/homeassistant/components/date/__init__.py +++ b/homeassistant/components/date/__init__.py @@ -4,23 +4,21 @@ from datetime import date, 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_DATE -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.const import ATTR_DATE # 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[DateEntity]] = 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", "DateEntity", "DateEntityDescription"] -async def _async_set_value(entity: DateEntity, service_call: ServiceCall) -> None: - """Service call wrapper to set a new date.""" - return await entity.async_set_value(service_call.data[ATTR_DATE]) - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Date entities.""" component = hass.data[DATA_COMPONENT] = EntityComponent[DateEntity]( @@ -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_DATE): cv.date}, _async_set_value - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/date/const.py b/homeassistant/components/date/const.py index 48eef31bed2d..e229e980eef5 100644 --- a/homeassistant/components/date/const.py +++ b/homeassistant/components/date/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 DateEntity DOMAIN: Final = "date" +DATA_COMPONENT: HassKey[EntityComponent[DateEntity]] = HassKey(DOMAIN) SERVICE_SET_VALUE = "set_value" diff --git a/homeassistant/components/date/services.py b/homeassistant/components/date/services.py new file mode 100644 index 000000000000..057bf1d54593 --- /dev/null +++ b/homeassistant/components/date/services.py @@ -0,0 +1,27 @@ +"""Services for the Date integration.""" + +from typing import TYPE_CHECKING + +import probatio + +from homeassistant.const import ATTR_DATE +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 DateEntity + + +async def _async_set_value(entity: DateEntity, service_call: ServiceCall) -> None: + """Service call wrapper to set a new date.""" + return await entity.async_set_value(service_call.data[ATTR_DATE]) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the date services.""" + hass.data[DATA_COMPONENT].async_register_entity_service( + SERVICE_SET_VALUE, {probatio.Required(ATTR_DATE): cv.date}, _async_set_value + )