def test_date_events_default_date(hass): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = hass.config.latitude longitude = hass.config.longitude dawn = astral.dawn_utc(utc_today, latitude, longitude) dusk = astral.dusk_utc(utc_today, latitude, longitude) midnight = astral.solar_midnight_utc(utc_today, longitude) noon = astral.solar_noon_utc(utc_today, longitude) sunrise = astral.sunrise_utc(utc_today, latitude, longitude) sunset = astral.sunset_utc(utc_today, latitude, longitude) with patch('homeassistant.util.dt.now', return_value=utc_now): assert dawn == sun.get_astral_event_date(hass, 'dawn', utc_today) assert dusk == sun.get_astral_event_date(hass, 'dusk', utc_today) assert midnight == sun.get_astral_event_date(hass, 'solar_midnight', utc_today) assert noon == sun.get_astral_event_date(hass, 'solar_noon', utc_today) assert sunrise == sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, utc_today) assert sunset == sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events(self): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude dawn = astral.dawn_utc(utc_today, latitude, longitude) dusk = astral.dusk_utc(utc_today, latitude, longitude) midnight = astral.solar_midnight_utc(utc_today, longitude) noon = astral.solar_noon_utc(utc_today, longitude) sunrise = astral.sunrise_utc(utc_today, latitude, longitude) sunset = astral.sunset_utc(utc_today, latitude, longitude) self.assertEqual( dawn, sun.get_astral_event_date(self.hass, 'dawn', utc_today)) self.assertEqual( dusk, sun.get_astral_event_date(self.hass, 'dusk', utc_today)) self.assertEqual( midnight, sun.get_astral_event_date(self.hass, 'solar_midnight', utc_today)) self.assertEqual( noon, sun.get_astral_event_date(self.hass, 'solar_noon', utc_today)) self.assertEqual( sunrise, sun.get_astral_event_date(self.hass, 'sunrise', utc_today)) self.assertEqual( sunset, sun.get_astral_event_date(self.hass, 'sunset', utc_today))
def test_date_events(hass): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = hass.config.latitude longitude = hass.config.longitude dawn = astral.dawn_utc(utc_today, latitude, longitude) dusk = astral.dusk_utc(utc_today, latitude, longitude) midnight = astral.solar_midnight_utc(utc_today, longitude) noon = astral.solar_noon_utc(utc_today, longitude) sunrise = astral.sunrise_utc(utc_today, latitude, longitude) sunset = astral.sunset_utc(utc_today, latitude, longitude) assert dawn == sun.get_astral_event_date(hass, "dawn", utc_today) assert dusk == sun.get_astral_event_date(hass, "dusk", utc_today) assert midnight == sun.get_astral_event_date(hass, "solar_midnight", utc_today) assert noon == sun.get_astral_event_date(hass, "solar_noon", utc_today) assert sunrise == sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, utc_today) assert sunset == sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events(self): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude dawn = astral.dawn_utc(utc_today, latitude, longitude) dusk = astral.dusk_utc(utc_today, latitude, longitude) midnight = astral.solar_midnight_utc(utc_today, longitude) noon = astral.solar_noon_utc(utc_today, longitude) sunrise = astral.sunrise_utc(utc_today, latitude, longitude) sunset = astral.sunset_utc(utc_today, latitude, longitude) assert dawn == sun.get_astral_event_date( self.hass, 'dawn', utc_today) assert dusk == sun.get_astral_event_date( self.hass, 'dusk', utc_today) assert midnight == sun.get_astral_event_date( self.hass, 'solar_midnight', utc_today) assert noon == sun.get_astral_event_date( self.hass, 'solar_noon', utc_today) assert sunrise == sun.get_astral_event_date( self.hass, SUN_EVENT_SUNRISE, utc_today) assert sunset == sun.get_astral_event_date( self.hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events(self): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude dawn = astral.dawn_utc(utc_today, latitude, longitude) dusk = astral.dusk_utc(utc_today, latitude, longitude) midnight = astral.solar_midnight_utc(utc_today, longitude) noon = astral.solar_noon_utc(utc_today, longitude) sunrise = astral.sunrise_utc(utc_today, latitude, longitude) sunset = astral.sunset_utc(utc_today, latitude, longitude) self.assertEqual(dawn, sun.get_astral_event_date( self.hass, 'dawn', utc_today)) self.assertEqual(dusk, sun.get_astral_event_date( self.hass, 'dusk', utc_today)) self.assertEqual(midnight, sun.get_astral_event_date( self.hass, 'solar_midnight', utc_today)) self.assertEqual(noon, sun.get_astral_event_date( self.hass, 'solar_noon', utc_today)) self.assertEqual(sunrise, sun.get_astral_event_date( self.hass, 'sunrise', utc_today)) self.assertEqual(sunset, sun.get_astral_event_date( self.hass, 'sunset', utc_today))
def test_Astral_SolarMidnight(): a = Astral() l = a['London'] test_data = { datetime.date(2016, 2, 18): datetime.datetime(2016, 2, 18, 0, 14), datetime.date(2016, 10, 26): datetime.datetime(2016, 10, 25, 23, 44), } for day, solar_midnight in test_data.items(): solar_midnight = pytz.UTC.localize(solar_midnight) solar_midnight_utc = a.solar_midnight_utc(day, l.longitude) assert datetime_almost_equal(solar_midnight, solar_midnight_utc)
def normalized_solar_elevation(date_time): """Return the solar elevation as a number from 0 to 1. If date_time is naive, it is assumed to be in UTC. The result is 0 for the lowest solar elevation (solar midnight), 0.5 for sunset/sunrise, and 1 for the highest solar elevation (solar noon). """ latitude, longitude = geolocation() astral = Astral() elevation = lambda t: astral.solar_elevation(t, latitude, longitude) highest = elevation(astral.solar_noon_utc(date_time.date(), longitude)) lowest = elevation(astral.solar_midnight_utc(date_time.date(), longitude)) actual = elevation(date_time) if actual > 0: return map_number(actual, (0, highest), (0.5, 1)) return map_number(actual, (lowest, 0), (0, 0.5))
async def test_setting_rising(hass, legacy_patchable_time): """Test retrieving sun setting and rising.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now): await async_setup_component( hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}} ) await hass.async_block_till_done() state = hass.states.get(sun.ENTITY_ID) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = hass.config.latitude longitude = hass.config.longitude mod = -1 while True: next_dawn = astral.dawn_utc( utc_today + timedelta(days=mod), latitude, longitude ) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = astral.dusk_utc( utc_today + timedelta(days=mod), latitude, longitude ) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = astral.solar_midnight_utc( utc_today + timedelta(days=mod), longitude ) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = astral.solar_noon_utc(utc_today + timedelta(days=mod), longitude) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = astral.sunrise_utc( utc_today + timedelta(days=mod), latitude, longitude ) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = astral.sunset_utc( utc_today + timedelta(days=mod), latitude, longitude ) if next_setting > utc_now: break mod += 1 assert next_dawn == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_DAWN] ) assert next_dusk == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_DUSK] ) assert next_midnight == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_MIDNIGHT] ) assert next_noon == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_NOON] ) assert next_rising == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_RISING] ) assert next_setting == dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_SETTING] )
def test_next_events(self): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude mod = -1 while True: next_dawn = (astral.dawn_utc(utc_today + timedelta(days=mod), latitude, longitude)) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = (astral.dusk_utc(utc_today + timedelta(days=mod), latitude, longitude)) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = (astral.solar_midnight_utc( utc_today + timedelta(days=mod), longitude)) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = (astral.solar_noon_utc(utc_today + timedelta(days=mod), longitude)) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = (astral.sunrise_utc(utc_today + timedelta(days=mod), latitude, longitude)) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = (astral.sunset_utc(utc_today + timedelta(days=mod), latitude, longitude)) if next_setting > utc_now: break mod += 1 with patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=utc_now): self.assertEqual(next_dawn, sun.get_astral_event_next(self.hass, 'dawn')) self.assertEqual(next_dusk, sun.get_astral_event_next(self.hass, 'dusk')) self.assertEqual( next_midnight, sun.get_astral_event_next(self.hass, 'solar_midnight')) self.assertEqual( next_noon, sun.get_astral_event_next(self.hass, 'solar_noon')) self.assertEqual(next_rising, sun.get_astral_event_next(self.hass, 'sunrise')) self.assertEqual(next_setting, sun.get_astral_event_next(self.hass, 'sunset'))
def test_next_events(hass): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = hass.config.latitude longitude = hass.config.longitude mod = -1 while True: next_dawn = astral.dawn_utc(utc_today + timedelta(days=mod), latitude, longitude) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = astral.dusk_utc(utc_today + timedelta(days=mod), latitude, longitude) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = astral.solar_midnight_utc( utc_today + timedelta(days=mod), longitude) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = astral.solar_noon_utc(utc_today + timedelta(days=mod), longitude) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = astral.sunrise_utc(utc_today + timedelta(days=mod), latitude, longitude) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = astral.sunset_utc(utc_today + timedelta(days=mod), latitude, longitude) if next_setting > utc_now: break mod += 1 with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now): assert next_dawn == sun.get_astral_event_next(hass, "dawn") assert next_dusk == sun.get_astral_event_next(hass, "dusk") assert next_midnight == sun.get_astral_event_next( hass, "solar_midnight") assert next_noon == sun.get_astral_event_next(hass, "solar_noon") assert next_rising == sun.get_astral_event_next( hass, SUN_EVENT_SUNRISE) assert next_setting == sun.get_astral_event_next( hass, SUN_EVENT_SUNSET)
def test_setting_rising(self): """Test retrieving sun setting and rising.""" setup_component(self.hass, sun.DOMAIN, {sun.DOMAIN: { sun.CONF_ELEVATION: 0 }}) from astral import Astral astral = Astral() utc_now = dt_util.utcnow() latitude = self.hass.config.latitude longitude = self.hass.config.longitude mod = -1 while True: next_dawn = (astral.dawn_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = (astral.dusk_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = (astral.solar_midnight_utc( utc_now + timedelta(days=mod), longitude)) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = (astral.solar_noon_utc(utc_now + timedelta(days=mod), longitude)) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = (astral.sunrise_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = (astral.sunset_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_setting > utc_now: break mod += 1 self.assertEqual(next_dawn, sun.next_dawn_utc(self.hass)) self.assertEqual(next_dusk, sun.next_dusk_utc(self.hass)) self.assertEqual(next_midnight, sun.next_midnight_utc(self.hass)) self.assertEqual(next_noon, sun.next_noon_utc(self.hass)) self.assertEqual(next_rising, sun.next_rising_utc(self.hass)) self.assertEqual(next_setting, sun.next_setting_utc(self.hass)) # Point it at a state without the proper attributes self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON) self.assertIsNone(sun.next_dawn(self.hass)) self.assertIsNone(sun.next_dusk(self.hass)) self.assertIsNone(sun.next_midnight(self.hass)) self.assertIsNone(sun.next_noon(self.hass)) self.assertIsNone(sun.next_rising(self.hass)) self.assertIsNone(sun.next_setting(self.hass)) # Point it at a non-existing state self.assertIsNone(sun.next_dawn(self.hass, 'non.existing')) self.assertIsNone(sun.next_dusk(self.hass, 'non.existing')) self.assertIsNone(sun.next_midnight(self.hass, 'non.existing')) self.assertIsNone(sun.next_noon(self.hass, 'non.existing')) self.assertIsNone(sun.next_rising(self.hass, 'non.existing')) self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
def test_next_events(self): """Test retrieving next sun events.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude mod = -1 while True: next_dawn = (astral.dawn_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = (astral.dusk_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = (astral.solar_midnight_utc( utc_today + timedelta(days=mod), longitude)) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = (astral.solar_noon_utc( utc_today + timedelta(days=mod), longitude)) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = (astral.sunrise_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = (astral.sunset_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_setting > utc_now: break mod += 1 with patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=utc_now): assert next_dawn == sun.get_astral_event_next( self.hass, 'dawn') assert next_dusk == sun.get_astral_event_next( self.hass, 'dusk') assert next_midnight == sun.get_astral_event_next( self.hass, 'solar_midnight') assert next_noon == sun.get_astral_event_next( self.hass, 'solar_noon') assert next_rising == sun.get_astral_event_next( self.hass, SUN_EVENT_SUNRISE) assert next_setting == sun.get_astral_event_next( self.hass, SUN_EVENT_SUNSET)
def test_setting_rising(self): """Test retrieving sun setting and rising.""" utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) with patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=utc_now): setup_component(self.hass, sun.DOMAIN, { sun.DOMAIN: {sun.CONF_ELEVATION: 0}}) self.hass.block_till_done() state = self.hass.states.get(sun.ENTITY_ID) from astral import Astral astral = Astral() utc_today = utc_now.date() latitude = self.hass.config.latitude longitude = self.hass.config.longitude mod = -1 while True: next_dawn = (astral.dawn_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = (astral.dusk_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = (astral.solar_midnight_utc( utc_today + timedelta(days=mod), longitude)) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = (astral.solar_noon_utc( utc_today + timedelta(days=mod), longitude)) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = (astral.sunrise_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = (astral.sunset_utc( utc_today + timedelta(days=mod), latitude, longitude)) if next_setting > utc_now: break mod += 1 self.assertEqual(next_dawn, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_DAWN])) self.assertEqual(next_dusk, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_DUSK])) self.assertEqual(next_midnight, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_MIDNIGHT])) self.assertEqual(next_noon, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_NOON])) self.assertEqual(next_rising, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_RISING])) self.assertEqual(next_setting, dt_util.parse_datetime( state.attributes[sun.STATE_ATTR_NEXT_SETTING]))
def test_setting_rising(self): """Test retrieving sun setting and rising.""" setup_component(self.hass, sun.DOMAIN, { sun.DOMAIN: {sun.CONF_ELEVATION: 0}}) from astral import Astral astral = Astral() utc_now = dt_util.utcnow() latitude = self.hass.config.latitude longitude = self.hass.config.longitude mod = -1 while True: next_dawn = (astral.dawn_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_dawn > utc_now: break mod += 1 mod = -1 while True: next_dusk = (astral.dusk_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_dusk > utc_now: break mod += 1 mod = -1 while True: next_midnight = (astral.solar_midnight_utc(utc_now + timedelta(days=mod), longitude)) if next_midnight > utc_now: break mod += 1 mod = -1 while True: next_noon = (astral.solar_noon_utc(utc_now + timedelta(days=mod), longitude)) if next_noon > utc_now: break mod += 1 mod = -1 while True: next_rising = (astral.sunrise_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_rising > utc_now: break mod += 1 mod = -1 while True: next_setting = (astral.sunset_utc(utc_now + timedelta(days=mod), latitude, longitude)) if next_setting > utc_now: break mod += 1 self.assertEqual(next_dawn, sun.next_dawn_utc(self.hass)) self.assertEqual(next_dusk, sun.next_dusk_utc(self.hass)) self.assertEqual(next_midnight, sun.next_midnight_utc(self.hass)) self.assertEqual(next_noon, sun.next_noon_utc(self.hass)) self.assertEqual(next_rising, sun.next_rising_utc(self.hass)) self.assertEqual(next_setting, sun.next_setting_utc(self.hass)) # Point it at a state without the proper attributes self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON) self.assertIsNone(sun.next_dawn(self.hass)) self.assertIsNone(sun.next_dusk(self.hass)) self.assertIsNone(sun.next_midnight(self.hass)) self.assertIsNone(sun.next_noon(self.hass)) self.assertIsNone(sun.next_rising(self.hass)) self.assertIsNone(sun.next_setting(self.hass)) # Point it at a non-existing state self.assertIsNone(sun.next_dawn(self.hass, 'non.existing')) self.assertIsNone(sun.next_dusk(self.hass, 'non.existing')) self.assertIsNone(sun.next_midnight(self.hass, 'non.existing')) self.assertIsNone(sun.next_noon(self.hass, 'non.existing')) self.assertIsNone(sun.next_rising(self.hass, 'non.existing')) self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))