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

Improved usability with single OAuth2 session #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions lib/OAuth2App.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ class OAuth2App extends Homey.App {
this[sClients][configId] = {};
}


/*
* OAuth2 Config Management
* @param {object} args
Expand Down Expand Up @@ -207,7 +206,7 @@ class OAuth2App extends Homey.App {
return this[sConfigs][configId];
}

/*
/**
* OAuth2 Client Management
* @param {object} args
* @param {string} args.sessionId
Expand All @@ -222,6 +221,21 @@ class OAuth2App extends Homey.App {
return !!this[sClients][configId][sessionId];
}

/**
* @returns {Promise<boolean>}
*/
async hasOAuth2Devices({
sessionId = '',
configId = 'default',
} = {}) {
const devices = await this.getOAuth2Devices({
sessionId,
configId,
});

return devices.length > 0;
}

/**
* @param {object} args
* @param {string} args.sessionId
Expand All @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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 });
}

/**
Expand All @@ -450,11 +478,17 @@ class OAuth2App extends Homey.App {
* @returns {Promise<array>}
*/
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.
Expand All @@ -469,7 +503,6 @@ class OAuth2App extends Homey.App {
break;
} catch (err) {
await OAuth2Util.wait(500);
continue;
}
}

Expand All @@ -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];
}
Expand Down