Skip to content

Commit

Permalink
try to refresh token earlier than necessary + retry on refresh-fail +…
Browse files Browse the repository at this point in the history
… updated config.schema.json
  • Loading branch information
skrollme committed Dec 17, 2022
1 parent 118a55d commit c907dd7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
39 changes: 36 additions & 3 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"type": "boolean",
"default": true,
"description": "Outputs log messages with loglevel set to info"
},
},
"auth": {
"title": "Netatmo credentials",
"type": "object",
Expand All @@ -72,11 +72,44 @@
"required": true,
"description": "Create this at https://dev.netatmo.com/"
},
"grant_type": {
"title": "grant_type",
"type": "string",
"required": true,
"default": "refresh_token",
"oneOf": [
{
"title": "refresh_token",
"enum": [
"refresh_token"
]
},
{
"title": "password",
"enum": [
"password"
]
}
],
"description": "use either 'password' or 'refresh_token'. Please see https://github.com/skrollme/homebridge-eveatmo/blob/master/README.md for more information"
},
"refresh_token": {
"title": "Refresh Token",
"type": "string",
"required": true,
"description": "A valid Netatmo refreshToken, see https://dev.netatmo.com/apidocumentation/oauth for more information"
"required": false,
"description": "Necessary, if you use grant_type='refresh_token'. A valid Netatmo refreshToken. You can generate this on the page where you got the 'client_id' and 'client_secret' from"
},
"username": {
"title": "Username",
"type": "string",
"required": false,
"description": "Necessary, if you use grant_type='password'. The email-address of your Netatmo account. Must be the same account as the developer app which the 'client_id' and 'client_secret' belong to"
},
"password": {
"title": "Password",
"type": "string",
"required": false,
"description": "Necessary, if you use grant_type='password'. The password of your Netatmo account. Must be the same account as the developer app which the 'client_id' and 'client_secret' belong to"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/netatmo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ netatmo.prototype.authenticate_refresh = function () {
form: form,
}, function (err, response, body) {
if (err || response.statusCode != 200) {
return this.handleRequestError(err, response, body, "Authenticate refresh error");
this.handleRequestError(err, response, body, "Authenticate refresh error");
setTimeout(this.authenticate_refresh.bind(this), 180 * 1000); // retry in 3min
return this
}

body = JSON.parse(body);
Expand All @@ -195,7 +197,7 @@ netatmo.prototype.authenticate_refresh = function () {
}));

if (body.expires_in) {
setTimeout(this.authenticate_refresh.bind(this), body.expires_in * 1000);
setTimeout(this.authenticate_refresh.bind(this), (body.expires_in - 300) * 1000); // try refreshing the tokens 5min early
}

return this;
Expand Down

0 comments on commit c907dd7

Please sign in to comment.