-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from GeertWille/master
Added simple LIFX example
- Loading branch information
Showing
3 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
/* | ||
* Example code for an LIFX light integration wrote by Geert Wille | ||
*/ | ||
const BluePromise = require('bluebird'); | ||
const Lifx = require('lifx-http-api'); | ||
const client = new Lifx({ | ||
bearerToken: '' // https://cloud.lifx.com/settings | ||
}); | ||
|
||
const LIFX_ALL_LIGHTS_SELECTOR = 'all'; | ||
const LIFX_LIGHT_OFF = 'off'; | ||
const LIFX_LIGHT_ON = 'on'; | ||
|
||
let sliderValue = 50; | ||
let switchValue = true; | ||
|
||
// Helper function | ||
function getLifxDeviceSelector(deviceId) { | ||
return 'id:' + deviceId; | ||
} | ||
|
||
/* | ||
* Device Controller | ||
* Events on that device from the Brain will be forwarded here for handling. | ||
*/ | ||
module.exports.onPulse = function (name, deviceId) { | ||
// Just randomly pulse the light | ||
client.pulse(getLifxDeviceSelector(deviceId), { | ||
color: '#' + Math.floor(Math.random() * 16777215).toString(16), // Generate random color | ||
from_color: '#' + Math.floor(Math.random() * 16777215).toString(16), // Generate random color | ||
period: 1, | ||
cycles: 5, | ||
persist: false, | ||
power_on: true, | ||
peak: 0.8 | ||
}); | ||
}; | ||
|
||
module.exports.allOff = function (name, deviceId) { | ||
// Just randomly pulse the light | ||
client.setState(LIFX_ALL_LIGHTS_SELECTOR, { | ||
power: LIFX_LIGHT_OFF | ||
}).then(console.log, console.error); | ||
}; | ||
|
||
/** | ||
* Getters and setters: | ||
* - The getters are used to send the current Values through the SDK (read) | ||
* - The setter allow changing values on the Brain and handling the changes here (write) | ||
*/ | ||
module.exports.switchSet = function (deviceId, value) { | ||
console.log('[CONTROLLER] switch set to', deviceId, value); | ||
switchValue = value; | ||
|
||
// Update LIFX light | ||
client.setState(getLifxDeviceSelector(deviceId), { | ||
power: switchValue == 'true' ? LIFX_LIGHT_ON : LIFX_LIGHT_OFF | ||
}).then(console.log, console.error); | ||
}; | ||
|
||
module.exports.switchGet = function (deviceId) { | ||
console.log('[CONTROLLER] return switch value', deviceId, switchValue); | ||
return BluePromise.resolve(switchValue); | ||
}; | ||
|
||
module.exports.sliderSet = function (deviceId, value) { | ||
console.log('[CONTROLLER] dimmer set to', deviceId, value); | ||
sliderValue = value; | ||
|
||
// Update LIFX light | ||
client.setState(LIFX_ALL_LIGHTS_SELECTOR, { | ||
brightness: sliderValue / 100 | ||
}).then(console.log, console.error); | ||
}; | ||
|
||
module.exports.sliderGet = function (deviceId) { | ||
console.log('[CONTROLLER] return current dimmer value', deviceId, sliderValue); | ||
return BluePromise.resolve(sliderValue); | ||
}; | ||
|
||
/* | ||
* Discover all LIFX lights and return them in the proper format | ||
*/ | ||
module.exports.discoverLifx = function () { | ||
return client.listLights(LIFX_ALL_LIGHTS_SELECTOR) | ||
.then((LifxLights) => { | ||
console.info(LifxLights); | ||
return LifxLights.map((lifx) => ({ | ||
id: lifx.id, | ||
name: lifx.label, | ||
reachable: lifx.reachable | ||
})); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR!', err); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
/* | ||
* Example code for an LIFX light integration wrote by Geert Wille | ||
*/ | ||
const neeoapi = require('neeo-sdk'); | ||
const controller = require('./controller'); | ||
|
||
console.log('NEEO SDK Example "LIFX" adapter'); | ||
console.log('---------------------------------------------'); | ||
|
||
/* | ||
* Adapter - simple LIFX integration. | ||
*/ | ||
|
||
// first we set the device info, used to identify it on the Brain | ||
const lifxLight = neeoapi.buildDevice('Smart Light') // LIFX LIFX looked weird in the app so I just named it Smart Light | ||
.setManufacturer('LIFX') | ||
.addAdditionalSearchToken('light') | ||
.setType('LIGHT') | ||
.enableDiscovery({ | ||
headerText: 'Ready to discover LIFX lights', | ||
description: 'Make sure that you\'re logged in in the LIFX app and that you\'ve create a new accessToken on https://cloud.lifx.com/settings so we can retrieve your lights' | ||
}, controller.discoverLifx) | ||
// Then we add the capabilities of the LIFX bulb | ||
.addSlider({ name: 'power-slider', label: 'Dimmer', range: [0, 100], unit: '%' }, { setter: controller.sliderSet, getter: controller.sliderGet }) | ||
.addSwitch({ name: 'toggle', label: 'Toggle On/Off' }, { setter: controller.switchSet, getter: controller.switchGet }) | ||
.addButton({ name: 'pulse', label: 'Pulse' }) | ||
.addButtonHander(controller.onPulse) | ||
.addButton({ name: 'all-off', label: 'All Off' }) | ||
.addButtonHander(controller.allOff); | ||
|
||
console.log('- discover one NEEO Brain...'); | ||
neeoapi.discoverOneBrain() | ||
.then((brain) => { | ||
console.log('- Brain discovered:', brain.name); | ||
|
||
console.log('- Start server'); | ||
return neeoapi.startServer({ | ||
brain, | ||
port: 6336, | ||
name: 'lifx', | ||
devices: [lifxLight] | ||
}); | ||
}) | ||
.then(() => { | ||
console.log('# READY! use the NEEO app to search for "LIFX".'); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR!', err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters