UNPKG

node-hue-api

Version:
171 lines (170 loc) 6.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Groups = void 0; const ApiDefinition_1 = require("./http/ApiDefinition"); const groups_1 = require("./http/endpoints/groups"); const HueRateLimiter_1 = require("./HueRateLimiter"); class Groups extends ApiDefinition_1.ApiDefinition { constructor(hueApi) { super(hueApi); this._groupStateLimiter = new HueRateLimiter_1.HueRateLimiter(hueApi.name, 'groups', hueApi.rateLimitConfig.groupRateLimit); } getAll() { // Lightset 0 (all lights) is a special case, so retrieve the bridge's definition of that and prepend to the // existing group definitions to provide the complete list of groups. return Promise.all([ this.execute(groups_1.groupsApi.getGroupAttributes, { id: 0 }), this.execute(groups_1.groupsApi.getAllGroups) ]).then((results) => { // @ts-ignore results[1].unshift(results[0]); return results[1]; }); } getGroup(id) { return this.execute(groups_1.groupsApi.getGroupAttributes, { id: id }); } //TODO // get(id: number | Group): AnyGroup { // util.deprecatedFunction('5.x', 'groups.get(id)', 'Use groups.getGroup(id) instead.'); // return this.getGroup(id); // } // // /** // * @deprecated Use getGroupByName(name) instead. // * @param name {string} // * @returns {Promise<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>} // */ // getByName(name) { // util.deprecatedFunction('5.x', 'groups.getByName(name)', 'Use groups.getGroupByName(name) instead.'); // return this.getGroupByName(name); // } /** * @param name {string} * @returns {Promise<Array<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>>} */ getGroupByName(name) { return this.getAll() .then(allGroups => { return allGroups.filter(group => group.name === name); }); } createGroup(group) { const self = this; return this.execute(groups_1.groupsApi.createGroup, { group: group }) .then(result => { return self.getGroup(result.id); }); // if (arguments.length === 1 && group instanceof Group) { // return this.execute(groupsApi.createGroup, {group: group}) // .then(result => { // return self.getGroup(result.id); // }); // } // // util.deprecatedFunction('5.x', 'groups.createGroup(name, lights)', 'Use groups.createGroup(group) instead.'); // const newGroup = new LightGroup(); // newGroup.name = arguments[0]; // newGroup.lights = arguments[1]; // return self.createGroup(newGroup); } // /** // * @deprecated use createGroup(group) instead // */ // createRoom(name, lights, roomClass) { // util.deprecatedFunction('5.x', 'groups.createRoom(name, lights, roomClass)', 'Use groups.createGroup(group) instead.'); // // const group = new Room(); // group.name = name; // group.lights = lights; // group.class = roomClass; // return this.createGroup(group); // } // /** // * @deprecated use createGroup(group) instead // */ // createZone(name, lights, roomClass) { // util.deprecatedFunction('5.x', 'groups.createZone(name, lights, roomClass)', 'Use groups.createGroup(group) instead.'); // // const group = new Zone(); // group.name = name; // group.lights = lights; // group.class = roomClass; // // return this.createGroup(group); // } /** * Update the Group attributes on the bridge for the specified Group object. * @param group The group with updates to be updated on the bridge. * @returns {Promise<boolean>} */ updateGroupAttributes(group) { return this.execute(groups_1.groupsApi.setGroupAttributes, { id: group.id, group: group }); } // /** // * Update the Group attributes on the bridge for the specified Group object. // * @param group {Group} The group with updates to be updated on the bridge. // * @returns {Promise<boolean>} // */ // updateAttributes(id, data) { // util.deprecatedFunction('5.x', 'groups.updateAttributes(id, data)', 'Use groups.updateGroupAttributes(group) instead.'); // return this.execute(groupsApi.setGroupAttributes, {id: id, group: data}); // } deleteGroup(id) { return this.execute(groups_1.groupsApi.deleteGroup, { id: id }); } getGroupState(id) { return this.getGroup(id).then((group) => { return group.state; }); } setGroupState(id, state) { const self = this; return self._groupStateLimiter.schedule(() => { return self.execute(groups_1.groupsApi.setGroupState, { id: id, state: state }); }); } getLightGroups() { // @ts-ignore return this._getByType('LightGroup'); } getLuminaries() { // @ts-ignore return this._getByType('Luminaire'); } getLightSources() { // @ts-ignore return this._getByType('Lightsource'); } getRooms() { // @ts-ignore return this._getByType('Room'); } getZones() { // @ts-ignore return this._getByType('Zone'); } getEntertainment() { // @ts-ignore return this._getByType('Entertainment'); } /** Enables the streaming functionality on an Entertainment Group */ enableStreaming(id) { return this.execute(groups_1.groupsApi.setStreaming, { id: id, active: true }); } /** * Disabled the streaming functionality on an Entertainment Group * @param id {int | Entertainment} * @returns {Promise<boolean>} */ disableStreaming(id) { return this.execute(groups_1.groupsApi.setStreaming, { id: id, active: false }); } _getByType(type) { return this.getAll() .then((groups) => { return groups.filter(group => group.type === type); }); } } exports.Groups = Groups;