Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Jewish Calendar binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import datetime as dt
8 from datetime import datetime
9 
10 import hdate
11 from hdate.zmanim import Zmanim
12 
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
19 from homeassistant.helpers import event
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 import homeassistant.util.dt as dt_util
22 
23 from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
24 
25 
26 @dataclass(frozen=True)
28  """Binary Sensor description mixin class for Jewish Calendar."""
29 
30  is_on: Callable[[Zmanim], bool] = lambda _: False
31 
32 
33 @dataclass(frozen=True)
35  JewishCalendarBinarySensorMixIns, BinarySensorEntityDescription
36 ):
37  """Binary Sensor Entity description for Jewish Calendar."""
38 
39 
40 BINARY_SENSORS: tuple[JewishCalendarBinarySensorEntityDescription, ...] = (
42  key="issur_melacha_in_effect",
43  name="Issur Melacha in Effect",
44  icon="mdi:power-plug-off",
45  is_on=lambda state: bool(state.issur_melacha_in_effect),
46  ),
48  key="erev_shabbat_hag",
49  name="Erev Shabbat/Hag",
50  is_on=lambda state: bool(state.erev_shabbat_chag),
51  entity_registry_enabled_default=False,
52  ),
54  key="motzei_shabbat_hag",
55  name="Motzei Shabbat/Hag",
56  is_on=lambda state: bool(state.motzei_shabbat_chag),
57  entity_registry_enabled_default=False,
58  ),
59 )
60 
61 
63  hass: HomeAssistant,
64  config_entry: JewishCalendarConfigEntry,
65  async_add_entities: AddEntitiesCallback,
66 ) -> None:
67  """Set up the Jewish Calendar binary sensors."""
69  JewishCalendarBinarySensor(config_entry, description)
70  for description in BINARY_SENSORS
71  )
72 
73 
75  """Representation of an Jewish Calendar binary sensor."""
76 
77  _attr_should_poll = False
78  _attr_entity_category = EntityCategory.DIAGNOSTIC
79  _update_unsub: CALLBACK_TYPE | None = None
80 
81  entity_description: JewishCalendarBinarySensorEntityDescription
82 
83  @property
84  def is_on(self) -> bool:
85  """Return true if sensor is on."""
86  zmanim = self._get_zmanim_get_zmanim()
87  return self.entity_descriptionentity_description.is_on(zmanim)
88 
89  def _get_zmanim(self) -> Zmanim:
90  """Return the Zmanim object for now()."""
91  return hdate.Zmanim(
92  date=dt_util.now(),
93  location=self._location_location,
94  candle_lighting_offset=self._candle_lighting_offset_candle_lighting_offset,
95  havdalah_offset=self._havdalah_offset_havdalah_offset,
96  hebrew=self._hebrew_hebrew,
97  )
98 
99  async def async_added_to_hass(self) -> None:
100  """Run when entity about to be added to hass."""
101  await super().async_added_to_hass()
102  self._schedule_update_schedule_update()
103 
104  async def async_will_remove_from_hass(self) -> None:
105  """Run when entity will be removed from hass."""
106  if self._update_unsub_update_unsub:
107  self._update_unsub_update_unsub()
108  self._update_unsub_update_unsub = None
109  return await super().async_will_remove_from_hass()
110 
111  @callback
112  def _update(self, now: datetime | None = None) -> None:
113  """Update the state of the sensor."""
114  self._update_unsub_update_unsub = None
115  self._schedule_update_schedule_update()
116  self.async_write_ha_stateasync_write_ha_state()
117 
118  def _schedule_update(self) -> None:
119  """Schedule the next update of the sensor."""
120  now = dt_util.now()
121  zmanim = self._get_zmanim_get_zmanim()
122  update = zmanim.zmanim["sunrise"] + dt.timedelta(days=1)
123  candle_lighting = zmanim.candle_lighting
124  if candle_lighting is not None and now < candle_lighting < update:
125  update = candle_lighting
126  havdalah = zmanim.havdalah
127  if havdalah is not None and now < havdalah < update:
128  update = havdalah
129  if self._update_unsub_update_unsub:
130  self._update_unsub_update_unsub()
131  self._update_unsub_update_unsub = event.async_track_point_in_time(
132  self.hasshass, self._update_update, update
133  )
None async_setup_entry(HomeAssistant hass, JewishCalendarConfigEntry config_entry, AddEntitiesCallback async_add_entities)