Skip to content

Commit 8527e76

Browse files
committed
Handle more types for "on"
1 parent d8eea85 commit 8527e76

File tree

4 files changed

+201
-32
lines changed

4 files changed

+201
-32
lines changed

example/dump_lights.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint no-console: off */
12
'use strict';
23

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

1112
client.on('light-new', function(light) {
12-
light.getHardwareVersion((err, data) => {
13+
light.getHardwareVersion((err, data) => {
1314
console.log('getHardwareVersion', light.id, err ? err.message : data);
1415
});
1516

lib/lifx-light.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function convertLifxState(lifxInfo) {
4141

4242
if (stateItem === true)
4343
coll[key] = value;
44-
if (typeof stateItem === 'string')
44+
if (_.isString(stateItem))
4545
coll[stateItem] = value;
4646
return coll;
4747
}, { on: false, brightness: 0});
@@ -180,8 +180,8 @@ LightItem.prototype.initialize = function initialize(callback) {
180180
self.state = convertLifxState(lifxInfo);
181181

182182
// Infrared
183-
if (self.info.capability | LightCapability.INFRARED &&
184-
_.isPlainObject(lifxMaxIR) &&
183+
if (self.info.capability | LightCapability.INFRARED &&
184+
_.isPlainObject(lifxMaxIR) &&
185185
_.isFinite(lifxMaxIR.brightness)) {
186186
self.info.maxIRLevel = limitValue(Math.round(lifxMaxIR.brightness), 0, 100);
187187
}
@@ -219,7 +219,7 @@ LightItem.prototype.pollChanges = function pollChanges(callback) {
219219
var self = this;
220220

221221
// Ensure callback is an function
222-
if (typeof callback !== 'function')
222+
if (!_.isFunction(callback))
223223
callback = function() {}
224224

225225
async.series([
@@ -271,7 +271,7 @@ LightItem.prototype.pollChanges = function pollChanges(callback) {
271271
self.state = newState;
272272

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

291291

292+
/**
293+
* Parse state variable (on/off)
294+
* @param {(number|boolean|string)} input New state
295+
* @return {bool} New state
296+
*/
297+
LightItem.prototype.parseState = function parseState(input) {
298+
var retVal = this.state.on;
299+
300+
// On/Off
301+
if (_.isBoolean(input)) {
302+
retVal = input;
303+
}
304+
else if (_.isNumber(input)) {
305+
// Zero off everything else is on
306+
retVal = (input !== 0);
307+
}
308+
else if (_.isString(input)) {
309+
switch(input.toLowerCase()) {
310+
case 'on':
311+
case 'true':
312+
retVal = true;
313+
break;
314+
case 'off':
315+
case 'false':
316+
retVal = false;
317+
break;
318+
case 'toggle':
319+
retVal = !this.state.on;
320+
break;
321+
default:
322+
break;
323+
}
324+
}
325+
return retVal;
326+
}
327+
328+
292329
/**
293330
* Check if we have new color and calculate updated properties
294331
* @param {object} input Input arguments
@@ -488,13 +525,9 @@ LightItem.prototype.setColor = function setColor(input) {
488525

489526
// Ensure that input is of correct type
490527
if (!_.isPlainObject(input)) {
491-
// Convert boolean to on
492-
if (_.isBoolean(input)) {
493-
input = { on: input };
494-
}
495-
// On/Off string
496-
else if (input === 'on' || input === 'off') {
497-
input = { on: (input === 'on') };
528+
// Boolean or string
529+
if (_.isBoolean(input) || _.isString(input)) {
530+
input = { on: this.parseState(input) };
498531
}
499532
// Convert number to brightness, also enables the light
500533
else if (_.isFinite(input)) {
@@ -520,10 +553,9 @@ LightItem.prototype.setColor = function setColor(input) {
520553
changedColor = true;
521554

522555
// On/Off
523-
if (_.isBoolean(input.on)) {
524-
newValues.on = input.on;
556+
newValues.on = this.parseState(input.on);
557+
if (newValues.on !== isOn)
525558
changedColor = true;
526-
}
527559

528560
// Infrared max level
529561
if ((this.info.capability & LightCapability.INFRARED) && _.isFinite(input.maxIR)) {

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-node-lifx",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"description": "Control Lifx lights using Node-RED.",
55
"author": "jdomeij",
66
"homepage": "https://github.com/jdomeij/node-red-contrib-node-lifx",
@@ -29,15 +29,15 @@
2929
"devDependencies": {
3030
"chai": "^4.1.2",
3131
"chai-spies": "^1.0.0",
32-
"mocha": "^3.5.3"
32+
"mocha": "^5.2.0"
3333
},
3434
"dependencies": {
35-
"async": "^2.6.0",
36-
"color-convert": "^1.9.1",
37-
"color-space": "^1.15.0",
35+
"async": "^2.6.1",
36+
"color-convert": "^1.9.2",
37+
"color-space": "^1.16.0",
3838
"color-temp": "0.0.2",
3939
"enum": "^2.5.0",
40-
"lodash": "^4.17.3",
40+
"lodash": "^4.17.10",
4141
"node-lifx": "^0.8.0"
4242
},
4343
"scripts": {

test/lifx-light.test.js

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,7 @@ describe('Lifx-Light', () => {
415415
done();
416416
});
417417

418-
it('false (off)', (done) => {
419-
lightItem.setColor(false);
420-
expect(lifxItem.off).to.have.been.called();
421-
done();
422-
});
423418

424-
it('"off"', (done) => {
425-
lightItem.setColor("off");
426-
expect(lifxItem.off).to.have.been.called();
427-
done();
428-
});
429419

430420
it('50 (brightness)', (done) => {
431421
lightItem.setColor(50);
@@ -450,6 +440,151 @@ describe('Lifx-Light', () => {
450440
expect(lifxItem.color).to.have.been.called.with.exactly(0, 100, 50, 4000, 0);
451441
done();
452442
});
443+
444+
describe('Off', () => {
445+
446+
beforeEach(() => {
447+
lightItem.state.on = true;
448+
});
449+
450+
it('false', (done) => {
451+
lightItem.setColor(false);
452+
expect(lifxItem.on).to.not.have.been.called();
453+
expect(lifxItem.off).to.have.been.called();
454+
done();
455+
});
456+
457+
it('"off"', (done) => {
458+
lightItem.setColor("off");
459+
expect(lifxItem.on).to.not.have.been.called();
460+
expect(lifxItem.off).to.have.been.called();
461+
done();
462+
});
463+
464+
it('{on:false}', (done) => {
465+
lightItem.setColor({on:false});
466+
expect(lifxItem.on).to.not.have.been.called();
467+
expect(lifxItem.off).to.have.been.called();
468+
done();
469+
});
470+
471+
it('{on:"false"}', (done) => {
472+
lightItem.setColor({on:"false"});
473+
expect(lifxItem.on).to.not.have.been.called();
474+
expect(lifxItem.off).to.have.been.called();
475+
done();
476+
});
477+
478+
it('{on:0}', (done) => {
479+
lightItem.setColor({on:0});
480+
expect(lifxItem.on).to.not.have.been.called();
481+
expect(lifxItem.off).to.have.been.called();
482+
done();
483+
});
484+
485+
it('{on:"off"}', (done) => {
486+
lightItem.setColor({on:"off"});
487+
expect(lifxItem.on).to.not.have.been.called();
488+
expect(lifxItem.off).to.have.been.called();
489+
done();
490+
});
491+
492+
it('{on:false}', (done) => {
493+
lightItem.setColor({on:"false"});
494+
expect(lifxItem.on).to.not.have.been.called();
495+
expect(lifxItem.off).to.have.been.called();
496+
done();
497+
});
498+
});
499+
describe('On', () => {
500+
beforeEach(() => {
501+
lightItem.state.on = false;
502+
});
503+
504+
it('true', (done) => {
505+
lightItem.setColor(true);
506+
expect(lifxItem.off).to.not.have.been.called();
507+
expect(lifxItem.on).to.have.been.called();
508+
done();
509+
});
510+
511+
it('"on"', (done) => {
512+
lightItem.setColor("on");
513+
expect(lifxItem.off).to.not.have.been.called();
514+
expect(lifxItem.on).to.have.been.called();
515+
done();
516+
});
517+
518+
it('{on:true}', (done) => {
519+
lightItem.setColor({on:true});
520+
expect(lifxItem.off).to.not.have.been.called();
521+
expect(lifxItem.on).to.have.been.called();
522+
done();
523+
});
524+
525+
it('{on:"true"}', (done) => {
526+
lightItem.setColor({on:"true"});
527+
expect(lifxItem.off).to.not.have.been.called();
528+
expect(lifxItem.on).to.have.been.called();
529+
done();
530+
});
531+
532+
it('{on:1}', (done) => {
533+
lightItem.setColor({on:1});
534+
expect(lifxItem.off).to.not.have.been.called();
535+
expect(lifxItem.on).to.have.been.called();
536+
done();
537+
});
538+
539+
it('{on:"on"}', (done) => {
540+
lightItem.setColor({on:"on"});
541+
expect(lifxItem.off).to.not.have.been.called();
542+
expect(lifxItem.on).to.have.been.called();
543+
done();
544+
});
545+
546+
it('{on:true}', (done) => {
547+
lightItem.setColor({on:"true"});
548+
expect(lifxItem.off).to.not.have.been.called();
549+
expect(lifxItem.on).to.have.been.called();
550+
done();
551+
});
552+
});
553+
554+
describe('Toggle', () => {
555+
it('"toggle" on => off', (done) => {
556+
lightItem.state.on = true;
557+
558+
lightItem.setColor('toggle');
559+
expect(lifxItem.on).to.not.have.been.called();
560+
expect(lifxItem.off).to.have.been.called();
561+
done();
562+
});
563+
it('"toggle" off => on', (done) => {
564+
lightItem.state.on = false;
565+
566+
lightItem.setColor('toggle');
567+
expect(lifxItem.off).to.not.have.been.called();
568+
expect(lifxItem.on).to.have.been.called();
569+
done();
570+
});
571+
it('{on:"toggle"} on => off', (done) => {
572+
lightItem.state.on = true;
573+
574+
lightItem.setColor({on:'toggle'});
575+
expect(lifxItem.on).to.not.have.been.called();
576+
expect(lifxItem.off).to.have.been.called();
577+
done();
578+
});
579+
it('{on:"toggle"} off => on', (done) => {
580+
lightItem.state.on = false;
581+
582+
lightItem.setColor({on:'toggle'});
583+
expect(lifxItem.off).to.not.have.been.called();
584+
expect(lifxItem.on).to.have.been.called();
585+
done();
586+
});
587+
});
453588
});
454589

455590

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

634769
expect(lifxItem.on).not.to.have.been.called();
770+
expect(lifxItem.off).not.to.have.been.called();
635771
expect(lifxItem.color).not.to.have.been.called();
636772

637773
expect(lifxItem.maxIR).to.have.been.called();

0 commit comments

Comments
 (0)