Skip to content

Commit

Permalink
Chat: Custom response when turning on/off the lights and no lights ha…
Browse files Browse the repository at this point in the history
…s been found (GladysAssistant#965)

* fix: Answer message when no lights found in a room

Closes GladysAssistant#959

* fix: Take in account PR feedbacks

- Do not check number of devices but number of devices with binary features

* fix(philips-hue): Use case where exception is thrown and no lights

Co-authored-by: Pierre-Gilles Leymarie <pierregilles.leymarie@gmail.com>
  • Loading branch information
cicoub13 and Pierre-Gilles authored Nov 23, 2020
1 parent fa23f7e commit c2c4c7c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
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."]
}
]
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 nbAffectedLights = 0;
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);
nbAffectedLights += 1;
}
});
this.messageManager.replyByIntent(message, 'light.turn-on.success', context);
if (nbAffectedLights > 0) {
this.messageManager.replyByIntent(message, 'light.turn-on.success', context);
}
break;
case 'light.turn-off':
// foreach devices in room
Expand All @@ -44,13 +48,19 @@ async function command(message, classification, context) {
// and if the binary feature exists, call it
if (deviceFeature) {
await this.turnOff(device, deviceFeature);
nbAffectedLights += 1;
}
});
this.messageManager.replyByIntent(message, 'light.turn-off.success', context);
if (nbAffectedLights > 0) {
this.messageManager.replyByIntent(message, 'light.turn-off.success', context);
}
break;
default:
throw new Error('Not found');
}
if (nbAffectedLights === 0) {
this.messageManager.replyByIntent(message, 'light.not-found', context);
}
} catch (e) {
logger.debug(e);
this.messageManager.replyByIntent(message, 'light.turn-on.fail', context);
Expand Down
28 changes: 28 additions & 0 deletions server/test/lib/device/light/light.command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,32 @@ 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, service);
// Mock getDeviceFeature to answer no binay feature
deviceManager.lightManager.getLightsInRoom = () =>
new Promise((resolve) => {
resolve([
{
device: {
getDeviceFeature: () => null,
},
},
]);
});
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);
});
});

0 comments on commit c2c4c7c

Please sign in to comment.