2019-04-03 15:40:03 +00:00
|
|
|
"""Platform to retrieve Jewish calendar information for Home Assistant."""
|
2018-09-17 20:43:31 +00:00
|
|
|
import logging
|
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
import hdate
|
|
|
|
|
|
|
|
from homeassistant.const import SUN_EVENT_SUNSET
|
2018-09-17 20:43:31 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2018-10-30 14:21:58 +00:00
|
|
|
from homeassistant.helpers.sun import get_astral_event_date
|
2018-09-17 20:43:31 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
from . import DOMAIN, SENSOR_TYPES
|
2018-09-17 20:43:31 +00:00
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-09-17 20:43:31 +00:00
|
|
|
"""Set up the Jewish calendar sensor platform."""
|
2019-09-06 11:24:10 +00:00
|
|
|
if discovery_info is None:
|
2018-09-17 20:43:31 +00:00
|
|
|
return
|
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
sensors = [
|
|
|
|
JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info)
|
|
|
|
for sensor, sensor_info in SENSOR_TYPES["data"].items()
|
|
|
|
]
|
|
|
|
sensors.extend(
|
2019-10-10 18:57:48 +00:00
|
|
|
JewishCalendarTimeSensor(hass.data[DOMAIN], sensor, sensor_info)
|
2019-09-06 11:24:10 +00:00
|
|
|
for sensor, sensor_info in SENSOR_TYPES["time"].items()
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors)
|
2018-09-17 20:43:31 +00:00
|
|
|
|
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
class JewishCalendarSensor(Entity):
|
2018-09-17 20:43:31 +00:00
|
|
|
"""Representation of an Jewish calendar sensor."""
|
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
def __init__(self, data, sensor, sensor_info):
|
2018-09-17 20:43:31 +00:00
|
|
|
"""Initialize the Jewish calendar sensor."""
|
2019-09-06 11:24:10 +00:00
|
|
|
self._location = data["location"]
|
|
|
|
self._type = sensor
|
|
|
|
self._name = f"{data['name']} {sensor_info[0]}"
|
|
|
|
self._icon = sensor_info[1]
|
|
|
|
self._hebrew = data["language"] == "hebrew"
|
|
|
|
self._candle_lighting_offset = data["candle_lighting_offset"]
|
|
|
|
self._havdalah_offset = data["havdalah_offset"]
|
|
|
|
self._diaspora = data["diaspora"]
|
2018-09-17 20:43:31 +00:00
|
|
|
self._state = None
|
2020-09-07 01:29:17 +00:00
|
|
|
self._prefix = data["prefix"]
|
2019-10-18 03:32:24 +00:00
|
|
|
self._holiday_attrs = {}
|
2018-09-17 20:43:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-06 11:24:10 +00:00
|
|
|
return self._name
|
2018-09-17 20:43:31 +00:00
|
|
|
|
2020-09-07 01:29:17 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Generate a unique id."""
|
|
|
|
return f"{self._prefix}_{self._type}"
|
|
|
|
|
2018-09-17 20:43:31 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to display in the front end."""
|
2019-09-06 11:24:10 +00:00
|
|
|
return self._icon
|
2018-09-17 20:43:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update the state of the sensor."""
|
2019-09-06 11:24:10 +00:00
|
|
|
now = dt_util.now()
|
2019-10-10 18:57:48 +00:00
|
|
|
_LOGGER.debug("Now: %s Location: %r", now, self._location)
|
2018-10-30 14:21:58 +00:00
|
|
|
|
|
|
|
today = now.date()
|
2019-07-31 19:25:30 +00:00
|
|
|
sunset = dt_util.as_local(
|
|
|
|
get_astral_event_date(self.hass, SUN_EVENT_SUNSET, today)
|
|
|
|
)
|
2018-10-30 14:21:58 +00:00
|
|
|
|
|
|
|
_LOGGER.debug("Now: %s Sunset: %s", now, sunset)
|
|
|
|
|
2020-02-15 23:00:17 +00:00
|
|
|
daytime_date = hdate.HDate(today, diaspora=self._diaspora, hebrew=self._hebrew)
|
2019-02-11 22:34:48 +00:00
|
|
|
|
2019-09-06 11:24:10 +00:00
|
|
|
# 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.
|
2020-02-15 23:00:17 +00:00
|
|
|
after_tzais_date = after_shkia_date = daytime_date
|
2019-10-10 18:57:48 +00:00
|
|
|
today_times = self.make_zmanim(today)
|
2019-09-06 11:24:10 +00:00
|
|
|
|
|
|
|
if now > sunset:
|
2020-02-15 23:00:17 +00:00
|
|
|
after_shkia_date = daytime_date.next_day
|
2019-09-06 11:24:10 +00:00
|
|
|
|
2019-02-11 22:34:48 +00:00
|
|
|
if today_times.havdalah and now > today_times.havdalah:
|
2020-02-15 23:00:17 +00:00
|
|
|
after_tzais_date = daytime_date.next_day
|
2019-02-11 22:34:48 +00:00
|
|
|
|
2020-02-15 23:00:17 +00:00
|
|
|
self._state = self.get_state(daytime_date, after_shkia_date, after_tzais_date)
|
2019-10-10 18:57:48 +00:00
|
|
|
_LOGGER.debug("New value for %s: %s", self._type, self._state)
|
|
|
|
|
|
|
|
def make_zmanim(self, 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,
|
|
|
|
)
|
|
|
|
|
2019-10-18 03:32:24 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2020-10-06 16:08:53 +00:00
|
|
|
if self._type != "holiday":
|
|
|
|
return {}
|
|
|
|
return self._holiday_attrs
|
2019-10-18 03:32:24 +00:00
|
|
|
|
2020-02-15 23:00:17 +00:00
|
|
|
def get_state(self, daytime_date, after_shkia_date, after_tzais_date):
|
2019-10-10 18:57:48 +00:00
|
|
|
"""For a given type of sensor, return the state."""
|
2019-02-11 22:34:48 +00:00
|
|
|
# Terminology note: by convention in py-libhdate library, "upcoming"
|
|
|
|
# refers to "current" or "upcoming" dates.
|
2019-09-06 11:24:10 +00:00
|
|
|
if self._type == "date":
|
2019-10-10 18:57:48 +00:00
|
|
|
return after_shkia_date.hebrew_date
|
|
|
|
if self._type == "weekly_portion":
|
2019-01-11 00:27:34 +00:00
|
|
|
# Compute the weekly portion based on the upcoming shabbat.
|
2019-10-10 18:57:48 +00:00
|
|
|
return after_tzais_date.upcoming_shabbat.parasha
|
2019-10-18 03:32:24 +00:00
|
|
|
if self._type == "holiday":
|
|
|
|
self._holiday_attrs["id"] = after_shkia_date.holiday_name
|
2019-11-27 19:52:03 +00:00
|
|
|
self._holiday_attrs["type"] = after_shkia_date.holiday_type.name
|
|
|
|
self._holiday_attrs["type_id"] = after_shkia_date.holiday_type.value
|
2019-10-10 18:57:48 +00:00
|
|
|
return after_shkia_date.holiday_description
|
|
|
|
if self._type == "omer_count":
|
|
|
|
return after_shkia_date.omer_day
|
2020-02-15 23:00:17 +00:00
|
|
|
if self._type == "daf_yomi":
|
|
|
|
return daytime_date.daf_yomi
|
2019-10-10 18:57:48 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class JewishCalendarTimeSensor(JewishCalendarSensor):
|
|
|
|
"""Implement attrbutes for sensors returning times."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return dt_util.as_utc(self._state) if self._state is not None else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
return "timestamp"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attrs = {}
|
|
|
|
|
|
|
|
if self._state is None:
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
attrs["timestamp"] = self._state.timestamp()
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
2020-02-15 23:00:17 +00:00
|
|
|
def get_state(self, daytime_date, after_shkia_date, after_tzais_date):
|
2019-10-10 18:57:48 +00:00
|
|
|
"""For a given type of sensor, return the state."""
|
|
|
|
if self._type == "upcoming_shabbat_candle_lighting":
|
|
|
|
times = self.make_zmanim(
|
|
|
|
after_tzais_date.upcoming_shabbat.previous_day.gdate
|
|
|
|
)
|
|
|
|
return times.candle_lighting
|
|
|
|
if self._type == "upcoming_candle_lighting":
|
|
|
|
times = self.make_zmanim(
|
2019-09-06 11:24:10 +00:00
|
|
|
after_tzais_date.upcoming_shabbat_or_yom_tov.first_day.previous_day.gdate
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-10-10 18:57:48 +00:00
|
|
|
return times.candle_lighting
|
|
|
|
if self._type == "upcoming_shabbat_havdalah":
|
|
|
|
times = self.make_zmanim(after_tzais_date.upcoming_shabbat.gdate)
|
|
|
|
return times.havdalah
|
|
|
|
if self._type == "upcoming_havdalah":
|
|
|
|
times = self.make_zmanim(
|
2019-09-06 11:24:10 +00:00
|
|
|
after_tzais_date.upcoming_shabbat_or_yom_tov.last_day.gdate
|
|
|
|
)
|
2019-10-10 18:57:48 +00:00
|
|
|
return times.havdalah
|
|
|
|
|
|
|
|
times = self.make_zmanim(dt_util.now()).zmanim
|
|
|
|
return times[self._type]
|