node-red-contrib-homebridge-automation 0.2.2-beta.5 → 0.3.0-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-homebridge-automation",
3
- "version": "0.2.2-beta.5",
3
+ "version": "0.3.0-beta.0",
4
4
  "description": "NodeRED Automation for HomeBridge",
5
5
  "main": "src/HAP-NodeRed.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  "watch": "nodemon",
9
9
  "lint": "eslint --max-warnings=10 .",
10
10
  "lint:fix": "eslint --fix --max-warnings=0 .",
11
- "test": "jest --detectOpenHandles",
11
+ "test": "jest --forceExit --detectOpenHandles --verbose=true",
12
12
  "test-coverage": "jest --coverage"
13
13
  },
14
14
  "keywords": [
@@ -1,34 +1,35 @@
1
1
  const { HapClient } = require('@homebridge/hap-client');
2
2
  const debug = require('debug')('hapNodeRed:hbConfigNode');
3
+ const fs = require('fs');
4
+ const path = require('path');
3
5
 
4
6
  class HBConfigNode {
5
7
  constructor(config, RED) {
6
- if (!config.jest) {
7
- RED.nodes.createNode(this, config);
8
-
9
- // Initialize properties
10
- this.username = config.username;
11
- this.macAddress = config.macAddress || '';
12
- this.users = {};
13
- this.homebridge = null;
14
- this.evDevices = [];
15
- this.ctDevices = [];
16
- this.hbDevices = [];
17
- this.clientNodes = [];
18
- // this.log = new Log(console, true);
19
- this.discoveryTimeout = null;
20
-
21
- // Initialize HAP client
22
- this.hapClient = new HapClient({
23
- config: { debug: false },
24
- pin: config.username
25
- });
8
+ RED.nodes.createNode(this, config);
9
+
10
+ // Initialize properties
11
+ this.username = config.username;
12
+ this.macAddress = config.macAddress || '';
13
+ this.users = {};
14
+ this.homebridge = null;
15
+ this.evDevices = [];
16
+ this.ctDevices = [];
17
+ this.hbDevices = [];
18
+ this.clientNodes = [];
19
+ // this.log = new Log(console, true);
20
+ this.discoveryTimeout = null;
21
+
22
+ // Initialize HAP client
23
+ this.hapClient = new HapClient({
24
+ config: { debug: false },
25
+ pin: config.username
26
+ });
26
27
 
27
- this.hapClient.on('instance-discovered', this.waitForNoMoreDiscoveries);
28
- this.hapClient.on('discovery-ended', this.hapClient.refreshInstances);
28
+ this.hapClient.on('instance-discovered', this.waitForNoMoreDiscoveries);
29
+ this.hapClient.on('discovery-ended', this.hapClient.refreshInstances);
30
+ if (this.on)
29
31
  this.on('close', this.close.bind(this));
30
- this.refreshInProcess = true; // Prevents multiple refreshes, hapClient kicks of a discovery on start
31
- }
32
+ this.refreshInProcess = true; // Prevents multiple refreshes, hapClient kicks of a discovery on start
32
33
  }
33
34
 
34
35
  /**
@@ -52,9 +53,18 @@ class HBConfigNode {
52
53
  */
53
54
  async handleReady() {
54
55
  const updatedDevices = await this.hapClient.getAllServices();
56
+ if (this.debug && updatedDevices && updatedDevices.length) {
57
+ try {
58
+ const storagePath = path.join(process.cwd(), '/homebridge-automation-endpoints.json');
59
+ this.warn(`Writing Homebridge endpoints to ${storagePath}`);
60
+ fs.writeFileSync(storagePath, JSON.stringify(updatedDevices, null, 2));
61
+ } catch (e) {
62
+ this.error(`Error writing Homebridge endpoints to file: ${e.message}`);
63
+ }
64
+ }
55
65
  // Fix broken uniqueId's from HAP-Client
56
66
  updatedDevices.forEach((service) => {
57
- const friendlyName = (service.serviceName ? service.serviceName : service.accessoryInformation.Name);
67
+ const friendlyName = (service.accessoryInformation.Name ? service.accessoryInformation.Name : service.serviceName);
58
68
  service.uniqueId = `${service.instance.name}${service.instance.username}${service.accessoryInformation.Manufacturer}${friendlyName}${service.uuid.slice(0, 8)}`;
59
69
  });
60
70
  updatedDevices.forEach((updatedService, index) => {
@@ -86,9 +96,9 @@ class HBConfigNode {
86
96
  return filterUnique(this.hbDevices)
87
97
  .filter(service => supportedTypes.has(service.humanType))
88
98
  .map(service => ({
89
- name: (service.serviceName ? service.serviceName : service.accessoryInformation.Name),
90
- fullName: `${(service.serviceName ? service.serviceName : service.accessoryInformation.Name)} - ${service.humanType}`,
91
- sortName: `${(service.serviceName ? service.serviceName : service.accessoryInformation.Name)}:${service.type}`,
99
+ name: (service.accessoryInformation.Name ? service.accessoryInformation.Name : service.serviceName),
100
+ fullName: `${(service.accessoryInformation.Name ? service.accessoryInformation.Name : service.serviceName)} - ${service.humanType}`,
101
+ sortName: `${(service.accessoryInformation.Name ? service.accessoryInformation.Name : service.serviceName)}:${service.type}`,
92
102
  uniqueId: service.uniqueId,
93
103
  homebridge: service.instance.name,
94
104
  service: service.type,