Skip to content

Commit de3f57f

Browse files
weather: fix UV index display and add WeatherFlow precipitation (#4108)
While looking at the WeatherFlow provider (to evaluate #4107), I noticed a few things that weren't quite right. 1. **UV index was broken for most providers in forecast/hourly views.** The templates read `uv_index`, but only the WeatherAPI provider actually wrote that key. All other providers (OpenWeatherMap, WeatherFlow, PirateWeather, etc.) use `uvIndex` - so UV was silently never displayed for them. This went unnoticed because `showUVIndex` defaults to `false` and there were no test assertions for it. Standardized everything on `uvIndex` and added test coverage. 2. **WeatherFlow didn't map precipitation for current weather.** The API provides `precip_accum_local_day` and `precip_probability`, but they weren't passed through. While adding them I also noticed the template used truthiness checks, which hid valid zero values. Fixed both. 3. **`||` vs `??` in WeatherFlow provider.** Several numeric fields used `|| null`, replacing valid `0` with `null`. Switched to `??` for correctness.
1 parent d3a3ad9 commit de3f57f

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

defaultmodules/weather/current.njk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{% if config.showUVIndex %}
3939
<td class="align-right bright uv-index">
4040
<div class="wi dimmed wi-hot"></div>
41-
{{ current.uv_index }}
41+
{{ current.uvIndex }}
4242
</td>
4343
{% endif %}
4444
</div>
@@ -78,11 +78,11 @@
7878
</span>
7979
<br />
8080
{% endif %}
81-
{% if config.showPrecipitationAmount and current.precipitationAmount %}
81+
{% if config.showPrecipitationAmount and current.precipitationAmount is defined and current.precipitationAmount is not none %}
8282
<span class="dimmed"> <span class="precipitationLeadText">{{ "PRECIP_AMOUNT" | translate }}</span> {{ current.precipitationAmount | unit("precip", current.precipitationUnits) }} </span>
8383
<br />
8484
{% endif %}
85-
{% if config.showPrecipitationProbability and current.precipitationProbability %}
85+
{% if config.showPrecipitationProbability and current.precipitationProbability is defined and current.precipitationProbability is not none %}
8686
<span class="dimmed"> <span class="precipitationLeadText">{{ "PRECIP_POP" | translate }}</span> {{ current.precipitationProbability }}% </span>
8787
{% endif %}
8888
</div>

defaultmodules/weather/forecast.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
{% endif %}
3232
{% if config.showUVIndex %}
3333
<td class="align-right dimmed uv-index">
34-
{{ f.uv_index }}
34+
{{ f.uvIndex }}
3535
<span class="wi dimmed weathericon wi-hot"></span>
3636
</td>
3737
{% endif %}

defaultmodules/weather/hourly.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<td class="align-right bright">{{ hour.temperature | roundValue | unit("temperature") }}</td>
1616
{% if config.showUVIndex %}
1717
<td class="align-right bright uv-index">
18-
{% if hour.uv_index!=0 %}
19-
{{ hour.uv_index }}
18+
{% if hour.uvIndex!=0 %}
19+
{{ hour.uvIndex }}
2020
<span class="wi weathericon wi-hot"></span>
2121
{% endif %}
2222
</td>

defaultmodules/weather/providers/weatherapi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class WeatherAPIProvider {
351351
weather.precipitationProbability = precipitationProbability;
352352
}
353353

354-
weather.uv_index = this.#toNumber(forecastDay.day?.uv);
354+
weather.uvIndex = this.#toNumber(forecastDay.day?.uv);
355355

356356
days.push(weather);
357357

@@ -410,7 +410,7 @@ class WeatherAPIProvider {
410410
const willSnow = this.#toNumber(hourData.will_it_snow) ?? 0;
411411
weather.precipitationProbability = (willRain + willSnow) * 50;
412412

413-
weather.uv_index = this.#toNumber(hourData.uv);
413+
weather.uvIndex = this.#toNumber(hourData.uv);
414414

415415
hours.push(weather);
416416

defaultmodules/weather/providers/weatherflow.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,15 @@ class WeatherFlowProvider {
145145

146146
const weather = {
147147
date: new Date(),
148-
humidity: current.relative_humidity || null,
149-
temperature: current.air_temperature || null,
150-
feelsLikeTemp: current.feels_like || null,
148+
humidity: current.relative_humidity ?? null,
149+
temperature: current.air_temperature ?? null,
150+
feelsLikeTemp: current.feels_like ?? null,
151151
windSpeed: current.wind_avg != null ? convertKmhToMs(current.wind_avg) : null,
152-
windFromDirection: current.wind_direction || null,
152+
windFromDirection: current.wind_direction ?? null,
153153
weatherType: this.#convertWeatherType(current.icon),
154+
precipitationAmount: current.precip_accum_local_day ?? null,
155+
precipitationUnits: "mm",
156+
precipitationProbability: current.precip_probability ?? null,
154157
uvIndex: current.uv || null,
155158
sunrise: daily.sunrise ? new Date(daily.sunrise * 1000) : null,
156159
sunset: daily.sunset ? new Date(daily.sunset * 1000) : null
@@ -175,9 +178,9 @@ class WeatherFlowProvider {
175178
for (const forecast of data.forecast.daily) {
176179
const weather = {
177180
date: new Date(forecast.day_start_local * 1000),
178-
minTemperature: forecast.air_temp_low || null,
179-
maxTemperature: forecast.air_temp_high || null,
180-
precipitationProbability: forecast.precip_probability || null,
181+
minTemperature: forecast.air_temp_low ?? null,
182+
maxTemperature: forecast.air_temp_high ?? null,
183+
precipitationProbability: forecast.precip_probability ?? null,
181184
weatherType: this.#convertWeatherType(forecast.icon),
182185
precipitationAmount: 0.0,
183186
precipitationUnits: "mm",
@@ -193,8 +196,8 @@ class WeatherFlowProvider {
193196
if (hourDate.getFullYear() === forecastDate.getFullYear()
194197
&& hourDate.getMonth() === forecastDate.getMonth()
195198
&& hourDate.getDate() === forecastDate.getDate()) {
196-
weather.uvIndex = Math.max(weather.uvIndex, hour.uv || 0);
197-
weather.precipitationAmount += hour.precip || 0;
199+
weather.uvIndex = Math.max(weather.uvIndex, hour.uv ?? 0);
200+
weather.precipitationAmount += hour.precip ?? 0;
198201
} else if (hourDate > forecastDate) {
199202
// Check if we've moved to the next day
200203
const diffMs = hourDate - forecastDate;
@@ -224,14 +227,14 @@ class WeatherFlowProvider {
224227
for (const hour of data.forecast.hourly) {
225228
const weather = {
226229
date: new Date(hour.time * 1000),
227-
temperature: hour.air_temperature || null,
228-
feelsLikeTemp: hour.feels_like || null,
229-
humidity: hour.relative_humidity || null,
230+
temperature: hour.air_temperature ?? null,
231+
feelsLikeTemp: hour.feels_like ?? null,
232+
humidity: hour.relative_humidity ?? null,
230233
windSpeed: hour.wind_avg != null ? convertKmhToMs(hour.wind_avg) : null,
231-
windFromDirection: hour.wind_direction || null,
234+
windFromDirection: hour.wind_direction ?? null,
232235
weatherType: this.#convertWeatherType(hour.icon),
233-
precipitationProbability: hour.precip_probability || null,
234-
precipitationAmount: hour.precip || 0,
236+
precipitationProbability: hour.precip_probability ?? null,
237+
precipitationAmount: hour.precip ?? 0,
235238
precipitationUnits: "mm",
236239
uvIndex: hour.uv || null
237240
};

tests/unit/modules/default/weather/providers/weatherapi_spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ describe("WeatherAPIProvider", () => {
240240
expect(result[0].minTemperature).toBe(-8);
241241
expect(result[0].maxTemperature).toBe(-1);
242242
expect(result[0].weatherType).toBe("day-sprinkle");
243+
expect(result[0].uvIndex).toBe(1);
243244
expect(result[0].sunrise).toBeInstanceOf(Date);
244245
expect(result[0].sunset).toBeInstanceOf(Date);
245246
});
@@ -275,6 +276,7 @@ describe("WeatherAPIProvider", () => {
275276
expect(result[0].humidity).toBe(85);
276277
expect(result[0].windFromDirection).toBe(210);
277278
expect(result[0].weatherType).toBe("night-sprinkle");
279+
expect(result[0].uvIndex).toBe(0);
278280
expect(result[0].precipitationProbability).toBe(50);
279281
});
280282
});

tests/unit/modules/default/weather/providers/weatherflow_spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ describe("WeatherFlowProvider", () => {
8585
expect(result).toBeDefined();
8686
expect(result.temperature).toBe(16);
8787
expect(result.humidity).toBe(28);
88+
expect(result.precipitationAmount).toBe(0);
89+
expect(result.precipitationUnits).toBe("mm");
90+
expect(result.precipitationProbability).toBe(0);
8891
expect(result.weatherType).not.toBeNull();
8992
});
9093

0 commit comments

Comments
 (0)