homebridge-yeelight-wifi
Version:
Homebridge plugin for YeeLight bulbs & lamps.
468 lines (458 loc) • 11.2 kB
JavaScript
const id = (function* () {
let i = 0;
while (true) {
yield (i += 1);
}
})();
const sleep = (duration) =>
new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
const pipe =
(...fns) =>
(x) =>
fns.reduce((v, f) => f(v), x);
const getName = (devId, config) =>
(config &&
config.defaultValue &&
config.defaultValue[devId] &&
config.defaultValue[devId].name) ||
devId;
const getDeviceId = (id) => id.slice(-6);
const blacklist = (devId, config) =>
(config &&
config.defaultValue &&
config.defaultValue[devId] &&
config.defaultValue[devId].blacklist) ||
[];
const handle =
(handlers = []) =>
(messages) => {
messages
.toString()
.split(global.EOL)
.filter((it) => it)
.map((payload) => JSON.parse(payload))
.forEach((message) => {
handlers.find((handler) => handler(message));
});
};
const colorFromTemperature = (mired) => {
// mired -> [saturation, hue]
const lookupTable = new Map([
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
[]],
]);
const temperature = Math.round(Math.min(Math.max(mired, 100), 500));
const [sat, hue] = lookupTable.get(temperature);
return { hue: Math.round(hue), sat: Math.round(sat) };
};
module.exports = {
id,
getDeviceId,
getName,
blacklist,
handle,
sleep,
pipe,
colorFromTemperature,
};