Skip to content

Commit 21b9fcd

Browse files
committed
Merge sensor updates between binary and regular sensors
1 parent e776209 commit 21b9fcd

File tree

3 files changed

+64
-70
lines changed

3 files changed

+64
-70
lines changed

homeassistant/components/jewish_calendar/binary_sensor.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
BinarySensorEntityDescription,
1414
)
1515
from homeassistant.const import EntityCategory
16-
from homeassistant.core import HomeAssistant, callback
17-
from homeassistant.helpers import event
16+
from homeassistant.core import HomeAssistant
1817
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1918
from homeassistant.util import dt as dt_util
2019

@@ -75,27 +74,3 @@ def is_on(self) -> bool:
7574
"""Return true if sensor is on."""
7675
zmanim = self.make_zmanim(dt.date.today())
7776
return self.entity_description.is_on(zmanim, dt_util.now())
78-
79-
@callback
80-
def _update(self, now: dt.datetime | None = None) -> None:
81-
"""Update the state of the sensor."""
82-
self._update_unsub = None
83-
self._schedule_update()
84-
self.async_write_ha_state()
85-
86-
def _schedule_update(self) -> None:
87-
"""Schedule the next update of the sensor."""
88-
now = dt_util.now()
89-
zmanim = self.make_zmanim(now.date())
90-
update = zmanim.netz_hachama.local + dt.timedelta(days=1)
91-
candle_lighting = zmanim.candle_lighting
92-
if candle_lighting is not None and now < candle_lighting < update:
93-
update = candle_lighting
94-
havdalah = zmanim.havdalah
95-
if havdalah is not None and now < havdalah < update:
96-
update = havdalah
97-
if self._update_unsub:
98-
self._update_unsub()
99-
self._update_unsub = event.async_track_point_in_time(
100-
self.hass, self._update, update
101-
)

homeassistant/components/jewish_calendar/entity.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
from dataclasses import dataclass
44
import datetime as dt
5+
import logging
56

67
from hdate import HDateInfo, Location, Zmanim
78
from hdate.translator import Language, set_language
89

910
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
1113
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
1214
from homeassistant.helpers.entity import Entity, EntityDescription
15+
from homeassistant.util import dt as dt_util
1316

1417
from .const import DOMAIN
1518

19+
_LOGGER = logging.getLogger(__name__)
20+
1621
type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]
1722

1823

@@ -81,3 +86,58 @@ async def async_added_to_hass(self) -> None:
8186

8287
def _schedule_update(self) -> None:
8388
"""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)

homeassistant/components/jewish_calendar/sensor.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@
1717
SensorEntityDescription,
1818
)
1919
from homeassistant.const import EntityCategory
20-
from homeassistant.core import HomeAssistant, callback
21-
from homeassistant.helpers import event
20+
from homeassistant.core import HomeAssistant
2221
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2322
from homeassistant.util import dt as dt_util
2423

25-
from .entity import (
26-
JewishCalendarConfigEntry,
27-
JewishCalendarDataResults,
28-
JewishCalendarEntity,
29-
)
24+
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
3025

3126
_LOGGER = logging.getLogger(__name__)
3227
PARALLEL_UPDATES = 0
@@ -235,42 +230,6 @@ class JewishCalendarBaseSensor(JewishCalendarEntity, SensorEntity):
235230

236231
entity_description: JewishCalendarBaseSensorDescription
237232

238-
def _schedule_update(self) -> None:
239-
"""Schedule the next update of the sensor."""
240-
now = dt_util.now()
241-
zmanim = self.make_zmanim(now.date())
242-
update = None
243-
if self.entity_description.next_update_fn:
244-
update = self.entity_description.next_update_fn(zmanim)
245-
next_midnight = dt_util.start_of_local_day() + dt.timedelta(days=1)
246-
if update is None or now > update:
247-
update = next_midnight
248-
if self._update_unsub:
249-
self._update_unsub()
250-
self._update_unsub = event.async_track_point_in_time(
251-
self.hass, self._update, update
252-
)
253-
254-
@callback
255-
def _update(self, now: dt.datetime | None = None) -> None:
256-
"""Update the sensor data."""
257-
self._update_unsub = None
258-
self._schedule_update()
259-
self.create_results(now)
260-
self.async_write_ha_state()
261-
262-
def create_results(self, now: dt.datetime | None = None) -> None:
263-
"""Create the results for the sensor."""
264-
if now is None:
265-
now = dt_util.now()
266-
267-
_LOGGER.debug("Now: %s Location: %r", now, self.data.location)
268-
269-
today = now.date()
270-
zmanim = self.make_zmanim(today)
271-
dateinfo = HDateInfo(today, diaspora=self.data.diaspora)
272-
self.data.results = JewishCalendarDataResults(dateinfo, zmanim)
273-
274233
def get_dateinfo(self, now: dt.datetime | None = None) -> HDateInfo:
275234
"""Get the next date info."""
276235
if self.data.results is None:

0 commit comments

Comments
 (0)