UNPKG

node-hue-api

Version:
109 lines (108 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Lights = void 0; const ApiDefinition_1 = require("./http/ApiDefinition"); const ApiError_1 = require("../ApiError"); const lights_1 = require("./http/endpoints/lights"); const LightIdPlaceholder_1 = require("./placeholders/LightIdPlaceholder"); const HueRateLimiter_1 = require("./HueRateLimiter"); const LIGHT_ID_PARSER = new LightIdPlaceholder_1.LightIdPlaceholder(); class Lights extends ApiDefinition_1.ApiDefinition { constructor(hueApi) { super(hueApi); this._lightStateLimiter = new HueRateLimiter_1.HueRateLimiter(hueApi.name, 'lights', hueApi.rateLimitConfig.lightRateLimit); } getAll() { return this.execute(lights_1.lightsApi.getAllLights); } getLight(id) { const lightId = getLightId(id); return this.getAll() .then(lights => { const found = lights.filter((light) => light.id === lightId); if (found.length === 0) { throw new ApiError_1.ApiError(`Light ${lightId} not found`); } return found[0]; }); } //TODO // /** // * @deprecated since 4.0. Use getLight(id) instead. // * @param id {number} The ide of the light to get. // * @returns {Promise<Light>} // */ // getLightById(id: LightId) { // util.deprecatedFunction('5.x', 'lights.getLightById(id)', 'Use lights.getLight(id) instead.'); // return this.getLight(id); // } getLightByName(name) { return this.getAll().then(lights => { return lights.filter(light => light.name === name); }); } /** Discovers the "new" lights detected by the Bridge. */ getNew() { return this.execute(lights_1.lightsApi.getNewLights); } /** Starts a search for "new"/undiscovered Lights by the bridge. This can take up to 30 seconds to complete. */ searchForNew() { return this.execute(lights_1.lightsApi.searchForNewLights); } getLightAttributesAndState(id) { return this.execute(lights_1.lightsApi.getLightAttributesAndState, { id: id }); } getLightState(id) { return this.getLightAttributesAndState(id).then(result => { // @ts-ignore return result.state; }); } /** * Sets the current state for the Light to desired settings. */ setLightState(id, state) { const lightId = getLightId(id); return this.hueApi.getLightDefinition(lightId) .then(device => { if (!device) { throw new ApiError_1.ApiError(`Light with id:${lightId} was not found on this bridge`); } return this._setLightState(id, state, device); }); } /** * Renames a Light on the Bridge to the specified name in the Light instance. */ renameLight(light) { return this.execute(lights_1.lightsApi.setLightAttributes, { id: light, light: light }); } // /** // * @deprecated since 4.x, use renameLight(light) instead // * @param id {int} The Light to rename. // * @param name {string} The new name. // * @returns {Promise} // */ // rename(id, name) { // if (arguments.length === 1) { // util.deprecatedFunction('5.x', 'lights.rename(id, name)', 'Use lights.renameLight(light) instead.'); // return this.renameLight(id); // } else { // util.deprecatedFunction('5.x', 'lights.rename(id, name)', 'Use lights.renameLight(light) instead.'); // return this.execute(lightsApi.setLightAttributes, {id: id, name: name}); // } // } deleteLight(id) { return this.execute(lights_1.lightsApi.deleteLight, { id: id }); } _setLightState(id, state, device) { const self = this; return this._lightStateLimiter.schedule(() => { return self.execute(lights_1.lightsApi.setLightState, { id: id, state: state, device: device }); }); } } exports.Lights = Lights; function getLightId(id) { return LIGHT_ID_PARSER.getValue({ id: id }); }