UNPKG

node-hue-api

Version:
94 lines (93 loc) 3.19 kB
import { ApiDefinition } from './http/ApiDefinition'; import { configurationApi } from './http/endpoints/configuration'; export class Users extends ApiDefinition { constructor(hueApi) { super(hueApi); } getAll() { return getAllUsersAsArray(this); } getUser(username) { return this.getAll() .then(users => { if (users.length > 0) { const filtered = users.filter(user => { return user.username === username; }); if (filtered) { return filtered[0]; } } return undefined; }); } // /** // * @deprecated Use getUserByName(username) instead // * @param username {string} // */ // get(username) { // util.deprecatedFunction('5.x', 'users.get(username)', 'Use users.getUser(username) instead.'); // return this.getUser(username); // } getUserByName(appName, deviceName) { let nameToMatch; if (deviceName) { nameToMatch = `${appName}#${deviceName}`; } else { nameToMatch = appName; } return getAllUsersAsArray(this) .then(users => { return users.filter(user => user.name === nameToMatch); }); } // /** // * @deprecated use getUserByName(appName, deviceName) instead. // * @param appName {string} // * @param deviceName {string} // * @returns {Promise<Object[]>} // */ // getByName(appName, deviceName) { // util.deprecatedFunction('5.x', 'users.getByName(appName, deviceName)', 'Use users.getUserByName(appName, deviceName) instead.'); // return this.getUserByName(appName, deviceName); // } createUser(appName, deviceName) { return this.hueApi.getCachedState() .then((state) => { //TODO may need to combine the modelid and API version, but am assuming that all newer bridges are kept up to // date, as do not know the specific version number of the introduction of the generateclientkey parameter. // Default to always reporting an old bridge if we cannot get the state let oldBridge = true; if (state) { oldBridge = state.modelid === 'BSB001'; } return this.execute(configurationApi.createUser, { appName: appName, deviceName: deviceName, generateKey: !oldBridge }); }); } deleteUser(username) { return this.execute(configurationApi.deleteUser, { element: username }); } } function getAllUsers(api) { return api.execute(configurationApi.getConfiguration) .then((config) => { return config.whitelist || undefined; }); } function getAllUsersAsArray(api) { return getAllUsers(api) .then((users) => { const results = []; if (users) { Object.keys(users).forEach(username => { results.push(Object.assign({ username: username }, users[username])); }); } return results; }); }