diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c583b94..9db5f266 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ _This release is scheduled to be released on 2024-10-01._ - Fixed `checks` badge in README.md - [weather] Fixed issue with the UK Met Office provider following a change in their API paths and header info. - [core] add check for node_helper loading for multiple instances of same module (#3502) +- [weather] Fixed issue for respecting unit config on broadcasted notifications ## [2.28.0] - 2024-07-01 diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 224aba06..369a7201 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -170,12 +170,19 @@ Module.register("weather", { } const notificationPayload = { - currentWeather: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, - forecastArray: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], - hourlyArray: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], + currentWeather: this.config.units === "imperial" + ? WeatherUtils.convertWeatherObjectToImperial(this.weatherProvider?.currentWeatherObject?.simpleClone()) ?? null + : this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, + forecastArray: this.config.units === "imperial" + ? this.weatherProvider?.weatherForecastArray?.map((ar) => WeatherUtils.convertWeatherObjectToImperial(ar.simpleClone())) ?? [] + : this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], + hourlyArray: this.config.units === "imperial" + ? this.weatherProvider?.weatherHourlyArray?.map((ar) => WeatherUtils.convertWeatherObjectToImperial(ar.simpleClone())) ?? [] + : this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], locationName: this.weatherProvider?.fetchedLocationName, providerName: this.weatherProvider.providerName }; + this.sendNotification("WEATHER_UPDATED", notificationPayload); }, diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index e75baf20..4d6ce45b 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -30,8 +30,7 @@ const WeatherUtils = { let convertedValue = value; let conversionUnit = valueUnit; if (outputUnit === "imperial") { - if (valueUnit && valueUnit.toLowerCase() === "cm") convertedValue = convertedValue * 0.3937007874; - else convertedValue = convertedValue * 0.03937007874; + convertedValue = this.convertPrecipitationToInch(value, valueUnit); conversionUnit = "in"; } else { conversionUnit = valueUnit ? valueUnit : "mm"; @@ -40,6 +39,17 @@ const WeatherUtils = { return `${convertedValue.toFixed(2)} ${conversionUnit}`; }, + /** + * Convert precipitation value into inch + * @param {number} value the precipitation value for convert + * @param {string} valueUnit can be 'mm' or 'cm' + * @returns {number} the converted precipitation value + */ + convertPrecipitationToInch (value, valueUnit) { + if (valueUnit && valueUnit.toLowerCase() === "cm") return value * 0.3937007874; + else return value * 0.03937007874; + }, + /** * Convert temp (from degrees C) into imperial or metric unit depending on * your config @@ -129,6 +139,28 @@ const WeatherUtils = { } return ((feelsLike - 32) * 5) / 9; + }, + + /** + * Converts the Weather Object's values into imperial unit + * @param {object} weatherObject the weather object + * @returns {object} the weather object with converted values to imperial + */ + convertWeatherObjectToImperial (weatherObject) { + if (!weatherObject || Object.keys(weatherObject).length === 0) return null; + + let imperialWeatherObject = { ...weatherObject }; + + if (imperialWeatherObject) { + if (imperialWeatherObject.feelsLikeTemp) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); + if (imperialWeatherObject.maxTemperature) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); + if (imperialWeatherObject.minTemperature) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); + if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); + if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); + if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); + } + + return imperialWeatherObject; } };