Move date service registration to services module (#182821)

This commit is contained in:
epenet
2026-09-21 18:43:30 +02:00
committed by GitHub
parent a0338b5a82
commit ed63699a4b
3 changed files with 41 additions and 15 deletions
+5 -14
View File
@@ -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
+9 -1
View File
@@ -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"
+27
View File
@@ -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
)