From a4dc466981d3a21e477474851015a97f6cdde332 Mon Sep 17 00:00:00 2001 From: atrovato <1839717+atrovato@users.noreply.github.com> Date: Sat, 5 Dec 2020 07:19:16 +0100 Subject: [PATCH] PhilipsHue color --- .../philips-hue/lib/light/light.setValue.js | 6 +- .../test/services/philips-hue/index.test.js | 4 +- .../light/light.configureBridge.test.js | 4 +- .../light/light.getBridges.test.js | 4 +- .../philips-hue/light/light.getLights.test.js | 4 +- .../philips-hue/light/light.poll.test.js | 4 +- .../philips-hue/light/light.setValue.test.js | 83 ++++++++++++++++--- .../light/lights.activateScene.test.js | 4 +- .../light/lights.getScenes.test.js | 4 +- .../test/services/philips-hue/mocks.test.js | 28 ++++--- 10 files changed, 107 insertions(+), 38 deletions(-) diff --git a/server/services/philips-hue/lib/light/light.setValue.js b/server/services/philips-hue/lib/light/light.setValue.js index c5ecc0176c..611e65ab46 100644 --- a/server/services/philips-hue/lib/light/light.setValue.js +++ b/server/services/philips-hue/lib/light/light.setValue.js @@ -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'); @@ -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); */ @@ -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; diff --git a/server/test/services/philips-hue/index.test.js b/server/test/services/philips-hue/index.test.js index 7910f62a04..b43f74e1c9 100644 --- a/server/test/services/philips-hue/index.test.js +++ b/server/test/services/philips-hue/index.test.js @@ -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', () => { diff --git a/server/test/services/philips-hue/light/light.configureBridge.test.js b/server/test/services/philips-hue/light/light.configureBridge.test.js index 3f9cbec7db..b470260657 100644 --- a/server/test/services/philips-hue/light/light.configureBridge.test.js +++ b/server/test/services/philips-hue/light/light.configureBridge.test.js @@ -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'); diff --git a/server/test/services/philips-hue/light/light.getBridges.test.js b/server/test/services/philips-hue/light/light.getBridges.test.js index eeb474bee8..9a340c6a0e 100644 --- a/server/test/services/philips-hue/light/light.getBridges.test.js +++ b/server/test/services/philips-hue/light/light.getBridges.test.js @@ -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', () => { diff --git a/server/test/services/philips-hue/light/light.getLights.test.js b/server/test/services/philips-hue/light/light.getLights.test.js index eb678269f5..f9c6f73fb8 100644 --- a/server/test/services/philips-hue/light/light.getLights.test.js +++ b/server/test/services/philips-hue/light/light.getLights.test.js @@ -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'); diff --git a/server/test/services/philips-hue/light/light.poll.test.js b/server/test/services/philips-hue/light/light.poll.test.js index 839cd204db..abccc98e65 100644 --- a/server/test/services/philips-hue/light/light.poll.test.js +++ b/server/test/services/philips-hue/light/light.poll.test.js @@ -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'); diff --git a/server/test/services/philips-hue/light/light.setValue.test.js b/server/test/services/philips-hue/light/light.setValue.test.js index 57b1592291..1890acf06f 100644 --- a/server/test/services/philips-hue/light/light.setValue.test.js +++ b/server/test/services/philips-hue/light/light.setValue.test.js @@ -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'); @@ -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( @@ -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', @@ -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'); + } }); }); diff --git a/server/test/services/philips-hue/light/lights.activateScene.test.js b/server/test/services/philips-hue/light/lights.activateScene.test.js index b5a68e6070..a6be7b37f7 100644 --- a/server/test/services/philips-hue/light/lights.activateScene.test.js +++ b/server/test/services/philips-hue/light/lights.activateScene.test.js @@ -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'); diff --git a/server/test/services/philips-hue/light/lights.getScenes.test.js b/server/test/services/philips-hue/light/lights.getScenes.test.js index 4fcc5d190c..9f7b0a807e 100644 --- a/server/test/services/philips-hue/light/lights.getScenes.test.js +++ b/server/test/services/philips-hue/light/lights.getScenes.test.js @@ -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'); diff --git a/server/test/services/philips-hue/mocks.test.js b/server/test/services/philips-hue/mocks.test.js index 7703b08bec..58d60d10f1 100644 --- a/server/test/services/philips-hue/mocks.test.js +++ b/server/test/services/philips-hue/mocks.test.js @@ -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: { @@ -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, +};