Bug in Weather Units for Broadcasted Notification (#3519)

This PR resolve Issue number #3419 .

I have added the method `convertWeatherObjectToImperial()` which
converts the units of the `notificationPayload` to imperial if needed,
in order to pass the object in `sendNotification()`.

---------

Co-authored-by: veeck <michael.veeck@nebenan.de>
This commit is contained in:
Panagiotis Skias 2024-08-18 11:04:51 +03:00 committed by GitHub
parent cc1d4ab240
commit 56736786fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 5 deletions

View File

@ -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

View File

@ -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);
},

View File

@ -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;
}
};