1 """Support for Jewish calendar sensors."""
3 from __future__
import annotations
5 from datetime
import date
as Date
7 from typing
import Any, cast
9 from hdate
import HDate, HebrewDate, htables
10 from hdate.zmanim
import Zmanim
15 SensorEntityDescription,
23 from .entity
import JewishCalendarConfigEntry, JewishCalendarEntity
25 _LOGGER = logging.getLogger(__name__)
27 INFO_SENSORS: tuple[SensorEntityDescription, ...] = (
31 icon=
"mdi:star-david",
32 translation_key=
"hebrew_date",
36 name=
"Parshat Hashavua",
37 icon=
"mdi:book-open-variant",
38 device_class=SensorDeviceClass.ENUM,
43 icon=
"mdi:calendar-star",
44 device_class=SensorDeviceClass.ENUM,
48 name=
"Day of the Omer",
50 entity_registry_enabled_default=
False,
55 icon=
"mdi:book-open-variant",
56 entity_registry_enabled_default=
False,
60 TIME_SENSORS: tuple[SensorEntityDescription, ...] = (
63 name=
"Alot Hashachar",
64 icon=
"mdi:weather-sunset-up",
65 entity_registry_enabled_default=
False,
69 name=
"Talit and Tefillin",
70 icon=
"mdi:calendar-clock",
71 entity_registry_enabled_default=
False,
75 name=
"Hanetz Hachama",
76 icon=
"mdi:calendar-clock",
80 name=
'Latest time for Shma Gr"a',
81 icon=
"mdi:calendar-clock",
82 entity_registry_enabled_default=
False,
86 name=
'Latest time for Shma MG"A',
87 icon=
"mdi:calendar-clock",
88 entity_registry_enabled_default=
False,
92 name=
'Latest time for Tefilla Gr"a',
93 icon=
"mdi:calendar-clock",
94 entity_registry_enabled_default=
False,
98 name=
'Latest time for Tefilla MG"A',
99 icon=
"mdi:calendar-clock",
100 entity_registry_enabled_default=
False,
104 name=
"Chatzot Hayom",
105 icon=
"mdi:calendar-clock",
106 entity_registry_enabled_default=
False,
110 name=
"Mincha Gedola",
111 icon=
"mdi:calendar-clock",
112 entity_registry_enabled_default=
False,
116 name=
"Mincha Ketana",
117 icon=
"mdi:calendar-clock",
118 entity_registry_enabled_default=
False,
122 name=
"Plag Hamincha",
123 icon=
"mdi:weather-sunset-down",
124 entity_registry_enabled_default=
False,
129 icon=
"mdi:weather-sunset",
133 name=
"T'set Hakochavim",
134 icon=
"mdi:weather-night",
135 entity_registry_enabled_default=
False,
139 name=
"T'set Hakochavim, 3 stars",
140 icon=
"mdi:weather-night",
141 entity_registry_enabled_default=
False,
144 key=
"upcoming_shabbat_candle_lighting",
145 name=
"Upcoming Shabbat Candle Lighting",
147 entity_registry_enabled_default=
False,
150 key=
"upcoming_shabbat_havdalah",
151 name=
"Upcoming Shabbat Havdalah",
152 icon=
"mdi:weather-night",
153 entity_registry_enabled_default=
False,
156 key=
"upcoming_candle_lighting",
157 name=
"Upcoming Candle Lighting",
161 key=
"upcoming_havdalah",
162 name=
"Upcoming Havdalah",
163 icon=
"mdi:weather-night",
170 config_entry: JewishCalendarConfigEntry,
171 async_add_entities: AddEntitiesCallback,
173 """Set up the Jewish calendar sensors ."""
179 for description
in TIME_SENSORS
186 """Representation of an Jewish calendar sensor."""
188 _attr_entity_category = EntityCategory.DIAGNOSTIC
192 config_entry: JewishCalendarConfigEntry,
193 description: SensorEntityDescription,
195 """Initialize the Jewish calendar sensor."""
196 super().
__init__(config_entry, description)
197 self.
_attrs_attrs: dict[str, str] = {}
200 """Update the state of the sensor."""
202 _LOGGER.debug(
"Now: %s Location: %r", now, self.
_location_location)
207 if event_date
is None:
208 _LOGGER.error(
"Can't get sunset event date for %s", today)
211 sunset = dt_util.as_local(event_date)
213 _LOGGER.debug(
"Now: %s Sunset: %s", now, sunset)
215 daytime_date = HDate(today, diaspora=self.
_diaspora_diaspora, hebrew=self.
_hebrew_hebrew)
225 after_tzais_date = after_shkia_date = daytime_date
229 after_shkia_date = daytime_date.next_day
231 if today_times.havdalah
and now > today_times.havdalah:
232 after_tzais_date = daytime_date.next_day
235 daytime_date, after_shkia_date, after_tzais_date
242 """Create a Zmanim object."""
253 """Return the state attributes."""
257 self, daytime_date: HDate, after_shkia_date: HDate, after_tzais_date: HDate
259 """For a given type of sensor, return the state."""
263 hdate = cast(HebrewDate, after_shkia_date.hdate)
264 month = htables.MONTHS[hdate.month.value - 1]
266 "hebrew_year": hdate.year,
267 "hebrew_month_name": month.hebrew
if self.
_hebrew_hebrew
else month.english,
268 "hebrew_day": hdate.day,
270 return after_shkia_date.hebrew_date
273 (p.hebrew
if self.
_hebrew_hebrew
else p.english)
for p
in htables.PARASHAOT
276 return after_tzais_date.upcoming_shabbat.parasha
278 _id = _type = _type_id =
""
279 _holiday_type = after_shkia_date.holiday_type
280 if isinstance(_holiday_type, list):
281 _id =
", ".join(after_shkia_date.holiday_name)
282 _type =
", ".join([_htype.name
for _htype
in _holiday_type])
283 _type_id =
", ".join([
str(_htype.value)
for _htype
in _holiday_type])
285 _id = after_shkia_date.holiday_name
286 _type = _holiday_type.name
287 _type_id = _holiday_type.value
288 self.
_attrs_attrs = {
"id": _id,
"type": _type,
"type_id": _type_id}
291 return after_shkia_date.holiday_description
293 return after_shkia_date.omer_day
295 return daytime_date.daf_yomi
301 """Implement attributes for sensors returning times."""
303 _attr_device_class = SensorDeviceClass.TIMESTAMP
306 self, daytime_date: HDate, after_shkia_date: HDate, after_tzais_date: HDate
308 """For a given type of sensor, return the state."""
309 if self.
entity_descriptionentity_description.key ==
"upcoming_shabbat_candle_lighting":
311 after_tzais_date.upcoming_shabbat.previous_day.gdate
313 return times.candle_lighting
316 after_tzais_date.upcoming_shabbat_or_yom_tov.first_day.previous_day.gdate
318 return times.candle_lighting
320 times = self.
make_zmanimmake_zmanim(after_tzais_date.upcoming_shabbat.gdate)
321 return times.havdalah
324 after_tzais_date.upcoming_shabbat_or_yom_tov.last_day.gdate
326 return times.havdalah
328 times = self.
make_zmanimmake_zmanim(dt_util.now()).zmanim
dict[str, str] extra_state_attributes(self)
Any|None get_state(self, HDate daytime_date, HDate after_shkia_date, HDate after_tzais_date)
Zmanim make_zmanim(self, Date date)
None __init__(self, JewishCalendarConfigEntry config_entry, SensorEntityDescription description)
Any|None get_state(self, HDate daytime_date, HDate after_shkia_date, HDate after_tzais_date)
None async_setup_entry(HomeAssistant hass, JewishCalendarConfigEntry config_entry, AddEntitiesCallback async_add_entities)
datetime.datetime|None get_astral_event_date(HomeAssistant hass, str event, datetime.date|datetime.datetime|None date=None)