From ee37a717f597519f0ac82a6fdc817a40acb624d9 Mon Sep 17 00:00:00 2001 From: Sebastian Haesselbarth Date: Thu, 30 Dec 2021 17:09:11 +0100 Subject: [PATCH] New AuthCode needed #6 --- README.md | 1 + nibe-fetcher.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf8fb25..6214e22 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ If you (later) get a "400 bad request" error in the log, you must get a new Auth ### 1.0.0 - 2021-12-30 * Support to manage Nibe heat pump (write support #6) + - You need to run the new version 30 minutes and then get a new AuthCode in the settings to use it! ### 0.5.3 - 2021-11-21 * Bump dependencies diff --git a/nibe-fetcher.js b/nibe-fetcher.js index b3c8583..566ac7f 100644 --- a/nibe-fetcher.js +++ b/nibe-fetcher.js @@ -17237,6 +17237,9 @@ class Fetcher extends EventEmitter { { this.adapter.log.debug("Fetch data."); try { + if (this._hasNewAuthCode()) { + this.clearSesssion(); + } if (!this._hasRefreshToken()) { if (this.options.authCode) { let token = await this.getToken(this.options.authCode); @@ -17494,6 +17497,9 @@ class Fetcher extends EventEmitter { setSesssion (auth) { this.adapter.log.debug("Set session."); + if (auth.authCode == null) { + auth.authCode = this.options.authCode; + } this._auth = auth; if (!this.options.sessionStore) return; @@ -17505,14 +17511,20 @@ class Fetcher extends EventEmitter { this.setSesssion({}); } + _hasNewAuthCode() { + let hasNewAuthCode = (this.getSession('authCode') != null) && (this.getSession('authCode') != this.options.authCode); + this.adapter.log.debug("Has new auth code: " + hasNewAuthCode); + return hasNewAuthCode; + } + _isTokenExpired () { - var expired = (this.getSession('expires_at') || 0) < (Date.now() + this.options.renewBeforeExpiry); + let expired = (this.getSession('expires_at') || 0) < (Date.now() + this.options.renewBeforeExpiry); this.adapter.log.debug("Is token expired: " + expired); return expired; } _hasRefreshToken () { - var hasToken = !!this.getSession('refresh_token'); + let hasToken = !!this.getSession('refresh_token'); this.adapter.log.debug("Has refresh token: " + hasToken); return hasToken; }