Skip to content

Commit

Permalink
Fix missing device
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Jan 10, 2024
1 parent 49af7ae commit b89df44
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('GoogleActions Handler - onExecute', () => {
{
ids: ['device-1'],
status: 'ERROR',
errorCode: 'deviceNotFound',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
};
});
Expand All @@ -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: {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit b89df44

Please sign in to comment.