|
| 1 | +import logging |
| 2 | +from weather_providers.base_provider import BaseWeatherProvider |
| 3 | + |
| 4 | + |
| 5 | +class SMHI(BaseWeatherProvider): |
| 6 | + def __init__(self, smhi_self_id, location_lat, location_long, units): |
| 7 | + self.smhi_self_id = smhi_self_id |
| 8 | + self.location_lat = location_lat |
| 9 | + self.location_long = location_long |
| 10 | + self.units = units |
| 11 | + |
| 12 | + # Map SMHI icons to local icons |
| 13 | + # Reference: https://opendata.smhi.se/apidocs/metfcst/parameters.html#parameter-wsymb |
| 14 | + def get_icon_from_smhi_weathercode(self, weathercode, is_daytime): |
| 15 | + |
| 16 | + icon_dict = { |
| 17 | + 1: "clear_sky_day" if is_daytime else "clearnight", # Clear sky |
| 18 | + 2: "clear_sky_day" if is_daytime else "clearnight", # Nearly clear sky |
| 19 | + 3: "few_clouds" if is_daytime else "partlycloudynight", # Variable cloudiness |
| 20 | + 4: "scattered_clouds" if is_daytime else "partlycloudynight", # Halfclear sky |
| 21 | + 5: "mostly_cloudy" if is_daytime else "mostly_cloudy_night", # Cloudy sky |
| 22 | + 6: "overcast", # Overcast |
| 23 | + 7: "climacell_fog", # Fog |
| 24 | + 8: 'climacell_rain_light' if is_daytime else 'rain_night_light', # Light rain showers |
| 25 | + 9: "climacell_rain" if is_daytime else "rain_night", # Moderate rain showers |
| 26 | + 10: "climacell_rain_heavy" if is_daytime else "rain_night_heavy", # Heavy rain showers |
| 27 | + 11: "thundershower_rain", # Thunderstorm |
| 28 | + 12: "sleet", # Light sleet showers |
| 29 | + 13: "sleet", # Moderate sleet showers |
| 30 | + 14: "sleet", # Heavy sleet showers |
| 31 | + 15: "climacell_snow_light", # Light snow showers |
| 32 | + 16: "snow", # Moderate snow showers |
| 33 | + 17: "snow", # Heavy snow showers |
| 34 | + 18: 'climacell_rain_light' if is_daytime else 'rain_night_light', # Light rain |
| 35 | + 19: "climacell_rain" if is_daytime else "rain_night", # Moderate rain |
| 36 | + 20: "climacell_rain_heavy" if is_daytime else "rain_night_heavy", # Heavy rain |
| 37 | + 21: "thundershower_rain", # Thunder |
| 38 | + 22: "sleet", # Light sleet |
| 39 | + 23: "sleet", # Moderate sleet |
| 40 | + 24: "sleet", # Heavy sleet |
| 41 | + 25: "climacell_snow_light", # Light snowfall |
| 42 | + 26: "snow", # Moderate snowfall |
| 43 | + 27: "snow", # Heavy snowfall |
| 44 | + } |
| 45 | + |
| 46 | + icon = icon_dict[weathercode] |
| 47 | + logging.debug( |
| 48 | + "get_icon_by_weathercode({}) - {}" |
| 49 | + .format(weathercode, icon)) |
| 50 | + |
| 51 | + return icon |
| 52 | + |
| 53 | + def get_description_from_smhi_weathercode(self, weathercode): |
| 54 | + description_dict = { |
| 55 | + 1: "Clear sky", |
| 56 | + 2: "Nearly clear sky", |
| 57 | + 3: "Variable cloudiness", |
| 58 | + 4: "Halfclear sky", |
| 59 | + 5: "Cloudy sky", |
| 60 | + 6: "Overcast", |
| 61 | + 7: "Fog", |
| 62 | + 8: "Light rain showers", |
| 63 | + 9: "Moderate rain showers", |
| 64 | + 10: "Heavy rain showers", |
| 65 | + 11: "Thunderstorm", |
| 66 | + 12: "Light sleet showers", |
| 67 | + 13: "Moderate sleet showers", |
| 68 | + 14: "Heavy sleet showers", |
| 69 | + 15: "Light snow showers", |
| 70 | + 16: "Moderate snow showers", |
| 71 | + 17: "Heavy snow showers", |
| 72 | + 18: "Light rain", |
| 73 | + 19: "Moderate rain", |
| 74 | + 20: "Heavy rain", |
| 75 | + 21: "Thunder", |
| 76 | + 22: "Light sleet", |
| 77 | + 23: "Moderate sleet", |
| 78 | + 24: "Heavy sleet", |
| 79 | + 25: "Light snowfall", |
| 80 | + 26: "Moderate snowfall", |
| 81 | + 27: "Heavy snowfall", |
| 82 | + } |
| 83 | + description = description_dict[weathercode] |
| 84 | + |
| 85 | + logging.debug( |
| 86 | + "get_description_by_weathercode({}) - {}" |
| 87 | + .format(weathercode, description)) |
| 88 | + |
| 89 | + return description.title() |
| 90 | + |
| 91 | + # Get weather from SMHI API |
| 92 | + # https://opendata.smhi.se/apidocs/metfcst/get-forecast.html#get-point-forecast |
| 93 | + # The API response is a complete forecast approximately 10 days ahead of the latest current forecast. |
| 94 | + # All times in the answer given in UTC. |
| 95 | + # Precipitation parameters have a distribution in time (a time interval) until the valid time for current data. |
| 96 | + # The interval starts at the time step before. At the beginning of the forecast, the interval is one hour. |
| 97 | + # Later in the forecast, the time interval increases (eg 3, 6 and 12 h). Unit remains mm / h. |
| 98 | + # So, current hour is index 0, next hour is index 1 |
| 99 | + # The API accepts 6 decimals in the lon/lat. With more decimals, it returns 404. |
| 100 | + |
| 101 | + def get_weather(self): |
| 102 | + |
| 103 | + url = ("https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/{}/lat/{}/data.json" |
| 104 | + .format(self.location_long, self.location_lat)) |
| 105 | + |
| 106 | + headers = {"User-Agent": self.smhi_self_id} |
| 107 | + |
| 108 | + response_data = self.get_response_json(url, headers=headers) |
| 109 | + logging.debug(response_data) |
| 110 | + weather_data = response_data["timeSeries"] |
| 111 | + logging.debug("get_weather() - {}".format(weather_data)) |
| 112 | + |
| 113 | + # Get the weather code of the first item. |
| 114 | + for data in weather_data[0]["parameters"]: |
| 115 | + if data["name"] == "Wsymb2": |
| 116 | + weather_code = data["values"][0] |
| 117 | + |
| 118 | + daytime = self.is_daytime(self.location_lat, self.location_long) |
| 119 | + |
| 120 | + # { "temperatureMin": "2.0", "temperatureMax": "15.1", "icon": "mostly_cloudy", "description": "Cloudy with light breezes" } |
| 121 | + # No Min or Max here. We just get the estimated temperature for the hour. |
| 122 | + # Since we get the forecast for several hours and days, get the min/max for the next 12 hours? |
| 123 | + weather = {} |
| 124 | + temp_list = [] |
| 125 | + |
| 126 | + for item in range(0, 12): |
| 127 | + for param in weather_data[item]['parameters']: |
| 128 | + if param['name'] == 't': |
| 129 | + temp = param['values'][0] |
| 130 | + temp_list.append(temp) |
| 131 | + |
| 132 | + weather["temperatureMin"] = min(temp_list) |
| 133 | + weather["temperatureMax"] = max(temp_list) |
| 134 | + weather["icon"] = self.get_icon_from_smhi_weathercode(weather_code, daytime) |
| 135 | + weather["description"] = self.get_description_from_smhi_weathercode(weather_code) |
| 136 | + logging.debug(weather) |
| 137 | + return weather |
0 commit comments