Move datetime service registration to services module (#182822)

This commit is contained in:
epenet
2026-09-21 18:41:43 +02:00
committed by GitHub
parent 256bb25591
commit c2bda8931b
3 changed files with 53 additions and 22 deletions
+9 -21
View File
@@ -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
+9 -1
View File
@@ -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"
@@ -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,
)