Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
NorthernMan54 committed Dec 3, 2024
1 parent eac10a3 commit c9d4c09
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/alexaActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ async function processStatusArray(statusArray, message) {

return (alexaMessages.alexaStateResponse(resultArray, message));
} catch (err) {
if(this.deviceCleanup) {
if (this.deviceCleanup) {
reportDeviceError(message);
}
return (alexaMessages.alexaStateResponse(err, message));
Expand Down Expand Up @@ -1066,7 +1066,7 @@ function reportDeviceError(message) {
"payload": {
"endpoints": [
{
"endpointId": message.endpoint.endpointId,
"endpointId": message.directive.endpoint.endpointId,
}
],
"scope": {
Expand Down
1 change: 1 addition & 0 deletions src/lib/parse/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function stateToProperties(statusObject, hbResponse) {
// Convert each individual HAP hbResponse to Alexa Format
statusObject.elements.forEach((element, i) => {
// debug("stateToProperties - element", element, i, hbResponse[i]);

switch (element.interface.toLowerCase()) {
case "alexa.speaker":
properties.push({
Expand Down
163 changes: 163 additions & 0 deletions src/lib/parse/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const { stateToProperties } = require('/Users/sgracey/Code/homebridge-alexa/src/lib/parse/messages');

describe('stateToProperties', () => {
const now = new Date();
const mockDate = now.toISOString();

beforeAll(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(now);
});

afterAll(() => {
jest.useRealTimers();
});

test('should convert Alexa.PowerController response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.PowerController', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: true }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.PowerController',
name: 'powerState',
value: 'ON',
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should convert Alexa.PowerController response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.PowerController', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: true }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.PowerController',
name: 'powerState',
value: 'ON',
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should convert Alexa.TemperatureSensor error response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.TemperatureSensor', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 123, iid: 10, status: -70410 }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.TemperatureSensor',
name: 'temperature',
value: {
"scale": "CELSIUS",
"value": 25,
},
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should convert Alexa.ColorController response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.ColorController', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: true }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.ColorController',
name: 'temperature',
value: {
"scale": "CELSIUS",
"value": 25,
},
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should convert Alexa.Speaker response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.Speaker', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: 50 }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([
{
namespace: 'Alexa.Speaker',
name: 'volume',
value: 50,
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
},
{
namespace: 'Alexa.Speaker',
name: 'muted',
value: false,
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}
]);
});

test('should convert Alexa.ColorTemperatureController response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.ColorTemperatureController', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: 5000 }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.ColorTemperatureController',
name: 'colorTemperatureInKelvin',
value: 200,
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should convert Alexa.TemperatureSensor response correctly', () => {
const statusObject = {
elements: [{ interface: 'Alexa.TemperatureSensor', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: 22.5 }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([{
namespace: 'Alexa.TemperatureSensor',
name: 'temperature',
value: {
value: 22.5,
scale: 'CELSIUS'
},
timeOfSample: mockDate,
uncertaintyInMilliseconds: 500
}]);
});

test('should handle unknown interface gracefully', () => {
const statusObject = {
elements: [{ interface: 'Unknown.Interface', aid: 101, iid: 10 }]
};
const hbResponse = [{ aid: 101, iid: 10, value: 100 }];

const result = stateToProperties(statusObject, hbResponse);

expect(result).toEqual([]);
});
});

0 comments on commit c9d4c09

Please sign in to comment.