Home Assistant Unofficial Reference 2024.12.1
time.py
Go to the documentation of this file.
1 """Provides time enties for Home Connect."""
2 
3 from datetime import time
4 import logging
5 
6 from homeconnect.api import HomeConnectError
7 
8 from homeassistant.components.time import TimeEntity, TimeEntityDescription
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ServiceValidationError
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import HomeConnectConfigEntry, get_dict_from_home_connect_error
14 from .const import (
15  ATTR_VALUE,
16  DOMAIN,
17  SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID,
18  SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY,
19  SVE_TRANSLATION_PLACEHOLDER_VALUE,
20 )
21 from .entity import HomeConnectEntity
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 TIME_ENTITIES = (
28  key="BSH.Common.Setting.AlarmClock",
29  translation_key="alarm_clock",
30  ),
31 )
32 
33 
35  hass: HomeAssistant,
36  entry: HomeConnectConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up the Home Connect switch."""
40 
41  def get_entities() -> list[HomeConnectTimeEntity]:
42  """Get a list of entities."""
43  return [
44  HomeConnectTimeEntity(device, description)
45  for description in TIME_ENTITIES
46  for device in entry.runtime_data.devices
47  if description.key in device.appliance.status
48  ]
49 
50  async_add_entities(await hass.async_add_executor_job(get_entities), True)
51 
52 
53 def seconds_to_time(seconds: int) -> time:
54  """Convert seconds to a time object."""
55  minutes, sec = divmod(seconds, 60)
56  hours, minutes = divmod(minutes, 60)
57  return time(hour=hours, minute=minutes, second=sec)
58 
59 
60 def time_to_seconds(t: time) -> int:
61  """Convert a time object to seconds."""
62  return t.hour * 3600 + t.minute * 60 + t.second
63 
64 
66  """Time setting class for Home Connect."""
67 
68  async def async_set_value(self, value: time) -> None:
69  """Set the native value of the entity."""
70  _LOGGER.debug(
71  "Tried to set value %s to %s for %s",
72  value,
73  self.bsh_keybsh_key,
74  self.entity_identity_id,
75  )
76  try:
77  await self.hasshass.async_add_executor_job(
78  self.devicedevice.appliance.set_setting,
79  self.bsh_keybsh_key,
80  time_to_seconds(value),
81  )
82  except HomeConnectError as err:
84  translation_domain=DOMAIN,
85  translation_key="set_setting",
86  translation_placeholders={
88  SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.entity_identity_id,
89  SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY: self.bsh_keybsh_key,
90  SVE_TRANSLATION_PLACEHOLDER_VALUE: str(value),
91  },
92  ) from err
93 
94  async def async_update(self) -> None:
95  """Update the Time setting status."""
96  data = self.devicedevice.appliance.status.get(self.bsh_keybsh_key)
97  if data is None:
98  _LOGGER.error("No value for %s", self.bsh_keybsh_key)
99  self._attr_native_value_attr_native_value = None
100  return
101  seconds = data.get(ATTR_VALUE, None)
102  if seconds is not None:
103  self._attr_native_value_attr_native_value = seconds_to_time(seconds)
104  else:
105  self._attr_native_value_attr_native_value = None
106  _LOGGER.debug("Updated, new value: %s", self._attr_native_value_attr_native_value)
list[BaseAprilaireSensor] get_entities(type[BaseAprilaireSensor] entity_class, AprilaireCoordinator coordinator, str unique_id, tuple[AprilaireSensorDescription,...] descriptions)
Definition: sensor.py:64
None async_setup_entry(HomeAssistant hass, HomeConnectConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: time.py:38
dict[str, Any] get_dict_from_home_connect_error(api.HomeConnectError err)
Definition: __init__.py:334
bool time(HomeAssistant hass, dt_time|str|None before=None, dt_time|str|None after=None, str|Container[str]|None weekday=None)
Definition: condition.py:802