Skip to content

Commit c47384f

Browse files
committed
Move value calculation to sensor description
1 parent 0eafa27 commit c47384f

File tree

2 files changed

+163
-138
lines changed

2 files changed

+163
-138
lines changed

homeassistant/components/jewish_calendar/entity.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22

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

6-
from hdate import Location, Zmanim
7+
from hdate import HDateInfo, Location, Zmanim
78
from hdate.translator import Language, set_language
89

910
from homeassistant.config_entries import ConfigEntry
11+
from homeassistant.const import SUN_EVENT_SUNSET
1012
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
1113
from homeassistant.helpers.entity import Entity, EntityDescription
14+
from homeassistant.helpers.sun import get_astral_event_date
15+
from homeassistant.util import dt as dt_util
1216

1317
from .const import DOMAIN
1418

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

1723

24+
@dataclass
25+
class JewishCalendarDataResults:
26+
"""Jewish Calendar results dataclass."""
27+
28+
daytime_date: HDateInfo
29+
after_shkia_date: HDateInfo
30+
after_tzais_date: HDateInfo
31+
zmanim: Zmanim
32+
33+
1834
@dataclass
1935
class JewishCalendarData:
2036
"""Jewish Calendar runtime dataclass."""
@@ -24,6 +40,7 @@ class JewishCalendarData:
2440
location: Location
2541
candle_lighting_offset: int
2642
havdalah_offset: int
43+
results: JewishCalendarDataResults | None = None
2744

2845

2946
class JewishCalendarEntity(Entity):
@@ -54,3 +71,42 @@ def make_zmanim(self, date: dt.date) -> Zmanim:
5471
candle_lighting_offset=self.data.candle_lighting_offset,
5572
havdalah_offset=self.data.havdalah_offset,
5673
)
74+
75+
async def async_update_data(self) -> None:
76+
"""Update the state of the sensor."""
77+
now = dt_util.now()
78+
_LOGGER.debug("Now: %s Location: %r", now, self.data.location)
79+
80+
today = now.date()
81+
event_date = get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today)
82+
83+
if event_date is None:
84+
_LOGGER.error("Can't get sunset event date for %s", today)
85+
return
86+
87+
sunset = dt_util.as_local(event_date)
88+
89+
_LOGGER.debug("Now: %s Sunset: %s", now, sunset)
90+
91+
daytime_date = HDateInfo(today, diaspora=self.data.diaspora)
92+
93+
# The Jewish day starts after darkness (called "tzais") and finishes at
94+
# sunset ("shkia"). The time in between is a gray area
95+
# (aka "Bein Hashmashot" # codespell:ignore
96+
# - literally: "in between the sun and the moon").
97+
98+
# For some sensors, it is more interesting to consider the date to be
99+
# tomorrow based on sunset ("shkia"), for others based on "tzais".
100+
# Hence the following variables.
101+
after_tzais_date = after_shkia_date = daytime_date
102+
today_times = self.make_zmanim(today)
103+
104+
if now > sunset:
105+
after_shkia_date = daytime_date.next_day
106+
107+
if today_times.havdalah and now > today_times.havdalah:
108+
after_tzais_date = daytime_date.next_day
109+
110+
self.data.results = JewishCalendarDataResults(
111+
daytime_date, after_shkia_date, after_tzais_date, today_times
112+
)

0 commit comments

Comments
 (0)