def test_Twilight_SunSetting(day, twilight, london): start, end = twilight start = pytz.utc.localize(start) end = pytz.utc.localize(end) info = sun.twilight(london.observer, day, direction=SunDirection.SETTING) start_utc = info[0] end_utc = info[1] assert datetime_almost_equal(start, start_utc) assert datetime_almost_equal(end, end_utc)
def publishLightInfo(dateAndTime): if state.timezone is None: print("Could not determine the time zone") else: # Calculate solar elevation and twilight start and end times angle = sun.elevation(location, dateAndTime) print('solar elevation: ' + str(angle)) mqtt_publish('solarElevation', str(angle)) times_setting = sun.twilight(location, dateAndTime, SunDirection.SETTING) times_rising = sun.twilight(location, dateAndTime, SunDirection.RISING) if debug: print("DEBUG: rising start " + str(times_rising[0].astimezone(state.timezone))) print("DEBUG: rising end " + str(times_rising[1].astimezone(state.timezone))) print("DEBUG: setting start " + str(times_setting[0].astimezone(state.timezone))) print("DEBUG: setting end " + str(times_setting[1].astimezone(state.timezone))) # Classify and publish the current light situation on track as one of # night, dawn, day or dusk lightinfo = 'day' if dateAndTime < times_rising[0].astimezone(state.timezone): lightinfo = 'night' elif dateAndTime < times_rising[1].astimezone(state.timezone): lightinfo = 'dawn' elif dateAndTime < times_setting[0].astimezone(state.timezone): lightinfo = 'day' elif dateAndTime < times_setting[1].astimezone(state.timezone): lightinfo = 'dusk' else: lightinfo = 'night' print('lightinfo: ' + lightinfo) mqtt_publish('lightinfo', lightinfo)
def test_Twilight_NoDate(london): start = pytz.utc.localize(datetime.datetime(2019, 8, 29, 18, 54)) end = pytz.utc.localize(datetime.datetime(2019, 8, 29, 19, 30)) ans = sun.twilight(london.observer, direction=SunDirection.SETTING) assert datetime_almost_equal(ans[0], start) assert datetime_almost_equal(ans[1], end)
def getCivilTwilight(self, event_date: date = None) -> datetime: '''Return the start of civil sunset on a given date. Default is today's date in the timezone ``WeeklySchedule.tzinfo``.''' twilight_start_end = twilight(self.observer, event_date, SunDirection.SETTING, self.tzinfo) return twilight_start_end[0]