From c9d4c091f453f5ba0a031818d526906bc62cb547 Mon Sep 17 00:00:00 2001 From: NorthernMan54 <19808920+NorthernMan54@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:48:47 -0500 Subject: [PATCH] Working --- src/lib/alexaActions.js | 4 +- src/lib/parse/messages.js | 1 + src/lib/parse/messages.test.ts | 163 +++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/lib/parse/messages.test.ts diff --git a/src/lib/alexaActions.js b/src/lib/alexaActions.js index ea49d73..3ea7ed9 100644 --- a/src/lib/alexaActions.js +++ b/src/lib/alexaActions.js @@ -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)); @@ -1066,7 +1066,7 @@ function reportDeviceError(message) { "payload": { "endpoints": [ { - "endpointId": message.endpoint.endpointId, + "endpointId": message.directive.endpoint.endpointId, } ], "scope": { diff --git a/src/lib/parse/messages.js b/src/lib/parse/messages.js index 2412da7..d169666 100644 --- a/src/lib/parse/messages.js +++ b/src/lib/parse/messages.js @@ -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({ diff --git a/src/lib/parse/messages.test.ts b/src/lib/parse/messages.test.ts new file mode 100644 index 0000000..334da21 --- /dev/null +++ b/src/lib/parse/messages.test.ts @@ -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([]); + }); +}); \ No newline at end of file