Skip to content

Commit

Permalink
PhilipsHue color
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Dec 5, 2020
1 parent 28cac4a commit a4dc466
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 38 deletions.
6 changes: 5 additions & 1 deletion server/services/philips-hue/lib/light/light.setValue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { DEVICE_FEATURE_TYPES } = require('../../../../utils/constants');
const { intToRgb } = require('../../../../utils/colors');

const logger = require('../../../../utils/logger');
const { parseExternalId } = require('../utils/parseExternalId');
Expand All @@ -8,7 +9,7 @@ const { NotFoundError } = require('../../../../utils/coreErrors');
* @description Change value of a Philips hue
* @param {Object} device - The device to control.
* @param {Object} deviceFeature - The binary deviceFeature to control.
* @param {string|number} value - The new value.
* @param {number} value - The new value.
* @example
* turnOff(device, deviceFeature, value);
*/
Expand All @@ -24,6 +25,9 @@ async function setValue(device, deviceFeature, value) {
case DEVICE_FEATURE_TYPES.LIGHT.BINARY:
state = value === 1 ? new this.LightState().on() : new this.LightState().off();
break;
case DEVICE_FEATURE_TYPES.LIGHT.COLOR:
state = new this.LightState().rgb(intToRgb(value));
break;
default:
logger.debug(`Philips Hue : Feature type = "${deviceFeature.type}" not handled`);
break;
Expand Down
4 changes: 2 additions & 2 deletions server/test/services/philips-hue/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { expect } = require('chai');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('./mocks.test');
const { MockedPhilipsHueClient } = require('./mocks.test');

const PhilipsHueService = proxyquire('../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

describe('PhilipsHueService', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { assert, expect } = require('chai');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { expect } = require('chai');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

describe('PhilipsHueService', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { expect } = require('chai');
const { fake } = require('sinon');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down
4 changes: 2 additions & 2 deletions server/test/services/philips-hue/light/light.poll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { assert } = require('chai');
const { fake } = require('sinon');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down
83 changes: 73 additions & 10 deletions server/test/services/philips-hue/light/light.setValue.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { assert } = require('chai');
const { fake } = require('sinon');
const { expect } = require('chai');
const sinon = require('sinon');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient, fakes } = require('../mocks.test');

const { fake, assert } = sinon;

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down Expand Up @@ -66,7 +68,11 @@ const gladys = {
};

describe('PhilipsHueService', () => {
it('should set value', async () => {
afterEach(() => {
sinon.reset();
});

it('should set binary value (on)', async () => {
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279');
await philipsHueService.device.init();
await philipsHueService.device.setValue(
Expand All @@ -85,13 +91,17 @@ describe('PhilipsHueService', () => {
},
1,
);

assert.calledOnce(fakes.on);
assert.notCalled(fakes.off);
assert.notCalled(fakes.rgb);
});
it('should return hue api not found', async () => {
it('should set binary value (off)', async () => {
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279');
await philipsHueService.device.init();
const promise = philipsHueService.device.setValue(
await philipsHueService.device.setValue(
{
external_id: 'light:not-found:1',
external_id: 'light:1234:1',
features: [
{
category: 'light',
Expand All @@ -103,8 +113,61 @@ describe('PhilipsHueService', () => {
category: 'light',
type: 'binary',
},
1,
0,
);
return assert.isRejected(promise, 'HUE_API_NOT_FOUND');

assert.calledOnce(fakes.off);
assert.notCalled(fakes.on);
assert.notCalled(fakes.rgb);
});
it('should set color value', async () => {
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279');
await philipsHueService.device.init();
await philipsHueService.device.setValue(
{
external_id: 'light:1234:1',
features: [
{
category: 'light',
type: 'color',
},
],
},
{
category: 'light',
type: 'color',
},
255,
);

assert.calledOnce(fakes.rgb);
assert.notCalled(fakes.off);
assert.notCalled(fakes.on);
});
it('should return hue api not found', async () => {
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279');
await philipsHueService.device.init();

try {
await philipsHueService.device.setValue(
{
external_id: 'light:not-found:1',
features: [
{
category: 'light',
type: 'binary',
},
],
},
{
category: 'light',
type: 'binary',
},
1,
);
expect.fail();
} catch (e) {
expect(e.message).eq('HUE_API_NOT_FOUND');
}
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { fake } = require('sinon');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { expect } = require('chai');
const { fake } = require('sinon');
const EventEmitter = require('events');
const proxyquire = require('proxyquire').noCallThru();
const PhilipsHueClient = require('../mocks.test');
const { MockedPhilipsHueClient } = require('../mocks.test');

const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', {
'node-hue-api': PhilipsHueClient,
'node-hue-api': MockedPhilipsHueClient,
});

const StateManager = require('../../../../lib/state');
Expand Down
28 changes: 15 additions & 13 deletions server/test/services/philips-hue/mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ const lights = require('./lights.json');
const STATE_ON = { _values: { on: true } };
const STATE_OFF = { _values: { off: true } };

class LightState {
on() {
this.test = 1; // useless, this is just for eslint
return STATE_ON;
}
const fakes = {
on: fake.returns(STATE_ON),
off: fake.returns(STATE_ON),
rgb: fake.returns(null),
};

off() {
this.test = 1; // useless, this is just for eslint
return STATE_OFF;
}
}
class LightState {}
LightState.prototype.on = fakes.on;
LightState.prototype.off = fakes.off;
LightState.prototype.rgb = fakes.rgb;

const hueApi = {
users: {
Expand Down Expand Up @@ -85,6 +84,9 @@ const MockedPhilipsHueClient = {
},
};

module.exports = MockedPhilipsHueClient;
module.exports.STATE_ON = STATE_ON;
module.exports.STATE_OFF = STATE_OFF;
module.exports = {
MockedPhilipsHueClient,
STATE_ON,
STATE_OFF,
fakes,
};

0 comments on commit a4dc466

Please sign in to comment.