Skip to content

Commit

Permalink
Alexa: Fix execute on multiple device feature + reject when device is…
Browse files Browse the repository at this point in the history
… not found (#1536)
  • Loading branch information
Pierre-Gilles authored May 20, 2022
1 parent 0525ac2 commit 50b42a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
12 changes: 10 additions & 2 deletions server/services/alexa/lib/alexa.onExecute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const uuid = require('uuid');
const get = require('get-value');
const { EVENTS, ACTIONS, ACTIONS_STATUS, DEVICE_FEATURE_CATEGORIES } = require('../../../utils/constants');
const {
EVENTS,
ACTIONS,
ACTIONS_STATUS,
DEVICE_FEATURE_CATEGORIES,
DEVICE_FEATURE_TYPES,
} = require('../../../utils/constants');
const { BadParameters, NotFoundError } = require('../../../utils/coreErrors');
const { writeValues, readValues } = require('./deviceMappings');

Expand All @@ -25,7 +31,9 @@ function onExecute(body) {
switch (directiveNamespace) {
case 'Alexa.PowerController':
deviceFeature = deviceInMemory.features.find(
(f) => f.category === DEVICE_FEATURE_CATEGORIES.SWITCH || f.category === DEVICE_FEATURE_CATEGORIES.LIGHT,
(f) =>
(f.category === DEVICE_FEATURE_CATEGORIES.SWITCH || f.category === DEVICE_FEATURE_CATEGORIES.LIGHT) &&
f.type === DEVICE_FEATURE_TYPES.LIGHT.BINARY,
);
value = writeValues['Alexa.PowerController'](directiveName);
break;
Expand Down
4 changes: 4 additions & 0 deletions server/services/alexa/lib/alexa.onReportState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const uuid = require('uuid');
const get = require('get-value');

const { mappings, readValues } = require('./deviceMappings');
const { NotFoundError } = require('../../../utils/coreErrors');

/**
* @public
Expand All @@ -13,6 +14,9 @@ const { mappings, readValues } = require('./deviceMappings');
function onReportState(body) {
const deviceSelector = get(body, 'directive.endpoint.endpointId');
const device = this.gladys.stateManager.get('device', deviceSelector);
if (!device) {
throw new NotFoundError(`Device "${deviceSelector}" not found`);
}
const properties = [];
const now = new Date().toISOString();
device.features.forEach((feature) => {
Expand Down
4 changes: 4 additions & 0 deletions server/test/services/alexa/lib/alexa.onExecute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const DEVICE_1_LIGHT = {
selector: 'device-1',
external_id: 'device-1-external-id',
features: [
{
category: 'light',
type: 'brightness',
},
{
category: 'light',
type: 'binary',
Expand Down
27 changes: 27 additions & 0 deletions server/test/services/alexa/lib/alexa.onReportState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,31 @@ describe('alexa.onReportState', () => {
},
});
});
it('Should return device not found', async () => {
const gladys = {
stateManager: {
get: fake.returns(null),
},
};

const alexaHandler = new AlexaHandler(gladys, serviceId);
const body = {
directive: {
header: {
namespace: 'Alexa',
name: 'ReportState',
payloadVersion: '3',
messageId: 'a05c8249-1cdd-41dd-bc1d-5a14ab4b98eb',
correlationToken:
'AAAAAAAAAQBe8ATzt+PzWVqbUXXQAv6JAAIAAAAAAADgCDHXLLn3nx8SmjtElD2w8CfsniSH6KxFhbRSgD/sELuMpZTr4Jl/E3Nip62gpI2QqFNm/TrQ/Pi+XSFtf/4AVCDxe4bV2FAXSVu61AsuUlhbdqdvjUoaHOuqSLW8F3Qj9z3HWhfvTCMEbbhw4XVDWOsyXb9nknvswimA+R4ftNdBx5POWZGxWtbvU+yeBStTV+QwSSZaHWjzQdi/LAo1KW35MkmLikny7Y7J097LTTL1Tof6IkLsi9/gxOtUUFvnD4yIkWeHTT110Ch6R4kDuonNtOiHsTmMMRtsY5kRWoIL9VMfX6QHWjamhvd+XJp4sXkLMBdtJ3aTzfsUNrQIdrcPTox9qTNjShunTlbAYkq1TSUXaylEGHvcwHrbo7ZoUlBvidqnJGUNRJPxOHHyfCm5VqFzuFI8AG1W/dj1W4Di0AAND/mwzjZKUTRsiX4uEaRw8/Na4Qj/GBMuT18hUoGpe7t/UYw5JFw+MXm0kn/5jKe9r62xil3TN8BK9ODQDP9zq08+iiT0CBtEX5F4Drrowb57IwcW7nt/hkCeeyR59B/Z6nPsSq0NQ+rd1w4a1iHIyaTU6acQsKwmaX1OeTvtT2p7U/HhqfhVMSqA7ybGhQDF4FPPzIbh+o+D1S+AX9m9nVSSJNwoevikdZimCbk1l1HmUrhz78GO+j0yFg==',
},
endpoint: { endpointId: 'device-1' },
payload: {},
},
user: { id: 'cbd42dc1-1b15-4c59-bea6-7e01968a9603', local_user_id: '275faa00-8a9c-4747-8fbe-417ddb966b16' },
};
expect(() => {
alexaHandler.onReportState(body);
}).to.throw('Device "device-1" not found');
});
});

0 comments on commit 50b42a1

Please sign in to comment.