diff --git a/homeassistant/components/datetime/__init__.py b/homeassistant/components/datetime/__init__.py index b5c4f7df9450..794bae68ddc5 100644 --- a/homeassistant/components/datetime/__init__.py +++ b/homeassistant/components/datetime/__init__.py @@ -4,23 +4,25 @@ from datetime import UTC, datetime, timedelta import logging from typing import final, override -import probatio from propcache.api import cached_property from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant, ServiceCall +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 import dt as dt_util -from homeassistant.util.hass_dict import HassKey -from .const import ATTR_DATETIME, DOMAIN, SERVICE_SET_VALUE +from .const import ( # noqa: F401 + ATTR_DATETIME, + DATA_COMPONENT, + DOMAIN, + SERVICE_SET_VALUE, +) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[DateTimeEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE @@ -30,14 +32,6 @@ SCAN_INTERVAL = timedelta(seconds=30) __all__ = ["ATTR_DATETIME", "DOMAIN", "DateTimeEntity", "DateTimeEntityDescription"] -async def _async_set_value(entity: DateTimeEntity, service_call: ServiceCall) -> None: - """Service call wrapper to set a new date/time.""" - value: datetime = service_call.data[ATTR_DATETIME] - if value.tzinfo is None: - value = value.replace(tzinfo=dt_util.get_default_time_zone()) - return await entity.async_set_value(value) - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Date/Time entities.""" component = hass.data[DATA_COMPONENT] = EntityComponent[DateTimeEntity]( @@ -45,13 +39,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_DATETIME): cv.datetime, - }, - _async_set_value, - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/datetime/const.py b/homeassistant/components/datetime/const.py index 983bf75e2436..aa532ce4ff79 100644 --- a/homeassistant/components/datetime/const.py +++ b/homeassistant/components/datetime/const.py @@ -1,8 +1,16 @@ """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 DateTimeEntity DOMAIN: Final = "datetime" +DATA_COMPONENT: HassKey[EntityComponent[DateTimeEntity]] = HassKey(DOMAIN) ATTR_DATETIME = "datetime" diff --git a/homeassistant/components/datetime/services.py b/homeassistant/components/datetime/services.py new file mode 100644 index 000000000000..9e47a75bc64e --- /dev/null +++ b/homeassistant/components/datetime/services.py @@ -0,0 +1,35 @@ +"""Services for the Date/Time integration.""" + +from datetime import datetime +from typing import TYPE_CHECKING + +import probatio + +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.helpers import config_validation as cv +from homeassistant.util import dt as dt_util + +from .const import ATTR_DATETIME, DATA_COMPONENT, SERVICE_SET_VALUE + +if TYPE_CHECKING: + from . import DateTimeEntity + + +async def _async_set_value(entity: DateTimeEntity, service_call: ServiceCall) -> None: + """Service call wrapper to set a new date/time.""" + value: datetime = service_call.data[ATTR_DATETIME] + if value.tzinfo is None: + value = value.replace(tzinfo=dt_util.get_default_time_zone()) + return await entity.async_set_value(value) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the date/time services.""" + hass.data[DATA_COMPONENT].async_register_entity_service( + SERVICE_SET_VALUE, + { + probatio.Required(ATTR_DATETIME): cv.datetime, + }, + _async_set_value, + )