homey-api
Version:
72 lines (55 loc) • 1.28 kB
JavaScript
const APIDefinition = require('../APIDefinition');
const Homey = require('./Homey');
const Device = require('./Device');
const APIErrorNotFound = require('../APIErrorNotFound');
/**
* A user on {@link AthomCloudAPI}.
* @class
* @hideconstructor
* @memberof AthomCloudAPI
*/
class User extends APIDefinition {
constructor({ ...props }) {
super({ ...props });
this.homeys = this.homeys.map(item => this.__makeClass(Homey, item));
this.devices = this.devices.map(item => this.__makeClass(Device, item));
}
/**
* @type {string}
*/
get id() {
return this._id;
}
/**
* @returns {AthomCloudAPI.Homey[]}
*/
getHomeys() {
return this.homeys;
}
/**
* @param {string} id - Homey ID
* @returns {AthomCloudAPI.Homey}
*/
getHomeyById(id) {
const homey = this.homeys.find(homey => homey.id === id);
if (!homey) {
throw new APIErrorNotFound(`Homey Not Found: ${id}`);
}
return homey;
}
/**
* @returns {AthomCloudAPI.Homey}
*/
getFirstHomey() {
const homey = this.homeys[0];
if (!homey) {
throw new Error('No Homey Available');
}
return homey;
}
hasRole(roleId) {
return this.roleIds.includes(roleId);
}
}
module.exports = User;
;