Skip to content

Commit

Permalink
Bug in Weather Units for Broadcasted Notification (#3519)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
skpanagiotis and rejas authored Aug 18, 2024
1 parent cc1d4ab commit 5673678
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions modules/default/weather/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand Down
36 changes: 34 additions & 2 deletions modules/default/weather/weatherutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
};

Expand Down

0 comments on commit 5673678

Please sign in to comment.