def test_is_dayTime_true_complex(self): timeZone = pytz.timezone('Europe/Berlin') sunrise = Helpers.timestamp_to_timezone(1604296782, timeZone) sunset = Helpers.timestamp_to_timezone(1604331478, timeZone) now = timeZone.localize( datetime(year=2020, month=11, day=2, hour=15, minute=0, second=0)) self.assertTrue(Helpers.is_dayTime(sunrise, sunset, now))
def fetch(self, pageName: str) -> Dict: weatherService = ServiceManager.get_instance( ).get_service_by_type_name('WeatherService') fetchIntervalInSeconds = 60 * 10 # query api less often timeZone = pytz.timezone(self._settings['timeZone']) # cache key will be determined in service weatherData = weatherService.get_data('', fetchIntervalInSeconds, self._settings) currentWeather = weatherData['current'] currentTemperature = currentWeather['temp'] feelsLike = currentWeather['feels_like'] windSpeed = currentWeather['wind_speed'] * 3.6 icon = currentWeather['weather'][0]['id'] sunrise = Helpers.timestamp_to_timezone(currentWeather['sunrise'], timeZone) sunset = Helpers.timestamp_to_timezone(currentWeather['sunset'], timeZone) isDayTime = Helpers.is_dayTime(sunrise, sunset, datetime.now(tz=timeZone)) temperatureRounded = Helpers.round_to_decimals(currentTemperature, 1) feelsLikeRounded = Helpers.round_to_decimals(feelsLike, 0) return { 'temperature': temperatureRounded, 'temperatureColor': Helpers.determine_color_for_temperature(float(temperatureRounded)), 'feelsLike': feelsLikeRounded, 'feelsLikeColor': Helpers.determine_color_for_temperature(float(feelsLikeRounded)), 'icon': icon, 'iconColor': Helpers.determine_color_for_weather_icon(icon, isDayTime), 'windDegrees': currentWeather['wind_deg'], 'windSpeed': f'{Helpers.round_to_decimals(windSpeed, 1)} km/h', 'windSpeedColor': Helpers.determine_color_for_wind(windSpeed), 'isDayTime': isDayTime, 'description': currentWeather['weather'][0]['description'] }
def fetch(self, pageName: str) -> Dict: weatherService = ServiceManager.get_instance().get_service_by_type_name('WeatherService') fetchIntervalInSeconds = 60 * 10 # query api less often timeZone = pytz.timezone(self._settings['timeZone']) # cache key will be determined in service weatherData = weatherService.get_data('', fetchIntervalInSeconds, self._settings) sunrise = Helpers.timestamp_to_timezone(weatherData['current']['sunrise'], timeZone) sunset = Helpers.timestamp_to_timezone(weatherData['current']['sunset'], timeZone) hourData = [] hourlyForecast = weatherData['hourly'] for entry in hourlyForecast[:12]: timestamp = Helpers.timestamp_to_timezone(entry['dt'] + 1800, timeZone) isDayTime = Helpers.is_dayTime(sunrise, sunset, currentTimestamp=timestamp) temperature = entry['temp'] iconId = entry['weather'][0]['id'] if isDayTime: icon = f'wi-owm-day-{iconId}' else: icon = f'wi-owm-night-{iconId}' rainProbability = round(entry['pop'] * 100, -1) # -1 rounds to the next ten windSpeed = entry['wind_speed'] * 3.6 temperatureRounded = Helpers.round_to_decimals(temperature, 0) windSpeedRounded = Helpers.round_to_decimals(windSpeed, 0) hourData.append({ 'hour': timestamp.strftime('%H'), 'temperature': temperatureRounded, 'temperatureColor': Helpers.determine_color_for_temperature(float(temperatureRounded)), 'icon': icon, 'iconColor': Helpers.determine_color_for_weather_icon(iconId, isDayTime), 'windSpeed': f'{windSpeedRounded} km/h', 'windSpeedColor': Helpers.determine_color_for_wind(float(windSpeedRounded)), 'rainProbability': f'{Helpers.round_to_decimals(rainProbability, 0)} %', 'isDayTime': isDayTime, 'description': entry['weather'][0]['description'] }) return { 'hours': hourData }
def test_is_dayTime_differentDays(self): sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0) sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0) currentTimestamp = datetime(year=2020, month=11, day=2, hour=12, minute=0, second=0) self.assertTrue(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
def test_is_dayTime_false_after(self): sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0) sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0) currentTimestamp = datetime(year=2020, month=11, day=1, hour=18, minute=0, second=0) self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))