From 9c859ea669f850c8fab4fbcbf1b8708a9ef4cb1e Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Sun, 28 Jul 2024 01:30:06 +0300 Subject: [PATCH 01/12] Add convertWeatherObjectToImperial, convert weatherObject units to imperial --- modules/default/weather/weatherutils.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index e75baf20f7..7920bfbfd3 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -129,6 +129,24 @@ const WeatherUtils = { } return ((feelsLike - 32) * 5) / 9; + }, + + 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.convertPrecipitationUnit(imperialWeatherObject.precipitationAmount, 'imperial'); + if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, 'imperial'); + if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, 'imperial'); + // TODO add if section and method for precipitationAmount convert + } + + return imperialWeatherObject; } }; From df81c34f7269f3bfc3aa63c13ec16302f66c9889 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Sun, 28 Jul 2024 01:30:57 +0300 Subject: [PATCH 02/12] Update notificationPayload object, use convertWeatherObjectToImperial method --- modules/default/weather/weather.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 224aba0647..679a02f12a 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -170,12 +170,22 @@ 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 }; + + + console.log(notificationPayload); + this.sendNotification("WEATHER_UPDATED", notificationPayload); }, From 2623a05c34a1f1fc7a15b07d8f0275ab193c6e2a Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Sun, 28 Jul 2024 22:07:43 +0300 Subject: [PATCH 03/12] Add convertPrecipitationToInch method, update convertWeatherObjectToImperial method and add comments --- modules/default/weather/weatherutils.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 7920bfbfd3..31c37e6eb3 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -131,6 +131,21 @@ const WeatherUtils = { return ((feelsLike - 32) * 5) / 9; }, + /** + * 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; }, + + /** + * Converts the Weather Object's values into imperial unit + * @param {WeatherObject} weatherObject the weather object + * @returns {WeatherObject} the weather object with converted values to imperial + */ convertWeatherObjectToImperial(weatherObject) { if (!weatherObject || Object.keys(weatherObject).length === 0) return null; @@ -140,10 +155,9 @@ const WeatherUtils = { 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.convertPrecipitationUnit(imperialWeatherObject.precipitationAmount, 'imperial'); + if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPerticipationToInch(imperialWeatherObject.precipitationAmount, 'imperial', imperialWeatherObject.precipitationUnits); if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, 'imperial'); if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, 'imperial'); - // TODO add if section and method for precipitationAmount convert } return imperialWeatherObject; From acc8e969ecac9a710540a343ab0d04d748cda255 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Sun, 4 Aug 2024 22:42:18 +0300 Subject: [PATCH 04/12] Remove console log --- modules/default/weather/weather.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 679a02f12a..cac428700f 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -183,9 +183,6 @@ Module.register("weather", { providerName: this.weatherProvider.providerName }; - - console.log(notificationPayload); - this.sendNotification("WEATHER_UPDATED", notificationPayload); }, From 884125bb60768559c999231ffee9ff3dece27f86 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Sun, 4 Aug 2024 23:21:09 +0300 Subject: [PATCH 05/12] Update changelog about the fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0569cd369..70974b5af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,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. +- [weather] Fixed issue for respecting unit config on broadcasted notifications ## [2.28.0] - 2024-07-01 From 601c3c7846a699a835aa9240dc629135384408b8 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Mon, 5 Aug 2024 21:22:45 +0300 Subject: [PATCH 06/12] Fixes for eslint in weather.js and weatherutils.js --- modules/default/weather/weather.js | 8 +++--- modules/default/weather/weatherutils.js | 34 ++++++++++++++----------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index cac428700f..369a720101 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -170,13 +170,13 @@ Module.register("weather", { } const notificationPayload = { - currentWeather: this.config.units === 'imperial' - ? WeatherUtils.convertWeatherObjectToImperial(this.weatherProvider?.currentWeatherObject?.simpleClone()) ?? null + currentWeather: this.config.units === "imperial" + ? WeatherUtils.convertWeatherObjectToImperial(this.weatherProvider?.currentWeatherObject?.simpleClone()) ?? null : this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, - forecastArray: this.config.units === 'imperial' + 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' + 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, diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 31c37e6eb3..e3f06d88c1 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -1,3 +1,6 @@ +const WeatherObject = require("./weatherobject"); + + const WeatherUtils = { /** @@ -44,7 +47,7 @@ const WeatherUtils = { * Convert temp (from degrees C) into imperial or metric unit depending on * your config * @param {number} tempInC the temperature in celsius you want to convert - * @param {string} unit can be 'imperial' or 'metric' + * @param {string} unit can be "imperial" or 'metric' * @returns {number} the converted temperature */ convertTemp (tempInC, unit) { @@ -54,7 +57,7 @@ const WeatherUtils = { /** * Convert wind speed into another unit. * @param {number} windInMS the windspeed in meter/sec you want to convert - * @param {string} unit can be 'beaufort', 'kmh', 'knots, 'imperial' (mph) + * @param {string} unit can be 'beaufort', 'kmh', 'knots, "imperial" (mph) * or 'metric' (mps) * @returns {number} the converted windspeed */ @@ -137,27 +140,28 @@ const WeatherUtils = { * @param {string} valueUnit can be 'mm' or 'cm' * @returns {number} the converted precipitation value */ - convertPrecipitationToInch(value, valueUnit) { + convertPrecipitationToInch (value, valueUnit) { if (valueUnit && valueUnit.toLowerCase() === "cm") return value * 0.3937007874; - else return value * 0.03937007874; }, + else return value * 0.03937007874; + }, - /** + /** * Converts the Weather Object's values into imperial unit * @param {WeatherObject} weatherObject the weather object * @returns {WeatherObject} the weather object with converted values to imperial - */ - convertWeatherObjectToImperial(weatherObject) { + */ + convertWeatherObjectToImperial (weatherObject) { if (!weatherObject || Object.keys(weatherObject).length === 0) return null; - - let imperialWeatherObject = {...weatherObject}; + + 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.convertPerticipationToInch(imperialWeatherObject.precipitationAmount, 'imperial', imperialWeatherObject.precipitationUnits); - if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, 'imperial'); - if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, 'imperial'); + 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.convertPerticipationToInch(imperialWeatherObject.precipitationAmount, "imperial", imperialWeatherObject.precipitationUnits); + if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); + if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); } return imperialWeatherObject; From f4a942a6800d7c305a78ae0fb0bb83ba77d54843 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Mon, 5 Aug 2024 22:40:55 +0300 Subject: [PATCH 07/12] Update the comment for convertWeatherObjectToImperial() method --- modules/default/weather/weatherutils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index e3f06d88c1..1f8f4f2e0d 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -1,6 +1,3 @@ -const WeatherObject = require("./weatherobject"); - - const WeatherUtils = { /** @@ -147,8 +144,8 @@ const WeatherUtils = { /** * Converts the Weather Object's values into imperial unit - * @param {WeatherObject} weatherObject the weather object - * @returns {WeatherObject} the weather object with converted values to imperial + * @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; From a1ababfaf9fbca64378eedbb87a705f46e57b815 Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Mon, 5 Aug 2024 23:26:44 +0300 Subject: [PATCH 08/12] Minor typo fix in weatherutils.js --- modules/default/weather/weatherutils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 1f8f4f2e0d..9ce5552b2c 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -148,6 +148,7 @@ const WeatherUtils = { * @returns {object} the weather object with converted values to imperial */ convertWeatherObjectToImperial (weatherObject) { + console.log("edo"); if (!weatherObject || Object.keys(weatherObject).length === 0) return null; let imperialWeatherObject = { ...weatherObject }; @@ -156,7 +157,7 @@ const WeatherUtils = { 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.convertPerticipationToInch(imperialWeatherObject.precipitationAmount, "imperial", imperialWeatherObject.precipitationUnits); + if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, "imperial", imperialWeatherObject.precipitationUnits); if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); } From 8655d941b013272d039bdc43788a06fa5142765d Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Tue, 6 Aug 2024 00:35:20 +0300 Subject: [PATCH 09/12] Revert changes from comments --- modules/default/weather/weatherutils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 9ce5552b2c..97f999d8e5 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -44,7 +44,7 @@ const WeatherUtils = { * Convert temp (from degrees C) into imperial or metric unit depending on * your config * @param {number} tempInC the temperature in celsius you want to convert - * @param {string} unit can be "imperial" or 'metric' + * @param {string} unit can be 'imperial' or 'metric' * @returns {number} the converted temperature */ convertTemp (tempInC, unit) { @@ -54,7 +54,7 @@ const WeatherUtils = { /** * Convert wind speed into another unit. * @param {number} windInMS the windspeed in meter/sec you want to convert - * @param {string} unit can be 'beaufort', 'kmh', 'knots, "imperial" (mph) + * @param {string} unit can be 'beaufort', 'kmh', 'knots, 'imperial' (mph) * or 'metric' (mps) * @returns {number} the converted windspeed */ From 665254958b09a67964a237b0dc46716049a2f02c Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Tue, 6 Aug 2024 00:54:56 +0300 Subject: [PATCH 10/12] Remove unnecessary console.log --- modules/default/weather/weatherutils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 97f999d8e5..875208d3c4 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -148,7 +148,6 @@ const WeatherUtils = { * @returns {object} the weather object with converted values to imperial */ convertWeatherObjectToImperial (weatherObject) { - console.log("edo"); if (!weatherObject || Object.keys(weatherObject).length === 0) return null; let imperialWeatherObject = { ...weatherObject }; From d6b4643166b28a6ee9ee57b5b4b2586e9d3df55f Mon Sep 17 00:00:00 2001 From: skpanagiotis Date: Tue, 6 Aug 2024 00:57:12 +0300 Subject: [PATCH 11/12] Minor fix when calling convertPrecipitationToInch method --- modules/default/weather/weatherutils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 875208d3c4..8cb36856d6 100644 --- a/modules/default/weather/weatherutils.js +++ b/modules/default/weather/weatherutils.js @@ -156,7 +156,7 @@ const WeatherUtils = { 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, "imperial", imperialWeatherObject.precipitationUnits); + 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"); } From e760197a25a71eb781207149181bb7e17a30857c Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 18 Aug 2024 09:59:33 +0200 Subject: [PATCH 12/12] reuse convertPrecipitationToInch in convertPrecipitationUnit method --- modules/default/weather/weatherutils.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/modules/default/weather/weatherutils.js b/modules/default/weather/weatherutils.js index 8cb36856d6..4d6ce45bf9 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 @@ -131,17 +141,6 @@ const WeatherUtils = { return ((feelsLike - 32) * 5) / 9; }, - /** - * 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; - }, - /** * Converts the Weather Object's values into imperial unit * @param {object} weatherObject the weather object