Skip to content

Commit

Permalink
Add reloading on data timeout & better error messaging (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael5r authored Mar 25, 2022
1 parent 5a8f114 commit 65e7d2f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
11 changes: 8 additions & 3 deletions mmm-nest-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,18 @@ Module.register('mmm-nest-status', {
} else if (notification === 'MMM_NEST_STATUS_DATA_ERROR') {
self.errMsg = 'Nest API Error: ' + payload;
self.updateDom(self.config.animationSpeed);
} else if (notification === 'MMM_NEST_STATUS_DATA_TIMEOUT_ERROR') {
self.scheduleUpdate(5 * 60 * 1000); // try loading the data again after 5 minutes
if (!self.loaded) {
// if no data has been loaded, we'll show the error message
self.errMsg = 'Nest API Timeout Error: ' + payload + '<br>This module will try to load data again in 5 minutes.';
self.updateDom(self.config.animationSpeed);
}
} else if (notification === 'MMM_NEST_STATUS_DATA_BLOCKED') {
// this is a specific error that occurs when the API rate limit has been exceeded.
// https://developers.google.com/nest/device-access/reference/errors/api
// we'll try again after 10 minutes
setTimeout(function() {
self.scheduleUpdate(self.config.updateInterval);
}, 10 * 60 * 1000);
self.scheduleUpdate(10 * 60 * 1000);
self.errMsg = 'The Device Access API rate limit has been exceeded.<br>This module will try to load data again in 10 minutes.';
self.updateDom(self.config.animationSpeed);
}
Expand Down
11 changes: 8 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = NodeHelper.create({
console.log('Starting node_helper for module [' + this.name + ']');
},

cleanErrorMessage: function(err) {
return err && err.message ? err.message : JSON.stringify(err);
},

getDeviceData: function(dataOptions, tokenData) {

var self = this;
Expand All @@ -18,9 +22,9 @@ module.exports = NodeHelper.create({
// access token has expired
self.sendSocketNotification('MMM_NEST_STATUS_ACCESS_EXPIRED', tokenData);
} else if (res.status === 429) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_BLOCKED', err);
self.sendSocketNotification('MMM_NEST_STATUS_DATA_BLOCKED', self.cleanErrorMessage(err));
} else if (res.status !== 200) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', err);
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', self.cleanErrorMessage(err));
} else {
if (res.data === {}) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', 'Token works, but no data was received.<br>Make sure you are using the master account for your Nest.');
Expand All @@ -33,7 +37,8 @@ module.exports = NodeHelper.create({

})
.catch(function (err) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', err);
// if you're here, this is most likely due to a timeout connecting to Google
self.sendSocketNotification('MMM_NEST_STATUS_DATA_TIMEOUT_ERROR', self.cleanErrorMessage(err));
});

},
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmm-nest-status",
"version": "2.0.1",
"version": "2.0.2",
"description": "Display Nest Thermostat data on your MagicMirror. Supports multiple modes, sizes & views to get you exactly the look you want.",
"repository": {
"type": "git",
Expand Down

0 comments on commit 65e7d2f

Please sign in to comment.