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 _prepare_measurement_data( self, measurements: List[Dict]) -> Tuple[List[str], List[str]]: x = [] y = [] for measurement in measurements: timestamp = measurement['timestamp'] x.append(timestamp) value = float(measurement['value']) y.append( Helpers.round_to_decimals(value, self._settings['decimals'])) x.reverse() y.reverse() return x, y
def test_round_to_decimals_zero(self): self.assertEqual('0', Helpers.round_to_decimals(0.428, 0))
def test_round_to_decimals_one(self): self.assertEqual('0.4', Helpers.round_to_decimals(0.428, 1))