Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Component to allow setting time as platforms."""
2 
3 from __future__ import annotations
4 
5 from datetime import time, 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_TIME
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[TimeEntity]] = 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", "TimeEntity", "TimeEntityDescription"]
33 
34 
35 async def _async_set_value(entity: TimeEntity, service_call: ServiceCall) -> None:
36  """Service call wrapper to set a new date."""
37  return await entity.async_set_value(service_call.data[ATTR_TIME])
38 
39 
40 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
41  """Set up Time entities."""
42  component = hass.data[DATA_COMPONENT] = EntityComponent[TimeEntity](
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_TIME): cv.time}, _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 TimeEntityDescription(EntityDescription, frozen_or_thawed=True):
65  """A class that describes time entities."""
66 
67 
68 CACHED_PROPERTIES_WITH_ATTR_ = {"native_value"}
69 
70 
71 class TimeEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
72  """Representation of a Time entity."""
73 
74  entity_description: TimeEntityDescription
75  _attr_native_value: time | None
76  _attr_device_class: None = 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) -> time | None:
101  """Return the value reported by the time."""
102  return self._attr_native_value
103 
104  def set_value(self, value: time) -> None:
105  """Change the time."""
106  raise NotImplementedError
107 
108  async def async_set_value(self, value: time) -> None:
109  """Change the time."""
110  await self.hasshass.async_add_executor_job(self.set_valueset_value, value)
None set_value(self, time value)
Definition: __init__.py:104
None async_set_value(self, time value)
Definition: __init__.py:108
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:54
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:59
None _async_set_value(TimeEntity entity, ServiceCall service_call)
Definition: __init__.py:35
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:40