Skip to content
58 changes: 10 additions & 48 deletions homeassistant/components/jewish_calendar/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import event
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import dt as dt_util

Expand All @@ -24,19 +23,12 @@


@dataclass(frozen=True)
class JewishCalendarBinarySensorMixIns(BinarySensorEntityDescription):
"""Binary Sensor description mixin class for Jewish Calendar."""
class JewishCalendarBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Binary Sensor Entity description for Jewish Calendar."""

is_on: Callable[[Zmanim, dt.datetime], bool] = lambda _, __: False


@dataclass(frozen=True)
class JewishCalendarBinarySensorEntityDescription(
JewishCalendarBinarySensorMixIns, BinarySensorEntityDescription
):
"""Binary Sensor Entity description for Jewish Calendar."""


BINARY_SENSORS: tuple[JewishCalendarBinarySensorEntityDescription, ...] = (
JewishCalendarBinarySensorEntityDescription(
key="issur_melacha_in_effect",
Expand Down Expand Up @@ -73,9 +65,7 @@ async def async_setup_entry(
class JewishCalendarBinarySensor(JewishCalendarEntity, BinarySensorEntity):
"""Representation of an Jewish Calendar binary sensor."""

_attr_should_poll = False
_attr_entity_category = EntityCategory.DIAGNOSTIC
_update_unsub: CALLBACK_TYPE | None = None

entity_description: JewishCalendarBinarySensorEntityDescription

Expand All @@ -85,38 +75,10 @@ def is_on(self) -> bool:
zmanim = self.make_zmanim(dt.date.today())
return self.entity_description.is_on(zmanim, dt_util.now())

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
self._schedule_update()

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._update_unsub:
self._update_unsub()
self._update_unsub = None
return await super().async_will_remove_from_hass()

@callback
def _update(self, now: dt.datetime | None = None) -> None:
"""Update the state of the sensor."""
self._update_unsub = None
self._schedule_update()
self.async_write_ha_state()

def _schedule_update(self) -> None:
"""Schedule the next update of the sensor."""
now = dt_util.now()
zmanim = self.make_zmanim(dt.date.today())
update = zmanim.netz_hachama.local + dt.timedelta(days=1)
candle_lighting = zmanim.candle_lighting
if candle_lighting is not None and now < candle_lighting < update:
update = candle_lighting
havdalah = zmanim.havdalah
if havdalah is not None and now < havdalah < update:
update = havdalah
if self._update_unsub:
self._update_unsub()
self._update_unsub = event.async_track_point_in_time(
self.hass, self._update, update
)
def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
"""Return a list of times to update the sensor."""
return [
zmanim.netz_hachama.local + dt.timedelta(days=1),
zmanim.candle_lighting,
zmanim.havdalah,
]
61 changes: 61 additions & 0 deletions homeassistant/components/jewish_calendar/entity.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
"""Entity representing a Jewish Calendar sensor."""

from abc import abstractmethod
from dataclasses import dataclass
import datetime as dt
import logging

from hdate import HDateInfo, Location, Zmanim
from hdate.translator import Language, set_language

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.helpers import event
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.util import dt as dt_util

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]


Expand Down Expand Up @@ -39,6 +46,8 @@
"""An HA implementation for Jewish Calendar entity."""

_attr_has_entity_name = True
_attr_should_poll = False
_update_unsub: CALLBACK_TYPE | None = None

def __init__(
self,
Expand All @@ -63,3 +72,55 @@
candle_lighting_offset=self.data.candle_lighting_offset,
havdalah_offset=self.data.havdalah_offset,
)

async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
self._schedule_update()

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._update_unsub:
self._update_unsub()
self._update_unsub = None
return await super().async_will_remove_from_hass()

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

def _schedule_update(self) -> None:
"""Schedule the next update of the sensor."""
now = dt_util.now()
zmanim = self.make_zmanim(now.date())
update = dt_util.start_of_local_day() + dt.timedelta(days=1)

for update_time in self._update_times(zmanim):
if update_time is not None and now < update_time < update:
update = update_time

if self._update_unsub:
self._update_unsub()

Check warning on line 103 in homeassistant/components/jewish_calendar/entity.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/jewish_calendar/entity.py#L103

Added line #L103 was not covered by tests
self._update_unsub = event.async_track_point_in_time(
self.hass, self._update, update
)

@callback
def _update(self, now: dt.datetime | None = None) -> None:
"""Update the sensor data."""
self._update_unsub = None
self._schedule_update()
self.create_results(now)
self.async_write_ha_state()

def create_results(self, now: dt.datetime | None = None) -> None:
"""Create the results for the sensor."""
if now is None:
now = dt_util.now()

_LOGGER.debug("Now: %s Location: %r", now, self.data.location)

today = now.date()
zmanim = self.make_zmanim(today)
dateinfo = HDateInfo(today, diaspora=self.data.diaspora)
self.data.results = JewishCalendarDataResults(dateinfo, zmanim)
63 changes: 7 additions & 56 deletions homeassistant/components/jewish_calendar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
SensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import event
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import dt as dt_util

from .entity import (
JewishCalendarConfigEntry,
JewishCalendarDataResults,
JewishCalendarEntity,
)
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity

_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
Expand Down Expand Up @@ -231,59 +226,15 @@ async def async_setup_entry(
class JewishCalendarBaseSensor(JewishCalendarEntity, SensorEntity):
"""Base class for Jewish calendar sensors."""

_attr_should_poll = False
_attr_entity_category = EntityCategory.DIAGNOSTIC
_update_unsub: CALLBACK_TYPE | None = None

entity_description: JewishCalendarBaseSensorDescription

async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
self._schedule_update()

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._update_unsub:
self._update_unsub()
self._update_unsub = None
return await super().async_will_remove_from_hass()

def _schedule_update(self) -> None:
"""Schedule the next update of the sensor."""
now = dt_util.now()
zmanim = self.make_zmanim(now.date())
update = None
if self.entity_description.next_update_fn:
update = self.entity_description.next_update_fn(zmanim)
next_midnight = dt_util.start_of_local_day() + dt.timedelta(days=1)
if update is None or now > update:
update = next_midnight
if self._update_unsub:
self._update_unsub()
self._update_unsub = event.async_track_point_in_time(
self.hass, self._update_data, update
)

@callback
def _update_data(self, now: dt.datetime | None = None) -> None:
"""Update the sensor data."""
self._update_unsub = None
self._schedule_update()
self.create_results(now)
self.async_write_ha_state()

def create_results(self, now: dt.datetime | None = None) -> None:
"""Create the results for the sensor."""
if now is None:
now = dt_util.now()

_LOGGER.debug("Now: %s Location: %r", now, self.data.location)

today = now.date()
zmanim = self.make_zmanim(today)
dateinfo = HDateInfo(today, diaspora=self.data.diaspora)
self.data.results = JewishCalendarDataResults(dateinfo, zmanim)
def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
"""Return a list of times to update the sensor."""
if self.entity_description.next_update_fn is None:
return []
return [self.entity_description.next_update_fn(zmanim)]

def get_dateinfo(self, now: dt.datetime | None = None) -> HDateInfo:
"""Get the next date info."""
Expand Down