Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Override Notification Option to Weather Module #3178

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _This release is scheduled to be released on 2023-10-01._

- Added UV Index support to OpenWeatherMap
- Added 'hideDuplicates' flag to the calendar module
- Added `allowOverrideNotification` to weather module to enable sending current weather objects with the `CURRENT_WEATHER_OVERRIDE` notification to supplement/replace the current weather displayed

### Removed

Expand Down
76 changes: 76 additions & 0 deletions modules/default/weather/providers/localWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* global Class */

/* Wrapper to enable the injection of local sensor data */
const LocalWrapper = Class.extend({
rejas marked this conversation as resolved.
Show resolved Hide resolved
baseProvider: null,
providerName: "localWrapper",
overrideWeatherObject: null,
currentWeatherObject: null,

init(baseProvider) {
this.baseProvider = baseProvider;

// baseProvider does not inherit LocalWrapper - need to be explicit to replace
// methods inherited from Weatherprovider
this.baseProvider.setCurrentWeather = this.setCurrentWeather.bind(this);
this.baseProvider.currentWeather = this.currentWeather.bind(this);
},

setConfig(config) {
this.baseProvider.setConfig(config);
},
start() {
this.baseProvider.start();
},
fetchCurrentWeather() {
this.baseProvider.fetchCurrentWeather();
},
fetchWeatherForecast() {
this.baseProvider.fetchWeatherForecast();
},
fetchWeatherHourly() {
this.baseProvider.fetchEatherHourly();
},

weatherForecast() {
this.baseProvider.weatherForecast();
},
weatherHourly() {
this.baseProvider.weatherHourly();
},
fetchedLocation() {
this.baseProvider.fetchedLocation();
},
setWeatherForecast(weatherForecastArray) {
this.baseProvider.setWeatherForecast(weatherForecastArray);
},
setWeatherHourly(weatherHourlyArray) {
this.baseProvider.setWeatherHourly(weatherHourlyArray);
},
setFetchedLocation(name) {
this.baseProvider.setFetchedLocation(name);
},
updateAvailable() {
this.baseProvider.updateAvailable();
},
async fetchData(url, type = "json", requestHeaders = undefined, expectedResponseHeaders = undefined) {
this.baseProvider.fetchData(url, type, requestHeaders, expectedResponseHeaders);
},

// "Override" to fetch the currentWeatherObject from this class, not the provider
currentWeather() {
return this.currentWeatherObject;
},

// "Override" to set the currentWeatherObject ot this class, not the provider
setCurrentWeather(currentWeatherObject) {
// Merge in any sensor data we got
this.currentWeatherObject = Object.assign(currentWeatherObject, this.overrideWeatherObject);
},

notificationReceived(payload) {
this.overrideWeatherObject = payload;
this.setCurrentWeather(this.currentWeatherObject);
this.updateAvailable();
}
});
5 changes: 4 additions & 1 deletion modules/default/weather/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Module.register("weather", {
showHumidity: false,
showIndoorHumidity: false,
showIndoorTemperature: false,
allowOverrideNotification: false,
showPeriod: true,
showPeriodUpper: false,
showPrecipitationAmount: false,
Expand Down Expand Up @@ -61,7 +62,7 @@ Module.register("weather", {

// Return the scripts that are necessary for the weather module.
getScripts: function () {
return ["moment.js", this.file("../utils.js"), "weatherutils.js", "weatherprovider.js", "weatherobject.js", "suncalc.js", this.file(`providers/${this.config.weatherProvider.toLowerCase()}.js`)];
return ["moment.js", this.file("../utils.js"), "weatherutils.js", "weatherobject.js", this.file("providers/localWrapper.js"), "weatherprovider.js", "suncalc.js", this.file(`providers/${this.config.weatherProvider.toLowerCase()}.js`)];
},

// Override getHeader method.
Expand Down Expand Up @@ -119,6 +120,8 @@ Module.register("weather", {
} else if (notification === "INDOOR_HUMIDITY") {
this.indoorHumidity = this.roundValue(payload);
this.updateDom(300);
} else if (notification === "CURRENT_WEATHER_OVERRIDE" && this.config.allowOverrideNotification) {
this.weatherProvider.notificationReceived(payload);
}
},

Expand Down
6 changes: 5 additions & 1 deletion modules/default/weather/weatherprovider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Class, performWebRequest */
/* global Class, performWebRequest, LocalWrapper */

/* MagicMirror²
* Module: Weather
Expand Down Expand Up @@ -164,5 +164,9 @@ WeatherProvider.initialize = function (providerIdentifier, delegate) {
provider.providerName = pi;
}

if (config.allowOverrideNotification) {
return new LocalWrapper(provider);
}

return provider;
};