Skip to content

Commit 66b8656

Browse files
authored
Fix icons, add hourly support, add other weatherflow changes (#3729)
I have updated weatherflow.js to implement the following changes (as described in #3728) - Fixed: Weather icons now show up properly - Added: Location Name support - Added: Hourly weather forecast support - Added to current conditions: - "Feels like" temp - Fixed icon for current conditions to be sourced from current conditions (rather than daily forecast) - UV index - Added to daily forecast - Precipitation amount and UV index (via hourly forecast data) Before: ![image](https://github.com/user-attachments/assets/cfef043c-75ef-4571-8bdc-462e75d3ed81) After: ![image](https://github.com/user-attachments/assets/e36118bb-a508-4ab1-a7ad-a775bd7a9bb3)
1 parent 4a398f0 commit 66b8656

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ planned for 2025-04-01
2424
- [core] Add issue templates for feature requests and bug reports (#3695)
2525
- [core] Adapt `start:x11:dev` script
2626
- [weather/yr] The Yr weather provider now enforces a minimum `updateInterval` of 600 000 ms (10 minutes) to comply with the terms of service. If a lower value is set, it will be automatically increased to this minimum.
27+
- [weather/weatherflow] Fixed icons and added hourly support as well as UV, precipitation, and location name support.
2728
- [workflow] Run `sudo apt-get update` before installing packages to avoid install errors
2829

2930
### Removed

modules/default/weather/providers/weatherflow.js

+81-4
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ WeatherProvider.register("weatherflow", {
2525
const currentWeather = new WeatherObject();
2626
currentWeather.date = moment();
2727

28+
// Other available values: air_density, brightness, delta_t, dew_point,
29+
// pressure_trend (i.e. rising/falling), sea_level_pressure, wind gust, and more.
30+
2831
currentWeather.humidity = data.current_conditions.relative_humidity;
2932
currentWeather.temperature = data.current_conditions.air_temperature;
33+
currentWeather.feelsLikeTemp = data.current_conditions.feels_like;
3034
currentWeather.windSpeed = WeatherUtils.convertWindToMs(data.current_conditions.wind_avg);
3135
currentWeather.windFromDirection = data.current_conditions.wind_direction;
32-
currentWeather.weatherType = data.forecast.daily[0].icon;
36+
currentWeather.weatherType = this.convertWeatherType(data.current_conditions.icon);
37+
currentWeather.uv_index = data.current_conditions.uv;
3338
currentWeather.sunrise = moment.unix(data.forecast.daily[0].sunrise);
3439
currentWeather.sunset = moment.unix(data.forecast.daily[0].sunset);
3540
this.setCurrentWeather(currentWeather);
41+
this.fetchedLocationName = data.location_name;
3642
})
3743
.catch(function (request) {
3844
Log.error("Could not load data ... ", request);
@@ -52,20 +58,91 @@ WeatherProvider.register("weatherflow", {
5258
weather.minTemperature = forecast.air_temp_low;
5359
weather.maxTemperature = forecast.air_temp_high;
5460
weather.precipitationProbability = forecast.precip_probability;
55-
weather.weatherType = forecast.icon;
56-
weather.snow = 0;
61+
weather.weatherType = this.convertWeatherType(forecast.icon);
62+
63+
// Must manually build UV and Precipitation from hourly
64+
weather.precipitationAmount = 0.0; // This will sum up rain and snow
65+
weather.precipitationUnits = "mm";
66+
weather.uv_index = 0;
5767

68+
for (const hour of data.forecast.hourly) {
69+
const hour_time = moment.unix(hour.time);
70+
if (hour_time.day() === weather.date.day()) { // Iterate though until day is reached
71+
// Get data from today
72+
weather.uv_index = Math.max(weather.uv_index, hour.uv);
73+
weather.precipitationAmount += (hour.precip ?? 0);
74+
} else if (hour_time.diff(weather.date) >= 86400) {
75+
break; // No more data to be found
76+
}
77+
}
5878
days.push(weather);
5979
}
60-
6180
this.setWeatherForecast(days);
81+
this.fetchedLocationName = data.location_name;
6282
})
6383
.catch(function (request) {
6484
Log.error("Could not load data ... ", request);
6585
})
6686
.finally(() => this.updateAvailable());
6787
},
6888

89+
fetchWeatherHourly () {
90+
this.fetchData(this.getUrl())
91+
.then((data) => {
92+
const hours = [];
93+
for (const hour of data.forecast.hourly) {
94+
const weather = new WeatherObject();
95+
96+
weather.date = moment.unix(hour.time);
97+
weather.temperature = hour.air_temperature;
98+
weather.feelsLikeTemp = hour.feels_like;
99+
weather.humidity = hour.relative_humidity;
100+
weather.windSpeed = hour.wind_avg;
101+
weather.windFromDirection = hour.wind_direction;
102+
weather.weatherType = this.convertWeatherType(hour.icon);
103+
weather.precipitationProbability = hour.precip_probability;
104+
weather.precipitationAmount = hour.precip; // NOTE: precipitation type is available
105+
weather.precipitationUnits = "mm"; // Hardcoded via request, TODO: Add conversion
106+
weather.uv_index = hour.uv;
107+
108+
hours.push(weather);
109+
if (hours.length >= 48) break; // 10 days of hours are available, best to trim down.
110+
}
111+
this.setWeatherHourly(hours);
112+
this.fetchedLocationName = data.location_name;
113+
})
114+
.catch(function (request) {
115+
Log.error("Could not load data ... ", request);
116+
})
117+
.finally(() => this.updateAvailable());
118+
},
119+
120+
convertWeatherType (weatherType) {
121+
const weatherTypes = {
122+
"clear-day": "day-sunny",
123+
"clear-night": "night-clear",
124+
cloudy: "cloudy",
125+
foggy: "fog",
126+
"partly-cloudy-day": "day-cloudy",
127+
"partly-cloudy-night": "night-alt-cloudy",
128+
"possibly-rainy-day": "day-rain",
129+
"possibly-rainy-night": "night-alt-rain",
130+
"possibly-sleet-day": "day-sleet",
131+
"possibly-sleet-night": "night-alt-sleet",
132+
"possibly-snow-day": "day-snow",
133+
"possibly-snow-night": "night-alt-snow",
134+
"possibly-thunderstorm-day": "day-thunderstorm",
135+
"possibly-thunderstorm-night": "night-alt-thunderstorm",
136+
rainy: "rain",
137+
sleet: "sleet",
138+
snow: "snow",
139+
thunderstorm: "thunderstorm",
140+
windy: "strong-wind"
141+
};
142+
143+
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
144+
},
145+
69146
// Create a URL from the config and base URL.
70147
getUrl () {
71148
return `${this.config.apiBase}better_forecast?station_id=${this.config.stationid}&units_temp=c&units_wind=kph&units_pressure=mb&units_precip=mm&units_distance=km&token=${this.config.token}`;

0 commit comments

Comments
 (0)