Add support for displaying Daf Yomi (#30628)

* Add support for displaying Daf Yomi

* Ran black --fast

* Added docstring to get_daf

* Further lint fixes

* Remove unnecessary else

* clarify code

* Use fstrings

* pull daf yomi from hdate

* Update manifest version for daf_yomi support

* fix variable usage

* Update requirements

* Also pass in today's date as well

* Rename date variable to daytime_date

* Add tests for daf yomi sensor

* Update stale test IDs
This commit is contained in:
Moshe Kaplan 2020-02-15 18:00:17 -05:00 committed by GitHub
parent 481d3295a6
commit 7dff5e79d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 10 deletions

View File

@ -21,6 +21,7 @@ SENSOR_TYPES = {
"weekly_portion": ["Parshat Hashavua", "mdi:book-open-variant"],
"holiday": ["Holiday", "mdi:calendar-star"],
"omer_count": ["Day of the Omer", "mdi:counter"],
"daf_yomi": ["Daf Yomi", "mdi:book-open-variant"],
},
"time": {
"first_light": ["Alot Hashachar", "mdi:weather-sunset-up"],

View File

@ -2,7 +2,7 @@
"domain": "jewish_calendar",
"name": "Jewish Calendar",
"documentation": "https://www.home-assistant.io/integrations/jewish_calendar",
"requirements": ["hdate==0.9.3"],
"requirements": ["hdate==0.9.5"],
"dependencies": [],
"codeowners": ["@tsvi"]
}

View File

@ -73,7 +73,7 @@ class JewishCalendarSensor(Entity):
_LOGGER.debug("Now: %s Sunset: %s", now, sunset)
date = hdate.HDate(today, diaspora=self._diaspora, hebrew=self._hebrew)
daytime_date = hdate.HDate(today, diaspora=self._diaspora, hebrew=self._hebrew)
# The Jewish day starts after darkness (called "tzais") and finishes at
# sunset ("shkia"). The time in between is a gray area (aka "Bein
@ -82,16 +82,16 @@ class JewishCalendarSensor(Entity):
# For some sensors, it is more interesting to consider the date to be
# tomorrow based on sunset ("shkia"), for others based on "tzais".
# Hence the following variables.
after_tzais_date = after_shkia_date = date
after_tzais_date = after_shkia_date = daytime_date
today_times = self.make_zmanim(today)
if now > sunset:
after_shkia_date = date.next_day
after_shkia_date = daytime_date.next_day
if today_times.havdalah and now > today_times.havdalah:
after_tzais_date = date.next_day
after_tzais_date = daytime_date.next_day
self._state = self.get_state(after_shkia_date, after_tzais_date)
self._state = self.get_state(daytime_date, after_shkia_date, after_tzais_date)
_LOGGER.debug("New value for %s: %s", self._type, self._state)
def make_zmanim(self, date):
@ -112,7 +112,7 @@ class JewishCalendarSensor(Entity):
return {}
def get_state(self, after_shkia_date, after_tzais_date):
def get_state(self, daytime_date, after_shkia_date, after_tzais_date):
"""For a given type of sensor, return the state."""
# Terminology note: by convention in py-libhdate library, "upcoming"
# refers to "current" or "upcoming" dates.
@ -128,6 +128,8 @@ class JewishCalendarSensor(Entity):
return after_shkia_date.holiday_description
if self._type == "omer_count":
return after_shkia_date.omer_day
if self._type == "daf_yomi":
return daytime_date.daf_yomi
return None
@ -157,7 +159,7 @@ class JewishCalendarTimeSensor(JewishCalendarSensor):
return attrs
def get_state(self, after_shkia_date, after_tzais_date):
def get_state(self, daytime_date, after_shkia_date, after_tzais_date):
"""For a given type of sensor, return the state."""
if self._type == "upcoming_shabbat_candle_lighting":
times = self.make_zmanim(

View File

@ -659,7 +659,7 @@ hass-nabucasa==0.31
hbmqtt==0.9.5
# homeassistant.components.jewish_calendar
hdate==0.9.3
hdate==0.9.5
# homeassistant.components.heatmiser
heatmiserV3==1.1.18

View File

@ -242,7 +242,7 @@ hass-nabucasa==0.31
hbmqtt==0.9.5
# homeassistant.components.jewish_calendar
hdate==0.9.3
hdate==0.9.5
# homeassistant.components.here_travel_time
herepy==2.0.0

View File

@ -568,3 +568,37 @@ async def test_omer_sensor(hass, test_time, result):
await hass.async_block_till_done()
assert hass.states.get("sensor.test_day_of_the_omer").state == result
DAFYOMI_PARAMS = [
(dt(2014, 4, 28, 0), "Beitzah 29"),
(dt(2020, 1, 4, 0), "Niddah 73"),
(dt(2020, 1, 5, 0), "Berachos 2"),
(dt(2020, 3, 7, 0), "Berachos 64"),
(dt(2020, 3, 8, 0), "Shabbos 2"),
]
DAFYOMI_TEST_IDS = [
"randomly_picked_date",
"end_of_cycle13",
"start_of_cycle14",
"cycle14_end_of_berachos",
"cycle14_start_of_shabbos",
]
@pytest.mark.parametrize(["test_time", "result"], DAFYOMI_PARAMS, ids=DAFYOMI_TEST_IDS)
async def test_dafyomi_sensor(hass, test_time, result):
"""Test Daf Yomi sensor output."""
test_time = hass.config.time_zone.localize(test_time)
with alter_time(test_time):
assert await async_setup_component(
hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"name": "test"}}
)
await hass.async_block_till_done()
future = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_daf_yomi").state == result