Add sensors to jewish_calendar for upcoming Shabbat times (#19278)
* Initial pass of cleanup for shabbat_times * Switch to async defs * First pass of unit tests + fixture data * Completion of first round of unit tests, 100% passing * Unit tests for state restoring * Style fixes * More style fixes * Lint fix * Add upcoming candelighting and havdalah sensors * Add unit tests, remove havdalah offset * More unit tests + small bugfix for weekly_portion * Add issur melacha sensor * Remove old shabbat_times work-in-progress files * Bump required version of hdate * Add havdalah offset config parameter * Bump hdate version required * Pin hdate requirement * Lint fixes * Changes based on review + API changes for hdate 0.8.7 * Add three-day holiday unit tests * Remove debugging line * Add missing docstring * Fix doc lint comment
This commit is contained in:
parent
4d52adb008
commit
4d187e08d4
4 changed files with 382 additions and 17 deletions
|
@ -17,7 +17,7 @@ from homeassistant.helpers.entity import Entity
|
|||
from homeassistant.helpers.sun import get_astral_event_date
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
REQUIREMENTS = ['hdate==0.7.5']
|
||||
REQUIREMENTS = ['hdate==0.8.7']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -31,11 +31,24 @@ SENSOR_TYPES = {
|
|||
'mga_end_shma': ['Latest time for Shm"a MG"A', 'mdi:calendar-clock'],
|
||||
'plag_mincha': ['Plag Hamincha', 'mdi:weather-sunset-down'],
|
||||
'first_stars': ['T\'set Hakochavim', 'mdi:weather-night'],
|
||||
'upcoming_shabbat_candle_lighting': ['Upcoming Shabbat Candle Lighting',
|
||||
'mdi:candle'],
|
||||
'upcoming_shabbat_havdalah': ['Upcoming Shabbat Havdalah',
|
||||
'mdi:weather-night'],
|
||||
'upcoming_candle_lighting': ['Upcoming Candle Lighting', 'mdi:candle'],
|
||||
'upcoming_havdalah': ['Upcoming Havdalah', 'mdi:weather-night'],
|
||||
'issur_melacha_in_effect': ['Issur Melacha in Effect',
|
||||
'mdi:power-plug-off'],
|
||||
'omer_count': ['Day of the Omer', 'mdi:counter'],
|
||||
}
|
||||
|
||||
CONF_DIASPORA = 'diaspora'
|
||||
CONF_LANGUAGE = 'language'
|
||||
CONF_SENSORS = 'sensors'
|
||||
CONF_CANDLE_LIGHT_MINUTES = 'candle_lighting_minutes_before_sunset'
|
||||
CONF_HAVDALAH_OFFSET_MINUTES = 'havdalah_minutes_after_sunset'
|
||||
|
||||
CANDLE_LIGHT_DEFAULT = 18
|
||||
|
||||
DEFAULT_NAME = 'Jewish Calendar'
|
||||
|
||||
|
@ -46,6 +59,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
vol.Optional(CONF_LONGITUDE): cv.longitude,
|
||||
vol.Optional(CONF_LANGUAGE, default='english'):
|
||||
vol.In(['hebrew', 'english']),
|
||||
vol.Optional(CONF_CANDLE_LIGHT_MINUTES, default=CANDLE_LIGHT_DEFAULT): int,
|
||||
# Default of 0 means use 8.5 degrees / 'three_stars' time.
|
||||
vol.Optional(CONF_HAVDALAH_OFFSET_MINUTES, default=0): int,
|
||||
vol.Optional(CONF_SENSORS, default=['date']):
|
||||
vol.All(cv.ensure_list, vol.Length(min=1), [vol.In(SENSOR_TYPES)]),
|
||||
})
|
||||
|
@ -59,6 +75,8 @@ async def async_setup_platform(
|
|||
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
||||
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
||||
diaspora = config.get(CONF_DIASPORA)
|
||||
candle_lighting_offset = config.get(CONF_CANDLE_LIGHT_MINUTES)
|
||||
havdalah_offset = config.get(CONF_HAVDALAH_OFFSET_MINUTES)
|
||||
|
||||
if None in (latitude, longitude):
|
||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||
|
@ -68,7 +86,8 @@ async def async_setup_platform(
|
|||
for sensor_type in config[CONF_SENSORS]:
|
||||
dev.append(JewishCalSensor(
|
||||
name, language, sensor_type, latitude, longitude,
|
||||
hass.config.time_zone, diaspora))
|
||||
hass.config.time_zone, diaspora, candle_lighting_offset,
|
||||
havdalah_offset))
|
||||
async_add_entities(dev, True)
|
||||
|
||||
|
||||
|
@ -77,7 +96,8 @@ class JewishCalSensor(Entity):
|
|||
|
||||
def __init__(
|
||||
self, name, language, sensor_type, latitude, longitude, timezone,
|
||||
diaspora):
|
||||
diaspora, candle_lighting_offset=CANDLE_LIGHT_DEFAULT,
|
||||
havdalah_offset=0):
|
||||
"""Initialize the Jewish calendar sensor."""
|
||||
self.client_name = name
|
||||
self._name = SENSOR_TYPES[sensor_type][0]
|
||||
|
@ -88,6 +108,8 @@ class JewishCalSensor(Entity):
|
|||
self.longitude = longitude
|
||||
self.timezone = timezone
|
||||
self.diaspora = diaspora
|
||||
self.candle_lighting_offset = candle_lighting_offset
|
||||
self.havdalah_offset = havdalah_offset
|
||||
_LOGGER.debug("Sensor %s initialized", self.type)
|
||||
|
||||
@property
|
||||
|
@ -124,18 +146,45 @@ class JewishCalSensor(Entity):
|
|||
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':
|
||||
self._state = date.parasha
|
||||
# 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 = hdate.Zmanim(
|
||||
date=today, latitude=self.latitude, longitude=self.longitude,
|
||||
timezone=self.timezone, hebrew=self._hebrew).zmanim
|
||||
times = make_zmanim(today).zmanim
|
||||
self._state = times[self.type].time()
|
||||
|
||||
_LOGGER.debug("New value: %s", self._state)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue