Skip to content

Commit 3ceb5b4

Browse files
tests(weather): fix EnvCanada provider to read current weather from currentConditions
Wind speed, bearing, temperature, and humidity now correctly read from currentConditions element instead of forecast, with fallback logic.
1 parent 310a5ac commit 3ceb5b4

1 file changed

Lines changed: 52 additions & 17 deletions

File tree

defaultmodules/weather/providers/envcanada.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,39 @@ class EnvCanadaProvider {
155155
#generateCurrentWeather (xml) {
156156
const current = { date: new Date() };
157157

158-
// Since <currentConditions/> is often empty, extract from first forecast period
158+
// Try to get temperature from currentConditions first
159+
const currentTempStr = this.#extract(xml, /<currentConditions>.*?<temperature[^>]*>(.*?)<\/temperature>/s);
160+
161+
if (currentTempStr && currentTempStr !== "") {
162+
current.temperature = parseFloat(currentTempStr);
163+
this.cacheCurrentTemp = current.temperature;
164+
} else {
165+
// Fallback: extract from first forecast period if currentConditions is empty
166+
const firstForecast = xml.match(/<forecast>(.*?)<\/forecast>/s);
167+
if (firstForecast) {
168+
const forecastXml = firstForecast[1];
169+
const temp = this.#extract(forecastXml, /<temperature[^>]*>(.*?)<\/temperature>/);
170+
if (temp && temp !== "") {
171+
current.temperature = parseFloat(temp);
172+
this.cacheCurrentTemp = current.temperature;
173+
} else if (this.cacheCurrentTemp !== null) {
174+
current.temperature = this.cacheCurrentTemp;
175+
} else {
176+
current.temperature = null;
177+
}
178+
}
179+
}
180+
181+
// Wind chill / humidex for feels like temperature
182+
const windChill = this.#extract(xml, /<windChill[^>]*>(.*?)<\/windChill>/);
183+
const humidex = this.#extract(xml, /<humidex[^>]*>(.*?)<\/humidex>/);
184+
if (windChill) {
185+
current.feelsLikeTemp = parseFloat(windChill);
186+
} else if (humidex) {
187+
current.feelsLikeTemp = parseFloat(humidex);
188+
}
189+
190+
// Get wind and icon from currentConditions or first forecast
159191
const firstForecast = xml.match(/<forecast>(.*?)<\/forecast>/s);
160192
if (!firstForecast) {
161193
Log.warn("[envcanada] No forecast data available");
@@ -164,31 +196,34 @@ class EnvCanadaProvider {
164196

165197
const forecastXml = firstForecast[1];
166198

167-
// Temperature from first forecast (class can be "high" or "low" depending on time of day)
168-
const temp = this.#extract(forecastXml, /<temperature[^>]*>(.*?)<\/temperature>/);
169-
if (temp && temp !== "") {
170-
current.temperature = parseFloat(temp);
171-
this.cacheCurrentTemp = current.temperature;
172-
} else if (this.cacheCurrentTemp !== null) {
173-
current.temperature = this.cacheCurrentTemp;
174-
} else {
175-
current.temperature = null;
199+
// Wind speed - try currentConditions first, fallback to forecast
200+
let windSpeed = this.#extract(xml, /<currentConditions>.*?<wind>.*?<speed[^>]*>(.*?)<\/speed>/s);
201+
if (!windSpeed) {
202+
windSpeed = this.#extract(forecastXml, /<speed[^>]*>(.*?)<\/speed>/);
176203
}
177-
178-
// Wind speed
179-
const windSpeed = this.#extract(forecastXml, /<speed[^>]*>(.*?)<\/speed>/);
180204
if (windSpeed) {
181205
current.windSpeed = (windSpeed === "calm") ? 0 : convertKmhToMs(parseFloat(windSpeed));
182206
}
183207

184-
const windBearing = this.#extract(forecastXml, /<bearing[^>]*>(.*?)<\/bearing>/);
208+
// Wind bearing - try currentConditions first, fallback to forecast
209+
let windBearing = this.#extract(xml, /<currentConditions>.*?<wind>.*?<bearing[^>]*>(.*?)<\/bearing>/s);
210+
if (!windBearing) {
211+
windBearing = this.#extract(forecastXml, /<bearing[^>]*>(.*?)<\/bearing>/);
212+
}
185213
if (windBearing) current.windFromDirection = parseFloat(windBearing);
186214

187-
// Weather type from icon code
188-
const iconCode = this.#extract(forecastXml, /<iconCode[^>]*>(.*?)<\/iconCode>/);
215+
// Try icon from currentConditions first, fallback to forecast
216+
let iconCode = this.#extract(xml, /<currentConditions>.*?<iconCode[^>]*>(.*?)<\/iconCode>/s);
217+
if (!iconCode) {
218+
iconCode = this.#extract(forecastXml, /<iconCode[^>]*>(.*?)<\/iconCode>/);
219+
}
189220
if (iconCode) current.weatherType = this.#convertWeatherType(iconCode);
190221

191-
// Precipitation probability
222+
// Humidity from currentConditions
223+
const humidity = this.#extract(xml, /<currentConditions>.*?<relativeHumidity[^>]*>(.*?)<\/relativeHumidity>/s);
224+
if (humidity) current.humidity = parseFloat(humidity);
225+
226+
// Precipitation probability from forecast
192227
const pop = this.#extract(forecastXml, /<pop[^>]*>(.*?)<\/pop>/);
193228
if (pop && pop !== "") {
194229
current.precipitationProbability = parseFloat(pop);

0 commit comments

Comments
 (0)