1 """Component to allow setting date/time as platforms."""
3 from __future__
import annotations
5 from datetime
import UTC, datetime, timedelta
7 from typing
import final
9 from propcache
import cached_property
10 import voluptuous
as vol
21 from .const
import ATTR_DATETIME, DOMAIN, SERVICE_SET_VALUE
23 _LOGGER = logging.getLogger(__name__)
25 DATA_COMPONENT: HassKey[EntityComponent[DateTimeEntity]] =
HassKey(DOMAIN)
26 ENTITY_ID_FORMAT = DOMAIN +
".{}"
27 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
28 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
32 __all__ = [
"ATTR_DATETIME",
"DOMAIN",
"DateTimeEntity",
"DateTimeEntityDescription"]
36 """Service call wrapper to set a new date/time."""
37 value: datetime = service_call.data[ATTR_DATETIME]
38 if value.tzinfo
is None:
39 value = value.replace(tzinfo=dt_util.get_default_time_zone())
40 return await entity.async_set_value(value)
43 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
44 """Set up Date/Time entities."""
45 component = hass.data[DATA_COMPONENT] = EntityComponent[DateTimeEntity](
46 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
48 await component.async_setup(config)
50 component.async_register_entity_service(
53 vol.Required(ATTR_DATETIME): cv.datetime,
62 """Set up a config entry."""
67 """Unload a config entry."""
72 """A class that describes date/time entities."""
75 CACHED_PROPERTIES_WITH_ATTR_ = {
81 """Representation of a Date/time entity."""
83 entity_description: DateTimeEntityDescription
84 _attr_device_class:
None =
None
85 _attr_state:
None =
None
86 _attr_native_value: datetime |
None
91 """Return entity device class."""
97 """Return the state attributes."""
103 """Return the entity state."""
106 if value.tzinfo
is None:
108 f
"Invalid datetime: {self.entity_id} provides state '{value}', "
109 "which is missing timezone information"
112 return value.astimezone(UTC).isoformat(timespec=
"seconds")
116 """Return the value reported by the datetime."""
117 return self._attr_native_value
120 """Change the date/time."""
121 raise NotImplementedError
124 """Change the date/time."""
125 await self.
hasshass.async_add_executor_job(self.
set_valueset_value, value)
None async_set_value(self, datetime value)
datetime|None native_value(self)
None set_value(self, datetime value)
None state_attributes(self)
None _async_set_value(DateTimeEntity entity, ServiceCall service_call)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)