forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Philips Hue sync with bridge button (GladysAssistant#2063)
- Loading branch information
1 parent
128f7e4
commit 59e4494
Showing
11 changed files
with
149 additions
and
4 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
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
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
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
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
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
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
30 changes: 30 additions & 0 deletions
30
server/services/philips-hue/lib/light/light.syncWithBridge.js
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,30 @@ | ||
const Promise = require('bluebird'); | ||
const { NotFoundError } = require('../../../../utils/coreErrors'); | ||
const logger = require('../../../../utils/logger'); | ||
|
||
const { getDeviceParam } = require('../../../../utils/device'); | ||
|
||
const { BRIDGE_SERIAL_NUMBER } = require('../utils/consts'); | ||
|
||
/** | ||
* @description Re-sync with bridge. | ||
* @returns {Promise} Resolve when sync is finished. | ||
* @example | ||
* syncWithBridge(); | ||
*/ | ||
async function syncWithBridge() { | ||
logger.info(`Philips Hue: syncWithBridge`); | ||
await Promise.map(this.connnectedBridges, async (device) => { | ||
const serialNumber = getDeviceParam(device, BRIDGE_SERIAL_NUMBER); | ||
const hueApi = this.hueApisBySerialNumber.get(serialNumber); | ||
if (!hueApi) { | ||
throw new NotFoundError(`HUE_API_NOT_FOUND`); | ||
} | ||
logger.info(`Philips Hue: Syncing with bridge ${serialNumber}`); | ||
await hueApi.syncWithBridge(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
syncWithBridge, | ||
}; |
19 changes: 19 additions & 0 deletions
19
server/test/services/philips-hue/controllers/syncWithBridge.controller.test.js
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,19 @@ | ||
const { assert, fake } = require('sinon'); | ||
const PhilipsHueControllers = require('../../../../services/philips-hue/api/hue.controller'); | ||
|
||
const philipsHueLightService = { | ||
syncWithBridge: fake.resolves({}), | ||
}; | ||
|
||
const res = { | ||
json: fake.returns(null), | ||
}; | ||
|
||
describe('POST /service/philips-hue/bridge/sync', () => { | ||
it('should sync bridge', async () => { | ||
const philipsHueController = PhilipsHueControllers(philipsHueLightService); | ||
const req = {}; | ||
await philipsHueController['post /api/v1/service/philips-hue/bridge/sync'].controller(req, res); | ||
assert.called(philipsHueLightService.syncWithBridge); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
server/test/services/philips-hue/light/light.syncWithBridge.test.js
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,45 @@ | ||
const { assert, fake } = require('sinon'); | ||
const chaiAssert = require('chai').assert; | ||
const EventEmitter = require('events'); | ||
const proxyquire = require('proxyquire').noCallThru(); | ||
const { MockedPhilipsHueClient, hueApi } = require('../mocks.test'); | ||
|
||
const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', { | ||
'node-hue-api': MockedPhilipsHueClient, | ||
}); | ||
|
||
const StateManager = require('../../../../lib/state'); | ||
const ServiceManager = require('../../../../lib/service'); | ||
const DeviceManager = require('../../../../lib/device'); | ||
const Job = require('../../../../lib/job'); | ||
|
||
const event = new EventEmitter(); | ||
const stateManager = new StateManager(event); | ||
const job = new Job(event); | ||
const serviceManager = new ServiceManager({}, stateManager); | ||
const brain = { | ||
addNamedEntity: fake.returns(null), | ||
}; | ||
const deviceManager = new DeviceManager(event, {}, stateManager, serviceManager, {}, {}, job, brain); | ||
|
||
const gladys = { | ||
device: deviceManager, | ||
}; | ||
|
||
describe('PhilipsHueService', () => { | ||
it('should sync with bridge', async () => { | ||
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279'); | ||
await philipsHueService.device.getBridges(); | ||
await philipsHueService.device.configureBridge('192.168.1.10'); | ||
await philipsHueService.device.syncWithBridge(); | ||
assert.called(hueApi.syncWithBridge); | ||
}); | ||
it('should reject (error with Api not found)', async () => { | ||
const philipsHueService = PhilipsHueService(gladys, 'a810b8db-6d04-4697-bed3-c4b72c996279'); | ||
await philipsHueService.device.getBridges(); | ||
await philipsHueService.device.configureBridge('192.168.1.10'); | ||
philipsHueService.device.hueApisBySerialNumber = new Map(); | ||
const promise = philipsHueService.device.syncWithBridge(); | ||
return chaiAssert.isRejected(promise); | ||
}); | ||
}); |
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