From 0d98f9783f63149ae78a73b5e609b5ed24f705db Mon Sep 17 00:00:00 2001 From: arigilder <43716164+arigilder@users.noreply.github.com> Date: Mon, 11 Feb 2019 17:34:48 -0500 Subject: [PATCH] Add lagging hdate for sensors that should lag to update (#20655) * Add lagging hdate for sensors that should lag to update * Fix indentation * Lint fix --- .../components/sensor/jewish_calendar.py | 37 ++++++++++++------- .../components/sensor/test_jewish_calendar.py | 9 +++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/sensor/jewish_calendar.py b/homeassistant/components/sensor/jewish_calendar.py index cc226337f02..65aeaf7fba9 100644 --- a/homeassistant/components/sensor/jewish_calendar.py +++ b/homeassistant/components/sensor/jewish_calendar.py @@ -4,7 +4,6 @@ Platform to retrieve Jewish calendar information for Home Assistant. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.jewish_calendar/ """ -from datetime import timedelta import logging import voluptuous as vol @@ -140,12 +139,6 @@ class JewishCalSensor(Entity): _LOGGER.debug("Now: %s Sunset: %s", now, sunset) - if now > sunset: - today += timedelta(1) - - date = hdate.HDate( - today, diaspora=self.diaspora, hebrew=self._hebrew) - location = hdate.Location(latitude=self.latitude, longitude=self.longitude, timezone=self.timezone, @@ -158,27 +151,43 @@ class JewishCalSensor(Entity): candle_lighting_offset=self.candle_lighting_offset, havdalah_offset=self.havdalah_offset, hebrew=self._hebrew) + date = hdate.HDate( + today, diaspora=self.diaspora, hebrew=self._hebrew) + lagging_date = date + + # Advance Hebrew date if sunset has passed. + # Not all sensors should advance immediately when the Hebrew date + # officially changes (i.e. after sunset), hence lagging_date. + if now > sunset: + date = date.next_day + today_times = make_zmanim(today) + if today_times.havdalah and now > today_times.havdalah: + lagging_date = lagging_date.next_day + + # Terminology note: by convention in py-libhdate library, "upcoming" + # refers to "current" or "upcoming" dates. if self.type == 'date': self._state = date.hebrew_date elif self.type == 'weekly_portion': # Compute the weekly portion based on the upcoming shabbat. - self._state = date.upcoming_shabbat.parasha + self._state = lagging_date.upcoming_shabbat.parasha elif self.type == 'holiday_name': self._state = date.holiday_description elif self.type == 'holyness': self._state = date.holiday_type elif self.type == 'upcoming_shabbat_candle_lighting': - times = make_zmanim(date.upcoming_shabbat.previous_day.gdate) - self._state = times.candle_lighting - elif self.type == 'upcoming_candle_lighting': - times = make_zmanim(date.upcoming_shabbat_or_yom_tov.first_day + times = make_zmanim(lagging_date.upcoming_shabbat .previous_day.gdate) self._state = times.candle_lighting + elif self.type == 'upcoming_candle_lighting': + times = make_zmanim(lagging_date.upcoming_shabbat_or_yom_tov + .first_day.previous_day.gdate) + self._state = times.candle_lighting elif self.type == 'upcoming_shabbat_havdalah': - times = make_zmanim(date.upcoming_shabbat.gdate) + times = make_zmanim(lagging_date.upcoming_shabbat.gdate) self._state = times.havdalah elif self.type == 'upcoming_havdalah': - times = make_zmanim(date.upcoming_shabbat_or_yom_tov + times = make_zmanim(lagging_date.upcoming_shabbat_or_yom_tov .last_day.gdate) self._state = times.havdalah elif self.type == 'issur_melacha_in_effect': diff --git a/tests/components/sensor/test_jewish_calendar.py b/tests/components/sensor/test_jewish_calendar.py index 639364164e0..7243874a41d 100644 --- a/tests/components/sensor/test_jewish_calendar.py +++ b/tests/components/sensor/test_jewish_calendar.py @@ -158,6 +158,14 @@ class TestJewishCalenderSensor(): 'weekly_portion': 'Ki Tavo', 'hebrew_weekly_portion': 'כי תבוא'}, havdalah_offset=50), + make_nyc_test_params( + dt(2018, 9, 1, 20, 0), + {'upcoming_shabbat_candle_lighting': dt(2018, 8, 31, 19, 15), + 'upcoming_shabbat_havdalah': dt(2018, 9, 1, 20, 14), + 'upcoming_candle_lighting': dt(2018, 8, 31, 19, 15), + 'upcoming_havdalah': dt(2018, 9, 1, 20, 14), + 'weekly_portion': 'Ki Tavo', + 'hebrew_weekly_portion': 'כי תבוא'}), make_nyc_test_params( dt(2018, 9, 1, 20, 21), {'upcoming_shabbat_candle_lighting': dt(2018, 9, 7, 19, 4), @@ -317,6 +325,7 @@ class TestJewishCalenderSensor(): shabbat_test_ids = [ "currently_first_shabbat", "currently_first_shabbat_with_havdalah_offset", + "currently_first_shabbat_bein_hashmashot_lagging_date", "after_first_shabbat", "friday_upcoming_shabbat", "upcoming_rosh_hashana",