diff --git a/lib/OAuth2App.js b/lib/OAuth2App.js index 7693c29..314cbf4 100644 --- a/lib/OAuth2App.js +++ b/lib/OAuth2App.js @@ -169,7 +169,6 @@ class OAuth2App extends Homey.App { this[sClients][configId] = {}; } - /* * OAuth2 Config Management * @param {object} args @@ -207,7 +206,7 @@ class OAuth2App extends Homey.App { return this[sConfigs][configId]; } - /* + /** * OAuth2 Client Management * @param {object} args * @param {string} args.sessionId @@ -222,6 +221,21 @@ class OAuth2App extends Homey.App { return !!this[sClients][configId][sessionId]; } + /** + * @returns {Promise} + */ + async hasOAuth2Devices({ + sessionId = '', + configId = 'default', + } = {}) { + const devices = await this.getOAuth2Devices({ + sessionId, + configId, + }); + + return devices.length > 0; + } + /** * @param {object} args * @param {string} args.sessionId @@ -237,6 +251,15 @@ class OAuth2App extends Homey.App { } } + /** + * @returns {boolean} + */ + hasSingleOAuth2Session() { + const sessions = this.getSavedOAuth2Sessions(); + + return Object.keys(sessions).length === 1; + } + /** * @param {object} args * @param {string} args.sessionId @@ -385,12 +408,8 @@ class OAuth2App extends Homey.App { * @returns {OAuth2Client} */ getFirstSavedOAuth2Client() { + const sessionId = this.getFirstOAuth2SessionId(); const sessions = this.getSavedOAuth2Sessions(); - if (Object.keys(sessions).length < 1) { - throw new OAuth2Error('No OAuth2 Client Found'); - } - - const [sessionId] = Object.keys(sessions); const { configId } = sessions[sessionId]; return this.getOAuth2Client({ @@ -399,6 +418,20 @@ class OAuth2App extends Homey.App { }); } + /** + * @returns {string} + */ + getFirstOAuth2SessionId() { + const sessions = this.getSavedOAuth2Sessions(); + if (Object.keys(sessions).length < 1) { + throw new OAuth2Error('No OAuth2 Client Found'); + } + + const [sessionId] = Object.keys(sessions); + + return sessionId; + } + /** * @param {object} args * @param {string} args.sessionId @@ -435,12 +468,7 @@ class OAuth2App extends Homey.App { sessionId, configId = 'default', }) { - const devices = await this.getOAuth2Devices({ - sessionId, - configId, - }); - - return devices.length === 0; + return !this.hasOAuth2Devices({ configId, sessionId }); } /** @@ -450,11 +478,17 @@ class OAuth2App extends Homey.App { * @returns {Promise} */ async getOAuth2Devices({ - sessionId, + sessionId = '', configId = 'default', - }) { + } = {}) { let result = []; + if (sessionId.length < 1) { + if (!this.hasSingleOAuth2Session()) return result; + + sessionId = this.getFirstOAuth2SessionId(); + } + // Loop drivers from manifest // Homey.drivers.getDrivers() may return an incomplete array // when the Driver hasn't been initialized yet. @@ -469,7 +503,6 @@ class OAuth2App extends Homey.App { break; } catch (err) { await OAuth2Util.wait(500); - continue; } } @@ -482,8 +515,7 @@ class OAuth2App extends Homey.App { OAuth2SessionId, } = device.getStore(); - if (OAuth2SessionId === sessionId && OAuth2ConfigId === configId) return true; - return false; + return OAuth2SessionId === sessionId && OAuth2ConfigId === configId; }); result = [...devices, ...result]; }