From df0f048ecc018d55119108d7bb7682f3ea3f80b0 Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 13:55:39 +0200 Subject: [PATCH] Added outgoing notification --- CHANGELOG.md | 2 ++ modules/default/weather/weather.js | 9 +++++++++ modules/default/weather/weatherobject.js | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f7e61de..5f306611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ _This release is scheduled to be released on 2022-07-01._ ### Added +- Added the notification emitting from the weather module on infromation updated. + ### Updated ### Fixed diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 350c4f2d..d86384f8 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -154,6 +154,15 @@ Module.register("weather", { if (this.weatherProvider.currentWeather()) { this.sendNotification("CURRENTWEATHER_TYPE", { type: this.weatherProvider.currentWeather().weatherType.replace("-", "_") }); } + + let notificationPayload = { + current: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, + forecast: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], + hourly: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], + location: this.weatherProvider?.fetchedLocationName, + provider: this.weatherProvider.providerName + }; + this.sendNotification("WEATHER_UPDATED", notificationPayload); }, scheduleUpdate: function (delay = null) { diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index e9359345..9b5a21a9 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -146,6 +146,20 @@ class WeatherObject { this.sunrise = moment(times.sunrise, "X"); this.sunset = moment(times.sunset, "X"); } + + /** + * Clone to simple object to prevent mutating and deprecated legacy library. + * + * @return {object} simple object cloned. + */ + simpleClone() { + const toFlat = ["date", "sunrise", "sunset"]; + let clone = { ...this }; + for (const prop of toFlat) { + clone[prop] = clone?.[prop]?.valueOf() ?? clone?.[prop]; + } + return clone; + } } /*************** DO NOT EDIT THE LINE BELOW ***************/