From b89df44775501ea840b3cb0b8bdf43046e2f7808 Mon Sep 17 00:00:00 2001 From: atrovato <1839717+atrovato@users.noreply.github.com> Date: Thu, 19 Oct 2023 18:02:21 +0200 Subject: [PATCH] Fix missing device --- .../lib/smarthome/googleActions.onExecute.js | 2 +- .../lib/smarthome/googleActions.onQuery.js | 9 ++- .../smarthome/googleActions.onExecute.test.js | 1 + .../smarthome/googleActions.onQuery.test.js | 71 +++++++++++++++---- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/server/services/google-actions/lib/smarthome/googleActions.onExecute.js b/server/services/google-actions/lib/smarthome/googleActions.onExecute.js index 374234f09e..48f3e46e9b 100644 --- a/server/services/google-actions/lib/smarthome/googleActions.onExecute.js +++ b/server/services/google-actions/lib/smarthome/googleActions.onExecute.js @@ -31,7 +31,7 @@ async function onExecute(body) { const gladysDevice = this.gladys.stateManager.get('device', selector); if (!gladysDevice) { - commands.push({ ids: [selector], status: 'ERROR' }); + commands.push({ ids: [selector], status: 'ERROR', errorCode: 'deviceNotFound' }); } else { // Each execution triggered await Promise.each(execution, async (exec) => { diff --git a/server/services/google-actions/lib/smarthome/googleActions.onQuery.js b/server/services/google-actions/lib/smarthome/googleActions.onQuery.js index 1913a114f1..b15feb5472 100644 --- a/server/services/google-actions/lib/smarthome/googleActions.onQuery.js +++ b/server/services/google-actions/lib/smarthome/googleActions.onQuery.js @@ -20,9 +20,14 @@ async function onQuery(body, headers) { const gladysDevice = this.gladys.stateManager.get('device', requestedDevice.id); // Convert it to managed Google devices - const device = queryDeviceConverter(gladysDevice); - if (device) { + if (gladysDevice) { + const device = queryDeviceConverter(gladysDevice); devices[gladysDevice.selector] = device; + } else { + devices[requestedDevice.id] = { + errorCode: 'deviceNotFound', + status: 'ERROR', + }; } }); }); diff --git a/server/test/services/google-actions/lib/smarthome/googleActions.onExecute.test.js b/server/test/services/google-actions/lib/smarthome/googleActions.onExecute.test.js index 13424611ee..52041e3471 100644 --- a/server/test/services/google-actions/lib/smarthome/googleActions.onExecute.test.js +++ b/server/test/services/google-actions/lib/smarthome/googleActions.onExecute.test.js @@ -158,6 +158,7 @@ describe('GoogleActions Handler - onExecute', () => { { ids: ['device-1'], status: 'ERROR', + errorCode: 'deviceNotFound', }, ], }, diff --git a/server/test/services/google-actions/lib/smarthome/googleActions.onQuery.test.js b/server/test/services/google-actions/lib/smarthome/googleActions.onQuery.test.js index 89a7524d35..ff0acac95e 100644 --- a/server/test/services/google-actions/lib/smarthome/googleActions.onQuery.test.js +++ b/server/test/services/google-actions/lib/smarthome/googleActions.onQuery.test.js @@ -13,21 +13,7 @@ describe('GoogleActions Handler - onQuery', () => { beforeEach(() => { gladys = { stateManager: { - get: fake.returns({ - name: 'Device 1', - selector: 'device-1', - features: [ - { - category: 'switch', - type: 'binary', - }, - ], - model: 'device-model', - room: { - name: 'living-room', - }, - last_value: 0, - }), + get: fake.returns(null), }, }; }); @@ -37,6 +23,22 @@ describe('GoogleActions Handler - onQuery', () => { }); it('should generate device payload', async () => { + gladys.stateManager.get = fake.returns({ + name: 'Device 1', + selector: 'device-1', + features: [ + { + category: 'switch', + type: 'binary', + }, + ], + model: 'device-model', + room: { + name: 'living-room', + }, + last_value: 0, + }); + const body = { requestId: 'request-id', user: { @@ -74,4 +76,43 @@ describe('GoogleActions Handler - onQuery', () => { expect(result).to.deep.eq(exptectedResult); assert.calledOnceWithExactly(gladys.stateManager.get, 'device', 'device-1'); }); + + it('should generate errorneous device payload', async () => { + const body = { + requestId: 'request-id', + user: { + id: 'user-id', + selector: 'user-selector', + }, + inputs: [ + { + payload: { + devices: [ + { + id: 'device-unknown', + }, + ], + }, + }, + ], + }; + + const googleActionsHandler = new GoogleActionsHandler(gladys, serviceId); + const result = await googleActionsHandler.onQuery(body); + + const exptectedResult = { + requestId: 'request-id', + payload: { + agentUserId: 'user-id', + devices: { + 'device-unknown': { + errorCode: 'deviceNotFound', + status: 'ERROR', + }, + }, + }, + }; + expect(result).to.deep.eq(exptectedResult); + assert.calledOnceWithExactly(gladys.stateManager.get, 'device', 'device-unknown'); + }); });