mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 01:11:51 -04:00
Move time service registration to services module (#182823)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
)
|
||||
Reference in New Issue
Block a user