Skip to content

Commit

Permalink
fix: Take in account PR feedbacks
Browse files Browse the repository at this point in the history
- Do not check number of devices but number of devices with binary features
  • Loading branch information
cicoub13 committed Nov 19, 2020
1 parent 1e3725a commit 980823f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions server/lib/device/light/light.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../ut
* light.command(message, classification, context);
*/
async function command(message, classification, context) {
let nbLightsAffected = 0;
try {
const devices = await this.getLightsInRoom(context.room);
if (devices.length === 0) {
this.messageManager.replyByIntent(message, 'light.not-found', context);
return;
}
switch (classification.intent) {
case 'light.turn-on':
// foreach devices in room
Expand All @@ -32,9 +29,12 @@ async function command(message, classification, context) {
// and if the binary feature exists, call it
if (deviceFeature) {
await this.turnOn(device, deviceFeature);
nbLightsAffected += 1;
}
});
this.messageManager.replyByIntent(message, 'light.turn-on.success', context);
if (nbLightsAffected > 0) {
this.messageManager.replyByIntent(message, 'light.turn-on.success', context);
}
break;
case 'light.turn-off':
// foreach devices in room
Expand All @@ -48,9 +48,12 @@ async function command(message, classification, context) {
// and if the binary feature exists, call it
if (deviceFeature) {
await this.turnOff(device, deviceFeature);
nbLightsAffected += 1;
}
});
this.messageManager.replyByIntent(message, 'light.turn-off.success', context);
if (nbLightsAffected > 0) {
this.messageManager.replyByIntent(message, 'light.turn-off.success', context);
}
break;
default:
throw new Error('Not found');
Expand All @@ -59,6 +62,9 @@ async function command(message, classification, context) {
logger.debug(e);
this.messageManager.replyByIntent(message, 'light.turn-on.fail', context);
}
if (nbLightsAffected === 0) {
this.messageManager.replyByIntent(message, 'light.not-found', context);
}
}

module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions server/test/lib/device/light/light.command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const testServiceBroken = {
},
};

const testServiceNoLight = {
getDeviceFeature: function() {
return null;
},
};

const service = {
getService: () => testService,
};
Expand Down Expand Up @@ -65,6 +71,12 @@ describe('Light.command', () => {
assert.calledWith(messageManager.replyByIntent, message, 'light.turn-off.success', context);
assert.called(testService.device.setValue);
});
it('should fail to send a command because no device with binary feature in this room', async () => {
const stateManager = new StateManager(event);
const deviceManager = new Device(event, messageManager, stateManager, testServiceNoLight);
await deviceManager.lightManager.command(message, { intent: 'light.turn-off' }, context);
assert.calledWith(messageManager.replyByIntent, message, 'light.not-found', context);
});
it('should fail to send a command because no device in this room', async () => {
const stateManager = new StateManager(event);
const deviceManager = new Device(event, messageManager, stateManager, service);
Expand Down

0 comments on commit 980823f

Please sign in to comment.