def update(self): """Get the latest data - current weather and forecasted weather from FMI.""" # Current Weather try: self.current = fmi.weather_by_coordinates(self.latitude, self.longitude) except ClientError as err: err_string = ("Client error with status " + str(err.status_code) + " and message " + err.message) _LOGGER.error(err_string) except ServerError as err: err_string = ("Server error with status " + str(err.status_code) + " and message " + err.body) _LOGGER.error(err_string) self.current = None # Hourly weather for 24hrs. try: self.hourly = fmi.forecast_by_coordinates( self.latitude, self.longitude, timestep_hours=self.time_step) except ClientError as err: err_string = ("Client error with status " + str(err.status_code) + " and message " + err.message) _LOGGER.error(err_string) except ServerError as err: err_string = ("Server error with status " + str(err.status_code) + " and message " + err.body) _LOGGER.error(err_string) self.hourly = None
import fmi_weather_client as fmi from fmi_weather_client.errors import ClientError, ServerError try: # Get current weather in Kilpisjärvi using coordinates kilpisjarvi_weather = fmi.weather_by_coordinates(69.0478, 20.7982) # Get forecast for Helsinki helsinki_forecast = fmi.forecast_by_place_name("Helsinki") # Print current temperature print() print(f"Temperature @ {kilpisjarvi_weather.place}: {kilpisjarvi_weather.data.temperature}") # Print temperature forecasts print() print(f"Forecast for {helsinki_forecast.place}") for forecast in helsinki_forecast.forecasts: print(f"- Temperature at {forecast.time}: {forecast.temperature}") except ClientError as err: # Catch and print client errors (invalid coordinate, unknown place etc) print(f"FMI returned a client error {err.status_code}: {err.message}", err) except ServerError as err: # Catch and print server errors print(f"FMI returned a server error {err.status_code}: {err.body}", err)
def update(self): """Get the latest and forecasted weather from FMI.""" def update_best_weather_condition(): if self.hourly is None: return if self.current is None: return curr_date = date.today() # Init values self.best_state = BEST_CONDITION_NOT_AVAIL self.best_time = self.current.data.time.astimezone(tz.tzlocal()) self.best_temperature = self.current.data.temperature.value self.best_humidity = self.current.data.humidity.value self.best_wind_speed = self.current.data.wind_speed.value self.best_precipitation = self.current.data.precipitation_amount.value for forecast in self.hourly.forecasts: local_time = forecast.time.astimezone(tz.tzlocal()) if local_time.day == curr_date.day + 1: # Tracking best conditions for only this day break if ( (forecast.symbol.value in BEST_COND_SYMBOLS) and (forecast.wind_speed.value >= self.min_wind_speed) and (forecast.wind_speed.value <= self.max_wind_speed) ): if ( forecast.temperature.value >= self.min_temperature and forecast.temperature.value <= self.max_temperature ): if ( forecast.humidity.value >= self.min_humidity and forecast.humidity.value <= self.max_humidity ): if ( forecast.precipitation_amount.value >= self.min_precip and forecast.precipitation_amount.value <= self.max_precip ): # What more can you ask for? # Compare with temperature value already stored and update if necessary self.best_state = BEST_CONDITION_AVAIL if self.best_state is BEST_CONDITION_AVAIL: if forecast.temperature.value > self.best_temperature: self.best_time = local_time self.best_temperature = forecast.temperature.value self.best_humidity = forecast.humidity.value self.best_wind_speed = forecast.wind_speed.value self.best_precipitation = forecast.precipitation_amount.value # Current Weather try: self.current = fmi.weather_by_coordinates(self.latitude, self.longitude) except ClientError as err: err_string = ( "Client error with status " + str(err.status_code) + " and message " + err.message ) _LOGGER.error(err_string) except ServerError as err: err_string = ( "Server error with status " + str(err.status_code) + " and message " + err.body ) _LOGGER.error(err_string) self.current = None # Hourly weather for 24hrs. try: self.hourly = fmi.forecast_by_coordinates( self.latitude, self.longitude, timestep_hours=self.time_step ) except ClientError as err: err_string = ( "Client error with status " + str(err.status_code) + " and message " + err.message ) _LOGGER.error(err_string) except ServerError as err: err_string = ( "Server error with status " + str(err.status_code) + " and message " + err.body ) _LOGGER.error(err_string) self.hourly = None # Update best time parameters update_best_weather_condition()
def test_no_location_exception_response(self, mock_get): with self.assertRaises(ClientError): fmi_weather_client.weather_by_coordinates(27.31317, 63.14343)
def test_nil_weather_response(self, mock_get): weather_coord = fmi_weather_client.weather_by_coordinates( 25.46816, 65.01236) weather_name = fmi_weather_client.weather_by_place_name('Oulu') self.assertIsNone(weather_coord) self.assertIsNone(weather_name)
def test_get_weather_by_coordinates(self, mock_get): weather = fmi_weather_client.weather_by_coordinates(63.14343, 27.31317) self.assert_coordinate_weather(weather)
def test_server_error_response(self, mock_get): with self.assertRaises(ServerError): fmi_weather_client.weather_by_coordinates(27.31317, 63.14343)