Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[philips-hue] Answer message when no lights found in a room #965

Merged
merged 6 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/config/brain/light/answers.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
{
"label": "light.turn-off.fail",
"answers": ["I didn't manage to turn off the light."]
},
{
"label": "light.not-found",
"answers": ["I didn't find any light in this room."]
}
]
4 changes: 4 additions & 0 deletions server/config/brain/light/answers.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
{
"label": "light.turn-off.fail",
"answers": ["Je n'ai pas réussi à éteindre la lumière."]
},
{
"label": "light.not-found",
"answers": ["Je n'ai pas trouvé de lumière dans cette pièce."]
Pierre-Gilles marked this conversation as resolved.
Show resolved Hide resolved
}
]
14 changes: 12 additions & 2 deletions server/lib/device/light/light.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../ut
* light.command(message, classification, context);
*/
async function command(message, classification, context) {
let nbLightsAffected = 0;
cicoub13 marked this conversation as resolved.
Show resolved Hide resolved
try {
const devices = await this.getLightsInRoom(context.room);
switch (classification.intent) {
Expand All @@ -28,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 @@ -44,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 @@ -55,6 +62,9 @@ async function command(message, classification, context) {
logger.debug(e);
this.messageManager.replyByIntent(message, 'light.turn-on.fail', context);
}
if (nbLightsAffected === 0) {
Pierre-Gilles marked this conversation as resolved.
Show resolved Hide resolved
this.messageManager.replyByIntent(message, 'light.not-found', context);
}
}

module.exports = {
Expand Down
23 changes: 23 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: () => {
return null;
},
};

const service = {
getService: () => testService,
};
Expand Down Expand Up @@ -65,4 +71,21 @@ 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);
// Mock getLightsInRoom to answer no devices
deviceManager.lightManager.getLightsInRoom = () =>
new Promise((resolve) => {
resolve([]);
});
await deviceManager.lightManager.command(message, { intent: 'light.turn-off' }, context);
assert.calledWith(messageManager.replyByIntent, message, 'light.not-found', context);
});
});