1 """Support for Jewish Calendar binary sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from datetime
import datetime
11 from hdate.zmanim
import Zmanim
15 BinarySensorEntityDescription,
23 from .entity
import JewishCalendarConfigEntry, JewishCalendarEntity
26 @dataclass(frozen=True)
28 """Binary Sensor description mixin class for Jewish Calendar."""
30 is_on: Callable[[Zmanim], bool] =
lambda _:
False
33 @dataclass(frozen=True)
35 JewishCalendarBinarySensorMixIns, BinarySensorEntityDescription
37 """Binary Sensor Entity description for Jewish Calendar."""
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),
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,
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,
64 config_entry: JewishCalendarConfigEntry,
65 async_add_entities: AddEntitiesCallback,
67 """Set up the Jewish Calendar binary sensors."""
70 for description
in BINARY_SENSORS
75 """Representation of an Jewish Calendar binary sensor."""
77 _attr_should_poll =
False
78 _attr_entity_category = EntityCategory.DIAGNOSTIC
79 _update_unsub: CALLBACK_TYPE |
None =
None
81 entity_description: JewishCalendarBinarySensorEntityDescription
85 """Return true if sensor is on."""
90 """Return the Zmanim object for now()."""
100 """Run when entity about to be added to hass."""
105 """Run when entity will be removed from hass."""
112 def _update(self, now: datetime |
None =
None) ->
None:
113 """Update the state of the sensor."""
119 """Schedule the next update of the sensor."""
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:
131 self.
_update_unsub_update_unsub = event.async_track_point_in_time(
None async_will_remove_from_hass(self)
None _update(self, datetime|None now=None)
None async_added_to_hass(self)
None _schedule_update(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, JewishCalendarConfigEntry config_entry, AddEntitiesCallback async_add_entities)