Skip to content

Commit

Permalink
Handle more types for "on"
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomeij committed Jul 20, 2018
1 parent d8eea85 commit 8527e76
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 32 deletions.
3 changes: 2 additions & 1 deletion example/dump_lights.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-console: off */
'use strict';

var Lifx = require('node-lifx').Client;
Expand All @@ -9,7 +10,7 @@ client.on('error', function(err) {
});

client.on('light-new', function(light) {
light.getHardwareVersion((err, data) => {
light.getHardwareVersion((err, data) => {
console.log('getHardwareVersion', light.id, err ? err.message : data);
});

Expand Down
62 changes: 47 additions & 15 deletions lib/lifx-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function convertLifxState(lifxInfo) {

if (stateItem === true)
coll[key] = value;
if (typeof stateItem === 'string')
if (_.isString(stateItem))
coll[stateItem] = value;
return coll;
}, { on: false, brightness: 0});
Expand Down Expand Up @@ -180,8 +180,8 @@ LightItem.prototype.initialize = function initialize(callback) {
self.state = convertLifxState(lifxInfo);

// Infrared
if (self.info.capability | LightCapability.INFRARED &&
_.isPlainObject(lifxMaxIR) &&
if (self.info.capability | LightCapability.INFRARED &&
_.isPlainObject(lifxMaxIR) &&
_.isFinite(lifxMaxIR.brightness)) {
self.info.maxIRLevel = limitValue(Math.round(lifxMaxIR.brightness), 0, 100);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ LightItem.prototype.pollChanges = function pollChanges(callback) {
var self = this;

// Ensure callback is an function
if (typeof callback !== 'function')
if (!_.isFunction(callback))
callback = function() {}

async.series([
Expand Down Expand Up @@ -271,7 +271,7 @@ LightItem.prototype.pollChanges = function pollChanges(callback) {
self.state = newState;

if (self.info.capability & LightCapability.INFRARED &&
_.isPlainObject(lifxMaxIR) &&
_.isPlainObject(lifxMaxIR) &&
_.isFinite(lifxMaxIR.brightness)) {
isUpdated = (self.info.maxIRLevel !== lifxMaxIR.lifxMaxIR) || isUpdated;
self.info.maxIRLevel = limitValue(Math.round(lifxMaxIR.brightness), 0, 100);
Expand All @@ -289,6 +289,43 @@ LightItem.prototype.pollChanges = function pollChanges(callback) {
}


/**
* Parse state variable (on/off)
* @param {(number|boolean|string)} input New state
* @return {bool} New state
*/
LightItem.prototype.parseState = function parseState(input) {
var retVal = this.state.on;

// On/Off
if (_.isBoolean(input)) {
retVal = input;
}
else if (_.isNumber(input)) {
// Zero off everything else is on
retVal = (input !== 0);
}
else if (_.isString(input)) {
switch(input.toLowerCase()) {
case 'on':
case 'true':
retVal = true;
break;
case 'off':
case 'false':
retVal = false;
break;
case 'toggle':
retVal = !this.state.on;
break;
default:
break;
}
}
return retVal;
}


/**
* Check if we have new color and calculate updated properties
* @param {object} input Input arguments
Expand Down Expand Up @@ -488,13 +525,9 @@ LightItem.prototype.setColor = function setColor(input) {

// Ensure that input is of correct type
if (!_.isPlainObject(input)) {
// Convert boolean to on
if (_.isBoolean(input)) {
input = { on: input };
}
// On/Off string
else if (input === 'on' || input === 'off') {
input = { on: (input === 'on') };
// Boolean or string
if (_.isBoolean(input) || _.isString(input)) {
input = { on: this.parseState(input) };
}
// Convert number to brightness, also enables the light
else if (_.isFinite(input)) {
Expand All @@ -520,10 +553,9 @@ LightItem.prototype.setColor = function setColor(input) {
changedColor = true;

// On/Off
if (_.isBoolean(input.on)) {
newValues.on = input.on;
newValues.on = this.parseState(input.on);
if (newValues.on !== isOn)
changedColor = true;
}

// Infrared max level
if ((this.info.capability & LightCapability.INFRARED) && _.isFinite(input.maxIR)) {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-node-lifx",
"version": "0.9.2",
"version": "0.9.3",
"description": "Control Lifx lights using Node-RED.",
"author": "jdomeij",
"homepage": "https://github.com/jdomeij/node-red-contrib-node-lifx",
Expand Down Expand Up @@ -29,15 +29,15 @@
"devDependencies": {
"chai": "^4.1.2",
"chai-spies": "^1.0.0",
"mocha": "^3.5.3"
"mocha": "^5.2.0"
},
"dependencies": {
"async": "^2.6.0",
"color-convert": "^1.9.1",
"color-space": "^1.15.0",
"async": "^2.6.1",
"color-convert": "^1.9.2",
"color-space": "^1.16.0",
"color-temp": "0.0.2",
"enum": "^2.5.0",
"lodash": "^4.17.3",
"lodash": "^4.17.10",
"node-lifx": "^0.8.0"
},
"scripts": {
Expand Down
156 changes: 146 additions & 10 deletions test/lifx-light.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,7 @@ describe('Lifx-Light', () => {
done();
});

it('false (off)', (done) => {
lightItem.setColor(false);
expect(lifxItem.off).to.have.been.called();
done();
});

it('"off"', (done) => {
lightItem.setColor("off");
expect(lifxItem.off).to.have.been.called();
done();
});

it('50 (brightness)', (done) => {
lightItem.setColor(50);
Expand All @@ -450,6 +440,151 @@ describe('Lifx-Light', () => {
expect(lifxItem.color).to.have.been.called.with.exactly(0, 100, 50, 4000, 0);
done();
});

describe('Off', () => {

beforeEach(() => {
lightItem.state.on = true;
});

it('false', (done) => {
lightItem.setColor(false);
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('"off"', (done) => {
lightItem.setColor("off");
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('{on:false}', (done) => {
lightItem.setColor({on:false});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('{on:"false"}', (done) => {
lightItem.setColor({on:"false"});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('{on:0}', (done) => {
lightItem.setColor({on:0});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('{on:"off"}', (done) => {
lightItem.setColor({on:"off"});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});

it('{on:false}', (done) => {
lightItem.setColor({on:"false"});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});
});
describe('On', () => {
beforeEach(() => {
lightItem.state.on = false;
});

it('true', (done) => {
lightItem.setColor(true);
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('"on"', (done) => {
lightItem.setColor("on");
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('{on:true}', (done) => {
lightItem.setColor({on:true});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('{on:"true"}', (done) => {
lightItem.setColor({on:"true"});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('{on:1}', (done) => {
lightItem.setColor({on:1});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('{on:"on"}', (done) => {
lightItem.setColor({on:"on"});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});

it('{on:true}', (done) => {
lightItem.setColor({on:"true"});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});
});

describe('Toggle', () => {
it('"toggle" on => off', (done) => {
lightItem.state.on = true;

lightItem.setColor('toggle');
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});
it('"toggle" off => on', (done) => {
lightItem.state.on = false;

lightItem.setColor('toggle');
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});
it('{on:"toggle"} on => off', (done) => {
lightItem.state.on = true;

lightItem.setColor({on:'toggle'});
expect(lifxItem.on).to.not.have.been.called();
expect(lifxItem.off).to.have.been.called();
done();
});
it('{on:"toggle"} off => on', (done) => {
lightItem.state.on = false;

lightItem.setColor({on:'toggle'});
expect(lifxItem.off).to.not.have.been.called();
expect(lifxItem.on).to.have.been.called();
done();
});
});
});


Expand Down Expand Up @@ -632,6 +767,7 @@ describe('Lifx-Light', () => {
lightItem.setColor({maxIR: 66});

expect(lifxItem.on).not.to.have.been.called();
expect(lifxItem.off).not.to.have.been.called();
expect(lifxItem.color).not.to.have.been.called();

expect(lifxItem.maxIR).to.have.been.called();
Expand Down

0 comments on commit 8527e76

Please sign in to comment.