Skip to content

Commit

Permalink
Add support for displaying Daf Yomi (#30628)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
moshekaplan committed Feb 15, 2020
1 parent 481d329 commit 7dff5e7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/jewish_calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"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"],
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/jewish_calendar/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
16 changes: 9 additions & 7 deletions homeassistant/components/jewish_calendar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def async_update(self):

_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
Expand All @@ -82,16 +82,16 @@ async def async_update(self):
# 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):
Expand All @@ -112,7 +112,7 @@ def device_state_attributes(self):

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.
Expand All @@ -128,6 +128,8 @@ def get_state(self, after_shkia_date, after_tzais_date):
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

Expand Down Expand Up @@ -157,7 +159,7 @@ def device_state_attributes(self):

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(
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/components/jewish_calendar/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7dff5e7

Please sign in to comment.