UNPKG

node-hue-api

Version:
140 lines (139 loc) 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lightsApi = void 0; const hue_bridge_model_1 = require("@peter-murray/hue-bridge-model"); const LightIdPlaceholder_1 = require("../../placeholders/LightIdPlaceholder"); const ApiEndpoint_1 = require("./ApiEndpoint"); const rgb_1 = require("../../../rgb"); const ApiError_1 = require("../../../ApiError"); const util_1 = require("../../../util"); const LIGHT_ID_PLACEHOLDER = new LightIdPlaceholder_1.LightIdPlaceholder(); const lightsApi = { getAllLights: new ApiEndpoint_1.ApiEndpoint() // .version('1.0') .get() .uri('/<username>/lights') .acceptJson() .pureJson() .postProcess(buildLightsResult), getNewLights: new ApiEndpoint_1.ApiEndpoint() // .version('1.0') .get() .uri('/<username>/lights/new') .acceptJson() .pureJson(), searchForNewLights: new ApiEndpoint_1.ApiEndpoint() .post() .uri('/<username>/lights') .acceptJson() .pureJson() .postProcess(util_1.wasSuccessful), getLightAttributesAndState: new ApiEndpoint_1.ApiEndpoint() .get() .uri('/<username>/lights/<id>') .placeholder(LIGHT_ID_PLACEHOLDER) .acceptJson() .pureJson() .postProcess(injectLightId), // rename lights setLightAttributes: new ApiEndpoint_1.ApiEndpoint() .put() .uri('/<username>/lights/<id>') .placeholder(LIGHT_ID_PLACEHOLDER) .acceptJson() .pureJson() .payload(buildLightNamePayload) .postProcess(util_1.wasSuccessful), setLightState: new ApiEndpoint_1.ApiEndpoint() .put() .uri('/<username>/lights/<id>/state') .placeholder(LIGHT_ID_PLACEHOLDER) .acceptJson() .pureJson() .payload(buildLightStateBody) .postProcess(validateLightStateResult), deleteLight: new ApiEndpoint_1.ApiEndpoint() .delete() .uri('/<username>/lights/<id>') .placeholder(LIGHT_ID_PLACEHOLDER) .acceptJson() .pureJson() }; exports.lightsApi = lightsApi; function buildLightsResult(result) { let lights = []; if (result) { Object.keys(result).forEach(function (id) { const light = hue_bridge_model_1.model.createFromBridge('light', id, result[id]); lights.push(light); }); } return lights; } function buildLightNamePayload(parameters) { // To support deprecation in the API where we take (id, name) and now just a (light) payload, cater for it here and // remove once lights.rename(id, name) is removed from API let light = null; if (parameters.light) { light = parameters.light; } else { // Set the name on a Light instance so that it can be validated using parameter constraints there light = hue_bridge_model_1.model.createFromBridge('light', 0, { name: parameters.name }); } return { type: 'application/json', body: { name: light.name }, }; } function injectLightId(result, requestParameters) { const id = LIGHT_ID_PLACEHOLDER.getValue(requestParameters); return Object.assign({ id: id }, result); } function buildLightStateBody(parameters) { const payload = getStateForDevice(parameters.device, parameters.state); return { type: 'application/json', body: payload }; } function validateLightStateResult(result) { if (!(0, util_1.wasSuccessful)(result)) { const parsed = (0, util_1.parseErrors)(result); throw new ApiError_1.ApiError(parsed ? parsed.join(', ') : `Unexpected result from bridge ${JSON.stringify(result)}`); } return true; } function getStateForDevice(device, desiredState) { if (!device) { throw new ApiError_1.ApiError('No light device provided'); } const allowedStates = device.getSupportedStates(), state = {}; let desiredStatePayload; if (desiredState instanceof hue_bridge_model_1.model.LightState) { desiredStatePayload = desiredState.getPayload(); } else { const lightState = new hue_bridge_model_1.model.LightState(); lightState.populate(desiredState); desiredStatePayload = lightState.getPayload(); } // Only allow the setting of parameters that the light supports in its state (e.g. do not set a color on a white light // Check for RGB and perform conversion (and remove any other settings) if (desiredStatePayload.rgb) { const colorGamut = device.colorGamut; if (!colorGamut) { throw new ApiError_1.ApiError('Cannot set an RGB color on a light that does not support a Color Gamut'); } state.xy = (0, rgb_1.rgbToXY)(desiredStatePayload.rgb, colorGamut); //TODO there is ordering here, in that xy wins if present, but we should remove the others if set (ct, hs) to reduce loading on bridge delete desiredStatePayload.rgb; } Object.keys(desiredStatePayload).forEach(desiredStateKey => { if (allowedStates.indexOf(desiredStateKey) > -1) { state[desiredStateKey] = desiredStatePayload[desiredStateKey]; } else { //TODO Switch to throwing errors when this occurs console.error(`Attempting to set a state ${desiredStateKey} on a light that does not support it, ${device.id}`); } }); return state; }