|
2 | 2 |
|
3 | 3 | from dataclasses import dataclass |
4 | 4 | import datetime as dt |
| 5 | +import logging |
5 | 6 |
|
6 | 7 | from hdate import HDateInfo, Location, Zmanim |
7 | 8 | from hdate.translator import Language, set_language |
8 | 9 |
|
9 | 10 | from homeassistant.config_entries import ConfigEntry |
10 | | -from homeassistant.core import CALLBACK_TYPE |
| 11 | +from homeassistant.core import CALLBACK_TYPE, callback |
| 12 | +from homeassistant.helpers import event |
11 | 13 | from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo |
12 | 14 | from homeassistant.helpers.entity import Entity, EntityDescription |
| 15 | +from homeassistant.util import dt as dt_util |
13 | 16 |
|
14 | 17 | from .const import DOMAIN |
15 | 18 |
|
| 19 | +_LOGGER = logging.getLogger(__name__) |
| 20 | + |
16 | 21 | type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData] |
17 | 22 |
|
18 | 23 |
|
@@ -81,3 +86,58 @@ async def async_added_to_hass(self) -> None: |
81 | 86 |
|
82 | 87 | def _schedule_update(self) -> None: |
83 | 88 | """Schedule the next update of the sensor.""" |
| 89 | + now = dt_util.now() |
| 90 | + zmanim = self.make_zmanim(now.date()) |
| 91 | + update = None |
| 92 | + |
| 93 | + # If the entity description has a next_update_fn, use it |
| 94 | + if ( |
| 95 | + hasattr(self.entity_description, "next_update_fn") |
| 96 | + and self.entity_description.next_update_fn |
| 97 | + ): |
| 98 | + update = self.entity_description.next_update_fn(zmanim) |
| 99 | + |
| 100 | + # If no next_update_fn, default to next midnight |
| 101 | + if update is None: |
| 102 | + update = dt_util.start_of_local_day() + dt.timedelta(days=1) |
| 103 | + |
| 104 | + # Check for earlier next netz_hachama (sunrise) + 1 day |
| 105 | + next_netz_hachama = zmanim.netz_hachama.local + dt.timedelta(days=1) |
| 106 | + if next_netz_hachama is not None and now < next_netz_hachama < update: |
| 107 | + update = next_netz_hachama |
| 108 | + |
| 109 | + # Check for earlier candle_lighting time |
| 110 | + candle_lighting = zmanim.candle_lighting |
| 111 | + if candle_lighting is not None and now < candle_lighting < update: |
| 112 | + update = candle_lighting |
| 113 | + |
| 114 | + # Check for earlier havdalah time |
| 115 | + havdalah = zmanim.havdalah |
| 116 | + if havdalah is not None and now < havdalah < update: |
| 117 | + update = havdalah |
| 118 | + |
| 119 | + if self._update_unsub: |
| 120 | + self._update_unsub() |
| 121 | + self._update_unsub = event.async_track_point_in_time( |
| 122 | + self.hass, self._update, update |
| 123 | + ) |
| 124 | + |
| 125 | + @callback |
| 126 | + def _update(self, now: dt.datetime | None = None) -> None: |
| 127 | + """Update the sensor data.""" |
| 128 | + self._update_unsub = None |
| 129 | + self._schedule_update() |
| 130 | + self.create_results(now) |
| 131 | + self.async_write_ha_state() |
| 132 | + |
| 133 | + def create_results(self, now: dt.datetime | None = None) -> None: |
| 134 | + """Create the results for the sensor.""" |
| 135 | + if now is None: |
| 136 | + now = dt_util.now() |
| 137 | + |
| 138 | + _LOGGER.debug("Now: %s Location: %r", now, self.data.location) |
| 139 | + |
| 140 | + today = now.date() |
| 141 | + zmanim = self.make_zmanim(today) |
| 142 | + dateinfo = HDateInfo(today, diaspora=self.data.diaspora) |
| 143 | + self.data.results = JewishCalendarDataResults(dateinfo, zmanim) |
0 commit comments