homebridge-alexa 0.8.0-beta.8 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. This project uses [Semantic Versioning](https://semver.org/).
4
4
 
5
- ## 0.8.0 (2024-09-24)
5
+ ## 0.8.0 (2024-12-24)
6
6
 
7
- ## [Version 0.7.1](https://github.com/northernman54/homebridge-alexa/compare/v0.7.1...v0.8.0)
7
+ ## [Version 0.8.0](https://github.com/northernman54/homebridge-alexa/compare/v0.7.1...v0.8.0)
8
8
 
9
9
  #### Changes
10
10
 
11
- - Simplified initial configuration
11
+ - Changed behaviour for missing devices, and have updated the plugin inform Alexa about some missing devices so they can be automatically deleted. Feature is enabled with the config option `deviceCleanup`. Currently only removes devices when it detects that the homebridge instance / child bridge associated with the device is no longer available.
12
+
13
+ - Improved performance on `Alexa turn off all the lights`
12
14
 
13
15
  ## 0.7.1 (2024-09-24)
14
16
 
@@ -62,6 +62,12 @@
62
62
  "placeholder": false,
63
63
  "description": "Enable beta test environment. Only available by invitation."
64
64
  },
65
+ "deviceCleanup": {
66
+ "title": "<b>Cleanup of Not Responding Devices</b>",
67
+ "type": "boolean",
68
+ "default": true,
69
+ "description": "Enables cleanup of devices that are not responding. This will remove devices from Alexa that are not responding. Defaults to true."
70
+ },
65
71
  "refresh": {
66
72
  "title": "<b>Accessory Cache Refresh Interval</b>",
67
73
  "type": "integer",
@@ -255,6 +261,7 @@
255
261
  "items": [
256
262
  "pin",
257
263
  "routines",
264
+ "deviceCleanup",
258
265
  "debug",
259
266
  "thermostatTurnOn",
260
267
  "enhancedSkip",
@@ -0,0 +1,34 @@
1
+ /** @type {import('eslint').FlatConfig[]} */
2
+ import pluginJs from "@eslint/js";
3
+ import pluginJest from "eslint-plugin-jest";
4
+ import globals from "globals";
5
+
6
+
7
+ export default [
8
+ {
9
+ files: ["**/*.js"],
10
+ languageOptions: {
11
+ sourceType: "commonjs", // This is necessary to parse imports/exports
12
+ globals: {
13
+ ...globals.browser,
14
+ ...globals.es2021,
15
+ ...globals.jest, // Add Jest globals
16
+ },
17
+ },
18
+ // Add any other specific rules here
19
+ },
20
+ pluginJs.configs.recommended,
21
+ {
22
+ plugins: {
23
+ jest: pluginJest,
24
+ },
25
+ rules: {
26
+ ...pluginJest.configs.recommended.rules,
27
+ "no-unused-vars": "warn", // Change no-unused-vars to a warning
28
+ },
29
+ },
30
+ {
31
+ // Exclude test, tools, and lib directories from linting
32
+ ignores: ["test/**/*", "tools/**/*js"], // Exclude these files from linting
33
+ }
34
+ ];
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
2
  "name": "homebridge-alexa",
3
- "version": "0.8.0-beta.8",
3
+ "version": "0.8.0",
4
4
  "description": "Control your Homebridge devices with Amazon Alexa.",
5
- "main": "plugin.js",
5
+ "main": "src/plugin.js",
6
6
  "scripts": {
7
7
  "test": "jest --forceExit --detectOpenHandles --verbose=true --silent ",
8
8
  "api": "documentation build plugin.js lib/alexaActions.js lib/alexaLocal.js lib/alexaMessages.js lib/alexaTranslator.js -f md --config docs/documentation.yml > docs/API.md",
9
9
  "document": "./gh-md-toc --hide-footer README.md ;./gh-md-toc --hide-footer Troubleshooting.md ;./gh-md-toc --hide-footer Installation.md; rm README.md.orig.* README.md.toc.* Troubleshooting.md.orig.* Troubleshooting.md.toc.* Installation.md.orig.* Installation.md.toc.*",
10
- "watch": "nodemon"
10
+ "watch": "nodemon",
11
+ "lint": "eslint --max-warnings=10 .",
12
+ "lint:fix": "eslint --fix --max-warnings=0 ."
11
13
  },
12
14
  "nodemonConfig": {
13
15
  "watch": [],
14
16
  "ext": "js",
15
17
  "ignore": [],
16
- "exec": "~/npm/bin/homebridge -D -R -P ~/Code",
18
+ "exec": "~/npm/bin/homebridge -U ./test/hbConfig -T -D -P .",
17
19
  "signal": "SIGTERM",
18
20
  "env": {
19
21
  "NODE_OPTIONS": "--trace-warnings"
@@ -32,26 +34,28 @@
32
34
  "homebridge-plugin"
33
35
  ],
34
36
  "devDependencies": {
35
- "@oznu/hap-client": "^1.9.0",
36
- "@types/jest": "^29.5.10",
37
- "@types/node": "^22.7.0",
37
+ "@homebridge/hap-client": "^2.0.0",
38
+ "@types/node": "^22.10.2",
39
+ "@types/jest": "^29.5.14",
40
+ "@eslint/js": "^9.14.0",
41
+ "eslint": "^8.57.1",
42
+ "eslint-plugin-format": "^0.1.2",
43
+ "eslint-plugin-jest": "^28.8.3",
38
44
  "babel-jest": "^29.7.0",
45
+ "globals": "^15.12.0",
39
46
  "jest": "^29.7.0",
40
47
  "mqtt-connection": "^4.1.0",
41
- "nodemon": "^3.1.4",
48
+ "nodemon": "^3.1.9",
42
49
  "rimraf": "^6.0.1",
43
- "semver": "^7.6.3",
44
- "ts-jest": "^29.2.4",
45
- "ts-node": "^10.9.1",
46
- "typescript": "^5.5.4",
47
- "ws": "^8.14.2"
50
+ "semver": "^7.6.3"
48
51
  },
49
52
  "dependencies": {
53
+ "axios": "^1.7.9",
50
54
  "bottleneck": "2.19.5",
51
- "debug": "4.3.7",
55
+ "debug": "4.4.0",
52
56
  "hap-node-client": "^0.2.9",
53
57
  "is-my-json-valid": "2.20.6",
54
- "mqtt": "5.10.1"
58
+ "mqtt": "5.10.3"
55
59
  },
56
60
  "author": "NorthernMan54",
57
61
  "license": "Apache-2.0",
@@ -60,26 +64,13 @@
60
64
  },
61
65
  "homepage": "https://github.com/NorthernMan54/homebridge-alexa#readme",
62
66
  "jest": {
63
- "moduleFileExtensions": [
64
- "js",
65
- "json",
66
- "ts"
67
- ],
68
- "rootDir": "./",
69
67
  "testEnvironment": "node",
70
- "testRegex": ".test.ts$",
71
- "testTimeout": 10000,
72
- "transform": {
73
- "^.+\\.(t)s$": "ts-jest"
74
- },
75
- "collectCoverageFrom": [
76
- "**/*.ts"
68
+ "modulePathIgnorePatterns": [],
69
+ "coverageReporters": [
70
+ "lcov"
77
71
  ],
78
- "coveragePathIgnorePatterns": [
79
- "/ui/",
80
- "/test/",
81
- "/dist/",
82
- "/scripts/"
72
+ "collectCoverageFrom": [
73
+ "src/**"
83
74
  ]
84
75
  }
85
- }
76
+ }
@@ -26,7 +26,8 @@ module.exports = {
26
26
  alexaChannelController: alexaChannelController,
27
27
  alexaInputController: alexaInputController,
28
28
  alexaRangeController: alexaRangeController,
29
- alexaModeController: alexaModeController
29
+ alexaModeController: alexaModeController,
30
+ destroy: destroy
30
31
  };
31
32
 
32
33
  function hapDiscovery(options) {
@@ -48,6 +49,10 @@ function hapDiscovery(options) {
48
49
  // debug("Event Relay - 1", homebridge);
49
50
  }
50
51
 
52
+ function destroy() {
53
+ homebridge.destroy();
54
+ }
55
+
51
56
  function registerEvents(message) {
52
57
  // debug("registerEvents", message);
53
58
 
@@ -116,7 +121,7 @@ function alexaDiscovery(message, callback) {
116
121
  }
117
122
 
118
123
  if (response && response.event.payload.endpoints.length < 1) {
119
- this.log("ERROR: HAP Discovery failed, please review config");
124
+ this.log.error("ERROR: HAP Discovery failed, please review config");
120
125
  } else {
121
126
  if (process.uptime() > 600) { // Only use console during startup
122
127
  debug("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
@@ -135,10 +140,11 @@ function alexaDiscovery(message, callback) {
135
140
  this.log("ERROR: Maximum devices/accessories of 300 exceeded.");
136
141
  }
137
142
  }
138
- // debug("Discovery Response", JSON.stringify(response, null, 2));
139
143
  if (this.debug) {
140
144
  const fs = require('fs');
141
- fs.writeFileSync('alexaDiscovery.json', JSON.stringify(response, null, 2));
145
+ const storagePath = this.api.user.storagePath() + '/homebridge-alexa-discovery.json';
146
+ this.log.warn("Writing Alexa Discovery Response to", storagePath);
147
+ fs.writeFileSync(storagePath, JSON.stringify(response, null, 2));
142
148
  }
143
149
  callback(null, response);
144
150
  }.bind(this));
@@ -260,7 +266,13 @@ function alexaColorTemperatureController(message, callback) {
260
266
  }]
261
267
  };
262
268
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
263
- this.log("ColorTemperatureController-set", action, haAction.deviceID, status, body, err);
269
+ if (err) {
270
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
271
+ } else if (status) {
272
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
273
+ } else {
274
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
275
+ }
264
276
  var response = alexaMessages.alexaResponse(message, status, err, message.directive.payload.colorTemperatureInKelvin);
265
277
  callback(err, response);
266
278
  }.bind(this));
@@ -287,7 +299,13 @@ function alexaPlaybackController(message, callback) {
287
299
  }]
288
300
  };
289
301
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
290
- this.log("PlaybackController", action, haAction.deviceID, status, err);
302
+ if (err) {
303
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
304
+ } else if (status) {
305
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
306
+ } else {
307
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
308
+ }
291
309
  var response = alexaMessages.alexaResponse(message, status, err);
292
310
  callback(err, response);
293
311
  }.bind(this));
@@ -312,7 +330,13 @@ function alexaInputController(message, callback) {
312
330
  }]
313
331
  };
314
332
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
315
- this.log("InputController", action, haAction.deviceID, status, err);
333
+ if (err) {
334
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
335
+ } else if (status) {
336
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
337
+ } else {
338
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
339
+ }
316
340
  var response = alexaMessages.alexaResponse(message, status, err);
317
341
  callback(err, response);
318
342
  }.bind(this));
@@ -337,7 +361,13 @@ function alexaPowerController(message, callback) {
337
361
  }]
338
362
  };
339
363
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
340
- this.log("PowerController", action, haAction.deviceID, status, err);
364
+ if (err) {
365
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
366
+ } else if (status) {
367
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
368
+ } else {
369
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
370
+ }
341
371
  var response = alexaMessages.alexaResponse(message, status, err);
342
372
  callback(err, response);
343
373
  }.bind(this));
@@ -378,7 +408,13 @@ function alexaModeController(message, callback) {
378
408
  }]
379
409
  };
380
410
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
381
- this.log("ModeController", action, haAction.deviceID, status, err);
411
+ if (err) {
412
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
413
+ } else if (status) {
414
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
415
+ } else {
416
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
417
+ }
382
418
  var response = alexaMessages.alexaResponse(message, status, err);
383
419
  callback(err, response);
384
420
  }.bind(this));
@@ -403,7 +439,13 @@ function alexaLockController(message, callback) {
403
439
  }]
404
440
  };
405
441
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
406
- this.log("alexaLockController", action, haAction.deviceID, status, err);
442
+ if (err) {
443
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
444
+ } else if (status) {
445
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
446
+ } else {
447
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
448
+ }
407
449
  var response = alexaMessages.alexaResponse(message, status, err);
408
450
  callback(err, response);
409
451
  }.bind(this));
@@ -468,7 +510,13 @@ function alexaChannelController(message, callback) {
468
510
  // debug("alexaChannelController", JSON.stringify(message), JSON.stringify(body));
469
511
 
470
512
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
471
- this.log("alexaChannelController", channel, haAction.deviceID, status, err);
513
+ if (err) {
514
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
515
+ } else if (status) {
516
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
517
+ } else {
518
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
519
+ }
472
520
  var response = alexaMessages.alexaResponse(message, status, err);
473
521
  callback(err, response);
474
522
  }.bind(this));
@@ -553,7 +601,13 @@ async function alexaThermostatController(message, callback) {
553
601
  "characteristics": characteristics
554
602
  };
555
603
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
556
- this.log("ThermostatController", action, haAction.deviceID, status, err);
604
+ if (err) {
605
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
606
+ } else if (status) {
607
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
608
+ } else {
609
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
610
+ }
557
611
  var response = alexaMessages.alexaResponse(message, status, err);
558
612
  callback(err, response);
559
613
  }.bind(this));
@@ -622,7 +676,13 @@ function alexaColorController(message, callback) {
622
676
  };
623
677
  debug("color HB command", body);
624
678
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
625
- this.log("ColorController", action, haAction.deviceID, status, err);
679
+ if (err) {
680
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
681
+ } else if (status) {
682
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
683
+ } else {
684
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
685
+ }
626
686
  var response = alexaMessages.alexaResponse(message, status, err);
627
687
  callback(err, response);
628
688
  }.bind(this));
@@ -677,7 +737,13 @@ function alexaRangeController(message, callback) {
677
737
  }]
678
738
  };
679
739
  homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function (err, status) {
680
- this.log("RangeController", action, haAction.deviceID, status, body, err);
740
+ if (err) {
741
+ this.log.error(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""), (err ? "ERROR: " + err : ""));
742
+ } else if (status) {
743
+ this.log.warn(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
744
+ } else {
745
+ this.log(message.directive.header.namespace, action, haAction.deviceID, message.directive.endpoint.endpointId, (status ? status : ""));
746
+ }
681
747
  var response = alexaMessages.alexaResponse(message, status, err, rangeValue);
682
748
  callback(err, response);
683
749
  }.bind(this));
@@ -863,7 +929,6 @@ function alexaStepSpeaker(message, callback) {
863
929
  }
864
930
 
865
931
  function alexaMessage(message, callback) {
866
- debug("alexaMessage:", JSON.stringify(message));
867
932
  switch (message.directive.header.name.toLowerCase()) {
868
933
  case "reportstate": // aka getStatus
869
934
  var action = message.directive.header.name;
@@ -932,9 +997,8 @@ function alexaMessage(message, callback) {
932
997
  // For performance HAP GET Characteristices supports getting multiple in one call
933
998
  // debug("alexaMessage - statusArray", statusArray);
934
999
 
935
- processStatusArray(statusArray, message).then(response => {
936
- debug("alexaMessage: Response", JSON.stringify(response,
937
- null, 2));
1000
+ processStatusArray.call(this, statusArray, message).then(response => {
1001
+ debug("alexaMessage:Response:", JSON.stringify(response));
938
1002
  callback(null, response);
939
1003
  }).catch(err => {
940
1004
  debug("alexaMessage:Error:", err.message);
@@ -959,7 +1023,8 @@ function alexaMessage(message, callback) {
959
1023
  "name": "ErrorResponse",
960
1024
  "namespace": "Alexa",
961
1025
  "payloadVersion": "3",
962
- "messageId": message.directive.header.messageId
1026
+ "messageId": message.directive.header.messageId,
1027
+ "correlationToken": message.directive.header.correlationToken,
963
1028
  },
964
1029
  "payload": {
965
1030
  "endpoints": []
@@ -982,21 +1047,25 @@ async function processStatusArray(statusArray, message) {
982
1047
  // debug("processStatusArray", messageArray);
983
1048
  var resultArray = await Promise.all(messageArray);
984
1049
 
1050
+ // debug("processStatusArray-2", JSON.stringify(resultArray));
985
1051
  return (alexaMessages.alexaStateResponse(resultArray, message));
986
1052
  } catch (err) {
1053
+ if (this.deviceCleanup) {
1054
+ reportDeviceError(message);
1055
+ }
987
1056
  return (alexaMessages.alexaStateResponse(err, message));
988
1057
  }
989
1058
  }
990
1059
 
991
1060
  function _HAPstatusByDeviceID(statusObject, message) {
992
- debug("_HAPstatusByDeviceID-1", statusObject);
1061
+ // debug("_HAPstatusByDeviceID-1", JSON.stringify(statusObject));
993
1062
  return new Promise((resolve, reject) => {
994
1063
  homebridge.HAPstatusByDeviceID(statusObject.deviceID, statusObject.body, function (err, status) {
995
1064
  if (err) {
996
1065
  // debug("Error: _HAPstatusByDeviceID", err);
997
1066
  reject(err);
998
1067
  } else {
999
- debug("_HAPstatusByDeviceID-2", statusObject.deviceID, status);
1068
+ // debug("_HAPstatusByDeviceID-2", statusObject.deviceID, JSON.stringify(status));
1000
1069
  resolve(messages.stateToProperties(statusObject, status.characteristics));
1001
1070
  }
1002
1071
  });
@@ -1046,6 +1115,30 @@ function alexaEvent(events) {
1046
1115
  });
1047
1116
  }
1048
1117
 
1118
+ function reportDeviceError(message) {
1119
+ alexaLocal.alexaEvent({
1120
+ "event": {
1121
+ "header": {
1122
+ "namespace": "Alexa.Discovery",
1123
+ "name": "DeleteReport",
1124
+ "messageId": messages.createMessageId(),
1125
+ "payloadVersion": "3"
1126
+ },
1127
+ "payload": {
1128
+ "endpoints": [
1129
+ {
1130
+ "endpointId": message.directive.endpoint.endpointId,
1131
+ }
1132
+ ],
1133
+ "scope": {
1134
+ "type": "BearerToken",
1135
+ "token": "OAuth2.0 bearer token"
1136
+ }
1137
+ }
1138
+ }
1139
+ });
1140
+ };
1141
+
1049
1142
  /*
1050
1143
 
1051
1144
  Utility functions
@@ -0,0 +1,95 @@
1
+ const { processStatusArray } = require('/Users/sgracey/Code/homebridge-alexa/src/lib/alexaActions.js');
2
+ const alexaMessages = require('/Users/sgracey/Code/homebridge-alexa/src/lib/alexaMessages.js');
3
+ const homebridge = require('hap-node-client').HAPNodeJSClient;
4
+
5
+ jest.mock('hap-node-client');
6
+ jest.mock('/Users/sgracey/Code/homebridge-alexa/src/lib/alexaMessages.js');
7
+
8
+ describe('processStatusArray', () => {
9
+ let statusArray;
10
+ let message;
11
+
12
+ beforeEach(() => {
13
+ statusArray = [
14
+ {
15
+ deviceID: 'device1',
16
+ body: '?id=1.1',
17
+ interface: 'Alexa.PowerController',
18
+ spacer: ',',
19
+ elements: [
20
+ { interface: 'Alexa.PowerController', aid: 1, iid: 1 }
21
+ ]
22
+ },
23
+ {
24
+ deviceID: 'device2',
25
+ body: '?id=2.1',
26
+ interface: 'Alexa.BrightnessController',
27
+ spacer: ',',
28
+ elements: [
29
+ { interface: 'Alexa.BrightnessController', aid: 2, iid: 1 }
30
+ ]
31
+ }
32
+ ];
33
+ message = { directive: { header: { messageId: '123' } } };
34
+
35
+ homebridge.HAPstatusByDeviceID.mockImplementation((deviceID, body, callback) => {
36
+ if (deviceID === 'device1') {
37
+ callback(null, { characteristics: [{ aid: 1, iid: 1, value: true }] });
38
+ } else if (deviceID === 'device2') {
39
+ callback(null, { characteristics: [{ aid: 2, iid: 1, value: 50 }] });
40
+ } else {
41
+ callback(new Error('Device not found'));
42
+ }
43
+ });
44
+
45
+ alexaMessages.alexaStateResponse.mockImplementation((resultArray, message) => {
46
+ return { event: { header: { messageId: message.directive.header.messageId }, payload: { properties: resultArray } } };
47
+ });
48
+ });
49
+
50
+ test.skip('should process status array successfully', async () => {
51
+ const result = await processStatusArray(statusArray, message);
52
+ expect(result).toEqual({
53
+ event: {
54
+ header: { messageId: '123' },
55
+ payload: {
56
+ properties: [
57
+ { interface: 'Alexa.PowerController', aid: 1, iid: 1, value: true },
58
+ { interface: 'Alexa.BrightnessController', aid: 2, iid: 1, value: 50 }
59
+ ]
60
+ }
61
+ }
62
+ });
63
+ });
64
+
65
+ test.skip('should handle errors during processing', async () => {
66
+ homebridge.HAPstatusByDeviceID.mockImplementationOnce((deviceID, body, callback) => {
67
+ callback(new Error('Device not found'));
68
+ });
69
+
70
+ const result = await processStatusArray(statusArray, message);
71
+ expect(result).toEqual({
72
+ event: {
73
+ header: { messageId: '123' },
74
+ payload: {
75
+ properties: [
76
+ { interface: 'Alexa.PowerController', aid: 1, iid: 1, value: true },
77
+ { interface: 'Alexa.BrightnessController', aid: 2, iid: 1, value: 50 }
78
+ ]
79
+ }
80
+ }
81
+ });
82
+ });
83
+
84
+ test.skip('should return correct response format', async () => {
85
+ const result = await processStatusArray(statusArray, message);
86
+ expect(result).toHaveProperty('event.header.messageId', '123');
87
+ expect(result).toHaveProperty('event.payload.properties');
88
+ expect(result.event.payload.properties).toHaveLength(2);
89
+ });
90
+
91
+ afterAll(() => {
92
+ jest.clearAllMocks();
93
+ homebridge.destroy();
94
+ });
95
+ });