async def async_update(self): """Update the state of the sensor.""" import hdate now = dt_util.as_local(dt_util.now()) _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() upcoming_saturday = today + timedelta((12 - today.weekday()) % 7) sunset = dt_util.as_local(get_astral_event_date( self.hass, SUN_EVENT_SUNSET, today)) _LOGGER.debug("Now: %s Sunset: %s", now, sunset) if now > sunset: today += timedelta(1) date = hdate.HDate( today, diaspora=self.diaspora, hebrew=self._hebrew) upcoming_shabbat = hdate.HDate( upcoming_saturday, diaspora=self.diaspora, hebrew=self._hebrew) if self.type == 'date': self._state = hdate.date.get_hebrew_date( date.h_day, date.h_month, date.h_year, hebrew=self._hebrew) elif self.type == 'weekly_portion': self._state = hdate.date.get_parashe( upcoming_shabbat.get_reading(self.diaspora), hebrew=self._hebrew) elif self.type == 'holiday_name': try: description = next( x.description[self._hebrew] for x in hdate.htables.HOLIDAYS if x.index == date.get_holyday()) if not self._hebrew: self._state = description else: self._state = description.long except StopIteration: self._state = None elif self.type == 'holyness': self._state = hdate.date.get_holyday_type(date.get_holyday()) else: times = hdate.Zmanim( date=today, latitude=self.latitude, longitude=self.longitude, timezone=self.timezone, hebrew=self._hebrew).zmanim self._state = times[self.type].time() _LOGGER.debug("New value: %s", self._state)
async def async_update(self): """Update the state of the sensor.""" now = dt_util.now() _LOGGER.debug("Now: %s Location: %r", now, self._location) today = now.date() sunset = dt_util.as_local( get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today) ) _LOGGER.debug("Now: %s Sunset: %s", now, sunset) 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 # Hashmashot" - literally: "in between the sun and the moon"). # 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 = daytime_date today_times = self.make_zmanim(today) if now > sunset: after_shkia_date = daytime_date.next_day if today_times.havdalah and now > today_times.havdalah: after_tzais_date = daytime_date.next_day self._state = self.get_state(daytime_date, after_shkia_date, after_tzais_date) _LOGGER.debug("New value for %s: %s", self._type, self._state)
async def grab_new_events(self): date = hdate.HDate(datetime.datetime.now(), hebrew=False) async with aiohttp.ClientSession() as session: async with session.get(self.api_url) as response: body = await response.text() sefaria_obj = json.loads(body) if date.is_holiday == False and 0 < date.omer_day < 50: self.events_str = f""" Omer: {date.omer_day} Parasha: {sefaria_obj["calendar_items"][0]["displayValue"]["en"]} Haftarah: {sefaria_obj["calendar_items"][1]["displayValue"]["en"]} Daf Yomi: {sefaria_obj["calendar_items"][2]["displayValue"]["en"]} Mishnah Yomit: {sefaria_obj["calendar_items"][4]["displayValue"]["en"]} Daily Rambam 1 Chapter: {sefaria_obj["calendar_items"][5]["displayValue"]["en"]} Daily Rambam 3 Chapters: {sefaria_obj["calendar_items"][6]["displayValue"]["en"]} Halakha Yomit: {sefaria_obj["calendar_items"][8]["displayValue"]["en"]} """ elif date.is_holiday == False: self.events_str = f""" Parasha: {sefaria_obj["calendar_items"][0]["displayValue"]["en"]} Haftarah: {sefaria_obj["calendar_items"][1]["displayValue"]["en"]} Daf Yomi: {sefaria_obj["calendar_items"][2]["displayValue"]["en"]} Mishnah Yomit: {sefaria_obj["calendar_items"][4]["displayValue"]["en"]} Daily Rambam 1 Chapter: {sefaria_obj["calendar_items"][5]["displayValue"]["en"]} Daily Rambam 3 Chapters: {sefaria_obj["calendar_items"][6]["displayValue"]["en"]} Halakha Yomit: {sefaria_obj["calendar_items"][8]["displayValue"]["en"]} """ else: self.events_str = f"""
async def change_status(self): async with aiohttp.ClientSession() as session: async with session.get(self.api_url) as response: body = await response.text() sefaria_obj = json.loads(body) date = hdate.HDate(datetime.datetime.now(), hebrew=False) status_string = f""" {self.bot.description} | {date.hebrew_date} | Today's Parasha: {sefaria_obj["calendar_items"][0]["displayValue"]["en"]} | Today's Haftarah: {sefaria_obj["calendar_items"][1]["displayValue"]["en"]} """ status_string = status_string.replace("\n", " ") status_string = status_string.replace("\r", " ") status_string = status_string.replace(" ", "") if len(status_string) > 128: status_string = f""" {date.hebrew_date} | Today's Parasha: {sefaria_obj["calendar_items"][0]["displayValue"]["en"]} | Today's Haftarah: {sefaria_obj["calendar_items"][1]["displayValue"]["en"]} """ status_string = status_string.replace("\n", " ") status_string = status_string.replace("\r", " ") status_string = status_string.replace(" ", "") print(f"The status string is {len(status_string)} chars") game = discord.Game(status_string) await self.bot.change_presence(status=discord.Status.online, activity=game)
async def async_update(self): """Update the state of the sensor.""" import hdate today = dt_util.now().date() date = hdate.HDate(today, diaspora=self.diaspora, hebrew=self._hebrew) if self.type == 'date': self._state = hdate.date.get_hebrew_date(date.h_day, date.h_month, date.h_year, hebrew=self._hebrew) elif self.type == 'weekly_portion': self._state = hdate.date.get_parashe(date.get_reading( self.diaspora), hebrew=self._hebrew) elif self.type == 'holiday_name': try: self._state = next(x.description[self._hebrew].long for x in hdate.htables.HOLIDAYS if x.index == date.get_holyday()) except StopIteration: self._state = None elif self.type == 'holyness': self._state = hdate.date.get_holyday_type(date.get_holyday()) else: times = hdate.Zmanim(date=today, latitude=self.latitude, longitude=self.longitude, hebrew=self._hebrew).zmanim self._state = times[self.type].time() _LOGGER.debug("New value: %s", self._state)
async def async_update(self): """Update the state of the sensor.""" import hdate now = dt_util.as_local(dt_util.now()) _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() sunset = dt_util.as_local( get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today)) _LOGGER.debug("Now: %s Sunset: %s", now, sunset) if now > sunset: today += timedelta(1) date = hdate.HDate(today, diaspora=self.diaspora, hebrew=self._hebrew) if self.type == 'date': self._state = date.hebrew_date elif self.type == 'weekly_portion': self._state = date.parasha elif self.type == 'holiday_name': self._state = date.holiday_description elif self.type == 'holyness': self._state = date.holiday_type else: times = hdate.Zmanim(date=today, latitude=self.latitude, longitude=self.longitude, timezone=self.timezone, hebrew=self._hebrew).zmanim self._state = times[self.type].time() _LOGGER.debug("New value: %s", self._state)
async def date_to_hebrew_command(self, ctx, year, month, day): gregorian_date = datetime.datetime(int(year), int(month), int(day)) date = hdate.HDate(gregorian_date, hebrew=False) await create_embed(ctx, date.hebrew_date)
async def hebrew_date_command(self, ctx): date = hdate.HDate(datetime.datetime.now(), hebrew=False) await create_embed(ctx, date.hebrew_date)
async def async_update(self): """Update the state of the sensor.""" import hdate now = dt_util.as_local(dt_util.now()) _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() sunset = dt_util.as_local( get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today)) _LOGGER.debug("Now: %s Sunset: %s", now, sunset) location = hdate.Location(latitude=self.latitude, longitude=self.longitude, timezone=self.timezone, diaspora=self.diaspora) def make_zmanim(date): """Create a Zmanim object.""" return hdate.Zmanim( date=date, location=location, 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 = 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( 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(lagging_date.upcoming_shabbat.gdate) self._state = times.havdalah elif self.type == 'upcoming_havdalah': times = make_zmanim( lagging_date.upcoming_shabbat_or_yom_tov.last_day.gdate) self._state = times.havdalah elif self.type == 'issur_melacha_in_effect': self._state = make_zmanim(now).issur_melacha_in_effect else: times = make_zmanim(today).zmanim self._state = times[self.type].time() _LOGGER.debug("New value: %s", self._state)
async def async_update(self): """Update the state of the sensor.""" import hdate now = dt_util.as_local(dt_util.now()) _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() sunset = dt_util.as_local( get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today)) _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, diaspora=self.diaspora) def make_zmanim(date): """Create a Zmanim object.""" return hdate.Zmanim( date=date, location=location, candle_lighting_offset=self.candle_lighting_offset, havdalah_offset=self.havdalah_offset, hebrew=self._hebrew) 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 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.previous_day.gdate) self._state = times.candle_lighting elif self.type == 'upcoming_shabbat_havdalah': times = make_zmanim(date.upcoming_shabbat.gdate) self._state = times.havdalah elif self.type == 'upcoming_havdalah': times = make_zmanim( date.upcoming_shabbat_or_yom_tov.last_day.gdate) self._state = times.havdalah elif self.type == 'issur_melacha_in_effect': self._state = make_zmanim(now).issur_melacha_in_effect else: times = make_zmanim(today).zmanim self._state = times[self.type].time() _LOGGER.debug("New value: %s", self._state)
async def async_update(self): """Update the state of the sensor.""" now = dt_util.now() _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() sunset = dt_util.as_local( get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today) ) _LOGGER.debug("Now: %s Sunset: %s", now, sunset) def make_zmanim(date): """Create a Zmanim object.""" return hdate.Zmanim( date=date, location=self._location, 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) # The Jewish day starts after darkness (called "tzais") and finishes at # sunset ("shkia"). The time in between is a gray area (aka "Bein # Hashmashot" - literally: "in between the sun and the moon"). # 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 today_times = make_zmanim(today) if now > sunset: after_shkia_date = date.next_day if today_times.havdalah and now > today_times.havdalah: after_tzais_date = date.next_day # Terminology note: by convention in py-libhdate library, "upcoming" # refers to "current" or "upcoming" dates. if self._type == "date": self._state = after_shkia_date.hebrew_date elif self._type == "weekly_portion": # Compute the weekly portion based on the upcoming shabbat. self._state = after_tzais_date.upcoming_shabbat.parasha elif self._type == "holiday_name": self._state = after_shkia_date.holiday_description elif self._type == "holiday_type": self._state = after_shkia_date.holiday_type elif self._type == "upcoming_shabbat_candle_lighting": times = make_zmanim(after_tzais_date.upcoming_shabbat.previous_day.gdate) self._state = times.candle_lighting elif self._type == "upcoming_candle_lighting": times = make_zmanim( after_tzais_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(after_tzais_date.upcoming_shabbat.gdate) self._state = times.havdalah elif self._type == "upcoming_havdalah": times = make_zmanim( after_tzais_date.upcoming_shabbat_or_yom_tov.last_day.gdate ) self._state = times.havdalah elif self._type == "omer_count": self._state = after_shkia_date.omer_day else: times = make_zmanim(today).zmanim self._state = times[self._type].time() _LOGGER.debug("New value: %s", self._state)
def rand_hdate(random_date): """Given a random date, generate a random HDate.""" return hdate.HDate(random_date)