const hue_auth = require('./auth');
const LightState = require('node-hue-api').v3.lightStates.LightState;
const time = require('../time');
hue_auth.authenticate().catch(console.error);
function assertAPI() {
return new Promise(function(resolve, reject) {
if (hue_auth.get_api()) {
resolve();
} else {
reject("Philips Hue API unavailable");
}
});
}
const lightNamePadding = 20;
function lightInfo(light) {
if (light.state.reachable) {
return `${(light.id + "").padEnd(2)} ${light.name.padEnd(lightNamePadding)} is ${
light.state.on ? "on" : "off"
} at brightness ${light.state.bri}${light.state.colormode ?
` and ${light.state.colormode} ${light.state[light.state.colormode]}` : ""
}`;
}
return `${light.name.padEnd(lightNamePadding)} is unreachable`;
}
module.exports = {
state: {
// https://github.com/peter-murray/node-hue-api/blob/master/docs/lightState.md#ct
night: function() {
return {
white: new LightState().on().brightness(0).transition(time.one_hour),
color: new LightState().on().white(500, 0).transition(time.one_hour)
};
},
night_fast: function() {
return {
white: new LightState().on().brightness(0),
color: new LightState().on().white(500, 0)
};
},
bright: function() {
return {
white: new LightState().brightness(100),
color: new LightState().white(153, 100)
};
},
on: function() {
return new LightState().on();
},
off: function() {
return new LightState().off();
},
},
color_of: function(light) {
return light.getSupportedStates().includes("ct") ? "color" : "white";
},
set_all_lights: function(get_state) {
return assertAPI().then(function() {
return hue_auth.get_api().lights.getAll();
}).then(function(lights) {
for (const light of lights) {
console.log(lightInfo(light));
if (!light.state.reachable) {
continue;
}
let state = get_state(light);
if (state) {
hue_auth.get_api().lights.setLightState(light.id, state).then(function() {
console.log(light.name.padEnd(lightNamePadding), "state updated");
}).catch(console.error);
}
}
});
}
};