Skip to content

Commit 0091daf

Browse files
tsvifrenck
authored andcommitted
Revert "Jewish Calendar add coordinator " (home-assistant#151780)
1 parent b387acf commit 0091daf

File tree

7 files changed

+163
-226
lines changed

7 files changed

+163
-226
lines changed

homeassistant/components/jewish_calendar/__init__.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
DEFAULT_LANGUAGE,
3030
DOMAIN,
3131
)
32-
from .coordinator import JewishCalendarData, JewishCalendarUpdateCoordinator
33-
from .entity import JewishCalendarConfigEntry
32+
from .entity import JewishCalendarConfigEntry, JewishCalendarData
3433
from .services import async_setup_services
3534

3635
_LOGGER = logging.getLogger(__name__)
@@ -70,33 +69,24 @@ async def async_setup_entry(
7069
)
7170
)
7271

73-
data = JewishCalendarData(
72+
config_entry.runtime_data = JewishCalendarData(
7473
language,
7574
diaspora,
7675
location,
7776
candle_lighting_offset,
7877
havdalah_offset,
7978
)
8079

81-
coordinator = JewishCalendarUpdateCoordinator(hass, config_entry, data)
82-
await coordinator.async_config_entry_first_refresh()
83-
84-
config_entry.runtime_data = coordinator
8580
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
81+
8682
return True
8783

8884

8985
async def async_unload_entry(
9086
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
9187
) -> bool:
9288
"""Unload a config entry."""
93-
if unload_ok := await hass.config_entries.async_unload_platforms(
94-
config_entry, PLATFORMS
95-
):
96-
coordinator = config_entry.runtime_data
97-
if coordinator.event_unsub:
98-
coordinator.event_unsub()
99-
return unload_ok
89+
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
10090

10191

10292
async def async_migrate_entry(

homeassistant/components/jewish_calendar/binary_sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class JewishCalendarBinarySensor(JewishCalendarEntity, BinarySensorEntity):
7272
@property
7373
def is_on(self) -> bool:
7474
"""Return true if sensor is on."""
75-
return self.entity_description.is_on(self.coordinator.zmanim)(dt_util.now())
75+
zmanim = self.make_zmanim(dt.date.today())
76+
return self.entity_description.is_on(zmanim)(dt_util.now())
7677

7778
def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
7879
"""Return a list of times to update the sensor."""

homeassistant/components/jewish_calendar/coordinator.py

Lines changed: 0 additions & 116 deletions
This file was deleted.

homeassistant/components/jewish_calendar/diagnostics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ async def async_get_config_entry_diagnostics(
2424

2525
return {
2626
"entry_data": async_redact_data(entry.data, TO_REDACT),
27-
"data": async_redact_data(asdict(entry.runtime_data.data), TO_REDACT),
27+
"data": async_redact_data(asdict(entry.runtime_data), TO_REDACT),
2828
}

homeassistant/components/jewish_calendar/entity.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
"""Entity representing a Jewish Calendar sensor."""
22

33
from abc import abstractmethod
4+
from dataclasses import dataclass
45
import datetime as dt
6+
import logging
57

6-
from hdate import Zmanim
8+
from hdate import HDateInfo, Location, Zmanim
9+
from hdate.translator import Language, set_language
710

11+
from homeassistant.config_entries import ConfigEntry
812
from homeassistant.core import CALLBACK_TYPE, callback
913
from homeassistant.helpers import event
1014
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
11-
from homeassistant.helpers.entity import EntityDescription
12-
from homeassistant.helpers.update_coordinator import CoordinatorEntity
15+
from homeassistant.helpers.entity import Entity, EntityDescription
1316
from homeassistant.util import dt as dt_util
1417

1518
from .const import DOMAIN
16-
from .coordinator import JewishCalendarConfigEntry, JewishCalendarUpdateCoordinator
1719

20+
_LOGGER = logging.getLogger(__name__)
1821

19-
class JewishCalendarEntity(CoordinatorEntity[JewishCalendarUpdateCoordinator]):
22+
type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]
23+
24+
25+
@dataclass
26+
class JewishCalendarDataResults:
27+
"""Jewish Calendar results dataclass."""
28+
29+
dateinfo: HDateInfo
30+
zmanim: Zmanim
31+
32+
33+
@dataclass
34+
class JewishCalendarData:
35+
"""Jewish Calendar runtime dataclass."""
36+
37+
language: Language
38+
diaspora: bool
39+
location: Location
40+
candle_lighting_offset: int
41+
havdalah_offset: int
42+
results: JewishCalendarDataResults | None = None
43+
44+
45+
class JewishCalendarEntity(Entity):
2046
"""An HA implementation for Jewish Calendar entity."""
2147

2248
_attr_has_entity_name = True
@@ -29,13 +55,23 @@ def __init__(
2955
description: EntityDescription,
3056
) -> None:
3157
"""Initialize a Jewish Calendar entity."""
32-
super().__init__(config_entry.runtime_data)
3358
self.entity_description = description
3459
self._attr_unique_id = f"{config_entry.entry_id}-{description.key}"
3560
self._attr_device_info = DeviceInfo(
3661
entry_type=DeviceEntryType.SERVICE,
3762
identifiers={(DOMAIN, config_entry.entry_id)},
3863
)
64+
self.data = config_entry.runtime_data
65+
set_language(self.data.language)
66+
67+
def make_zmanim(self, date: dt.date) -> Zmanim:
68+
"""Create a Zmanim object."""
69+
return Zmanim(
70+
date=date,
71+
location=self.data.location,
72+
candle_lighting_offset=self.data.candle_lighting_offset,
73+
havdalah_offset=self.data.havdalah_offset,
74+
)
3975

4076
async def async_added_to_hass(self) -> None:
4177
"""Call when entity is added to hass."""
@@ -56,9 +92,10 @@ def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
5692
def _schedule_update(self) -> None:
5793
"""Schedule the next update of the sensor."""
5894
now = dt_util.now()
95+
zmanim = self.make_zmanim(now.date())
5996
update = dt_util.start_of_local_day() + dt.timedelta(days=1)
6097

61-
for update_time in self._update_times(self.coordinator.zmanim):
98+
for update_time in self._update_times(zmanim):
6299
if update_time is not None and now < update_time < update:
63100
update = update_time
64101

@@ -73,4 +110,17 @@ def _update(self, now: dt.datetime | None = None) -> None:
73110
"""Update the sensor data."""
74111
self._update_unsub = None
75112
self._schedule_update()
113+
self.create_results(now)
76114
self.async_write_ha_state()
115+
116+
def create_results(self, now: dt.datetime | None = None) -> None:
117+
"""Create the results for the sensor."""
118+
if now is None:
119+
now = dt_util.now()
120+
121+
_LOGGER.debug("Now: %s Location: %r", now, self.data.location)
122+
123+
today = now.date()
124+
zmanim = self.make_zmanim(today)
125+
dateinfo = HDateInfo(today, diaspora=self.data.diaspora)
126+
self.data.results = JewishCalendarDataResults(dateinfo, zmanim)

homeassistant/components/jewish_calendar/sensor.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from homeassistant.const import EntityCategory
2020
from homeassistant.core import HomeAssistant
2121
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
22-
import homeassistant.util.dt as dt_util
22+
from homeassistant.util import dt as dt_util
2323

2424
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
2525

@@ -236,18 +236,25 @@ def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
236236
return []
237237
return [self.entity_description.next_update_fn(zmanim)]
238238

239-
def get_dateinfo(self) -> HDateInfo:
239+
def get_dateinfo(self, now: dt.datetime | None = None) -> HDateInfo:
240240
"""Get the next date info."""
241-
now = dt_util.now()
242-
update = None
241+
if self.data.results is None:
242+
self.create_results()
243+
assert self.data.results is not None, "Results should be available"
244+
245+
if now is None:
246+
now = dt_util.now()
243247

248+
today = now.date()
249+
zmanim = self.make_zmanim(today)
250+
update = None
244251
if self.entity_description.next_update_fn:
245-
update = self.entity_description.next_update_fn(self.coordinator.zmanim)
252+
update = self.entity_description.next_update_fn(zmanim)
246253

247-
_LOGGER.debug("Today: %s, update: %s", now.date(), update)
254+
_LOGGER.debug("Today: %s, update: %s", today, update)
248255
if update is not None and now >= update:
249-
return self.coordinator.dateinfo.next_day
250-
return self.coordinator.dateinfo
256+
return self.data.results.dateinfo.next_day
257+
return self.data.results.dateinfo
251258

252259

253260
class JewishCalendarSensor(JewishCalendarBaseSensor):
@@ -264,9 +271,7 @@ def __init__(
264271
super().__init__(config_entry, description)
265272
# Set the options for enumeration sensors
266273
if self.entity_description.options_fn is not None:
267-
self._attr_options = self.entity_description.options_fn(
268-
self.coordinator.data.diaspora
269-
)
274+
self._attr_options = self.entity_description.options_fn(self.data.diaspora)
270275

271276
@property
272277
def native_value(self) -> str | int | dt.datetime | None:
@@ -290,8 +295,9 @@ class JewishCalendarTimeSensor(JewishCalendarBaseSensor):
290295
@property
291296
def native_value(self) -> dt.datetime | None:
292297
"""Return the state of the sensor."""
298+
if self.data.results is None:
299+
self.create_results()
300+
assert self.data.results is not None, "Results should be available"
293301
if self.entity_description.value_fn is None:
294-
return self.coordinator.zmanim.zmanim[self.entity_description.key].local
295-
return self.entity_description.value_fn(
296-
self.get_dateinfo(), self.coordinator.make_zmanim
297-
)
302+
return self.data.results.zmanim.zmanim[self.entity_description.key].local
303+
return self.entity_description.value_fn(self.get_dateinfo(), self.make_zmanim)

0 commit comments

Comments
 (0)