From a4433fd99c9e61786eec167771b109240ef072da Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 22 Aug 2019 17:45:05 +0300 Subject: [PATCH 01/39] Move jewish calendar to its own platform --- .../components/jewish_calendar/__init__.py | 118 ++++++++++++ .../jewish_calendar/binary_sensor.py | 70 +++++++ .../components/jewish_calendar/sensor.py | 175 +++++------------- .../components/jewish_calendar/test_sensor.py | 76 +++----- 4 files changed, 258 insertions(+), 181 deletions(-) create mode 100644 homeassistant/components/jewish_calendar/binary_sensor.py diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 93a60e363e1aa2..c507d2ef486c42 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -1 +1,119 @@ """The jewish_calendar component.""" +import logging + +import voluptuous as vol + +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME +from homeassistant.helpers.discovery import async_load_platform +import homeassistant.helpers.config_validation as cv + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "jewish_calendar" + +SENSOR_TYPES = { + "binary": { + "issur_melacha_in_effect": ["Issur Melacha in Effect", "mdi:power-plug-off"] + }, + "data": { + "date": ["Date", "mdi:judaism"], + "weekly_portion": ["Parshat Hashavua", "mdi:book-open-variant"], + "holiday_name": ["Holiday", "mdi:calendar-star"], + "holiday_type": ["Holiday type", "mdi:counter"], + "omer_count": ["Day of the Omer", "mdi:counter"], + }, + "time": { + "first_light": ["Alot Hashachar", "mdi:weather-sunset-up"], + "gra_end_shma": ['Latest time for Shm"a GR"A', "mdi:calendar-clock"], + "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"], + }, +} + +CONF_DIASPORA = "diaspora" +CONF_LANGUAGE = "language" +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" + +CONFIG_SCHEMA = vol.Schema( + { + DOMAIN: vol.Schema( + { + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Optional(CONF_DIASPORA, default=False): cv.boolean, + vol.Optional(CONF_LATITUDE): cv.latitude, + 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, + } + ) + }, + extra=vol.ALLOW_EXTRA, +) + + +async def async_setup(hass, config): + """Set up the Jewish Calendar component.""" + import hdate + + _LOGGER.debug("Configuration loaded: %s", config) + + name = config[DOMAIN].get(CONF_NAME) + language = config[DOMAIN].get(CONF_LANGUAGE) + + latitude = config[DOMAIN].get(CONF_LATITUDE, hass.config.latitude) + longitude = config[DOMAIN].get(CONF_LONGITUDE, hass.config.longitude) + diaspora = config[DOMAIN].get(CONF_DIASPORA) + + candle_lighting_offset = config[DOMAIN].get(CONF_CANDLE_LIGHT_MINUTES) + havdalah_offset = config[DOMAIN].get(CONF_HAVDALAH_OFFSET_MINUTES) + + if None in (latitude, longitude): + _LOGGER.error("Latitude or longitude not set in Home Assistant config") + return + + location = hdate.Location( + latitude=latitude, + longitude=longitude, + timezone=hass.config.time_zone, + diaspora=diaspora, + ) + + _LOGGER.debug("Location created: %r", location) + + hass.data[DOMAIN] = { + "location": location, + "name": name, + "language": language, + "candle_lighting_offset": candle_lighting_offset, + "havdalah_offset": havdalah_offset, + "diaspora": diaspora, + } + + _LOGGER.debug("Loading platform with data %s", hass.data[DOMAIN]) + + hass.async_create_task(async_load_platform(hass, "sensor", DOMAIN, {}, config)) + + hass.async_create_task( + async_load_platform(hass, "binary_sensor", DOMAIN, {}, config) + ) + + return True diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py new file mode 100644 index 00000000000000..acfebba408da72 --- /dev/null +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -0,0 +1,70 @@ +"""Support for Jewish Calendar binary sensors.""" +import logging + +from homeassistant.components.binary_sensor import BinarySensorDevice +import homeassistant.util.dt as dt_util + +from . import DOMAIN, SENSOR_TYPES + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): + """Set up the Jewish Calendar binary sensor devices.""" + if discovery_info is None: + return + + for sensor, sensor_info in SENSOR_TYPES["binary"].items(): + async_add_entities( + [JewishCalendarBinarySensor(hass.data[DOMAIN], sensor, sensor_info)] + ) + + +class JewishCalendarBinarySensor(BinarySensorDevice): + """Representation of an Jewish Calendar binary sensor.""" + + def __init__(self, data, sensor, sensor_info): + """Initialize the binary sensor.""" + 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._state = False + _LOGGER.debug("Sensor %s initialized", self._type) + + @property + def icon(self): + """Return the icon of the entity.""" + return self._icon + + @property + def name(self): + """Return the name of the entity.""" + return self._name + + @property + def should_poll(self): + """No polling needed.""" + return False + + @property + def is_on(self): + """Return true if sensor is on.""" + import hdate + + zmanim = hdate.Zmanim( + date=dt_util.now(), + location=self._location, + candle_lighting_offset=self._candle_lighting_offset, + havdalah_offset=self._havdalah_offset, + hebrew=self._hebrew, + ) + + if self._type == "issur_melacha_in_effect": + self._state = zmanim.issur_melacha_in_effect + else: + self._state = False + _LOGGER.error("Undefined sensor type %s", self._type) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index d298aee91436b4..ee2503d9bc11b9 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -1,140 +1,59 @@ """Platform to retrieve Jewish calendar information for Home Assistant.""" import logging -import voluptuous as vol - -from homeassistant.components.sensor import PLATFORM_SCHEMA -from homeassistant.const import ( - CONF_LATITUDE, - CONF_LONGITUDE, - CONF_NAME, - SUN_EVENT_SUNSET, -) -import homeassistant.helpers.config_validation as cv +from homeassistant.const import SUN_EVENT_SUNSET from homeassistant.helpers.entity import Entity from homeassistant.helpers.sun import get_astral_event_date import homeassistant.util.dt as dt_util -_LOGGER = logging.getLogger(__name__) +from . import DOMAIN, SENSOR_TYPES -SENSOR_TYPES = { - "date": ["Date", "mdi:judaism"], - "weekly_portion": ["Parshat Hashavua", "mdi:book-open-variant"], - "holiday_name": ["Holiday", "mdi:calendar-star"], - "holyness": ["Holyness", "mdi:counter"], - "first_light": ["Alot Hashachar", "mdi:weather-sunset-up"], - "gra_end_shma": ['Latest time for Shm"a GR"A', "mdi:calendar-clock"], - "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" - -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - { - vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, - vol.Optional(CONF_DIASPORA, default=False): cv.boolean, - vol.Optional(CONF_LATITUDE): cv.latitude, - 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)] - ), - } -) +_LOGGER = logging.getLogger(__name__) async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Jewish calendar sensor platform.""" - language = config.get(CONF_LANGUAGE) - name = config.get(CONF_NAME) - 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") + _LOGGER.debug("Configuration loaded: %s", config) + + if discovery_info is None: return - dev = [] - for sensor_type in config[CONF_SENSORS]: - dev.append( - JewishCalSensor( - name, - language, - sensor_type, - latitude, - longitude, - hass.config.time_zone, - diaspora, - candle_lighting_offset, - havdalah_offset, - ) + for sensor, sensor_info in SENSOR_TYPES["data"].items(): + async_add_entities( + [JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info)] + ) + + for sensor, sensor_info in SENSOR_TYPES["time"].items(): + async_add_entities( + [JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info)] ) - async_add_entities(dev, True) -class JewishCalSensor(Entity): +class JewishCalendarSensor(Entity): """Representation of an Jewish calendar sensor.""" - def __init__( - self, - name, - language, - sensor_type, - latitude, - longitude, - timezone, - diaspora, - candle_lighting_offset=CANDLE_LIGHT_DEFAULT, - havdalah_offset=0, - ): + def __init__(self, data, sensor, sensor_info): """Initialize the Jewish calendar sensor.""" - self.client_name = name - self._name = SENSOR_TYPES[sensor_type][0] - self.type = sensor_type - self._hebrew = language == "hebrew" + 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"] self._state = None - self.latitude = latitude - 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) + _LOGGER.debug("Sensor %s initialized", self._type) @property def name(self): """Return the name of the sensor.""" - return f"{self.client_name} {self._name}" + return self._name @property def icon(self): """Icon to display in the front end.""" - return SENSOR_TYPES[self.type][1] + return self._icon @property def state(self): @@ -145,8 +64,7 @@ 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) + now = dt_util.now() today = now.date() sunset = dt_util.as_local( @@ -155,24 +73,17 @@ async def async_update(self): _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, + 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) + date = hdate.HDate(today, diaspora=self._diaspora, hebrew=self._hebrew) lagging_date = date # Advance Hebrew date if sunset has passed. @@ -186,35 +97,35 @@ def make_zmanim(date): # Terminology note: by convention in py-libhdate library, "upcoming" # refers to "current" or "upcoming" dates. - if self.type == "date": + if self._type == "date": self._state = date.hebrew_date - elif self.type == "weekly_portion": + 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": + elif self._type == "holiday_name": self._state = date.holiday_description - elif self.type == "holyness": + elif self._type == "holyness": self._state = date.holiday_type - elif self.type == "upcoming_shabbat_candle_lighting": + 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": + 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": + elif self._type == "upcoming_shabbat_havdalah": times = make_zmanim(lagging_date.upcoming_shabbat.gdate) self._state = times.havdalah - elif self.type == "upcoming_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": + elif self._type == "issur_melacha_in_effect": self._state = make_zmanim(now).issur_melacha_in_effect - elif self.type == "omer_count": + elif self._type == "omer_count": self._state = date.omer_day else: times = make_zmanim(today).zmanim - self._state = times[self.type].time() + self._state = times[self._type].time() _LOGGER.debug("New value: %s", self._state) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index f8c214f9800007..872ef4fb846f8c 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -8,12 +8,9 @@ from homeassistant.util.async_ import run_coroutine_threadsafe from homeassistant.util.dt import get_time_zone, set_default_time_zone -from homeassistant.setup import setup_component -from homeassistant.components.jewish_calendar.sensor import ( - JewishCalSensor, - CANDLE_LIGHT_DEFAULT, -) -from tests.common import get_test_home_assistant +from homeassistant.setup import async_setup_component +from homeassistant.components import jewish_calendar +from homeassistant.components.jewish_calendar.sensor import JewishCalendarSensor _LatLng = namedtuple("_LatLng", ["lat", "lng"]) @@ -26,7 +23,7 @@ def make_nyc_test_params(dtime, results, havdalah_offset=0): """Make test params for NYC.""" return ( dtime, - CANDLE_LIGHT_DEFAULT, + jewish_calendar.CANDLE_LIGHT_DEFAULT, havdalah_offset, True, "America/New_York", @@ -40,7 +37,7 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): """Make test params for Jerusalem.""" return ( dtime, - CANDLE_LIGHT_DEFAULT, + jewish_calendar.CANDLE_LIGHT_DEFAULT, havdalah_offset, False, "Asia/Jerusalem", @@ -50,52 +47,33 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): ) +async def test_jewish_calendar_min_config(hass): + """Test minimum jewish calendar configuration.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {}} + ) + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None + + +async def test_jewish_calendar_hebrew(hass): + """Test jewish calendar sensor with language set to hebrew.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"language": "hebrew"}} + ) + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None + + class TestJewishCalenderSensor: """Test the Jewish Calendar sensor.""" - # pylint: disable=attribute-defined-outside-init - def setup_method(self, method): - """Set up things to run when tests begin.""" - self.hass = get_test_home_assistant() - def teardown_method(self, method): """Stop everything that was started.""" self.hass.stop() # Reset the default timezone, so we don't affect other tests set_default_time_zone(get_time_zone("UTC")) - def test_jewish_calendar_min_config(self): - """Test minimum jewish calendar configuration.""" - config = {"sensor": {"platform": "jewish_calendar"}} - assert setup_component(self.hass, "sensor", config) - - def test_jewish_calendar_hebrew(self): - """Test jewish calendar sensor with language set to hebrew.""" - config = {"sensor": {"platform": "jewish_calendar", "language": "hebrew"}} - - assert setup_component(self.hass, "sensor", config) - - def test_jewish_calendar_multiple_sensors(self): - """Test jewish calendar sensor with multiple sensors setup.""" - config = { - "sensor": { - "platform": "jewish_calendar", - "sensors": [ - "date", - "weekly_portion", - "holiday_name", - "holyness", - "first_light", - "gra_end_shma", - "mga_end_shma", - "plag_mincha", - "first_stars", - ], - } - } - - assert setup_component(self.hass, "sensor", config) - test_params = [ ( dt(2018, 9, 3), @@ -237,7 +215,7 @@ def test_jewish_calendar_sensor( test_time = time_zone.localize(cur_time) self.hass.config.latitude = latitude self.hass.config.longitude = longitude - sensor = JewishCalSensor( + sensor = JewishCalendarSensor( name="test", language=language, sensor_type=sensor, @@ -564,7 +542,7 @@ def test_shabbat_times_sensor( if sensor_type.startswith("hebrew_"): language = "hebrew" sensor_type = sensor_type.replace("hebrew_", "") - sensor = JewishCalSensor( + sensor = JewishCalendarSensor( name="test", language=language, sensor_type=sensor_type, @@ -642,7 +620,7 @@ def test_issur_melacha_sensor( test_time = time_zone.localize(now) self.hass.config.latitude = latitude self.hass.config.longitude = longitude - sensor = JewishCalSensor( + sensor = JewishCalendarSensor( name="test", language="english", sensor_type="issur_melacha_in_effect", @@ -718,7 +696,7 @@ def test_omer_sensor( test_time = time_zone.localize(now) self.hass.config.latitude = latitude self.hass.config.longitude = longitude - sensor = JewishCalSensor( + sensor = JewishCalendarSensor( name="test", language="english", sensor_type="omer_count", From ee155a5f04c6871c8fa8f85ea985725381d050d2 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Mon, 26 Aug 2019 12:43:43 +0300 Subject: [PATCH 02/39] Fix tests for Jewish Calendar platform As part of this, move tests to use async_setup_component instead of testing JewishCalendarSensor as suggested by @MartinHjelmare here: https://github.com/home-assistant/home-assistant/pull/24958#pullrequestreview-259394226 --- .../components/jewish_calendar/test_sensor.py | 239 ++++++++++-------- 1 file changed, 133 insertions(+), 106 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 872ef4fb846f8c..0303b06b58b0c3 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -6,11 +6,13 @@ import pytest -from homeassistant.util.async_ import run_coroutine_threadsafe -from homeassistant.util.dt import get_time_zone, set_default_time_zone +from homeassistant.util.dt import ( + get_time_zone, + set_default_time_zone, + DEFAULT_TIME_ZONE, +) from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar -from homeassistant.components.jewish_calendar.sensor import JewishCalendarSensor _LatLng = namedtuple("_LatLng", ["lat", "lng"]) @@ -47,32 +49,28 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): ) -async def test_jewish_calendar_min_config(hass): - """Test minimum jewish calendar configuration.""" - assert await async_setup_component( - hass, jewish_calendar.DOMAIN, {"jewish_calendar": {}} - ) - await hass.async_block_till_done() - assert hass.states.get("sensor.jewish_calendar_date") is not None - - -async def test_jewish_calendar_hebrew(hass): - """Test jewish calendar sensor with language set to hebrew.""" - assert await async_setup_component( - hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"language": "hebrew"}} - ) - await hass.async_block_till_done() - assert hass.states.get("sensor.jewish_calendar_date") is not None - - class TestJewishCalenderSensor: """Test the Jewish Calendar sensor.""" - def teardown_method(self, method): - """Stop everything that was started.""" - self.hass.stop() - # Reset the default timezone, so we don't affect other tests - set_default_time_zone(get_time_zone("UTC")) + def teardown(self): + """Restore.""" + set_default_time_zone(DEFAULT_TIME_ZONE) + + async def test_jewish_calendar_min_config(self, hass): + """Test minimum jewish calendar configuration.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {}} + ) + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None + + async def test_jewish_calendar_hebrew(self, hass): + """Test jewish calendar sensor with language set to hebrew.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"language": "hebrew"}} + ) + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None test_params = [ ( @@ -206,33 +204,46 @@ def teardown_method(self, method): test_params, ids=test_ids, ) - def test_jewish_calendar_sensor( - self, cur_time, tzname, latitude, longitude, language, sensor, diaspora, result + async def test_jewish_calendar_sensor( + self, + hass, + cur_time, + tzname, + latitude, + longitude, + language, + sensor, + diaspora, + result, ): """Test Jewish calendar sensor output.""" time_zone = get_time_zone(tzname) set_default_time_zone(time_zone) test_time = time_zone.localize(cur_time) - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude - sensor = JewishCalendarSensor( - name="test", - language=language, - sensor_type=sensor, - latitude=latitude, - longitude=longitude, - timezone=time_zone, - diaspora=diaspora, - ) - sensor.hass = self.hass + hass.config.latitude = latitude + hass.config.longitude = longitude + with patch("homeassistant.util.dt.now", return_value=test_time): - run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result() - assert sensor.state == result + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + } + }, + ) + await hass.async_block_till_done() + assert hass.states.get(f"sensor.test_{sensor}").state == result shabbat_params = [ make_nyc_test_params( dt(2018, 9, 1, 16, 0), { + "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "upcoming_havdalah": dt(2018, 9, 1, 20, 14), "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), "weekly_portion": "Ki Tavo", @@ -242,6 +253,8 @@ def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 1, 16, 0), { + "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "upcoming_havdalah": dt(2018, 9, 1, 20, 22), "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), "weekly_portion": "Ki Tavo", @@ -263,6 +276,8 @@ def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 1, 20, 21), { + "upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "upcoming_havdalah": dt(2018, 9, 8, 20, 2), "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), "weekly_portion": "Nitzavim", @@ -272,6 +287,8 @@ def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 7, 13, 1), { + "upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "upcoming_havdalah": dt(2018, 9, 8, 20, 2), "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), "weekly_portion": "Nitzavim", @@ -320,6 +337,8 @@ def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 28, 21, 25), { + "upcoming_candle_lighting": dt(2018, 9, 28, 18, 28), + "upcoming_havdalah": dt(2018, 9, 29, 19, 25), "upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), "upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), "weekly_portion": "none", @@ -394,6 +413,8 @@ def test_jewish_calendar_sensor( make_jerusalem_test_params( dt(2018, 10, 1, 21, 25), { + "upcoming_candle_lighting": dt(2018, 10, 5, 18, 3), + "upcoming_havdalah": dt(2018, 10, 6, 18, 56), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), "weekly_portion": "Bereshit", @@ -506,8 +527,9 @@ def test_jewish_calendar_sensor( shabbat_params, ids=shabbat_test_ids, ) - def test_shabbat_times_sensor( + async def test_shabbat_times_sensor( self, + hass, now, candle_lighting, havdalah, @@ -524,39 +546,33 @@ def test_shabbat_times_sensor( for sensor_type, value in result.items(): if isinstance(value, dt): result[sensor_type] = time_zone.localize(value) - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude + hass.config.latitude = latitude + hass.config.longitude = longitude - if ( - "upcoming_shabbat_candle_lighting" in result - and "upcoming_candle_lighting" not in result - ): - result["upcoming_candle_lighting"] = result[ - "upcoming_shabbat_candle_lighting" - ] - if "upcoming_shabbat_havdalah" in result and "upcoming_havdalah" not in result: - result["upcoming_havdalah"] = result["upcoming_shabbat_havdalah"] + with patch("homeassistant.util.dt.now", return_value=test_time): + for sensor_type, result_value in result.items(): + language = "english" + if sensor_type.startswith("hebrew_"): + language = "hebrew" + sensor_type = sensor_type.replace("hebrew_", "") - for sensor_type, result_value in result.items(): - language = "english" - if sensor_type.startswith("hebrew_"): - language = "hebrew" - sensor_type = sensor_type.replace("hebrew_", "") - sensor = JewishCalendarSensor( - name="test", - language=language, - sensor_type=sensor_type, - latitude=latitude, - longitude=longitude, - timezone=time_zone, - diaspora=diaspora, - havdalah_offset=havdalah, - candle_lighting_offset=candle_lighting, - ) - sensor.hass = self.hass - with patch("homeassistant.util.dt.now", return_value=test_time): - run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result() - assert sensor.state == result_value, "Value for {}".format(sensor_type) + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + assert ( + hass.states.get(f"sensor.test_{sensor_type}").state == result_value + ), f"Value for {sensor_type}" melacha_params = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), True), @@ -603,8 +619,9 @@ def test_shabbat_times_sensor( melacha_params, ids=melacha_test_ids, ) - def test_issur_melacha_sensor( + async def test_issur_melacha_sensor( self, + hass, now, candle_lighting, havdalah, @@ -618,23 +635,28 @@ def test_issur_melacha_sensor( time_zone = get_time_zone(tzname) set_default_time_zone(time_zone) test_time = time_zone.localize(now) - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude - sensor = JewishCalendarSensor( - name="test", - language="english", - sensor_type="issur_melacha_in_effect", - latitude=latitude, - longitude=longitude, - timezone=time_zone, - diaspora=diaspora, - havdalah_offset=havdalah, - candle_lighting_offset=candle_lighting, - ) - sensor.hass = self.hass + hass.config.latitude = latitude + hass.config.longitude = longitude + with patch("homeassistant.util.dt.now", return_value=test_time): - run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result() - assert sensor.state == result + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + assert ( + hass.states.get(f"binary_sensor.test_issur_melacha_in_effect").state + == result + ) omer_params = [ make_nyc_test_params(dt(2019, 4, 21, 0, 0), 1), @@ -679,8 +701,9 @@ def test_issur_melacha_sensor( omer_params, ids=omer_test_ids, ) - def test_omer_sensor( + async def test_omer_sensor( self, + hass, now, candle_lighting, havdalah, @@ -694,18 +717,22 @@ def test_omer_sensor( time_zone = get_time_zone(tzname) set_default_time_zone(time_zone) test_time = time_zone.localize(now) - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude - sensor = JewishCalendarSensor( - name="test", - language="english", - sensor_type="omer_count", - latitude=latitude, - longitude=longitude, - timezone=time_zone, - diaspora=diaspora, - ) - sensor.hass = self.hass + hass.config.latitude = latitude + hass.config.longitude = longitude + with patch("homeassistant.util.dt.now", return_value=test_time): - run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result() - assert sensor.state == result + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + assert hass.states.get(f"sensor.test_omer_count").state == result From 5e1c8dcf9a248c0e178d7e2f1266d5c623c64378 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 13:05:53 +0300 Subject: [PATCH 03/39] Get sensors to update during test --- .../components/jewish_calendar/__init__.py | 2 +- .../components/jewish_calendar/sensor.py | 2 +- .../components/jewish_calendar/test_sensor.py | 178 ++++++++++-------- 3 files changed, 106 insertions(+), 76 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index c507d2ef486c42..7b86cf4da9486a 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -18,7 +18,7 @@ "data": { "date": ["Date", "mdi:judaism"], "weekly_portion": ["Parshat Hashavua", "mdi:book-open-variant"], - "holiday_name": ["Holiday", "mdi:calendar-star"], + "holiday_name": ["Holiday name", "mdi:calendar-star"], "holiday_type": ["Holiday type", "mdi:counter"], "omer_count": ["Day of the Omer", "mdi:counter"], }, diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index ee2503d9bc11b9..72ba750868e5cd 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -104,7 +104,7 @@ def make_zmanim(date): self._state = lagging_date.upcoming_shabbat.parasha elif self._type == "holiday_name": self._state = date.holiday_description - elif self._type == "holyness": + elif self._type == "holiday_type": self._state = date.holiday_type elif self._type == "upcoming_shabbat_candle_lighting": times = make_zmanim(lagging_date.upcoming_shabbat.previous_day.gdate) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 0303b06b58b0c3..00b7befad27d3e 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -1,5 +1,6 @@ """The tests for the Jewish calendar sensor platform.""" from collections import namedtuple +from contextlib import contextmanager from datetime import time from datetime import datetime as dt from unittest.mock import patch @@ -49,6 +50,16 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): ) +@contextmanager +def alter_time(retval): + """Manage multiple time mocks.""" + patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) + patch2 = patch("homeassistant.util.dt.now", return_value=retval) + + with patch1, patch2: + yield + + class TestJewishCalenderSensor: """Test the Jewish Calendar sensor.""" @@ -113,14 +124,14 @@ async def test_jewish_calendar_hebrew(self, hass): False, "Rosh Hashana I", ), - (dt(2018, 9, 10), "UTC", 31.778, 35.235, "english", "holyness", False, 1), + (dt(2018, 9, 10), "UTC", 31.778, 35.235, "english", "holiday_type", False, 1), ( dt(2018, 9, 8), "UTC", 31.778, 35.235, "hebrew", - "weekly_portion", + "parshat_hashavua", False, "נצבים", ), @@ -130,7 +141,7 @@ async def test_jewish_calendar_hebrew(self, hass): 40.7128, -74.0060, "hebrew", - "first_stars", + "t_set_hakochavim", True, time(19, 48), ), @@ -140,7 +151,7 @@ async def test_jewish_calendar_hebrew(self, hass): 31.778, 35.235, "hebrew", - "first_stars", + "t_set_hakochavim", False, time(19, 21), ), @@ -150,7 +161,7 @@ async def test_jewish_calendar_hebrew(self, hass): 31.778, 35.235, "hebrew", - "weekly_portion", + "parshat_hashavua", False, "לך לך", ), @@ -223,20 +234,25 @@ async def test_jewish_calendar_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - with patch("homeassistant.util.dt.now", return_value=test_time): - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - } - }, + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + f"sensor.test_{sensor}" ) - await hass.async_block_till_done() - assert hass.states.get(f"sensor.test_{sensor}").state == result + + assert hass.states.get(f"sensor.test_{sensor}").state == str(result) shabbat_params = [ make_nyc_test_params( @@ -549,30 +565,34 @@ async def test_shabbat_times_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - with patch("homeassistant.util.dt.now", return_value=test_time): - for sensor_type, result_value in result.items(): - language = "english" - if sensor_type.startswith("hebrew_"): - language = "hebrew" - sensor_type = sensor_type.replace("hebrew_", "") - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, + for sensor_type, result_value in result.items(): + language = "english" + if sensor_type.startswith("hebrew_"): + language = "hebrew" + sensor_type = sensor_type.replace("hebrew_", "") + + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + f"sensor.test_{sensor_type}" ) - await hass.async_block_till_done() - assert ( - hass.states.get(f"sensor.test_{sensor_type}").state == result_value - ), f"Value for {sensor_type}" + assert ( + hass.states.get(f"sensor.test_{sensor_type}").state == result_value + ), f"Value for {sensor_type}" melacha_params = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), True), @@ -638,26 +658,31 @@ async def test_issur_melacha_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - with patch("homeassistant.util.dt.now", return_value=test_time): - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() - assert ( - hass.states.get(f"binary_sensor.test_issur_melacha_in_effect").state - == result + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + "binary_sensor.test_issur_melacha_in_effect" ) + assert ( + hass.states.get("binary_sensor.test_issur_melacha_in_effect").state + == result + ) + omer_params = [ make_nyc_test_params(dt(2019, 4, 21, 0, 0), 1), make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), 1), @@ -720,19 +745,24 @@ async def test_omer_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - with patch("homeassistant.util.dt.now", return_value=test_time): - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + "sensor.test_omer_count" ) - await hass.async_block_till_done() - assert hass.states.get(f"sensor.test_omer_count").state == result + + assert hass.states.get("sensor.test_omer_count").state == result From 6b9c0af63b788b4be2978a656acc4ff18c4042aa Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 15:18:46 +0300 Subject: [PATCH 04/39] Use hass.config.set_time_zone instead of directly calling set_default_time_zone in tests --- .../components/jewish_calendar/test_sensor.py | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 00b7befad27d3e..39531fc998f977 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -7,11 +7,7 @@ import pytest -from homeassistant.util.dt import ( - get_time_zone, - set_default_time_zone, - DEFAULT_TIME_ZONE, -) +from homeassistant.util.dt import get_time_zone from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar @@ -63,10 +59,6 @@ def alter_time(retval): class TestJewishCalenderSensor: """Test the Jewish Calendar sensor.""" - def teardown(self): - """Restore.""" - set_default_time_zone(DEFAULT_TIME_ZONE) - async def test_jewish_calendar_min_config(self, hass): """Test minimum jewish calendar configuration.""" assert await async_setup_component( @@ -229,8 +221,9 @@ async def test_jewish_calendar_sensor( ): """Test Jewish calendar sensor output.""" time_zone = get_time_zone(tzname) - set_default_time_zone(time_zone) test_time = time_zone.localize(cur_time) + + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -557,8 +550,9 @@ async def test_shabbat_times_sensor( ): """Test sensor output for upcoming shabbat/yomtov times.""" time_zone = get_time_zone(tzname) - set_default_time_zone(time_zone) test_time = time_zone.localize(now) + + hass.config.set_time_zone(tzname) for sensor_type, value in result.items(): if isinstance(value, dt): result[sensor_type] = time_zone.localize(value) @@ -591,7 +585,7 @@ async def test_shabbat_times_sensor( f"sensor.test_{sensor_type}" ) assert ( - hass.states.get(f"sensor.test_{sensor_type}").state == result_value + hass.states.get(f"sensor.test_{sensor_type}").state == str(result_value) ), f"Value for {sensor_type}" melacha_params = [ @@ -653,8 +647,9 @@ async def test_issur_melacha_sensor( ): """Test Issur Melacha sensor output.""" time_zone = get_time_zone(tzname) - set_default_time_zone(time_zone) test_time = time_zone.localize(now) + + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -740,8 +735,9 @@ async def test_omer_sensor( ): """Test Omer Count sensor output.""" time_zone = get_time_zone(tzname) - set_default_time_zone(time_zone) test_time = time_zone.localize(now) + + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude From 3bbf625309752c6d92cf73b2a113d79d28fd4bfd Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 15:19:33 +0300 Subject: [PATCH 05/39] Cleanup log messages --- homeassistant/components/jewish_calendar/sensor.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 72ba750868e5cd..9d628d3e6e430e 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -13,8 +13,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Jewish calendar sensor platform.""" - _LOGGER.debug("Configuration loaded: %s", config) - if discovery_info is None: return @@ -65,6 +63,7 @@ async def async_update(self): import hdate now = dt_util.now() + _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) today = now.date() sunset = dt_util.as_local( From 65d29c463cc252c1dc83f63726c8769c493a7d93 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 15:20:23 +0300 Subject: [PATCH 06/39] Rename result from weekly_portion to parshat_hashavua --- .../components/jewish_calendar/test_sensor.py | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 39531fc998f977..b0dbaa9a41182f 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -255,8 +255,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 1, 20, 14), "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), - "weekly_portion": "Ki Tavo", - "hebrew_weekly_portion": "כי תבוא", + "parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", }, ), make_nyc_test_params( @@ -266,8 +266,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 1, 20, 22), "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), - "weekly_portion": "Ki Tavo", - "hebrew_weekly_portion": "כי תבוא", + "parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", }, havdalah_offset=50, ), @@ -278,8 +278,8 @@ async def test_jewish_calendar_sensor( "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), "upcoming_havdalah": dt(2018, 9, 1, 20, 14), - "weekly_portion": "Ki Tavo", - "hebrew_weekly_portion": "כי תבוא", + "parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", }, ), make_nyc_test_params( @@ -289,8 +289,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 8, 20, 2), "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "weekly_portion": "Nitzavim", - "hebrew_weekly_portion": "נצבים", + "parshat_hashavua": "Nitzavim", + "hebrew_parshat_hashavua": "נצבים", }, ), make_nyc_test_params( @@ -300,8 +300,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 8, 20, 2), "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "weekly_portion": "Nitzavim", - "hebrew_weekly_portion": "נצבים", + "parshat_hashavua": "Nitzavim", + "hebrew_parshat_hashavua": "נצבים", }, ), make_nyc_test_params( @@ -311,8 +311,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 11, 19, 57), "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "weekly_portion": "Vayeilech", - "hebrew_weekly_portion": "וילך", + "parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", "holiday_name": "Erev Rosh Hashana", "hebrew_holiday_name": "ערב ראש השנה", }, @@ -324,8 +324,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 11, 19, 57), "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "weekly_portion": "Vayeilech", - "hebrew_weekly_portion": "וילך", + "parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", "holiday_name": "Rosh Hashana I", "hebrew_holiday_name": "א' ראש השנה", }, @@ -337,8 +337,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 11, 19, 57), "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "weekly_portion": "Vayeilech", - "hebrew_weekly_portion": "וילך", + "parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", "holiday_name": "Rosh Hashana II", "hebrew_holiday_name": "ב' ראש השנה", }, @@ -350,8 +350,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 9, 29, 19, 25), "upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), "upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), - "weekly_portion": "none", - "hebrew_weekly_portion": "none", + "parshat_hashavua": "none", + "hebrew_parshat_hashavua": "none", }, ), make_nyc_test_params( @@ -361,8 +361,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 2, 19, 20), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", "holiday_name": "Hoshana Raba", "hebrew_holiday_name": "הושענא רבה", }, @@ -374,8 +374,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 2, 19, 20), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", "holiday_name": "Shmini Atzeret", "hebrew_holiday_name": "שמיני עצרת", }, @@ -387,8 +387,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 2, 19, 20), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", "holiday_name": "Simchat Torah", "hebrew_holiday_name": "שמחת תורה", }, @@ -400,8 +400,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 1, 19, 2), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", "holiday_name": "Hoshana Raba", "hebrew_holiday_name": "הושענא רבה", }, @@ -413,8 +413,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 1, 19, 2), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", "holiday_name": "Shmini Atzeret", "hebrew_holiday_name": "שמיני עצרת", }, @@ -426,8 +426,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2018, 10, 6, 18, 56), "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "weekly_portion": "Bereshit", - "hebrew_weekly_portion": "בראשית", + "parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", }, ), make_nyc_test_params( @@ -437,8 +437,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2016, 6, 13, 21, 17), "upcoming_shabbat_candle_lighting": dt(2016, 6, 10, 20, 7), "upcoming_shabbat_havdalah": None, - "weekly_portion": "Bamidbar", - "hebrew_weekly_portion": "במדבר", + "parshat_hashavua": "Bamidbar", + "hebrew_parshat_hashavua": "במדבר", "holiday_name": "Erev Shavuot", "hebrew_holiday_name": "ערב שבועות", }, @@ -450,8 +450,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2016, 6, 13, 21, 17), "upcoming_shabbat_candle_lighting": dt(2016, 6, 17, 20, 10), "upcoming_shabbat_havdalah": dt(2016, 6, 18, 21, 19), - "weekly_portion": "Nasso", - "hebrew_weekly_portion": "נשא", + "parshat_hashavua": "Nasso", + "hebrew_parshat_hashavua": "נשא", "holiday_name": "Shavuot", "hebrew_holiday_name": "שבועות", }, @@ -463,8 +463,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2017, 9, 23, 19, 13), "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "weekly_portion": "Ha'Azinu", - "hebrew_weekly_portion": "האזינו", + "parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", "holiday_name": "Rosh Hashana I", "hebrew_holiday_name": "א' ראש השנה", }, @@ -476,8 +476,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2017, 9, 23, 19, 13), "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "weekly_portion": "Ha'Azinu", - "hebrew_weekly_portion": "האזינו", + "parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", "holiday_name": "Rosh Hashana II", "hebrew_holiday_name": "ב' ראש השנה", }, @@ -489,8 +489,8 @@ async def test_jewish_calendar_sensor( "upcoming_havdalah": dt(2017, 9, 23, 19, 13), "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "weekly_portion": "Ha'Azinu", - "hebrew_weekly_portion": "האזינו", + "parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", "holiday_name": "", "hebrew_holiday_name": "", }, @@ -584,8 +584,8 @@ async def test_shabbat_times_sensor( await hass.helpers.entity_component.async_update_entity( f"sensor.test_{sensor_type}" ) - assert ( - hass.states.get(f"sensor.test_{sensor_type}").state == str(result_value) + assert hass.states.get(f"sensor.test_{sensor_type}").state == str( + result_value ), f"Value for {sensor_type}" melacha_params = [ From a467dbd43c167a5ede8a10d097fd6e3be73afaae Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 16:41:04 +0300 Subject: [PATCH 07/39] Fix english/hebrew tests --- .../components/jewish_calendar/test_sensor.py | 297 +++++++++--------- 1 file changed, 151 insertions(+), 146 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index b0dbaa9a41182f..e1dfa8ae222ff9 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -251,22 +251,22 @@ async def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 1, 16, 0), { - "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_havdalah": dt(2018, 9, 1, 20, 14), - "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), - "parshat_hashavua": "Ki Tavo", + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), + "english_parshat_hashavua": "Ki Tavo", "hebrew_parshat_hashavua": "כי תבוא", }, ), make_nyc_test_params( dt(2018, 9, 1, 16, 0), { - "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_havdalah": dt(2018, 9, 1, 20, 22), - "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), - "parshat_hashavua": "Ki Tavo", + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 22), + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), + "english_parshat_hashavua": "Ki Tavo", "hebrew_parshat_hashavua": "כי תבוא", }, havdalah_offset=50, @@ -274,224 +274,224 @@ async def test_jewish_calendar_sensor( make_nyc_test_params( dt(2018, 9, 1, 20, 0), { - "upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), - "upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "upcoming_havdalah": dt(2018, 9, 1, 20, 14), - "parshat_hashavua": "Ki Tavo", + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), + "english_parshat_hashavua": "Ki Tavo", "hebrew_parshat_hashavua": "כי תבוא", }, ), make_nyc_test_params( dt(2018, 9, 1, 20, 21), { - "upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), - "upcoming_havdalah": dt(2018, 9, 8, 20, 2), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), - "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "parshat_hashavua": "Nitzavim", + "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), + "english_parshat_hashavua": "Nitzavim", "hebrew_parshat_hashavua": "נצבים", }, ), make_nyc_test_params( dt(2018, 9, 7, 13, 1), { - "upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), - "upcoming_havdalah": dt(2018, 9, 8, 20, 2), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), - "upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "parshat_hashavua": "Nitzavim", + "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), + "english_parshat_hashavua": "Nitzavim", "hebrew_parshat_hashavua": "נצבים", }, ), make_nyc_test_params( dt(2018, 9, 8, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "parshat_hashavua": "Vayeilech", + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", "hebrew_parshat_hashavua": "וילך", - "holiday_name": "Erev Rosh Hashana", + "english_holiday_name": "Erev Rosh Hashana", "hebrew_holiday_name": "ערב ראש השנה", }, ), make_nyc_test_params( dt(2018, 9, 9, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "parshat_hashavua": "Vayeilech", + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", "hebrew_parshat_hashavua": "וילך", - "holiday_name": "Rosh Hashana I", + "english_holiday_name": "Rosh Hashana I", "hebrew_holiday_name": "א' ראש השנה", }, ), make_nyc_test_params( dt(2018, 9, 10, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "parshat_hashavua": "Vayeilech", + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", "hebrew_parshat_hashavua": "וילך", - "holiday_name": "Rosh Hashana II", + "english_holiday_name": "Rosh Hashana II", "hebrew_holiday_name": "ב' ראש השנה", }, ), make_nyc_test_params( dt(2018, 9, 28, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 28, 18, 28), - "upcoming_havdalah": dt(2018, 9, 29, 19, 25), - "upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), - "upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), - "parshat_hashavua": "none", + "english_upcoming_candle_lighting": dt(2018, 9, 28, 18, 28), + "english_upcoming_havdalah": dt(2018, 9, 29, 19, 25), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), + "english_parshat_hashavua": "none", "hebrew_parshat_hashavua": "none", }, ), make_nyc_test_params( dt(2018, 9, 29, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", - "holiday_name": "Hoshana Raba", + "english_holiday_name": "Hoshana Raba", "hebrew_holiday_name": "הושענא רבה", }, ), make_nyc_test_params( dt(2018, 9, 30, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", - "holiday_name": "Shmini Atzeret", + "english_holiday_name": "Shmini Atzeret", "hebrew_holiday_name": "שמיני עצרת", }, ), make_nyc_test_params( dt(2018, 10, 1, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", - "holiday_name": "Simchat Torah", + "english_holiday_name": "Simchat Torah", "hebrew_holiday_name": "שמחת תורה", }, ), make_jerusalem_test_params( dt(2018, 9, 29, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), - "upcoming_havdalah": dt(2018, 10, 1, 19, 2), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), + "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", - "holiday_name": "Hoshana Raba", + "english_holiday_name": "Hoshana Raba", "hebrew_holiday_name": "הושענא רבה", }, ), make_jerusalem_test_params( dt(2018, 9, 30, 21, 25), { - "upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), - "upcoming_havdalah": dt(2018, 10, 1, 19, 2), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), + "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", - "holiday_name": "Shmini Atzeret", + "english_holiday_name": "Shmini Atzeret", "hebrew_holiday_name": "שמיני עצרת", }, ), make_jerusalem_test_params( dt(2018, 10, 1, 21, 25), { - "upcoming_candle_lighting": dt(2018, 10, 5, 18, 3), - "upcoming_havdalah": dt(2018, 10, 6, 18, 56), - "upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "parshat_hashavua": "Bereshit", + "english_upcoming_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_havdalah": dt(2018, 10, 6, 18, 56), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", "hebrew_parshat_hashavua": "בראשית", }, ), make_nyc_test_params( dt(2016, 6, 11, 8, 25), { - "upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), - "upcoming_havdalah": dt(2016, 6, 13, 21, 17), - "upcoming_shabbat_candle_lighting": dt(2016, 6, 10, 20, 7), - "upcoming_shabbat_havdalah": None, - "parshat_hashavua": "Bamidbar", + "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), + "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_shabbat_havdalah": "unknown", + "english_parshat_hashavua": "Bamidbar", "hebrew_parshat_hashavua": "במדבר", - "holiday_name": "Erev Shavuot", + "english_holiday_name": "Erev Shavuot", "hebrew_holiday_name": "ערב שבועות", }, ), make_nyc_test_params( dt(2016, 6, 12, 8, 25), { - "upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), - "upcoming_havdalah": dt(2016, 6, 13, 21, 17), - "upcoming_shabbat_candle_lighting": dt(2016, 6, 17, 20, 10), - "upcoming_shabbat_havdalah": dt(2016, 6, 18, 21, 19), - "parshat_hashavua": "Nasso", + "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), + "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 17, 20, 10), + "english_upcoming_shabbat_havdalah": dt(2016, 6, 18, 21, 19), + "english_parshat_hashavua": "Nasso", "hebrew_parshat_hashavua": "נשא", - "holiday_name": "Shavuot", + "english_holiday_name": "Shavuot", "hebrew_holiday_name": "שבועות", }, ), make_jerusalem_test_params( dt(2017, 9, 21, 8, 25), { - "upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "parshat_hashavua": "Ha'Azinu", + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", "hebrew_parshat_hashavua": "האזינו", - "holiday_name": "Rosh Hashana I", + "english_holiday_name": "Rosh Hashana I", "hebrew_holiday_name": "א' ראש השנה", }, ), make_jerusalem_test_params( dt(2017, 9, 22, 8, 25), { - "upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "parshat_hashavua": "Ha'Azinu", + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", "hebrew_parshat_hashavua": "האזינו", - "holiday_name": "Rosh Hashana II", + "english_holiday_name": "Rosh Hashana II", "hebrew_holiday_name": "ב' ראש השנה", }, ), make_jerusalem_test_params( dt(2017, 9, 23, 8, 25), { - "upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "parshat_hashavua": "Ha'Azinu", + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", "hebrew_parshat_hashavua": "האזינו", - "holiday_name": "", + "english_holiday_name": "", "hebrew_holiday_name": "", }, ), @@ -522,6 +522,7 @@ async def test_jewish_calendar_sensor( "currently_third_day_of_three_day_type2_yomtov_in_israel", ] + @pytest.mark.parametrize("language", ["english", "hebrew"]) @pytest.mark.parametrize( [ "now", @@ -539,6 +540,7 @@ async def test_jewish_calendar_sensor( async def test_shabbat_times_sensor( self, hass, + language, now, candle_lighting, havdalah, @@ -552,56 +554,59 @@ async def test_shabbat_times_sensor( time_zone = get_time_zone(tzname) test_time = time_zone.localize(now) - hass.config.set_time_zone(tzname) for sensor_type, value in result.items(): if isinstance(value, dt): + value = value.replace(tzinfo=None) result[sensor_type] = time_zone.localize(value) + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + for sensor_type, result_value in result.items(): - language = "english" - if sensor_type.startswith("hebrew_"): - language = "hebrew" - sensor_type = sensor_type.replace("hebrew_", "") - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() + if not sensor_type.startswith(language): + print(f"Not checking {sensor_type} for {language}") + continue + + sensor_type = sensor_type.replace(f"{language}_", "") with alter_time(test_time): await hass.helpers.entity_component.async_update_entity( f"sensor.test_{sensor_type}" ) + assert hass.states.get(f"sensor.test_{sensor_type}").state == str( result_value ), f"Value for {sensor_type}" melacha_params = [ - make_nyc_test_params(dt(2018, 9, 1, 16, 0), True), - make_nyc_test_params(dt(2018, 9, 1, 20, 21), False), - make_nyc_test_params(dt(2018, 9, 7, 13, 1), False), - make_nyc_test_params(dt(2018, 9, 8, 21, 25), False), - make_nyc_test_params(dt(2018, 9, 9, 21, 25), True), - make_nyc_test_params(dt(2018, 9, 10, 21, 25), True), - make_nyc_test_params(dt(2018, 9, 28, 21, 25), True), - make_nyc_test_params(dt(2018, 9, 29, 21, 25), False), - make_nyc_test_params(dt(2018, 9, 30, 21, 25), True), - make_nyc_test_params(dt(2018, 10, 1, 21, 25), True), - make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), False), - make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), True), - make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), False), + make_nyc_test_params(dt(2018, 9, 1, 16, 0), "on"), + make_nyc_test_params(dt(2018, 9, 1, 20, 21), "off"), + make_nyc_test_params(dt(2018, 9, 7, 13, 1), "off"), + make_nyc_test_params(dt(2018, 9, 8, 21, 25), "off"), + make_nyc_test_params(dt(2018, 9, 9, 21, 25), "on"), + make_nyc_test_params(dt(2018, 9, 10, 21, 25), "on"), + make_nyc_test_params(dt(2018, 9, 28, 21, 25), "on"), + make_nyc_test_params(dt(2018, 9, 29, 21, 25), "off"), + make_nyc_test_params(dt(2018, 9, 30, 21, 25), "on"), + make_nyc_test_params(dt(2018, 10, 1, 21, 25), "on"), + make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), "off"), + make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), "on"), + make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), "off"), ] melacha_test_ids = [ "currently_first_shabbat", From a766b7296c3ad1444c7b55bbe585bdf268c75642 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 23:13:40 +0300 Subject: [PATCH 08/39] Fix updating of issue melacha binary sensor --- homeassistant/components/jewish_calendar/binary_sensor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index acfebba408da72..447828cd3426d4 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -52,6 +52,9 @@ def should_poll(self): @property def is_on(self): + return self._state + + async def async_update(self): """Return true if sensor is on.""" import hdate From d97a5992efe03f72abf8e5b395e2e85352f65650 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 23:20:50 +0300 Subject: [PATCH 09/39] Fix docstrings of binary sensor --- homeassistant/components/jewish_calendar/binary_sensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 447828cd3426d4..a3ae4c976cdfd5 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -52,10 +52,11 @@ def should_poll(self): @property def is_on(self): + """Return true if sensor is on.""" return self._state async def async_update(self): - """Return true if sensor is on.""" + """Update the state of the sensor.""" import hdate zmanim = hdate.Zmanim( From f2d3bc5988a7c4641628eb314a3da9d01aa6e60a Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 27 Aug 2019 23:50:21 +0300 Subject: [PATCH 10/39] Reset timezones before and after each test --- tests/components/jewish_calendar/test_sensor.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index e1dfa8ae222ff9..b775553b5ef602 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -223,6 +223,7 @@ async def test_jewish_calendar_sensor( time_zone = get_time_zone(tzname) test_time = time_zone.localize(cur_time) + default_time_zone = str(hass.config.time_zone) hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -246,6 +247,7 @@ async def test_jewish_calendar_sensor( ) assert hass.states.get(f"sensor.test_{sensor}").state == str(result) + hass.config.set_time_zone(default_time_zone) shabbat_params = [ make_nyc_test_params( @@ -558,6 +560,7 @@ async def test_shabbat_times_sensor( if isinstance(value, dt): value = value.replace(tzinfo=None) result[sensor_type] = time_zone.localize(value) + default_time_zone = str(hass.config.time_zone) hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -592,6 +595,7 @@ async def test_shabbat_times_sensor( assert hass.states.get(f"sensor.test_{sensor_type}").state == str( result_value ), f"Value for {sensor_type}" + hass.config.set_time_zone(default_time_zone) melacha_params = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), "on"), @@ -654,6 +658,7 @@ async def test_issur_melacha_sensor( time_zone = get_time_zone(tzname) test_time = time_zone.localize(now) + default_time_zone = str(hass.config.time_zone) hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -682,6 +687,7 @@ async def test_issur_melacha_sensor( hass.states.get("binary_sensor.test_issur_melacha_in_effect").state == result ) + hass.config.set_time_zone(default_time_zone) omer_params = [ make_nyc_test_params(dt(2019, 4, 21, 0, 0), 1), @@ -742,6 +748,7 @@ async def test_omer_sensor( time_zone = get_time_zone(tzname) test_time = time_zone.localize(now) + default_time_zone = str(hass.config.time_zone) hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -767,3 +774,4 @@ async def test_omer_sensor( ) assert hass.states.get("sensor.test_omer_count").state == result + hass.config.set_time_zone(default_time_zone) From 9a6682387971be27948e753222cb627bdda4aeb2 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 00:41:20 +0300 Subject: [PATCH 11/39] Use correct entity_id for day of the omer tests --- tests/components/jewish_calendar/test_sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index b775553b5ef602..2d6523c91a6e67 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -770,8 +770,8 @@ async def test_omer_sensor( with alter_time(test_time): await hass.helpers.entity_component.async_update_entity( - "sensor.test_omer_count" + "sensor.test_day_of_the_omer" ) - assert hass.states.get("sensor.test_omer_count").state == result + assert hass.states.get("sensor.test_day_of_the_omer").state == result hass.config.set_time_zone(default_time_zone) From 156daab9ee7b1eacbcd0d94a7933d44803aa8152 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 05:13:05 +0300 Subject: [PATCH 12/39] Fix omer tests --- tests/components/jewish_calendar/test_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 2d6523c91a6e67..1b53cf5fe07e6b 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -773,5 +773,5 @@ async def test_omer_sensor( "sensor.test_day_of_the_omer" ) - assert hass.states.get("sensor.test_day_of_the_omer").state == result + assert hass.states.get("sensor.test_day_of_the_omer").state == str(result) hass.config.set_time_zone(default_time_zone) From 42ede7a7b61649db79ce942e98d50615066ef583 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 07:38:10 +0300 Subject: [PATCH 13/39] Cleanup and rearrange tests --- tests/components/jewish_calendar/__init__.py | 49 +++++ .../jewish_calendar/test_binary_sensor.py | 107 +++++++++++ .../components/jewish_calendar/test_sensor.py | 167 ++---------------- 3 files changed, 171 insertions(+), 152 deletions(-) create mode 100644 tests/components/jewish_calendar/test_binary_sensor.py diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index d6928c189e8c57..0fa76098989fd9 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -1 +1,50 @@ """Tests for the jewish_calendar component.""" +from collections import namedtuple +from contextlib import contextmanager +from unittest.mock import patch + +from homeassistant.components import jewish_calendar + + +_LatLng = namedtuple("_LatLng", ["lat", "lng"]) + +NYC_LATLNG = _LatLng(40.7128, -74.0060) +JERUSALEM_LATLNG = _LatLng(31.778, 35.235) + + +def make_nyc_test_params(dtime, results, havdalah_offset=0): + """Make test params for NYC.""" + return ( + dtime, + jewish_calendar.CANDLE_LIGHT_DEFAULT, + havdalah_offset, + True, + "America/New_York", + NYC_LATLNG.lat, + NYC_LATLNG.lng, + results, + ) + + +def make_jerusalem_test_params(dtime, results, havdalah_offset=0): + """Make test params for Jerusalem.""" + return ( + dtime, + jewish_calendar.CANDLE_LIGHT_DEFAULT, + havdalah_offset, + False, + "Asia/Jerusalem", + JERUSALEM_LATLNG.lat, + JERUSALEM_LATLNG.lng, + results, + ) + + +@contextmanager +def alter_time(retval): + """Manage multiple time mocks.""" + patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) + patch2 = patch("homeassistant.util.dt.now", return_value=retval) + + with patch1, patch2: + yield diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py new file mode 100644 index 00000000000000..0b0e5d6f1fa5a9 --- /dev/null +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -0,0 +1,107 @@ +"""The tests for the Jewish calendar binary sensors.""" +from datetime import datetime as dt + +import pytest + +from homeassistant.const import STATE_ON, STATE_OFF +from homeassistant.util.dt import get_time_zone +from homeassistant.setup import async_setup_component +from homeassistant.components import jewish_calendar + +from . import alter_time, make_nyc_test_params, make_jerusalem_test_params + + +class TestJewishCalenderBinarySensor: + """Test the Jewish Calendar binary sensors.""" + + melacha_params = [ + make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), + make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 7, 13, 1), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 8, 21, 25), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 9, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 10, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 28, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 10, 1, 21, 25), STATE_ON), + make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), + make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), + make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), STATE_OFF), + ] + melacha_test_ids = [ + "currently_first_shabbat", + "after_first_shabbat", + "friday_upcoming_shabbat", + "upcoming_rosh_hashana", + "currently_rosh_hashana", + "second_day_rosh_hashana", + "currently_shabbat_chol_hamoed", + "upcoming_two_day_yomtov_in_diaspora", + "currently_first_day_of_two_day_yomtov_in_diaspora", + "currently_second_day_of_two_day_yomtov_in_diaspora", + "upcoming_one_day_yom_tov_in_israel", + "currently_one_day_yom_tov_in_israel", + "after_one_day_yom_tov_in_israel", + ] + + @pytest.mark.parametrize( + [ + "now", + "candle_lighting", + "havdalah", + "diaspora", + "tzname", + "latitude", + "longitude", + "result", + ], + melacha_params, + ids=melacha_test_ids, + ) + async def test_issur_melacha_sensor( + self, + hass, + now, + candle_lighting, + havdalah, + diaspora, + tzname, + latitude, + longitude, + result, + ): + """Test Issur Melacha sensor output.""" + time_zone = get_time_zone(tzname) + test_time = time_zone.localize(now) + + default_time_zone = str(hass.config.time_zone) + hass.config.set_time_zone(tzname) + hass.config.latitude = latitude + hass.config.longitude = longitude + + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + "binary_sensor.test_issur_melacha_in_effect" + ) + + assert ( + hass.states.get("binary_sensor.test_issur_melacha_in_effect").state + == result + ) + hass.config.set_time_zone(default_time_zone) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 1b53cf5fe07e6b..199de9fa505feb 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -1,9 +1,6 @@ -"""The tests for the Jewish calendar sensor platform.""" -from collections import namedtuple -from contextlib import contextmanager +"""The tests for the Jewish calendar sensors.""" from datetime import time from datetime import datetime as dt -from unittest.mock import patch import pytest @@ -11,49 +8,7 @@ from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar - -_LatLng = namedtuple("_LatLng", ["lat", "lng"]) - -NYC_LATLNG = _LatLng(40.7128, -74.0060) -JERUSALEM_LATLNG = _LatLng(31.778, 35.235) - - -def make_nyc_test_params(dtime, results, havdalah_offset=0): - """Make test params for NYC.""" - return ( - dtime, - jewish_calendar.CANDLE_LIGHT_DEFAULT, - havdalah_offset, - True, - "America/New_York", - NYC_LATLNG.lat, - NYC_LATLNG.lng, - results, - ) - - -def make_jerusalem_test_params(dtime, results, havdalah_offset=0): - """Make test params for Jerusalem.""" - return ( - dtime, - jewish_calendar.CANDLE_LIGHT_DEFAULT, - havdalah_offset, - False, - "Asia/Jerusalem", - JERUSALEM_LATLNG.lat, - JERUSALEM_LATLNG.lng, - results, - ) - - -@contextmanager -def alter_time(retval): - """Manage multiple time mocks.""" - patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) - patch2 = patch("homeassistant.util.dt.now", return_value=retval) - - with patch1, patch2: - yield +from . import alter_time, make_nyc_test_params, make_jerusalem_test_params class TestJewishCalenderSensor: @@ -597,111 +552,19 @@ async def test_shabbat_times_sensor( ), f"Value for {sensor_type}" hass.config.set_time_zone(default_time_zone) - melacha_params = [ - make_nyc_test_params(dt(2018, 9, 1, 16, 0), "on"), - make_nyc_test_params(dt(2018, 9, 1, 20, 21), "off"), - make_nyc_test_params(dt(2018, 9, 7, 13, 1), "off"), - make_nyc_test_params(dt(2018, 9, 8, 21, 25), "off"), - make_nyc_test_params(dt(2018, 9, 9, 21, 25), "on"), - make_nyc_test_params(dt(2018, 9, 10, 21, 25), "on"), - make_nyc_test_params(dt(2018, 9, 28, 21, 25), "on"), - make_nyc_test_params(dt(2018, 9, 29, 21, 25), "off"), - make_nyc_test_params(dt(2018, 9, 30, 21, 25), "on"), - make_nyc_test_params(dt(2018, 10, 1, 21, 25), "on"), - make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), "off"), - make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), "on"), - make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), "off"), - ] - melacha_test_ids = [ - "currently_first_shabbat", - "after_first_shabbat", - "friday_upcoming_shabbat", - "upcoming_rosh_hashana", - "currently_rosh_hashana", - "second_day_rosh_hashana", - "currently_shabbat_chol_hamoed", - "upcoming_two_day_yomtov_in_diaspora", - "currently_first_day_of_two_day_yomtov_in_diaspora", - "currently_second_day_of_two_day_yomtov_in_diaspora", - "upcoming_one_day_yom_tov_in_israel", - "currently_one_day_yom_tov_in_israel", - "after_one_day_yom_tov_in_israel", - ] - - @pytest.mark.parametrize( - [ - "now", - "candle_lighting", - "havdalah", - "diaspora", - "tzname", - "latitude", - "longitude", - "result", - ], - melacha_params, - ids=melacha_test_ids, - ) - async def test_issur_melacha_sensor( - self, - hass, - now, - candle_lighting, - havdalah, - diaspora, - tzname, - latitude, - longitude, - result, - ): - """Test Issur Melacha sensor output.""" - time_zone = get_time_zone(tzname) - test_time = time_zone.localize(now) - - default_time_zone = str(hass.config.time_zone) - hass.config.set_time_zone(tzname) - hass.config.latitude = latitude - hass.config.longitude = longitude - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() - - with alter_time(test_time): - await hass.helpers.entity_component.async_update_entity( - "binary_sensor.test_issur_melacha_in_effect" - ) - - assert ( - hass.states.get("binary_sensor.test_issur_melacha_in_effect").state - == result - ) - hass.config.set_time_zone(default_time_zone) - omer_params = [ - make_nyc_test_params(dt(2019, 4, 21, 0, 0), 1), - make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), 1), - make_nyc_test_params(dt(2019, 4, 21, 23, 0), 2), - make_jerusalem_test_params(dt(2019, 4, 21, 23, 0), 2), - make_nyc_test_params(dt(2019, 5, 23, 0, 0), 33), - make_jerusalem_test_params(dt(2019, 5, 23, 0, 0), 33), - make_nyc_test_params(dt(2019, 6, 8, 0, 0), 49), - make_jerusalem_test_params(dt(2019, 6, 8, 0, 0), 49), - make_nyc_test_params(dt(2019, 6, 9, 0, 0), 0), - make_jerusalem_test_params(dt(2019, 6, 9, 0, 0), 0), - make_nyc_test_params(dt(2019, 1, 1, 0, 0), 0), - make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), 0), + make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), + make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), "1"), + make_nyc_test_params(dt(2019, 4, 21, 23, 0), "2"), + make_jerusalem_test_params(dt(2019, 4, 21, 23, 0), "2"), + make_nyc_test_params(dt(2019, 5, 23, 0, 0), "33"), + make_jerusalem_test_params(dt(2019, 5, 23, 0, 0), "33"), + make_nyc_test_params(dt(2019, 6, 8, 0, 0), "49"), + make_jerusalem_test_params(dt(2019, 6, 8, 0, 0), "49"), + make_nyc_test_params(dt(2019, 6, 9, 0, 0), "0"), + make_jerusalem_test_params(dt(2019, 6, 9, 0, 0), "0"), + make_nyc_test_params(dt(2019, 1, 1, 0, 0), "0"), + make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), "0"), ] omer_test_ids = [ "nyc_first_day_of_omer", @@ -773,5 +636,5 @@ async def test_omer_sensor( "sensor.test_day_of_the_omer" ) - assert hass.states.get("sensor.test_day_of_the_omer").state == str(result) + assert hass.states.get("sensor.test_day_of_the_omer").state == result hass.config.set_time_zone(default_time_zone) From ff4cc58304d068d716429f1b1021bfcad6a5da2d Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 08:13:26 +0300 Subject: [PATCH 14/39] Remove the old issur_melacha_in_effect sensor --- homeassistant/components/jewish_calendar/sensor.py | 2 -- tests/components/jewish_calendar/test_sensor.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 9d628d3e6e430e..e7397eac361095 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -119,8 +119,6 @@ def make_zmanim(date): 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 elif self._type == "omer_count": self._state = date.omer_day else: diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 199de9fa505feb..cb81b3d2d6cadb 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -12,7 +12,7 @@ class TestJewishCalenderSensor: - """Test the Jewish Calendar sensor.""" + """Test the Jewish Calendar sensors.""" async def test_jewish_calendar_min_config(self, hass): """Test minimum jewish calendar configuration.""" From 4d657271c938452a36af3120f336c326e5c3b524 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 08:20:29 +0300 Subject: [PATCH 15/39] Rename variables to make the code clearer Instead of using lagging_date, use after_tzais and after_shkia --- .../components/jewish_calendar/sensor.py | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index e7397eac361095..b8fe49ccf2b8b7 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -83,44 +83,52 @@ def make_zmanim(date): ) 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 + # 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: - lagging_date = lagging_date.next_day + 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 = date.hebrew_date + self._state = after_shkia_date.hebrew_date elif self._type == "weekly_portion": # Compute the weekly portion based on the upcoming shabbat. - self._state = lagging_date.upcoming_shabbat.parasha + self._state = after_tzais_date.upcoming_shabbat.parasha elif self._type == "holiday_name": - self._state = date.holiday_description + self._state = after_shkia_date.holiday_description elif self._type == "holiday_type": - self._state = date.holiday_type + self._state = after_shkia_date.holiday_type elif self._type == "upcoming_shabbat_candle_lighting": - times = make_zmanim(lagging_date.upcoming_shabbat.previous_day.gdate) + 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( - lagging_date.upcoming_shabbat_or_yom_tov.first_day.previous_day.gdate + 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(lagging_date.upcoming_shabbat.gdate) + times = make_zmanim(after_tzais_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) + 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 = date.omer_day + self._state = after_shkia_date.omer_day else: times = make_zmanim(today).zmanim self._state = times[self._type].time() From 096199c5d11215f8bc51cba35c6abaa9f18e0331 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 11:18:50 +0300 Subject: [PATCH 16/39] Use dt_util.set_default_time_zone instead of hass.config.set_time_zone so as not to break other tests --- tests/components/jewish_calendar/__init__.py | 14 ++++++ .../jewish_calendar/test_binary_sensor.py | 14 ++++-- .../components/jewish_calendar/test_sensor.py | 46 +++++++------------ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 0fa76098989fd9..453e2ba4543533 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -1,9 +1,11 @@ """Tests for the jewish_calendar component.""" +from datetime import datetime from collections import namedtuple from contextlib import contextmanager from unittest.mock import patch from homeassistant.components import jewish_calendar +import homeassistant.util.dt as dt_util _LatLng = namedtuple("_LatLng", ["lat", "lng"]) @@ -14,6 +16,12 @@ def make_nyc_test_params(dtime, results, havdalah_offset=0): """Make test params for NYC.""" + if isinstance(results, dict): + time_zone = dt_util.get_time_zone("America/New_York") + results = { + key: time_zone.localize(value) if isinstance(value, datetime) else value + for key, value in results.items() + } return ( dtime, jewish_calendar.CANDLE_LIGHT_DEFAULT, @@ -28,6 +36,12 @@ def make_nyc_test_params(dtime, results, havdalah_offset=0): def make_jerusalem_test_params(dtime, results, havdalah_offset=0): """Make test params for Jerusalem.""" + if isinstance(results, dict): + time_zone = dt_util.get_time_zone("Asia/Jerusalem") + results = { + key: time_zone.localize(value) if isinstance(value, datetime) else value + for key, value in results.items() + } return ( dtime, jewish_calendar.CANDLE_LIGHT_DEFAULT, diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 0b0e5d6f1fa5a9..00abdf842c7151 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -4,16 +4,22 @@ import pytest from homeassistant.const import STATE_ON, STATE_OFF -from homeassistant.util.dt import get_time_zone +import homeassistant.util.dt as dt_util from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar from . import alter_time, make_nyc_test_params, make_jerusalem_test_params +ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE + class TestJewishCalenderBinarySensor: """Test the Jewish Calendar binary sensors.""" + def tearDown(self): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) + melacha_params = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), @@ -72,11 +78,10 @@ async def test_issur_melacha_sensor( result, ): """Test Issur Melacha sensor output.""" - time_zone = get_time_zone(tzname) + time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - default_time_zone = str(hass.config.time_zone) - hass.config.set_time_zone(tzname) + dt_util.set_default_time_zone(time_zone) hass.config.latitude = latitude hass.config.longitude = longitude @@ -104,4 +109,3 @@ async def test_issur_melacha_sensor( hass.states.get("binary_sensor.test_issur_melacha_in_effect").state == result ) - hass.config.set_time_zone(default_time_zone) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index cb81b3d2d6cadb..1ff28ba4d6c4c8 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -4,16 +4,22 @@ import pytest -from homeassistant.util.dt import get_time_zone +import homeassistant.util.dt as dt_util from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar from . import alter_time, make_nyc_test_params, make_jerusalem_test_params +ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE + class TestJewishCalenderSensor: """Test the Jewish Calendar sensors.""" + def tearDown(self): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) + async def test_jewish_calendar_min_config(self, hass): """Test minimum jewish calendar configuration.""" assert await async_setup_component( @@ -150,7 +156,7 @@ async def test_jewish_calendar_hebrew(self, hass): @pytest.mark.parametrize( [ - "cur_time", + "now", "tzname", "latitude", "longitude", @@ -163,23 +169,13 @@ async def test_jewish_calendar_hebrew(self, hass): ids=test_ids, ) async def test_jewish_calendar_sensor( - self, - hass, - cur_time, - tzname, - latitude, - longitude, - language, - sensor, - diaspora, - result, + self, hass, now, tzname, latitude, longitude, language, sensor, diaspora, result ): """Test Jewish calendar sensor output.""" - time_zone = get_time_zone(tzname) - test_time = time_zone.localize(cur_time) + time_zone = dt_util.get_time_zone(tzname) + test_time = time_zone.localize(now) - default_time_zone = str(hass.config.time_zone) - hass.config.set_time_zone(tzname) + dt_util.set_default_time_zone(time_zone) hass.config.latitude = latitude hass.config.longitude = longitude @@ -202,7 +198,6 @@ async def test_jewish_calendar_sensor( ) assert hass.states.get(f"sensor.test_{sensor}").state == str(result) - hass.config.set_time_zone(default_time_zone) shabbat_params = [ make_nyc_test_params( @@ -508,15 +503,11 @@ async def test_shabbat_times_sensor( result, ): """Test sensor output for upcoming shabbat/yomtov times.""" - time_zone = get_time_zone(tzname) + time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - for sensor_type, value in result.items(): - if isinstance(value, dt): - value = value.replace(tzinfo=None) - result[sensor_type] = time_zone.localize(value) - default_time_zone = str(hass.config.time_zone) - hass.config.set_time_zone(tzname) + dt_util.set_default_time_zone(time_zone) + hass.config.latitude = latitude hass.config.longitude = longitude @@ -550,7 +541,6 @@ async def test_shabbat_times_sensor( assert hass.states.get(f"sensor.test_{sensor_type}").state == str( result_value ), f"Value for {sensor_type}" - hass.config.set_time_zone(default_time_zone) omer_params = [ make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), @@ -608,11 +598,10 @@ async def test_omer_sensor( result, ): """Test Omer Count sensor output.""" - time_zone = get_time_zone(tzname) + time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - default_time_zone = str(hass.config.time_zone) - hass.config.set_time_zone(tzname) + dt_util.set_default_time_zone(time_zone) hass.config.latitude = latitude hass.config.longitude = longitude @@ -637,4 +626,3 @@ async def test_omer_sensor( ) assert hass.states.get("sensor.test_day_of_the_omer").state == result - hass.config.set_time_zone(default_time_zone) From 27b1aac9951fb1001266be25e7fac033980e2fb3 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 11:28:04 +0300 Subject: [PATCH 17/39] Remove should_poll set to false (accidental copy/paste) --- homeassistant/components/jewish_calendar/binary_sensor.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index a3ae4c976cdfd5..ac0e99e3b10efe 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -45,11 +45,6 @@ def name(self): """Return the name of the entity.""" return self._name - @property - def should_poll(self): - """No polling needed.""" - return False - @property def is_on(self): """Return true if sensor is on.""" From ca03279339e8fd0ac11d675ce420cf05d6cde49b Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 11:37:53 +0300 Subject: [PATCH 18/39] Remove _LOGGER messaging during init and impossible cases --- .../components/jewish_calendar/__init__.py | 14 ++++---------- .../components/jewish_calendar/binary_sensor.py | 1 - 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 7b86cf4da9486a..3169a3b35d9ac2 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -7,6 +7,8 @@ from homeassistant.helpers.discovery import async_load_platform import homeassistant.helpers.config_validation as cv +import hdate + _LOGGER = logging.getLogger(__name__) DOMAIN = "jewish_calendar" @@ -53,8 +55,8 @@ { vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_DIASPORA, default=False): cv.boolean, - vol.Optional(CONF_LATITUDE): cv.latitude, - vol.Optional(CONF_LONGITUDE): cv.longitude, + vol.Inclusive(CONF_LATITUDE): cv.latitude, + vol.Inclusive(CONF_LONGITUDE): cv.longlongitude, vol.Optional(CONF_LANGUAGE, default="english"): vol.In( ["hebrew", "english"] ), @@ -72,10 +74,6 @@ async def async_setup(hass, config): """Set up the Jewish Calendar component.""" - import hdate - - _LOGGER.debug("Configuration loaded: %s", config) - name = config[DOMAIN].get(CONF_NAME) language = config[DOMAIN].get(CONF_LANGUAGE) @@ -86,10 +84,6 @@ async def async_setup(hass, config): candle_lighting_offset = config[DOMAIN].get(CONF_CANDLE_LIGHT_MINUTES) havdalah_offset = config[DOMAIN].get(CONF_HAVDALAH_OFFSET_MINUTES) - if None in (latitude, longitude): - _LOGGER.error("Latitude or longitude not set in Home Assistant config") - return - location = hdate.Location( latitude=latitude, longitude=longitude, diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index ac0e99e3b10efe..8b7e475f340279 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -33,7 +33,6 @@ def __init__(self, data, sensor, sensor_info): self._candle_lighting_offset = data["candle_lighting_offset"] self._havdalah_offset = data["havdalah_offset"] self._state = False - _LOGGER.debug("Sensor %s initialized", self._type) @property def icon(self): From 73ddbbab89e71639ea6b50a52b659e16a81210bb Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 11:49:26 +0300 Subject: [PATCH 19/39] Move binary tests to standalone test functions Move sensor tests to standalone test functions --- .../components/jewish_calendar/__init__.py | 4 +- .../jewish_calendar/test_binary_sensor.py | 161 ++- .../components/jewish_calendar/test_sensor.py | 1182 ++++++++--------- 3 files changed, 661 insertions(+), 686 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 3169a3b35d9ac2..1eb55358cb5232 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -55,8 +55,8 @@ { vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_DIASPORA, default=False): cv.boolean, - vol.Inclusive(CONF_LATITUDE): cv.latitude, - vol.Inclusive(CONF_LONGITUDE): cv.longlongitude, + vol.Inclusive(CONF_LATITUDE, "coordinates"): cv.latitude, + vol.Inclusive(CONF_LONGITUDE, "coordinates"): cv.longitude, vol.Optional(CONF_LANGUAGE, default="english"): vol.In( ["hebrew", "english"] ), diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 00abdf842c7151..9b94b7b62c8576 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -13,97 +13,88 @@ ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE -class TestJewishCalenderBinarySensor: - """Test the Jewish Calendar binary sensors.""" +def tearDown(): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) - def tearDown(self): - """Reset time zone.""" - dt_util.set_default_time_zone(ORIG_TIME_ZONE) - melacha_params = [ - make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), - make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), - make_nyc_test_params(dt(2018, 9, 7, 13, 1), STATE_OFF), - make_nyc_test_params(dt(2018, 9, 8, 21, 25), STATE_OFF), - make_nyc_test_params(dt(2018, 9, 9, 21, 25), STATE_ON), - make_nyc_test_params(dt(2018, 9, 10, 21, 25), STATE_ON), - make_nyc_test_params(dt(2018, 9, 28, 21, 25), STATE_ON), - make_nyc_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), - make_nyc_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), - make_nyc_test_params(dt(2018, 10, 1, 21, 25), STATE_ON), - make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), - make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), - make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), STATE_OFF), - ] - melacha_test_ids = [ - "currently_first_shabbat", - "after_first_shabbat", - "friday_upcoming_shabbat", - "upcoming_rosh_hashana", - "currently_rosh_hashana", - "second_day_rosh_hashana", - "currently_shabbat_chol_hamoed", - "upcoming_two_day_yomtov_in_diaspora", - "currently_first_day_of_two_day_yomtov_in_diaspora", - "currently_second_day_of_two_day_yomtov_in_diaspora", - "upcoming_one_day_yom_tov_in_israel", - "currently_one_day_yom_tov_in_israel", - "after_one_day_yom_tov_in_israel", - ] +melacha_params = [ + make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), + make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 7, 13, 1), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 8, 21, 25), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 9, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 10, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 28, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), + make_nyc_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), + make_nyc_test_params(dt(2018, 10, 1, 21, 25), STATE_ON), + make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF), + make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), STATE_ON), + make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), STATE_OFF), +] - @pytest.mark.parametrize( - [ - "now", - "candle_lighting", - "havdalah", - "diaspora", - "tzname", - "latitude", - "longitude", - "result", - ], - melacha_params, - ids=melacha_test_ids, - ) - async def test_issur_melacha_sensor( - self, - hass, - now, - candle_lighting, - havdalah, - diaspora, - tzname, - latitude, - longitude, - result, - ): - """Test Issur Melacha sensor output.""" - time_zone = dt_util.get_time_zone(tzname) - test_time = time_zone.localize(now) +melacha_test_ids = [ + "currently_first_shabbat", + "after_first_shabbat", + "friday_upcoming_shabbat", + "upcoming_rosh_hashana", + "currently_rosh_hashana", + "second_day_rosh_hashana", + "currently_shabbat_chol_hamoed", + "upcoming_two_day_yomtov_in_diaspora", + "currently_first_day_of_two_day_yomtov_in_diaspora", + "currently_second_day_of_two_day_yomtov_in_diaspora", + "upcoming_one_day_yom_tov_in_israel", + "currently_one_day_yom_tov_in_israel", + "after_one_day_yom_tov_in_israel", +] - dt_util.set_default_time_zone(time_zone) - hass.config.latitude = latitude - hass.config.longitude = longitude - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() +@pytest.mark.parametrize( + [ + "now", + "candle_lighting", + "havdalah", + "diaspora", + "tzname", + "latitude", + "longitude", + "result", + ], + melacha_params, + ids=melacha_test_ids, +) +async def test_issur_melacha_sensor( + hass, now, candle_lighting, havdalah, diaspora, tzname, latitude, longitude, result +): + """Test Issur Melacha sensor output.""" + time_zone = dt_util.get_time_zone(tzname) + test_time = time_zone.localize(now) + + dt_util.set_default_time_zone(time_zone) + hass.config.latitude = latitude + hass.config.longitude = longitude - with alter_time(test_time): - await hass.helpers.entity_component.async_update_entity( - "binary_sensor.test_issur_melacha_in_effect" - ) + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + "binary_sensor.test_issur_melacha_in_effect" + ) assert ( hass.states.get("binary_sensor.test_issur_melacha_in_effect").state diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 1ff28ba4d6c4c8..a3cd2d0fea37e5 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -13,616 +13,600 @@ ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE -class TestJewishCalenderSensor: - """Test the Jewish Calendar sensors.""" +def tearDown(): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) - def tearDown(self): - """Reset time zone.""" - dt_util.set_default_time_zone(ORIG_TIME_ZONE) - async def test_jewish_calendar_min_config(self, hass): - """Test minimum jewish calendar configuration.""" - assert await async_setup_component( - hass, jewish_calendar.DOMAIN, {"jewish_calendar": {}} - ) - await hass.async_block_till_done() - assert hass.states.get("sensor.jewish_calendar_date") is not None - - async def test_jewish_calendar_hebrew(self, hass): - """Test jewish calendar sensor with language set to hebrew.""" - assert await async_setup_component( - hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"language": "hebrew"}} - ) - await hass.async_block_till_done() - assert hass.states.get("sensor.jewish_calendar_date") is not None - - test_params = [ - ( - dt(2018, 9, 3), - "UTC", - 31.778, - 35.235, - "english", - "date", - False, - "23 Elul 5778", - ), - ( - dt(2018, 9, 3), - "UTC", - 31.778, - 35.235, - "hebrew", - "date", - False, - 'כ"ג אלול ה\' תשע"ח', - ), - ( - dt(2018, 9, 10), - "UTC", - 31.778, - 35.235, - "hebrew", - "holiday_name", - False, - "א' ראש השנה", - ), - ( - dt(2018, 9, 10), - "UTC", - 31.778, - 35.235, - "english", - "holiday_name", - False, - "Rosh Hashana I", - ), - (dt(2018, 9, 10), "UTC", 31.778, 35.235, "english", "holiday_type", False, 1), - ( - dt(2018, 9, 8), - "UTC", - 31.778, - 35.235, - "hebrew", - "parshat_hashavua", - False, - "נצבים", - ), - ( - dt(2018, 9, 8), - "America/New_York", - 40.7128, - -74.0060, - "hebrew", - "t_set_hakochavim", - True, - time(19, 48), - ), - ( - dt(2018, 9, 8), - "Asia/Jerusalem", - 31.778, - 35.235, - "hebrew", - "t_set_hakochavim", - False, - time(19, 21), - ), - ( - dt(2018, 10, 14), - "Asia/Jerusalem", - 31.778, - 35.235, - "hebrew", - "parshat_hashavua", - False, - "לך לך", - ), - ( - dt(2018, 10, 14, 17, 0, 0), - "Asia/Jerusalem", - 31.778, - 35.235, - "hebrew", - "date", - False, - "ה' מרחשוון ה' תשע\"ט", - ), - ( - dt(2018, 10, 14, 19, 0, 0), - "Asia/Jerusalem", - 31.778, - 35.235, - "hebrew", - "date", - False, - "ו' מרחשוון ה' תשע\"ט", - ), - ] - - test_ids = [ - "date_output", - "date_output_hebrew", - "holiday_name", - "holiday_name_english", - "holyness", - "torah_reading", - "first_stars_ny", - "first_stars_jerusalem", - "torah_reading_weekday", - "date_before_sunset", - "date_after_sunset", - ] - - @pytest.mark.parametrize( - [ - "now", - "tzname", - "latitude", - "longitude", - "language", - "sensor", - "diaspora", - "result", - ], - test_params, - ids=test_ids, +async def test_jewish_calendar_min_config(hass): + """Test minimum jewish calendar configuration.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {}} ) - async def test_jewish_calendar_sensor( - self, hass, now, tzname, latitude, longitude, language, sensor, diaspora, result - ): - """Test Jewish calendar sensor output.""" - time_zone = dt_util.get_time_zone(tzname) - test_time = time_zone.localize(now) - - dt_util.set_default_time_zone(time_zone) - hass.config.latitude = latitude - hass.config.longitude = longitude - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - } - }, - ) - await hass.async_block_till_done() + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None - with alter_time(test_time): - await hass.helpers.entity_component.async_update_entity( - f"sensor.test_{sensor}" - ) - assert hass.states.get(f"sensor.test_{sensor}").state == str(result) - - shabbat_params = [ - make_nyc_test_params( - dt(2018, 9, 1, 16, 0), - { - "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), - "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), - "english_parshat_hashavua": "Ki Tavo", - "hebrew_parshat_hashavua": "כי תבוא", - }, - ), - make_nyc_test_params( - dt(2018, 9, 1, 16, 0), - { - "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_havdalah": dt(2018, 9, 1, 20, 22), - "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), - "english_parshat_hashavua": "Ki Tavo", - "hebrew_parshat_hashavua": "כי תבוא", - }, - havdalah_offset=50, - ), - make_nyc_test_params( - dt(2018, 9, 1, 20, 0), - { - "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), - "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), - "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), - "english_parshat_hashavua": "Ki Tavo", - "hebrew_parshat_hashavua": "כי תבוא", - }, - ), - make_nyc_test_params( - dt(2018, 9, 1, 20, 21), - { - "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), - "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "english_parshat_hashavua": "Nitzavim", - "hebrew_parshat_hashavua": "נצבים", - }, - ), - make_nyc_test_params( - dt(2018, 9, 7, 13, 1), - { - "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), - "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), - "english_parshat_hashavua": "Nitzavim", - "hebrew_parshat_hashavua": "נצבים", - }, - ), - make_nyc_test_params( - dt(2018, 9, 8, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "english_parshat_hashavua": "Vayeilech", - "hebrew_parshat_hashavua": "וילך", - "english_holiday_name": "Erev Rosh Hashana", - "hebrew_holiday_name": "ערב ראש השנה", - }, - ), - make_nyc_test_params( - dt(2018, 9, 9, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "english_parshat_hashavua": "Vayeilech", - "hebrew_parshat_hashavua": "וילך", - "english_holiday_name": "Rosh Hashana I", - "hebrew_holiday_name": "א' ראש השנה", - }, - ), - make_nyc_test_params( - dt(2018, 9, 10, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), - "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), - "english_parshat_hashavua": "Vayeilech", - "hebrew_parshat_hashavua": "וילך", - "english_holiday_name": "Rosh Hashana II", - "hebrew_holiday_name": "ב' ראש השנה", - }, - ), - make_nyc_test_params( - dt(2018, 9, 28, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 28, 18, 28), - "english_upcoming_havdalah": dt(2018, 9, 29, 19, 25), - "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), - "english_upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), - "english_parshat_hashavua": "none", - "hebrew_parshat_hashavua": "none", - }, - ), - make_nyc_test_params( - dt(2018, 9, 29, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - "english_holiday_name": "Hoshana Raba", - "hebrew_holiday_name": "הושענא רבה", - }, - ), - make_nyc_test_params( - dt(2018, 9, 30, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - "english_holiday_name": "Shmini Atzeret", - "hebrew_holiday_name": "שמיני עצרת", - }, - ), - make_nyc_test_params( - dt(2018, 10, 1, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), - "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - "english_holiday_name": "Simchat Torah", - "hebrew_holiday_name": "שמחת תורה", - }, - ), - make_jerusalem_test_params( - dt(2018, 9, 29, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), - "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - "english_holiday_name": "Hoshana Raba", - "hebrew_holiday_name": "הושענא רבה", - }, - ), - make_jerusalem_test_params( - dt(2018, 9, 30, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), - "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - "english_holiday_name": "Shmini Atzeret", - "hebrew_holiday_name": "שמיני עצרת", - }, - ), - make_jerusalem_test_params( - dt(2018, 10, 1, 21, 25), - { - "english_upcoming_candle_lighting": dt(2018, 10, 5, 18, 3), - "english_upcoming_havdalah": dt(2018, 10, 6, 18, 56), - "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), - "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), - "english_parshat_hashavua": "Bereshit", - "hebrew_parshat_hashavua": "בראשית", - }, - ), - make_nyc_test_params( - dt(2016, 6, 11, 8, 25), - { - "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), - "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), - "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 10, 20, 7), - "english_upcoming_shabbat_havdalah": "unknown", - "english_parshat_hashavua": "Bamidbar", - "hebrew_parshat_hashavua": "במדבר", - "english_holiday_name": "Erev Shavuot", - "hebrew_holiday_name": "ערב שבועות", - }, - ), - make_nyc_test_params( - dt(2016, 6, 12, 8, 25), - { - "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), - "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), - "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 17, 20, 10), - "english_upcoming_shabbat_havdalah": dt(2016, 6, 18, 21, 19), - "english_parshat_hashavua": "Nasso", - "hebrew_parshat_hashavua": "נשא", - "english_holiday_name": "Shavuot", - "hebrew_holiday_name": "שבועות", - }, - ), - make_jerusalem_test_params( - dt(2017, 9, 21, 8, 25), - { - "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "english_parshat_hashavua": "Ha'Azinu", - "hebrew_parshat_hashavua": "האזינו", - "english_holiday_name": "Rosh Hashana I", - "hebrew_holiday_name": "א' ראש השנה", - }, - ), - make_jerusalem_test_params( - dt(2017, 9, 22, 8, 25), - { - "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "english_parshat_hashavua": "Ha'Azinu", - "hebrew_parshat_hashavua": "האזינו", - "english_holiday_name": "Rosh Hashana II", - "hebrew_holiday_name": "ב' ראש השנה", - }, - ), - make_jerusalem_test_params( - dt(2017, 9, 23, 8, 25), - { - "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), - "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), - "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), - "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), - "english_parshat_hashavua": "Ha'Azinu", - "hebrew_parshat_hashavua": "האזינו", - "english_holiday_name": "", - "hebrew_holiday_name": "", - }, - ), - ] - - shabbat_test_ids = [ - "currently_first_shabbat", - "currently_first_shabbat_with_havdalah_offset", - "currently_first_shabbat_bein_hashmashot_lagging_date", - "after_first_shabbat", - "friday_upcoming_shabbat", - "upcoming_rosh_hashana", - "currently_rosh_hashana", - "second_day_rosh_hashana", - "currently_shabbat_chol_hamoed", - "upcoming_two_day_yomtov_in_diaspora", - "currently_first_day_of_two_day_yomtov_in_diaspora", - "currently_second_day_of_two_day_yomtov_in_diaspora", - "upcoming_one_day_yom_tov_in_israel", - "currently_one_day_yom_tov_in_israel", - "after_one_day_yom_tov_in_israel", - # Type 1 = Sat/Sun/Mon - "currently_first_day_of_three_day_type1_yomtov_in_diaspora", - "currently_second_day_of_three_day_type1_yomtov_in_diaspora", - # Type 2 = Thurs/Fri/Sat - "currently_first_day_of_three_day_type2_yomtov_in_israel", - "currently_second_day_of_three_day_type2_yomtov_in_israel", - "currently_third_day_of_three_day_type2_yomtov_in_israel", - ] - - @pytest.mark.parametrize("language", ["english", "hebrew"]) - @pytest.mark.parametrize( - [ - "now", - "candle_lighting", - "havdalah", - "diaspora", - "tzname", - "latitude", - "longitude", - "result", - ], - shabbat_params, - ids=shabbat_test_ids, +async def test_jewish_calendar_hebrew(hass): + """Test jewish calendar sensor with language set to hebrew.""" + assert await async_setup_component( + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"language": "hebrew"}} ) - async def test_shabbat_times_sensor( - self, + await hass.async_block_till_done() + assert hass.states.get("sensor.jewish_calendar_date") is not None + + +test_params = [ + (dt(2018, 9, 3), "UTC", 31.778, 35.235, "english", "date", False, "23 Elul 5778"), + ( + dt(2018, 9, 3), + "UTC", + 31.778, + 35.235, + "hebrew", + "date", + False, + 'כ"ג אלול ה\' תשע"ח', + ), + ( + dt(2018, 9, 10), + "UTC", + 31.778, + 35.235, + "hebrew", + "holiday_name", + False, + "א' ראש השנה", + ), + ( + dt(2018, 9, 10), + "UTC", + 31.778, + 35.235, + "english", + "holiday_name", + False, + "Rosh Hashana I", + ), + (dt(2018, 9, 10), "UTC", 31.778, 35.235, "english", "holiday_type", False, 1), + ( + dt(2018, 9, 8), + "UTC", + 31.778, + 35.235, + "hebrew", + "parshat_hashavua", + False, + "נצבים", + ), + ( + dt(2018, 9, 8), + "America/New_York", + 40.7128, + -74.0060, + "hebrew", + "t_set_hakochavim", + True, + time(19, 48), + ), + ( + dt(2018, 9, 8), + "Asia/Jerusalem", + 31.778, + 35.235, + "hebrew", + "t_set_hakochavim", + False, + time(19, 21), + ), + ( + dt(2018, 10, 14), + "Asia/Jerusalem", + 31.778, + 35.235, + "hebrew", + "parshat_hashavua", + False, + "לך לך", + ), + ( + dt(2018, 10, 14, 17, 0, 0), + "Asia/Jerusalem", + 31.778, + 35.235, + "hebrew", + "date", + False, + "ה' מרחשוון ה' תשע\"ט", + ), + ( + dt(2018, 10, 14, 19, 0, 0), + "Asia/Jerusalem", + 31.778, + 35.235, + "hebrew", + "date", + False, + "ו' מרחשוון ה' תשע\"ט", + ), +] + +test_ids = [ + "date_output", + "date_output_hebrew", + "holiday_name", + "holiday_name_english", + "holyness", + "torah_reading", + "first_stars_ny", + "first_stars_jerusalem", + "torah_reading_weekday", + "date_before_sunset", + "date_after_sunset", +] + + +@pytest.mark.parametrize( + [ + "now", + "tzname", + "latitude", + "longitude", + "language", + "sensor", + "diaspora", + "result", + ], + test_params, + ids=test_ids, +) +async def test_jewish_calendar_sensor( + hass, now, tzname, latitude, longitude, language, sensor, diaspora, result +): + """Test Jewish calendar sensor output.""" + time_zone = dt_util.get_time_zone(tzname) + test_time = time_zone.localize(now) + + dt_util.set_default_time_zone(time_zone) + hass.config.latitude = latitude + hass.config.longitude = longitude + + assert await async_setup_component( hass, - language, - now, - candle_lighting, - havdalah, - diaspora, - tzname, - latitude, - longitude, - result, - ): - """Test sensor output for upcoming shabbat/yomtov times.""" - time_zone = dt_util.get_time_zone(tzname) - test_time = time_zone.localize(now) - - dt_util.set_default_time_zone(time_zone) - - hass.config.latitude = latitude - hass.config.longitude = longitude - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() - - for sensor_type, result_value in result.items(): - if not sensor_type.startswith(language): - print(f"Not checking {sensor_type} for {language}") - continue - - sensor_type = sensor_type.replace(f"{language}_", "") - - with alter_time(test_time): - await hass.helpers.entity_component.async_update_entity( - f"sensor.test_{sensor_type}" - ) - - assert hass.states.get(f"sensor.test_{sensor_type}").state == str( - result_value - ), f"Value for {sensor_type}" - - omer_params = [ - make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), - make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), "1"), - make_nyc_test_params(dt(2019, 4, 21, 23, 0), "2"), - make_jerusalem_test_params(dt(2019, 4, 21, 23, 0), "2"), - make_nyc_test_params(dt(2019, 5, 23, 0, 0), "33"), - make_jerusalem_test_params(dt(2019, 5, 23, 0, 0), "33"), - make_nyc_test_params(dt(2019, 6, 8, 0, 0), "49"), - make_jerusalem_test_params(dt(2019, 6, 8, 0, 0), "49"), - make_nyc_test_params(dt(2019, 6, 9, 0, 0), "0"), - make_jerusalem_test_params(dt(2019, 6, 9, 0, 0), "0"), - make_nyc_test_params(dt(2019, 1, 1, 0, 0), "0"), - make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), "0"), - ] - omer_test_ids = [ - "nyc_first_day_of_omer", - "israel_first_day_of_omer", - "nyc_first_day_of_omer_after_tzeit", - "israel_first_day_of_omer_after_tzeit", - "nyc_lag_baomer", - "israel_lag_baomer", - "nyc_last_day_of_omer", - "israel_last_day_of_omer", - "nyc_shavuot_no_omer", - "israel_shavuot_no_omer", - "nyc_jan_1st_no_omer", - "israel_jan_1st_no_omer", - ] - - @pytest.mark.parametrize( - [ - "now", - "candle_lighting", - "havdalah", - "diaspora", - "tzname", - "latitude", - "longitude", - "result", - ], - omer_params, - ids=omer_test_ids, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + } + }, ) - async def test_omer_sensor( - self, + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity(f"sensor.test_{sensor}") + + assert hass.states.get(f"sensor.test_{sensor}").state == str(result) + + +shabbat_params = [ + make_nyc_test_params( + dt(2018, 9, 1, 16, 0), + { + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), + "english_parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", + }, + ), + make_nyc_test_params( + dt(2018, 9, 1, 16, 0), + { + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 22), + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 22), + "english_parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", + }, + havdalah_offset=50, + ), + make_nyc_test_params( + dt(2018, 9, 1, 20, 0), + { + "english_upcoming_shabbat_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 1, 20, 14), + "english_upcoming_candle_lighting": dt(2018, 8, 31, 19, 15), + "english_upcoming_havdalah": dt(2018, 9, 1, 20, 14), + "english_parshat_hashavua": "Ki Tavo", + "hebrew_parshat_hashavua": "כי תבוא", + }, + ), + make_nyc_test_params( + dt(2018, 9, 1, 20, 21), + { + "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), + "english_parshat_hashavua": "Nitzavim", + "hebrew_parshat_hashavua": "נצבים", + }, + ), + make_nyc_test_params( + dt(2018, 9, 7, 13, 1), + { + "english_upcoming_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_havdalah": dt(2018, 9, 8, 20, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 7, 19, 4), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 8, 20, 2), + "english_parshat_hashavua": "Nitzavim", + "hebrew_parshat_hashavua": "נצבים", + }, + ), + make_nyc_test_params( + dt(2018, 9, 8, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", + "english_holiday_name": "Erev Rosh Hashana", + "hebrew_holiday_name": "ערב ראש השנה", + }, + ), + make_nyc_test_params( + dt(2018, 9, 9, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", + "english_holiday_name": "Rosh Hashana I", + "hebrew_holiday_name": "א' ראש השנה", + }, + ), + make_nyc_test_params( + dt(2018, 9, 10, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 9, 19, 1), + "english_upcoming_havdalah": dt(2018, 9, 11, 19, 57), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 14, 18, 52), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 15, 19, 50), + "english_parshat_hashavua": "Vayeilech", + "hebrew_parshat_hashavua": "וילך", + "english_holiday_name": "Rosh Hashana II", + "hebrew_holiday_name": "ב' ראש השנה", + }, + ), + make_nyc_test_params( + dt(2018, 9, 28, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 28, 18, 28), + "english_upcoming_havdalah": dt(2018, 9, 29, 19, 25), + "english_upcoming_shabbat_candle_lighting": dt(2018, 9, 28, 18, 28), + "english_upcoming_shabbat_havdalah": dt(2018, 9, 29, 19, 25), + "english_parshat_hashavua": "none", + "hebrew_parshat_hashavua": "none", + }, + ), + make_nyc_test_params( + dt(2018, 9, 29, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + "english_holiday_name": "Hoshana Raba", + "hebrew_holiday_name": "הושענא רבה", + }, + ), + make_nyc_test_params( + dt(2018, 9, 30, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + "english_holiday_name": "Shmini Atzeret", + "hebrew_holiday_name": "שמיני עצרת", + }, + ), + make_nyc_test_params( + dt(2018, 10, 1, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 25), + "english_upcoming_havdalah": dt(2018, 10, 2, 19, 20), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 17), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 19, 13), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + "english_holiday_name": "Simchat Torah", + "hebrew_holiday_name": "שמחת תורה", + }, + ), + make_jerusalem_test_params( + dt(2018, 9, 29, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), + "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + "english_holiday_name": "Hoshana Raba", + "hebrew_holiday_name": "הושענא רבה", + }, + ), + make_jerusalem_test_params( + dt(2018, 9, 30, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 9, 30, 18, 10), + "english_upcoming_havdalah": dt(2018, 10, 1, 19, 2), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + "english_holiday_name": "Shmini Atzeret", + "hebrew_holiday_name": "שמיני עצרת", + }, + ), + make_jerusalem_test_params( + dt(2018, 10, 1, 21, 25), + { + "english_upcoming_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_havdalah": dt(2018, 10, 6, 18, 56), + "english_upcoming_shabbat_candle_lighting": dt(2018, 10, 5, 18, 3), + "english_upcoming_shabbat_havdalah": dt(2018, 10, 6, 18, 56), + "english_parshat_hashavua": "Bereshit", + "hebrew_parshat_hashavua": "בראשית", + }, + ), + make_nyc_test_params( + dt(2016, 6, 11, 8, 25), + { + "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), + "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_shabbat_havdalah": "unknown", + "english_parshat_hashavua": "Bamidbar", + "hebrew_parshat_hashavua": "במדבר", + "english_holiday_name": "Erev Shavuot", + "hebrew_holiday_name": "ערב שבועות", + }, + ), + make_nyc_test_params( + dt(2016, 6, 12, 8, 25), + { + "english_upcoming_candle_lighting": dt(2016, 6, 10, 20, 7), + "english_upcoming_havdalah": dt(2016, 6, 13, 21, 17), + "english_upcoming_shabbat_candle_lighting": dt(2016, 6, 17, 20, 10), + "english_upcoming_shabbat_havdalah": dt(2016, 6, 18, 21, 19), + "english_parshat_hashavua": "Nasso", + "hebrew_parshat_hashavua": "נשא", + "english_holiday_name": "Shavuot", + "hebrew_holiday_name": "שבועות", + }, + ), + make_jerusalem_test_params( + dt(2017, 9, 21, 8, 25), + { + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", + "english_holiday_name": "Rosh Hashana I", + "hebrew_holiday_name": "א' ראש השנה", + }, + ), + make_jerusalem_test_params( + dt(2017, 9, 22, 8, 25), + { + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", + "english_holiday_name": "Rosh Hashana II", + "hebrew_holiday_name": "ב' ראש השנה", + }, + ), + make_jerusalem_test_params( + dt(2017, 9, 23, 8, 25), + { + "english_upcoming_candle_lighting": dt(2017, 9, 20, 18, 23), + "english_upcoming_havdalah": dt(2017, 9, 23, 19, 13), + "english_upcoming_shabbat_candle_lighting": dt(2017, 9, 22, 19, 14), + "english_upcoming_shabbat_havdalah": dt(2017, 9, 23, 19, 13), + "english_parshat_hashavua": "Ha'Azinu", + "hebrew_parshat_hashavua": "האזינו", + "english_holiday_name": "", + "hebrew_holiday_name": "", + }, + ), +] + +shabbat_test_ids = [ + "currently_first_shabbat", + "currently_first_shabbat_with_havdalah_offset", + "currently_first_shabbat_bein_hashmashot_lagging_date", + "after_first_shabbat", + "friday_upcoming_shabbat", + "upcoming_rosh_hashana", + "currently_rosh_hashana", + "second_day_rosh_hashana", + "currently_shabbat_chol_hamoed", + "upcoming_two_day_yomtov_in_diaspora", + "currently_first_day_of_two_day_yomtov_in_diaspora", + "currently_second_day_of_two_day_yomtov_in_diaspora", + "upcoming_one_day_yom_tov_in_israel", + "currently_one_day_yom_tov_in_israel", + "after_one_day_yom_tov_in_israel", + # Type 1 = Sat/Sun/Mon + "currently_first_day_of_three_day_type1_yomtov_in_diaspora", + "currently_second_day_of_three_day_type1_yomtov_in_diaspora", + # Type 2 = Thurs/Fri/Sat + "currently_first_day_of_three_day_type2_yomtov_in_israel", + "currently_second_day_of_three_day_type2_yomtov_in_israel", + "currently_third_day_of_three_day_type2_yomtov_in_israel", +] + + +@pytest.mark.parametrize("language", ["english", "hebrew"]) +@pytest.mark.parametrize( + [ + "now", + "candle_lighting", + "havdalah", + "diaspora", + "tzname", + "latitude", + "longitude", + "result", + ], + shabbat_params, + ids=shabbat_test_ids, +) +async def test_shabbat_times_sensor( + hass, + language, + now, + candle_lighting, + havdalah, + diaspora, + tzname, + latitude, + longitude, + result, +): + """Test sensor output for upcoming shabbat/yomtov times.""" + time_zone = dt_util.get_time_zone(tzname) + test_time = time_zone.localize(now) + + dt_util.set_default_time_zone(time_zone) + + hass.config.latitude = latitude + hass.config.longitude = longitude + + assert await async_setup_component( hass, - now, - candle_lighting, - havdalah, - diaspora, - tzname, - latitude, - longitude, - result, - ): - """Test Omer Count sensor output.""" - time_zone = dt_util.get_time_zone(tzname) - test_time = time_zone.localize(now) - - dt_util.set_default_time_zone(time_zone) - hass.config.latitude = latitude - hass.config.longitude = longitude - - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + for sensor_type, result_value in result.items(): + if not sensor_type.startswith(language): + print(f"Not checking {sensor_type} for {language}") + continue + + sensor_type = sensor_type.replace(f"{language}_", "") with alter_time(test_time): await hass.helpers.entity_component.async_update_entity( - "sensor.test_day_of_the_omer" + f"sensor.test_{sensor_type}" ) - assert hass.states.get("sensor.test_day_of_the_omer").state == result + assert hass.states.get(f"sensor.test_{sensor_type}").state == str( + result_value + ), f"Value for {sensor_type}" + + +omer_params = [ + make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), + make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), "1"), + make_nyc_test_params(dt(2019, 4, 21, 23, 0), "2"), + make_jerusalem_test_params(dt(2019, 4, 21, 23, 0), "2"), + make_nyc_test_params(dt(2019, 5, 23, 0, 0), "33"), + make_jerusalem_test_params(dt(2019, 5, 23, 0, 0), "33"), + make_nyc_test_params(dt(2019, 6, 8, 0, 0), "49"), + make_jerusalem_test_params(dt(2019, 6, 8, 0, 0), "49"), + make_nyc_test_params(dt(2019, 6, 9, 0, 0), "0"), + make_jerusalem_test_params(dt(2019, 6, 9, 0, 0), "0"), + make_nyc_test_params(dt(2019, 1, 1, 0, 0), "0"), + make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), "0"), +] +omer_test_ids = [ + "nyc_first_day_of_omer", + "israel_first_day_of_omer", + "nyc_first_day_of_omer_after_tzeit", + "israel_first_day_of_omer_after_tzeit", + "nyc_lag_baomer", + "israel_lag_baomer", + "nyc_last_day_of_omer", + "israel_last_day_of_omer", + "nyc_shavuot_no_omer", + "israel_shavuot_no_omer", + "nyc_jan_1st_no_omer", + "israel_jan_1st_no_omer", +] + + +@pytest.mark.parametrize( + [ + "now", + "candle_lighting", + "havdalah", + "diaspora", + "tzname", + "latitude", + "longitude", + "result", + ], + omer_params, + ids=omer_test_ids, +) +async def test_omer_sensor( + hass, now, candle_lighting, havdalah, diaspora, tzname, latitude, longitude, result +): + """Test Omer Count sensor output.""" + time_zone = dt_util.get_time_zone(tzname) + test_time = time_zone.localize(now) + + dt_util.set_default_time_zone(time_zone) + hass.config.latitude = latitude + hass.config.longitude = longitude + + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + await hass.async_block_till_done() + + with alter_time(test_time): + await hass.helpers.entity_component.async_update_entity( + "sensor.test_day_of_the_omer" + ) + + assert hass.states.get("sensor.test_day_of_the_omer").state == result From acd6ddb6f057d060bc61cce29b700c991b93536b Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 12:11:28 +0300 Subject: [PATCH 20/39] Collect entities before calling add_entities --- .../jewish_calendar/binary_sensor.py | 10 +++++---- .../components/jewish_calendar/sensor.py | 22 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 8b7e475f340279..9f6308cc0b1a5c 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -14,10 +14,12 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= if discovery_info is None: return - for sensor, sensor_info in SENSOR_TYPES["binary"].items(): - async_add_entities( - [JewishCalendarBinarySensor(hass.data[DOMAIN], sensor, sensor_info)] - ) + async_add_entities( + [ + JewishCalendarBinarySensor(hass.data[DOMAIN], sensor, sensor_info) + for sensor, sensor_info in SENSOR_TYPES["binary"].items() + ] + ) class JewishCalendarBinarySensor(BinarySensorDevice): diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index b8fe49ccf2b8b7..9e9530353c4739 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -16,15 +16,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= if discovery_info is None: return - for sensor, sensor_info in SENSOR_TYPES["data"].items(): - async_add_entities( - [JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info)] - ) - - for sensor, sensor_info in SENSOR_TYPES["time"].items(): - async_add_entities( - [JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info)] - ) + async_add_entities( + [ + JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) + for sensor, sensor_info in SENSOR_TYPES["data"].items() + ] + ) + + async_add_entities( + [ + JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) + for sensor, sensor_info in SENSOR_TYPES["time"].items() + ] + ) class JewishCalendarSensor(Entity): From 6b72775213535cddc92e2fa4a274339b4df045ec Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 13:26:40 +0300 Subject: [PATCH 21/39] Fix pylint errors --- homeassistant/components/jewish_calendar/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 1eb55358cb5232..2258e0d9dfdfc3 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -3,11 +3,12 @@ import voluptuous as vol +import hdate + from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.helpers.discovery import async_load_platform import homeassistant.helpers.config_validation as cv -import hdate _LOGGER = logging.getLogger(__name__) From 9677156663f747317886bceffb00c05c394bf479 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 15:32:21 +0300 Subject: [PATCH 22/39] Simplify logic in binary sensor until a future a PR adds more sensors --- homeassistant/components/jewish_calendar/binary_sensor.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 9f6308cc0b1a5c..12cc2750277fb1 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -63,8 +63,4 @@ async def async_update(self): hebrew=self._hebrew, ) - if self._type == "issur_melacha_in_effect": - self._state = zmanim.issur_melacha_in_effect - else: - self._state = False - _LOGGER.error("Undefined sensor type %s", self._type) + self._state = zmanim.issur_melacha_in_effect From 779065c2b4f4ee70f653630990fe81c03e1727a6 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 16:39:45 +0300 Subject: [PATCH 23/39] Rename test_id holyness to holiday_type --- tests/components/jewish_calendar/test_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index a3cd2d0fea37e5..3cfc54b456ae2c 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -136,7 +136,7 @@ async def test_jewish_calendar_hebrew(hass): "date_output_hebrew", "holiday_name", "holiday_name_english", - "holyness", + "holiday_type", "torah_reading", "first_stars_ny", "first_stars_jerusalem", From be0ec1835312e81b094f39b4c2417f5efe4307bc Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 18:58:48 +0300 Subject: [PATCH 24/39] Fix time zone for binary sensor tests Fix time zone for sensor tests --- tests/components/jewish_calendar/test_binary_sensor.py | 2 +- tests/components/jewish_calendar/test_sensor.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 9b94b7b62c8576..7bfb322f8501bb 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -72,7 +72,7 @@ async def test_issur_melacha_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - dt_util.set_default_time_zone(time_zone) + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 3cfc54b456ae2c..e5ec8fe787dd5a 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -167,7 +167,7 @@ async def test_jewish_calendar_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - dt_util.set_default_time_zone(time_zone) + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -497,8 +497,7 @@ async def test_shabbat_times_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - dt_util.set_default_time_zone(time_zone) - + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude @@ -585,7 +584,7 @@ async def test_omer_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - dt_util.set_default_time_zone(time_zone) + hass.config.set_time_zone(tzname) hass.config.latitude = latitude hass.config.longitude = longitude From 1ccca669d8a63144cd0085db83545d784aff0177 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 28 Aug 2019 19:09:12 +0300 Subject: [PATCH 25/39] Don't use unnecessary alter_time in sensors Don't use unnecessary alter time in binary sensor Remove unused alter_time --- tests/components/jewish_calendar/__init__.py | 12 ------------ .../components/jewish_calendar/test_binary_sensor.py | 5 +++-- tests/components/jewish_calendar/test_sensor.py | 9 +++++---- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 453e2ba4543533..98504dffdcc04e 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -1,8 +1,6 @@ """Tests for the jewish_calendar component.""" from datetime import datetime from collections import namedtuple -from contextlib import contextmanager -from unittest.mock import patch from homeassistant.components import jewish_calendar import homeassistant.util.dt as dt_util @@ -52,13 +50,3 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): JERUSALEM_LATLNG.lng, results, ) - - -@contextmanager -def alter_time(retval): - """Manage multiple time mocks.""" - patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) - patch2 = patch("homeassistant.util.dt.now", return_value=retval) - - with patch1, patch2: - yield diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 7bfb322f8501bb..0081c5ff6c2c9b 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -1,5 +1,6 @@ """The tests for the Jewish calendar binary sensors.""" from datetime import datetime as dt +from unittest.mock import patch import pytest @@ -8,7 +9,7 @@ from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar -from . import alter_time, make_nyc_test_params, make_jerusalem_test_params +from . import make_nyc_test_params, make_jerusalem_test_params ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE @@ -91,7 +92,7 @@ async def test_issur_melacha_sensor( ) await hass.async_block_till_done() - with alter_time(test_time): + with patch("homeassistant.util.dt.now", return_value=test_time): await hass.helpers.entity_component.async_update_entity( "binary_sensor.test_issur_melacha_in_effect" ) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index e5ec8fe787dd5a..4d574eaa1900c3 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -1,6 +1,7 @@ """The tests for the Jewish calendar sensors.""" from datetime import time from datetime import datetime as dt +from unittest.mock import patch import pytest @@ -8,7 +9,7 @@ from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar -from . import alter_time, make_nyc_test_params, make_jerusalem_test_params +from . import make_nyc_test_params, make_jerusalem_test_params ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE @@ -184,7 +185,7 @@ async def test_jewish_calendar_sensor( ) await hass.async_block_till_done() - with alter_time(test_time): + with patch("homeassistant.util.dt.now", return_value=test_time): await hass.helpers.entity_component.async_update_entity(f"sensor.test_{sensor}") assert hass.states.get(f"sensor.test_{sensor}").state == str(result) @@ -523,7 +524,7 @@ async def test_shabbat_times_sensor( sensor_type = sensor_type.replace(f"{language}_", "") - with alter_time(test_time): + with patch("homeassistant.util.dt.now", return_value=test_time): await hass.helpers.entity_component.async_update_entity( f"sensor.test_{sensor_type}" ) @@ -603,7 +604,7 @@ async def test_omer_sensor( ) await hass.async_block_till_done() - with alter_time(test_time): + with patch("homeassistant.util.dt.now", return_value=test_time): await hass.helpers.entity_component.async_update_entity( "sensor.test_day_of_the_omer" ) From 39605474164c6c7be5ee5907b8d10670b5d4ef6e Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 29 Aug 2019 09:11:04 +0300 Subject: [PATCH 26/39] Simply set hass.config.time_zone instead of murking around with global values --- .../jewish_calendar/test_binary_sensor.py | 10 +--------- tests/components/jewish_calendar/test_sensor.py | 13 +++---------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 0081c5ff6c2c9b..b2203c23420568 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -11,14 +11,6 @@ from . import make_nyc_test_params, make_jerusalem_test_params -ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE - - -def tearDown(): - """Reset time zone.""" - dt_util.set_default_time_zone(ORIG_TIME_ZONE) - - melacha_params = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), @@ -73,7 +65,7 @@ async def test_issur_melacha_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - hass.config.set_time_zone(tzname) + hass.config.time_zone = time_zone hass.config.latitude = latitude hass.config.longitude = longitude diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 4d574eaa1900c3..c49d303d8dd2e6 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -11,13 +11,6 @@ from . import make_nyc_test_params, make_jerusalem_test_params -ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE - - -def tearDown(): - """Reset time zone.""" - dt_util.set_default_time_zone(ORIG_TIME_ZONE) - async def test_jewish_calendar_min_config(hass): """Test minimum jewish calendar configuration.""" @@ -168,7 +161,7 @@ async def test_jewish_calendar_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - hass.config.set_time_zone(tzname) + hass.config.time_zone = time_zone hass.config.latitude = latitude hass.config.longitude = longitude @@ -498,7 +491,7 @@ async def test_shabbat_times_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - hass.config.set_time_zone(tzname) + hass.config.time_zone = time_zone hass.config.latitude = latitude hass.config.longitude = longitude @@ -585,7 +578,7 @@ async def test_omer_sensor( time_zone = dt_util.get_time_zone(tzname) test_time = time_zone.localize(now) - hass.config.set_time_zone(tzname) + hass.config.time_zone = time_zone hass.config.latitude = latitude hass.config.longitude = longitude From 77508c685eff9aa5ede4182a4c38918b60a13c27 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 29 Aug 2019 09:21:15 +0300 Subject: [PATCH 27/39] Use async_fire_time_changed instead of directly calling async_update_entity --- .../jewish_calendar/test_binary_sensor.py | 9 ++++++--- .../components/jewish_calendar/test_sensor.py | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index b2203c23420568..a5fba3438f3c16 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -1,4 +1,5 @@ """The tests for the Jewish calendar binary sensors.""" +from datetime import timedelta from datetime import datetime as dt from unittest.mock import patch @@ -9,6 +10,8 @@ from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar +from tests.common import async_fire_time_changed + from . import make_nyc_test_params, make_jerusalem_test_params melacha_params = [ @@ -85,9 +88,9 @@ async def test_issur_melacha_sensor( await hass.async_block_till_done() with patch("homeassistant.util.dt.now", return_value=test_time): - await hass.helpers.entity_component.async_update_entity( - "binary_sensor.test_issur_melacha_in_effect" - ) + future = dt_util.utcnow() + timedelta(seconds=30) + async_fire_time_changed(hass, future) + await hass.async_block_till_done() assert ( hass.states.get("binary_sensor.test_issur_melacha_in_effect").state diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index c49d303d8dd2e6..91a7558ad89a08 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -1,5 +1,5 @@ """The tests for the Jewish calendar sensors.""" -from datetime import time +from datetime import time, timedelta from datetime import datetime as dt from unittest.mock import patch @@ -8,6 +8,7 @@ import homeassistant.util.dt as dt_util from homeassistant.setup import async_setup_component from homeassistant.components import jewish_calendar +from tests.common import async_fire_time_changed from . import make_nyc_test_params, make_jerusalem_test_params @@ -179,7 +180,9 @@ async def test_jewish_calendar_sensor( await hass.async_block_till_done() with patch("homeassistant.util.dt.now", return_value=test_time): - await hass.helpers.entity_component.async_update_entity(f"sensor.test_{sensor}") + future = dt_util.utcnow() + timedelta(seconds=30) + async_fire_time_changed(hass, future) + await hass.async_block_till_done() assert hass.states.get(f"sensor.test_{sensor}").state == str(result) @@ -518,9 +521,9 @@ async def test_shabbat_times_sensor( sensor_type = sensor_type.replace(f"{language}_", "") with patch("homeassistant.util.dt.now", return_value=test_time): - await hass.helpers.entity_component.async_update_entity( - f"sensor.test_{sensor_type}" - ) + future = dt_util.utcnow() + timedelta(seconds=30) + async_fire_time_changed(hass, future) + await hass.async_block_till_done() assert hass.states.get(f"sensor.test_{sensor_type}").state == str( result_value @@ -598,8 +601,8 @@ async def test_omer_sensor( await hass.async_block_till_done() with patch("homeassistant.util.dt.now", return_value=test_time): - await hass.helpers.entity_component.async_update_entity( - "sensor.test_day_of_the_omer" - ) + 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_day_of_the_omer").state == result From 7c92dd639438ca6073cb94f639ddd1305617606e Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 29 Aug 2019 09:27:46 +0300 Subject: [PATCH 28/39] Removing debug messaging during init of integration --- homeassistant/components/jewish_calendar/__init__.py | 4 ---- homeassistant/components/jewish_calendar/sensor.py | 1 - 2 files changed, 5 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 2258e0d9dfdfc3..a9499c9ece7981 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -92,8 +92,6 @@ async def async_setup(hass, config): diaspora=diaspora, ) - _LOGGER.debug("Location created: %r", location) - hass.data[DOMAIN] = { "location": location, "name": name, @@ -103,8 +101,6 @@ async def async_setup(hass, config): "diaspora": diaspora, } - _LOGGER.debug("Loading platform with data %s", hass.data[DOMAIN]) - hass.async_create_task(async_load_platform(hass, "sensor", DOMAIN, {}, config)) hass.async_create_task( diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 9e9530353c4739..efb0c8681efd3c 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -45,7 +45,6 @@ def __init__(self, data, sensor, sensor_info): self._havdalah_offset = data["havdalah_offset"] self._diaspora = data["diaspora"] self._state = None - _LOGGER.debug("Sensor %s initialized", self._type) @property def name(self): From a71ed76854beb5d4c7d3b61dcaa37349000b34e5 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 29 Aug 2019 17:06:32 +0300 Subject: [PATCH 29/39] Capitalize constants --- .../jewish_calendar/test_binary_sensor.py | 8 +++---- .../components/jewish_calendar/test_sensor.py | 24 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index a5fba3438f3c16..13a38dfb32a069 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -14,7 +14,7 @@ from . import make_nyc_test_params, make_jerusalem_test_params -melacha_params = [ +MELACHA_PARAMS = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF), make_nyc_test_params(dt(2018, 9, 7, 13, 1), STATE_OFF), @@ -30,7 +30,7 @@ make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), STATE_OFF), ] -melacha_test_ids = [ +MELACHA_TEST_IDS = [ "currently_first_shabbat", "after_first_shabbat", "friday_upcoming_shabbat", @@ -58,8 +58,8 @@ "longitude", "result", ], - melacha_params, - ids=melacha_test_ids, + MELACHA_PARAMS, + ids=MELACHA_TEST_IDS, ) async def test_issur_melacha_sensor( hass, now, candle_lighting, havdalah, diaspora, tzname, latitude, longitude, result diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 91a7558ad89a08..56d2dc44cff61b 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -31,7 +31,7 @@ async def test_jewish_calendar_hebrew(hass): assert hass.states.get("sensor.jewish_calendar_date") is not None -test_params = [ +TEST_PARAMS = [ (dt(2018, 9, 3), "UTC", 31.778, 35.235, "english", "date", False, "23 Elul 5778"), ( dt(2018, 9, 3), @@ -126,7 +126,7 @@ async def test_jewish_calendar_hebrew(hass): ), ] -test_ids = [ +TEST_IDS = [ "date_output", "date_output_hebrew", "holiday_name", @@ -152,8 +152,8 @@ async def test_jewish_calendar_hebrew(hass): "diaspora", "result", ], - test_params, - ids=test_ids, + TEST_PARAMS, + ids=TEST_IDS, ) async def test_jewish_calendar_sensor( hass, now, tzname, latitude, longitude, language, sensor, diaspora, result @@ -187,7 +187,7 @@ async def test_jewish_calendar_sensor( assert hass.states.get(f"sensor.test_{sensor}").state == str(result) -shabbat_params = [ +SHABBAT_PARAMS = [ make_nyc_test_params( dt(2018, 9, 1, 16, 0), { @@ -437,7 +437,7 @@ async def test_jewish_calendar_sensor( ), ] -shabbat_test_ids = [ +SHABBAT_TEST_IDS = [ "currently_first_shabbat", "currently_first_shabbat_with_havdalah_offset", "currently_first_shabbat_bein_hashmashot_lagging_date", @@ -475,8 +475,8 @@ async def test_jewish_calendar_sensor( "longitude", "result", ], - shabbat_params, - ids=shabbat_test_ids, + SHABBAT_PARAMS, + ids=SHABBAT_TEST_IDS, ) async def test_shabbat_times_sensor( hass, @@ -530,7 +530,7 @@ async def test_shabbat_times_sensor( ), f"Value for {sensor_type}" -omer_params = [ +OMER_PARAMS = [ make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), "1"), make_nyc_test_params(dt(2019, 4, 21, 23, 0), "2"), @@ -544,7 +544,7 @@ async def test_shabbat_times_sensor( make_nyc_test_params(dt(2019, 1, 1, 0, 0), "0"), make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), "0"), ] -omer_test_ids = [ +OMER_TEST_IDS = [ "nyc_first_day_of_omer", "israel_first_day_of_omer", "nyc_first_day_of_omer_after_tzeit", @@ -571,8 +571,8 @@ async def test_shabbat_times_sensor( "longitude", "result", ], - omer_params, - ids=omer_test_ids, + OMER_PARAMS, + ids=OMER_TEST_IDS, ) async def test_omer_sensor( hass, now, candle_lighting, havdalah, diaspora, tzname, latitude, longitude, result From 732920831e82e57ab19a8c4fff7bb6d671ef9a89 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Thu, 29 Aug 2019 17:07:28 +0300 Subject: [PATCH 30/39] Collect all Entities before calling async_add_entities --- .../components/jewish_calendar/sensor.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index efb0c8681efd3c..829eb029ffbdc6 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -16,19 +16,16 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= if discovery_info is None: return - async_add_entities( - [ - JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) - for sensor, sensor_info in SENSOR_TYPES["data"].items() - ] + sensors = [ + JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) + for sensor, sensor_info in SENSOR_TYPES["data"].items() + ] + sensors.extend( + JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) + for sensor, sensor_info in SENSOR_TYPES["time"].items() ) - async_add_entities( - [ - JewishCalendarSensor(hass.data[DOMAIN], sensor, sensor_info) - for sensor, sensor_info in SENSOR_TYPES["time"].items() - ] - ) + async_add_entities(sensors) class JewishCalendarSensor(Entity): From 26fb77f996ca11580f280cdc041c244adde47c20 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Sat, 31 Aug 2019 23:58:49 +0300 Subject: [PATCH 31/39] Revert "Don't use unnecessary alter_time in sensors" This reverts commit 74371740eaeb6e73c1a374725b05207071648ee1. --- tests/components/jewish_calendar/__init__.py | 12 ++++++++++++ .../components/jewish_calendar/test_binary_sensor.py | 12 +++++++++--- tests/components/jewish_calendar/test_sensor.py | 9 ++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 98504dffdcc04e..453e2ba4543533 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -1,6 +1,8 @@ """Tests for the jewish_calendar component.""" from datetime import datetime from collections import namedtuple +from contextlib import contextmanager +from unittest.mock import patch from homeassistant.components import jewish_calendar import homeassistant.util.dt as dt_util @@ -50,3 +52,13 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): JERUSALEM_LATLNG.lng, results, ) + + +@contextmanager +def alter_time(retval): + """Manage multiple time mocks.""" + patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) + patch2 = patch("homeassistant.util.dt.now", return_value=retval) + + with patch1, patch2: + yield diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 13a38dfb32a069..c5bcc79d541bea 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -1,7 +1,6 @@ """The tests for the Jewish calendar binary sensors.""" from datetime import timedelta from datetime import datetime as dt -from unittest.mock import patch import pytest @@ -11,8 +10,15 @@ from homeassistant.components import jewish_calendar from tests.common import async_fire_time_changed +from . import alter_time, make_nyc_test_params, make_jerusalem_test_params + +ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE + + +def tearDown(): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) -from . import make_nyc_test_params, make_jerusalem_test_params MELACHA_PARAMS = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), @@ -87,7 +93,7 @@ async def test_issur_melacha_sensor( ) await hass.async_block_till_done() - with patch("homeassistant.util.dt.now", return_value=test_time): + with alter_time(test_time): future = dt_util.utcnow() + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 56d2dc44cff61b..02a1c6cd80d2ee 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -1,7 +1,6 @@ """The tests for the Jewish calendar sensors.""" from datetime import time, timedelta from datetime import datetime as dt -from unittest.mock import patch import pytest @@ -10,7 +9,7 @@ from homeassistant.components import jewish_calendar from tests.common import async_fire_time_changed -from . import make_nyc_test_params, make_jerusalem_test_params +from . import alter_time, make_nyc_test_params, make_jerusalem_test_params async def test_jewish_calendar_min_config(hass): @@ -179,7 +178,7 @@ async def test_jewish_calendar_sensor( ) await hass.async_block_till_done() - with patch("homeassistant.util.dt.now", return_value=test_time): + with alter_time(test_time): future = dt_util.utcnow() + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() @@ -520,7 +519,7 @@ async def test_shabbat_times_sensor( sensor_type = sensor_type.replace(f"{language}_", "") - with patch("homeassistant.util.dt.now", return_value=test_time): + with alter_time(test_time): future = dt_util.utcnow() + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() @@ -600,7 +599,7 @@ async def test_omer_sensor( ) await hass.async_block_till_done() - with patch("homeassistant.util.dt.now", return_value=test_time): + with alter_time(test_time): future = dt_util.utcnow() + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() From 05797b9f097e49aef4735129cfe096333a7e4590 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Sun, 1 Sep 2019 00:11:59 +0300 Subject: [PATCH 32/39] Use test time instead of utc_now --- tests/components/jewish_calendar/test_binary_sensor.py | 2 +- tests/components/jewish_calendar/test_sensor.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index c5bcc79d541bea..2d69040094c8c5 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -94,7 +94,7 @@ async def test_issur_melacha_sensor( await hass.async_block_till_done() with alter_time(test_time): - future = dt_util.utcnow() + timedelta(seconds=30) + future = test_time + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 02a1c6cd80d2ee..784a70202aab56 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -179,7 +179,7 @@ async def test_jewish_calendar_sensor( await hass.async_block_till_done() with alter_time(test_time): - future = dt_util.utcnow() + timedelta(seconds=30) + future = test_time + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() @@ -520,7 +520,7 @@ async def test_shabbat_times_sensor( sensor_type = sensor_type.replace(f"{language}_", "") with alter_time(test_time): - future = dt_util.utcnow() + timedelta(seconds=30) + future = test_time + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() @@ -600,7 +600,7 @@ async def test_omer_sensor( await hass.async_block_till_done() with alter_time(test_time): - future = dt_util.utcnow() + timedelta(seconds=30) + future = test_time + timedelta(seconds=30) async_fire_time_changed(hass, future) await hass.async_block_till_done() From 781adb8824acfc5a5621b13298ecffda17b80061 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 11:43:44 +0300 Subject: [PATCH 33/39] Remove superfluous testing --- .../components/jewish_calendar/test_sensor.py | 75 ++++--------------- 1 file changed, 16 insertions(+), 59 deletions(-) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 784a70202aab56..3dbb02f9f5bd32 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -530,74 +530,31 @@ async def test_shabbat_times_sensor( OMER_PARAMS = [ - make_nyc_test_params(dt(2019, 4, 21, 0, 0), "1"), - make_jerusalem_test_params(dt(2019, 4, 21, 0, 0), "1"), - make_nyc_test_params(dt(2019, 4, 21, 23, 0), "2"), - make_jerusalem_test_params(dt(2019, 4, 21, 23, 0), "2"), - make_nyc_test_params(dt(2019, 5, 23, 0, 0), "33"), - make_jerusalem_test_params(dt(2019, 5, 23, 0, 0), "33"), - make_nyc_test_params(dt(2019, 6, 8, 0, 0), "49"), - make_jerusalem_test_params(dt(2019, 6, 8, 0, 0), "49"), - make_nyc_test_params(dt(2019, 6, 9, 0, 0), "0"), - make_jerusalem_test_params(dt(2019, 6, 9, 0, 0), "0"), - make_nyc_test_params(dt(2019, 1, 1, 0, 0), "0"), - make_jerusalem_test_params(dt(2019, 1, 1, 0, 0), "0"), + (dt(2019, 4, 21, 0), "1"), + (dt(2019, 4, 21, 23), "2"), + (dt(2019, 5, 23, 0), "33"), + (dt(2019, 6, 8, 0), "49"), + (dt(2019, 6, 9, 0), "0"), + (dt(2019, 1, 1, 0), "0"), ] OMER_TEST_IDS = [ - "nyc_first_day_of_omer", - "israel_first_day_of_omer", - "nyc_first_day_of_omer_after_tzeit", - "israel_first_day_of_omer_after_tzeit", - "nyc_lag_baomer", - "israel_lag_baomer", - "nyc_last_day_of_omer", - "israel_last_day_of_omer", - "nyc_shavuot_no_omer", - "israel_shavuot_no_omer", - "nyc_jan_1st_no_omer", - "israel_jan_1st_no_omer", + "first_day_of_omer", + "first_day_of_omer_after_tzeit", + "lag_baomer", + "last_day_of_omer", + "shavuot_no_omer", + "jan_1st_no_omer", ] -@pytest.mark.parametrize( - [ - "now", - "candle_lighting", - "havdalah", - "diaspora", - "tzname", - "latitude", - "longitude", - "result", - ], - OMER_PARAMS, - ids=OMER_TEST_IDS, -) -async def test_omer_sensor( - hass, now, candle_lighting, havdalah, diaspora, tzname, latitude, longitude, result -): +@pytest.mark.parametrize(["test_time", "result"], OMER_PARAMS, ids=OMER_TEST_IDS) +async def test_omer_sensor(hass, test_time, result): """Test Omer Count sensor output.""" - time_zone = dt_util.get_time_zone(tzname) - test_time = time_zone.localize(now) - - hass.config.time_zone = time_zone - hass.config.latitude = latitude - hass.config.longitude = longitude - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, + hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"name": "test"}} ) await hass.async_block_till_done() + test_time = hass.config.time_zone.localize(test_time) with alter_time(test_time): future = test_time + timedelta(seconds=30) From b0a12a6ff74e3f3bdf4aca45a557a7ee7ba2983c Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 12:22:54 +0300 Subject: [PATCH 34/39] Fix triggering of time changed --- tests/components/jewish_calendar/__init__.py | 7 +- .../jewish_calendar/test_binary_sensor.py | 32 ++++---- .../components/jewish_calendar/test_sensor.py | 77 ++++++++++--------- 3 files changed, 59 insertions(+), 57 deletions(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 453e2ba4543533..4b1a628fa12724 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -55,10 +55,11 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): @contextmanager -def alter_time(retval): +def alter_time(local_time): """Manage multiple time mocks.""" - patch1 = patch("homeassistant.util.dt.utcnow", return_value=retval) - patch2 = patch("homeassistant.util.dt.now", return_value=retval) + utc_time = dt_util.UTC.localize(local_time.replace(tzinfo=None)) + patch1 = patch("homeassistant.util.dt.utcnow", return_value=utc_time) + patch2 = patch("homeassistant.util.dt.now", return_value=local_time) with patch1, patch2: yield diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 2d69040094c8c5..72ee711fdbfa16 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -78,23 +78,23 @@ async def test_issur_melacha_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": "english", - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() - with alter_time(test_time): - future = test_time + timedelta(seconds=30) + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": "english", + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + 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() diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 3dbb02f9f5bd32..8d72830b3698ab 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -165,21 +165,21 @@ async def test_jewish_calendar_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - } - }, - ) - await hass.async_block_till_done() - with alter_time(test_time): - future = test_time + timedelta(seconds=30) + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + } + }, + ) + 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() @@ -497,20 +497,25 @@ async def test_shabbat_times_sensor( hass.config.latitude = latitude hass.config.longitude = longitude - assert await async_setup_component( - hass, - jewish_calendar.DOMAIN, - { - "jewish_calendar": { - "name": "test", - "language": language, - "diaspora": diaspora, - "candle_lighting_minutes_before_sunset": candle_lighting, - "havdalah_minutes_after_sunset": havdalah, - } - }, - ) - await hass.async_block_till_done() + with alter_time(test_time): + assert await async_setup_component( + hass, + jewish_calendar.DOMAIN, + { + "jewish_calendar": { + "name": "test", + "language": language, + "diaspora": diaspora, + "candle_lighting_minutes_before_sunset": candle_lighting, + "havdalah_minutes_after_sunset": havdalah, + } + }, + ) + 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() for sensor_type, result_value in result.items(): if not sensor_type.startswith(language): @@ -519,11 +524,6 @@ async def test_shabbat_times_sensor( sensor_type = sensor_type.replace(f"{language}_", "") - with alter_time(test_time): - future = test_time + timedelta(seconds=30) - async_fire_time_changed(hass, future) - await hass.async_block_till_done() - assert hass.states.get(f"sensor.test_{sensor_type}").state == str( result_value ), f"Value for {sensor_type}" @@ -550,14 +550,15 @@ async def test_shabbat_times_sensor( @pytest.mark.parametrize(["test_time", "result"], OMER_PARAMS, ids=OMER_TEST_IDS) async def test_omer_sensor(hass, test_time, result): """Test Omer Count sensor output.""" - assert await async_setup_component( - hass, jewish_calendar.DOMAIN, {"jewish_calendar": {"name": "test"}} - ) - await hass.async_block_till_done() test_time = hass.config.time_zone.localize(test_time) with alter_time(test_time): - future = test_time + timedelta(seconds=30) + 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() From e39e6245035ff0c19979fafc9f5b736c8530b957 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 14:26:26 +0300 Subject: [PATCH 35/39] Fix failing tests due to side-effects --- tests/components/jewish_calendar/__init__.py | 7 +++++++ tests/components/jewish_calendar/test_binary_sensor.py | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 4b1a628fa12724..47fb89d6c7caaf 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -13,6 +13,13 @@ NYC_LATLNG = _LatLng(40.7128, -74.0060) JERUSALEM_LATLNG = _LatLng(31.778, 35.235) +ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE + + +def teardown_module(): + """Reset time zone.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) + def make_nyc_test_params(dtime, results, havdalah_offset=0): """Make test params for NYC.""" diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index 72ee711fdbfa16..64745d8929f7e7 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -12,13 +12,6 @@ from tests.common import async_fire_time_changed from . import alter_time, make_nyc_test_params, make_jerusalem_test_params -ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE - - -def tearDown(): - """Reset time zone.""" - dt_util.set_default_time_zone(ORIG_TIME_ZONE) - MELACHA_PARAMS = [ make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON), From 879e2f658666f8d497023637d820882a0c4e3d77 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 14:33:55 +0300 Subject: [PATCH 36/39] Use dt_util.as_utc instead of reimplementing it's functionality --- tests/components/jewish_calendar/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/jewish_calendar/__init__.py b/tests/components/jewish_calendar/__init__.py index 47fb89d6c7caaf..54589a640cc0cd 100644 --- a/tests/components/jewish_calendar/__init__.py +++ b/tests/components/jewish_calendar/__init__.py @@ -64,7 +64,7 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0): @contextmanager def alter_time(local_time): """Manage multiple time mocks.""" - utc_time = dt_util.UTC.localize(local_time.replace(tzinfo=None)) + utc_time = dt_util.as_utc(local_time) patch1 = patch("homeassistant.util.dt.utcnow", return_value=utc_time) patch2 = patch("homeassistant.util.dt.now", return_value=local_time) From 611b668fb4fcf2f6ece7d206f452cbeb0208b482 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 19:24:03 +0300 Subject: [PATCH 37/39] Use dict[key] for default values --- homeassistant/components/jewish_calendar/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index a9499c9ece7981..01cae779a74a27 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -75,15 +75,15 @@ async def async_setup(hass, config): """Set up the Jewish Calendar component.""" - name = config[DOMAIN].get(CONF_NAME) - language = config[DOMAIN].get(CONF_LANGUAGE) + name = config[DOMAIN][CONF_NAME] + language = config[DOMAIN][CONF_LANGUAGE] latitude = config[DOMAIN].get(CONF_LATITUDE, hass.config.latitude) longitude = config[DOMAIN].get(CONF_LONGITUDE, hass.config.longitude) - diaspora = config[DOMAIN].get(CONF_DIASPORA) + diaspora = config[DOMAIN][CONF_DIASPORA] - candle_lighting_offset = config[DOMAIN].get(CONF_CANDLE_LIGHT_MINUTES) - havdalah_offset = config[DOMAIN].get(CONF_HAVDALAH_OFFSET_MINUTES) + candle_lighting_offset = config[DOMAIN][CONF_CANDLE_LIGHT_MINUTES] + havdalah_offset = config[DOMAIN][CONF_HAVDALAH_OFFSET_MINUTES] location = hdate.Location( latitude=latitude, From 726914c2d944a10b9fb80176855a4e38318294e6 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 19:26:06 +0300 Subject: [PATCH 38/39] Move 3rd party imports to the top of the module --- homeassistant/components/jewish_calendar/binary_sensor.py | 4 ++-- homeassistant/components/jewish_calendar/sensor.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 12cc2750277fb1..2d5807d9e3576d 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -6,6 +6,8 @@ from . import DOMAIN, SENSOR_TYPES +import hdate + _LOGGER = logging.getLogger(__name__) @@ -53,8 +55,6 @@ def is_on(self): async def async_update(self): """Update the state of the sensor.""" - import hdate - zmanim = hdate.Zmanim( date=dt_util.now(), location=self._location, diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 829eb029ffbdc6..93fdbf3eb61e71 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -8,6 +8,8 @@ from . import DOMAIN, SENSOR_TYPES +import hdate + _LOGGER = logging.getLogger(__name__) @@ -60,8 +62,6 @@ def state(self): async def async_update(self): """Update the state of the sensor.""" - import hdate - now = dt_util.now() _LOGGER.debug("Now: %s Timezone = %s", now, now.tzinfo) From d0ad1c8a5adaf1d88204877521b3f696813bff07 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 3 Sep 2019 23:29:08 +0300 Subject: [PATCH 39/39] Fix imports --- homeassistant/components/jewish_calendar/__init__.py | 1 - homeassistant/components/jewish_calendar/binary_sensor.py | 4 ++-- homeassistant/components/jewish_calendar/sensor.py | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 01cae779a74a27..c7bbbdb2d907a9 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -2,7 +2,6 @@ import logging import voluptuous as vol - import hdate from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 2d5807d9e3576d..7362fce3cd0301 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -1,13 +1,13 @@ """Support for Jewish Calendar binary sensors.""" import logging +import hdate + from homeassistant.components.binary_sensor import BinarySensorDevice import homeassistant.util.dt as dt_util from . import DOMAIN, SENSOR_TYPES -import hdate - _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 93fdbf3eb61e71..405838b1fb10f9 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -1,6 +1,8 @@ """Platform to retrieve Jewish calendar information for Home Assistant.""" import logging +import hdate + from homeassistant.const import SUN_EVENT_SUNSET from homeassistant.helpers.entity import Entity from homeassistant.helpers.sun import get_astral_event_date @@ -8,8 +10,6 @@ from . import DOMAIN, SENSOR_TYPES -import hdate - _LOGGER = logging.getLogger(__name__)