philips-hue-light-api
Version:
An API client for the Philips Hue API.
161 lines • 6.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const hue_fetch_client_1 = require("./utils/hue-fetch-client");
const ColorConverter_1 = require("./utils/ColorConverter");
class Lightgroup {
constructor(ip, apiKey, id, name) {
this._apiKey = apiKey;
this._id = id;
this._name = name;
this._fetchClient = new hue_fetch_client_1.HueFetchClient(ip);
}
get id() {
return this._id;
}
get name() {
return this._name;
}
async on(immediate = false) {
const route = `/groups/${this._id}/action`;
const path = `/${this._apiKey}${route}`;
const order = `${route}/on`;
const body = JSON.stringify({
on: true,
transitiontime: immediate ? 0 : undefined,
});
const options = {
body: body,
};
const response = await this._fetchClient.put(path, options);
if (response.error) {
throw new Error(response.error.description);
}
return response.value.find((value) => value.success[order] !== undefined).success[order];
}
async off(immediate = false) {
const route = `/groups/${this._id}/action`;
const path = `/${this._apiKey}${route}`;
const order = `${route}/on`;
const body = JSON.stringify({
on: false,
transitiontime: immediate ? 0 : undefined,
});
const options = {
body: body,
};
const response = await this._fetchClient.put(path, options);
if (response.error) {
throw new Error(response.error.description);
}
return !response.value.find((value) => value.success[order] !== undefined).success[order];
}
async turn(shouldTurnOn, immediate = false) {
if (shouldTurnOn) {
return this.on(immediate);
}
return this.off(immediate);
}
async getColor() {
const state = await this.getState();
const xy = {
x: state.xy[0],
y: state.xy[1],
};
const brightness = state.bri;
const rgb = ColorConverter_1.ColorConverter.convertXYtoRGB(xy, brightness);
return rgb;
}
async setColor(color, immediate = false) {
const route = `/groups/${this._id}/action`;
const path = `/${this._apiKey}${route}`;
const order = `${route}/xy`;
const xy = ColorConverter_1.ColorConverter.convertRGBToXY(color);
const body = JSON.stringify({
xy: [xy.x, xy.y],
transitiontime: immediate ? 0 : undefined,
});
const options = {
body: body,
};
const response = await this._fetchClient.put(path, options);
if (response.error) {
throw new Error(response.error.description);
}
const colorResult = response.value.find((value) => value.success[order] !== undefined).success[order];
return xy.x - colorResult[0] < 0.01 && xy.y - colorResult[1] < 0.01;
}
async setBrightness(brightnessPercent, immediate = false) {
const route = `/groups/${this._id}/action`;
const path = `/${this._apiKey}${route}`;
const order = `${route}/bri`;
const brightness = Math.floor(brightnessPercent * 2.54);
const body = JSON.stringify({
bri: brightness,
transitiontime: immediate ? 0 : undefined,
});
const options = {
body: body,
};
const response = await this._fetchClient.put(path, options);
if (response.error) {
throw new Error(response.error.description);
}
const brightnessResponse = response.value.find((value) => value.success[order] !== undefined).success[order];
return brightnessResponse === brightness;
}
async changeState(on, color, brightnessPercent, immediate = false) {
const route = `/groups/${this._id}/action`;
const path = `/${this._apiKey}${route}`;
const brightnessOrder = `${route}/bri`;
const onOrder = `${route}/on`;
const colorOrder = `${route}/xy`;
const body = {
transitiontime: immediate ? 0 : undefined,
on: on,
};
if (color !== undefined) {
const xy = ColorConverter_1.ColorConverter.convertRGBToXY(color);
body.xy = [xy.x, xy.y];
}
if (brightnessPercent !== undefined) {
const brightness = Math.floor(brightnessPercent * 2.54);
body.bri = brightness;
}
const options = {
body: JSON.stringify(body),
};
const response = await this._fetchClient.put(path, options);
if (response.error) {
throw new Error(response.error.description);
}
let success = true;
if (on !== undefined) {
const onResult = response.value.find((value) => value.success[onOrder] !== undefined).success[onOrder];
success = success && onResult === on;
}
if (brightnessPercent !== undefined) {
const brightnessResponse = response.value.find((value) => value.success[brightnessOrder] !== undefined).success[brightnessOrder];
success = success && brightnessResponse === Math.floor(brightnessPercent * 2.54);
}
if (color !== undefined) {
const colorResult = response.value.find((value) => value.success[colorOrder] !== undefined).success[colorOrder];
const xy = ColorConverter_1.ColorConverter.convertRGBToXY(color);
success = success && xy.x - colorResult[0] < 0.01 && xy.y - colorResult[1] < 0.01;
}
return success;
}
async getState() {
const data = await this.getData();
return data.action;
}
async getData() {
const path = `/${this._apiKey}/groups/${this._id}`;
const response = await this._fetchClient.get(path);
if (response.error) {
throw new Error(response.error.description);
}
return response.value;
}
}
exports.Lightgroup = Lightgroup;
//# sourceMappingURL=Lightgroup.js.map