Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Component to allow setting date as platforms."""
2 
3 from __future__ import annotations
4 
5 from datetime import date, timedelta
6 import logging
7 from typing import final
8 
9 from propcache import cached_property
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import ATTR_DATE
14 from homeassistant.core import HomeAssistant, ServiceCall
15 from homeassistant.helpers import config_validation as cv
16 from homeassistant.helpers.entity import Entity, EntityDescription
17 from homeassistant.helpers.entity_component import EntityComponent
18 from homeassistant.helpers.typing import ConfigType
19 from homeassistant.util.hass_dict import HassKey
20 
21 from .const import DOMAIN, SERVICE_SET_VALUE
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 DATA_COMPONENT: HassKey[EntityComponent[DateEntity]] = HassKey(DOMAIN)
26 ENTITY_ID_FORMAT = DOMAIN + ".{}"
27 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
28 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
29 SCAN_INTERVAL = timedelta(seconds=30)
30 
31 
32 __all__ = ["DOMAIN", "DateEntity", "DateEntityDescription"]
33 
34 
35 async def _async_set_value(entity: DateEntity, service_call: ServiceCall) -> None:
36  """Service call wrapper to set a new date."""
37  return await entity.async_set_value(service_call.data[ATTR_DATE])
38 
39 
40 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
41  """Set up Date entities."""
42  component = hass.data[DATA_COMPONENT] = EntityComponent[DateEntity](
43  _LOGGER, DOMAIN, hass, SCAN_INTERVAL
44  )
45  await component.async_setup(config)
46 
47  component.async_register_entity_service(
48  SERVICE_SET_VALUE, {vol.Required(ATTR_DATE): cv.date}, _async_set_value
49  )
50 
51  return True
52 
53 
54 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
55  """Set up a config entry."""
56  return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
57 
58 
59 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
60  """Unload a config entry."""
61  return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
62 
63 
64 class DateEntityDescription(EntityDescription, frozen_or_thawed=True):
65  """A class that describes date entities."""
66 
67 
68 CACHED_PROPERTIES_WITH_ATTR_ = {"native_value"}
69 
70 
71 class DateEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
72  """Representation of a Date entity."""
73 
74  entity_description: DateEntityDescription
75  _attr_device_class: None
76  _attr_native_value: date | None
77  _attr_state: None = None
78 
79  @cached_property
80  @final
81  def device_class(self) -> None:
82  """Return the device class for the entity."""
83  return None
84 
85  @cached_property
86  @final
87  def state_attributes(self) -> None:
88  """Return the state attributes."""
89  return None
90 
91  @property
92  @final
93  def state(self) -> str | None:
94  """Return the entity state."""
95  if self.native_valuenative_value is None:
96  return None
97  return self.native_valuenative_value.isoformat()
98 
99  @cached_property
100  def native_value(self) -> date | None:
101  """Return the value reported by the date."""
102  return self._attr_native_value
103 
104  def set_value(self, value: date) -> None:
105  """Change the date."""
106  raise NotImplementedError
107 
108  async def async_set_value(self, value: date) -> None:
109  """Change the date."""
110  await self.hasshass.async_add_executor_job(self.set_valueset_value, value)
None set_value(self, date value)
Definition: __init__.py:104
None async_set_value(self, date value)
Definition: __init__.py:108
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:59
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:40
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:54
None _async_set_value(DateEntity entity, ServiceCall service_call)
Definition: __init__.py:35