@switchbot/homebridge-switchbot 1.8.3-beta.0 → 1.9.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.
Files changed (105) hide show
  1. package/.eslintrc +1 -1
  2. package/.vscode/settings.json +1 -1
  3. package/CHANGELOG.md +8 -0
  4. package/config.schema.json +9 -0
  5. package/dist/devices/bots.d.ts +37 -34
  6. package/dist/devices/bots.d.ts.map +1 -1
  7. package/dist/devices/bots.js +196 -243
  8. package/dist/devices/bots.js.map +1 -1
  9. package/dist/devices/colorbulb.d.ts +50 -50
  10. package/dist/devices/colorbulb.d.ts.map +1 -1
  11. package/dist/devices/colorbulb.js +131 -145
  12. package/dist/devices/colorbulb.js.map +1 -1
  13. package/dist/devices/contact.d.ts +29 -27
  14. package/dist/devices/contact.d.ts.map +1 -1
  15. package/dist/devices/contact.js +115 -138
  16. package/dist/devices/contact.js.map +1 -1
  17. package/dist/devices/curtains.d.ts +31 -29
  18. package/dist/devices/curtains.d.ts.map +1 -1
  19. package/dist/devices/curtains.js +176 -211
  20. package/dist/devices/curtains.js.map +1 -1
  21. package/dist/devices/humidifiers.d.ts +33 -33
  22. package/dist/devices/humidifiers.d.ts.map +1 -1
  23. package/dist/devices/humidifiers.js +189 -214
  24. package/dist/devices/humidifiers.js.map +1 -1
  25. package/dist/devices/meters.d.ts +31 -30
  26. package/dist/devices/meters.d.ts.map +1 -1
  27. package/dist/devices/meters.js +100 -124
  28. package/dist/devices/meters.js.map +1 -1
  29. package/dist/devices/motion.d.ts +26 -25
  30. package/dist/devices/motion.d.ts.map +1 -1
  31. package/dist/devices/motion.js +102 -120
  32. package/dist/devices/motion.js.map +1 -1
  33. package/dist/devices/plugs.d.ts +23 -23
  34. package/dist/devices/plugs.d.ts.map +1 -1
  35. package/dist/devices/plugs.js +63 -81
  36. package/dist/devices/plugs.js.map +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -22
  40. package/dist/index.js.map +1 -1
  41. package/dist/irdevices/airconditioners.d.ts +6 -6
  42. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  43. package/dist/irdevices/airconditioners.js +79 -105
  44. package/dist/irdevices/airconditioners.js.map +1 -1
  45. package/dist/irdevices/airpurifiers.d.ts +5 -5
  46. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  47. package/dist/irdevices/airpurifiers.js +42 -56
  48. package/dist/irdevices/airpurifiers.js.map +1 -1
  49. package/dist/irdevices/cameras.d.ts +5 -5
  50. package/dist/irdevices/cameras.d.ts.map +1 -1
  51. package/dist/irdevices/cameras.js +27 -31
  52. package/dist/irdevices/cameras.js.map +1 -1
  53. package/dist/irdevices/fans.d.ts +5 -5
  54. package/dist/irdevices/fans.d.ts.map +1 -1
  55. package/dist/irdevices/fans.js +38 -45
  56. package/dist/irdevices/fans.js.map +1 -1
  57. package/dist/irdevices/lights.d.ts +5 -5
  58. package/dist/irdevices/lights.d.ts.map +1 -1
  59. package/dist/irdevices/lights.js +33 -37
  60. package/dist/irdevices/lights.js.map +1 -1
  61. package/dist/irdevices/others.d.ts +5 -5
  62. package/dist/irdevices/others.d.ts.map +1 -1
  63. package/dist/irdevices/others.js +29 -34
  64. package/dist/irdevices/others.js.map +1 -1
  65. package/dist/irdevices/tvs.d.ts +5 -5
  66. package/dist/irdevices/tvs.d.ts.map +1 -1
  67. package/dist/irdevices/tvs.js +76 -91
  68. package/dist/irdevices/tvs.js.map +1 -1
  69. package/dist/irdevices/vacuumcleaners.d.ts +5 -5
  70. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  71. package/dist/irdevices/vacuumcleaners.js +27 -31
  72. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  73. package/dist/irdevices/waterheaters.d.ts +5 -5
  74. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  75. package/dist/irdevices/waterheaters.js +27 -31
  76. package/dist/irdevices/waterheaters.js.map +1 -1
  77. package/dist/platform.d.ts +12 -12
  78. package/dist/platform.d.ts.map +1 -1
  79. package/dist/platform.js +172 -241
  80. package/dist/platform.js.map +1 -1
  81. package/dist/settings.d.ts +2 -1
  82. package/dist/settings.d.ts.map +1 -1
  83. package/dist/settings.js +4 -4
  84. package/dist/settings.js.map +1 -1
  85. package/package.json +71 -72
  86. package/src/devices/bots.ts +455 -929
  87. package/src/devices/colorbulb.ts +246 -473
  88. package/src/devices/contact.ts +208 -381
  89. package/src/devices/curtains.ts +321 -613
  90. package/src/devices/humidifiers.ts +323 -594
  91. package/src/devices/meters.ts +186 -331
  92. package/src/devices/motion.ts +184 -327
  93. package/src/devices/plugs.ts +107 -175
  94. package/src/index.ts +5 -5
  95. package/src/irdevices/airconditioners.ts +149 -360
  96. package/src/irdevices/airpurifiers.ts +80 -178
  97. package/src/irdevices/cameras.ts +51 -109
  98. package/src/irdevices/fans.ts +77 -174
  99. package/src/irdevices/lights.ts +57 -110
  100. package/src/irdevices/others.ts +62 -137
  101. package/src/irdevices/tvs.ts +124 -249
  102. package/src/irdevices/vacuumcleaners.ts +52 -112
  103. package/src/irdevices/waterheaters.ts +53 -115
  104. package/src/platform.ts +357 -768
  105. package/src/settings.ts +24 -17
@@ -1,8 +1,8 @@
1
- import * as rxjs from "rxjs";
2
- import * as operators from "rxjs/operators";
3
- import * as platform from "../platform";
4
- import * as homebridge from "homebridge";
5
- import * as settings from "../settings";
1
+ import { interval, Subject } from 'rxjs';
2
+ import { skipWhile } from 'rxjs/operators';
3
+ import { SwitchBotPlatform } from '../platform';
4
+ import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -11,29 +11,29 @@ import * as settings from "../settings";
11
11
  */
12
12
  export class Motion {
13
13
  // Services
14
- private service: homebridge.Service;
15
- private lightSensorService?: homebridge.Service;
16
- private batteryService?: homebridge.Service;
14
+ private service: Service;
15
+ private lightSensorService?: Service;
16
+ private batteryService?: Service;
17
17
 
18
18
  // Characteristic Values
19
- MotionDetected!: homebridge.CharacteristicValue;
20
- CurrentAmbientLightLevel!: homebridge.CharacteristicValue;
21
- BatteryLevel?: homebridge.CharacteristicValue;
22
- StatusLowBattery?: homebridge.CharacteristicValue;
19
+ MotionDetected!: CharacteristicValue;
20
+ CurrentAmbientLightLevel!: CharacteristicValue;
21
+ BatteryLevel?: CharacteristicValue;
22
+ StatusLowBattery?: CharacteristicValue;
23
23
 
24
24
  // OpenAPI Others
25
- deviceStatus!: settings.deviceStatusResponse;
26
- moveDetected: settings.deviceStatus["moveDetected"];
27
- brightness: settings.deviceStatus["brightness"];
25
+ deviceStatus!: deviceStatusResponse;
26
+ moveDetected: deviceStatus['moveDetected'];
27
+ brightness: deviceStatus['brightness'];
28
28
 
29
29
  // BLE Others
30
30
  connected?: boolean;
31
- switchbot!: settings.switchbot;
31
+ switchbot!: switchbot;
32
32
  SwitchToOpenAPI?: boolean;
33
- serviceData!: settings.serviceData;
34
- battery!: settings.serviceData["battery"];
35
- movement!: settings.serviceData["movement"];
36
- lightLevel!: settings.serviceData["lightLevel"];
33
+ serviceData!: serviceData;
34
+ battery!: serviceData['battery'];
35
+ movement!: serviceData['movement'];
36
+ lightLevel!: serviceData['lightLevel'];
37
37
 
38
38
  // Config
39
39
  set_minLux!: number;
@@ -44,12 +44,12 @@ export class Motion {
44
44
 
45
45
  // Updates
46
46
  motionUbpdateInProgress!: boolean;
47
- doMotionUpdate!: rxjs.Subject<void>;
47
+ doMotionUpdate!: Subject<void>;
48
48
 
49
49
  constructor(
50
- private readonly platform: platform.SwitchBotPlatform,
51
- private accessory: homebridge.PlatformAccessory,
52
- public device: settings.device & settings.devicesConfig,
50
+ private readonly platform: SwitchBotPlatform,
51
+ private accessory: PlatformAccessory,
52
+ public device: device & devicesConfig,
53
53
  ) {
54
54
  // default placeholders
55
55
  this.logs(device);
@@ -58,8 +58,13 @@ export class Motion {
58
58
  this.config(device);
59
59
  this.MotionDetected = false;
60
60
 
61
+ // Motion Config
62
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline},`
63
+ + ` hide_lightsensor: ${device.motion?.hide_lightsensor},`
64
+ + ` set_minLux: ${this.device.motion?.set_minLux}, set_maxLux: ${this.device.motion?.set_maxLux})`);
65
+
61
66
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
- this.doMotionUpdate = new rxjs.Subject();
67
+ this.doMotionUpdate = new Subject();
63
68
  this.motionUbpdateInProgress = false;
64
69
 
65
70
  // Retrieve initial values and updateHomekit
@@ -68,22 +73,15 @@ export class Motion {
68
73
  // set accessory information
69
74
  accessory
70
75
  .getService(this.platform.Service.AccessoryInformation)!
71
- .setCharacteristic(this.platform.Characteristic.Manufacturer, "SwitchBot")
72
- .setCharacteristic(
73
- this.platform.Characteristic.Model,
74
- "SWITCHBOT-WOMOTION-W1101500",
75
- )
76
- .setCharacteristic(
77
- this.platform.Characteristic.SerialNumber,
78
- device.deviceId!,
79
- );
76
+ .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
77
+ .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-WOMOTION-W1101500')
78
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
80
79
 
81
80
  // get the Battery service if it exists, otherwise create a new Motion service
82
81
  // you can create multiple services for each accessory
83
82
  (this.service =
84
83
  accessory.getService(this.platform.Service.MotionSensor) ||
85
- accessory.addService(this.platform.Service.MotionSensor)),
86
- `${accessory.displayName} Motion Sensor`;
84
+ accessory.addService(this.platform.Service.MotionSensor)), `${accessory.displayName} Motion Sensor`;
87
85
 
88
86
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
89
87
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -91,77 +89,51 @@ export class Motion {
91
89
 
92
90
  // set the service name, this is what is displayed as the default name on the Home app
93
91
  // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
94
- this.service.setCharacteristic(
95
- this.platform.Characteristic.Name,
96
- accessory.displayName,
97
- );
92
+ this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
98
93
 
99
94
  // each service must implement at-minimum the "required characteristics" for the given service type
100
95
  // see https://developers.homebridge.io/#/service/MotionSensor
101
96
 
102
97
  // Light Sensor Service
103
98
  if (device.motion?.hide_lightsensor) {
104
- this.debugLog(
105
- `Motion Sensor: ${accessory.displayName} Removing Light Sensor Service`,
106
- );
107
- this.lightSensorService = this.accessory.getService(
108
- this.platform.Service.LightSensor,
109
- );
99
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Light Sensor Service`);
100
+ this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
110
101
  accessory.removeService(this.lightSensorService!);
111
102
  } else if (!this.lightSensorService) {
112
- this.debugLog(
113
- `Motion Sensor: ${accessory.displayName} Add Light Sensor Service`,
114
- );
103
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Light Sensor Service`);
115
104
  (this.lightSensorService =
116
105
  this.accessory.getService(this.platform.Service.LightSensor) ||
117
- this.accessory.addService(this.platform.Service.LightSensor)),
118
- `${accessory.displayName} Light Sensor`;
106
+ this.accessory.addService(this.platform.Service.LightSensor)), `${accessory.displayName} Light Sensor`;
107
+
108
+ this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
119
109
 
120
- this.lightSensorService.setCharacteristic(
121
- this.platform.Characteristic.Name,
122
- `${accessory.displayName} Light Sensor`,
123
- );
124
110
  } else {
125
- this.debugLog(
126
- `Motion Sensor: ${accessory.displayName} Light Sensor Service Not Added`,
127
- );
111
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
128
112
  }
129
113
 
130
114
  // Battery Service
131
115
  if (!device.ble) {
132
- this.debugLog(
133
- `Motion Sensor: ${accessory.displayName} Removing Battery Service`,
134
- );
135
- this.batteryService = this.accessory.getService(
136
- this.platform.Service.Battery,
137
- );
116
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
117
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
138
118
  accessory.removeService(this.batteryService!);
139
119
  } else if (device.ble && !this.batteryService) {
140
- this.debugLog(
141
- `Motion Sensor: ${accessory.displayName} Add Battery Service`,
142
- );
120
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
143
121
  (this.batteryService =
144
122
  this.accessory.getService(this.platform.Service.Battery) ||
145
- this.accessory.addService(this.platform.Service.Battery)),
146
- `${accessory.displayName} Battery`;
123
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
124
+
125
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
147
126
 
148
- this.batteryService.setCharacteristic(
149
- this.platform.Characteristic.Name,
150
- `${accessory.displayName} Battery`,
151
- );
152
127
  } else {
153
- this.debugLog(
154
- `Motion Sensor: ${accessory.displayName} Battery Service Not Added`,
155
- );
128
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Battery Service Not Added`);
156
129
  }
157
130
 
158
131
  // Retrieve initial values and updateHomekit
159
132
  this.updateHomeKitCharacteristics();
160
133
 
161
134
  // Start an update interval
162
- rxjs
163
- .interval(this.deviceRefreshRate * 1000)
164
- .pipe(operators.skipWhile(() => this.motionUbpdateInProgress))
135
+ interval(this.deviceRefreshRate * 1000)
136
+ .pipe(skipWhile(() => this.motionUbpdateInProgress))
165
137
  .subscribe(() => {
166
138
  this.refreshStatus();
167
139
  });
@@ -179,30 +151,24 @@ export class Motion {
179
151
  }
180
152
 
181
153
  private async BLEparseStatus() {
182
- this.debugLog(
183
- `Motion Sensor: ${this.accessory.displayName} BLE parseStatus`,
184
- );
154
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
185
155
  // Movement
186
156
  this.MotionDetected = this.movement!;
187
- this.debugLog(
188
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
189
- );
157
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
190
158
  // Light Level
191
159
  if (!this.device.motion?.hide_lightsensor) {
192
160
  this.set_minLux = this.minLux();
193
161
  this.set_maxLux = this.maxLux();
194
162
  switch (this.lightLevel) {
195
- case "dark":
163
+ case 'dark':
196
164
  case 1:
197
165
  this.CurrentAmbientLightLevel = this.set_minLux;
198
166
  break;
199
167
  default:
200
168
  this.CurrentAmbientLightLevel = this.set_maxLux;
201
169
  }
202
- this.debugLog(
203
- `Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
204
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
205
- );
170
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
171
+ + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
206
172
  }
207
173
  // Battery
208
174
  if (this.battery === undefined) {
@@ -210,15 +176,11 @@ export class Motion {
210
176
  }
211
177
  this.BatteryLevel = this.battery!;
212
178
  if (this.BatteryLevel < 10) {
213
- this.StatusLowBattery =
214
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
179
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
215
180
  } else {
216
- this.StatusLowBattery =
217
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
181
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
218
182
  }
219
- this.debugLog(
220
- `Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`,
221
- );
183
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
222
184
  }
223
185
 
224
186
  private async openAPIparseStatus() {
@@ -226,29 +188,24 @@ export class Motion {
226
188
  this.SwitchToOpenAPI = false;
227
189
  }
228
190
  if (this.platform.config.credentials?.openToken) {
229
- this.debugLog(
230
- `Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`,
231
- );
191
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
232
192
  // Motion State
233
193
  this.MotionDetected = this.moveDetected!;
234
- this.debugLog(
235
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
236
- );
194
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
237
195
  // Light Level
238
196
  if (!this.device.motion?.hide_lightsensor) {
239
197
  this.set_minLux = this.minLux();
240
198
  this.set_maxLux = this.maxLux();
241
199
  switch (this.brightness) {
242
- case "dim":
200
+ case 'dim':
243
201
  this.CurrentAmbientLightLevel = this.set_minLux;
244
202
  break;
245
- case "bright":
203
+ case 'bright':
246
204
  default:
247
205
  this.CurrentAmbientLightLevel = this.set_maxLux;
248
206
  }
249
- this.debugLog(
250
- `Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
251
- );
207
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
208
+
252
209
  }
253
210
  }
254
211
  }
@@ -265,133 +222,99 @@ export class Motion {
265
222
  }
266
223
 
267
224
  private async BLERefreshStatus() {
268
- this.debugLog(
269
- `Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`,
270
- );
225
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
271
226
  const switchbot = await this.platform.connectBLE();
272
227
  // Convert to BLE Address
273
- this.device.bleMac = this.device
274
- .deviceId!.match(/.{1,2}/g)!
275
- .join(":")
276
- .toLowerCase();
277
- this.debugLog(
278
- `Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
279
- );
228
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
229
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
280
230
  // Start to monitor advertisement packets
281
- try {
282
- await switchbot.discover({
283
- model: "s",
284
- quick: false,
285
- });
286
- // Start to monitor advertisement packets
287
- await switchbot.startScan({
288
- model: "s",
231
+ if (switchbot !== false) {
232
+ switchbot.startScan({
233
+ model: 's',
289
234
  id: this.device.bleMac,
290
- });
291
- // Set an event hander
292
- switchbot.onadvertisement = (ad: settings.ad) => {
293
- this.serviceData = ad.serviceData;
294
- this.movement = ad.serviceData.movement;
295
- this.battery = ad.serviceData.battery;
296
- this.lightLevel = ad.serviceData.lightLevel;
297
- this.debugLog(
298
- `Motion Sensor: ${
299
- this.accessory.displayName
300
- } serviceData: ${JSON.stringify(ad.serviceData)}`,
301
- );
302
- this.debugLog(
303
- `Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: ` +
304
- `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
305
- );
306
-
307
- if (this.serviceData) {
308
- this.connected = true;
309
- this.debugLog(
310
- `Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`,
311
- );
235
+ }).then(() => {
236
+ // Set an event hander
237
+ switchbot.onadvertisement = (ad: any) => {
238
+ this.serviceData = ad.serviceData;
239
+ this.movement = ad.serviceData.movement;
240
+ this.battery = ad.serviceData.battery;
241
+ this.lightLevel = ad.serviceData.lightLevel;
242
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
243
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
244
+ + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
245
+
246
+ if (this.serviceData) {
247
+ this.connected = true;
248
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
249
+ } else {
250
+ this.connected = false;
251
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
252
+ }
253
+ };
254
+ // Wait 10 seconds
255
+ return switchbot.wait(this.scanDuration * 1000);
256
+ }).then(async () => {
257
+ // Stop to monitor
258
+ switchbot.stopScan();
259
+ if (this.connected) {
260
+ this.parseStatus();
261
+ this.updateHomeKitCharacteristics();
312
262
  } else {
313
- this.connected = false;
314
- this.debugLog(
315
- `Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`,
316
- );
263
+ await this.BLEconnection(switchbot);
264
+ }
265
+ }).catch(async (e: any) => {
266
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
267
+ if (this.deviceLogging === 'debug') {
268
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
269
+ + ` Error Message: ${JSON.stringify(e.message)}`);
270
+ }
271
+ if (this.platform.debugMode) {
272
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
273
+ + ` Error: ${JSON.stringify(e)}`);
317
274
  }
318
- };
319
- // Wait 10 seconds
320
- await switchbot.wait(this.scanDuration * 1000);
321
- // Stop to monitor
322
- switchbot.stopScan();
323
- if (this.connected) {
324
- this.parseStatus();
325
- this.updateHomeKitCharacteristics();
326
- } else {
327
- this.errorLog(
328
- `Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`,
329
- );
330
275
  if (this.platform.config.credentials?.openToken) {
331
- this.warnLog(
332
- `Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`,
333
- );
276
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
334
277
  this.SwitchToOpenAPI = true;
335
278
  await this.openAPIRefreshStatus();
336
279
  }
337
- }
338
- } catch (e: any) {
339
- this.errorLog(
340
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`,
341
- );
342
- if (this.deviceLogging.includes("debug")) {
343
- this.errorLog(
344
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
345
- ` Error Message: ${JSON.stringify(e.message)}`,
346
- );
347
- }
348
- if (this.platform.config.credentials?.openToken) {
349
- this.warnLog(
350
- `Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`,
351
- );
352
- this.SwitchToOpenAPI = true;
353
- await this.openAPIRefreshStatus();
354
- }
355
- this.apiError(e);
280
+ this.apiError(e);
281
+ });
282
+ } else {
283
+ await this.BLEconnection(switchbot);
284
+ }
285
+ }
286
+
287
+ public async BLEconnection(switchbot: any) {
288
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
289
+ if (this.platform.config.credentials?.openToken) {
290
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
291
+ this.SwitchToOpenAPI = true;
292
+ await this.openAPIRefreshStatus();
356
293
  }
357
294
  }
358
295
 
359
296
  private async openAPIRefreshStatus() {
360
297
  if (this.platform.config.credentials?.openToken) {
361
- this.debugLog(
362
- `Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`,
363
- );
298
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`);
364
299
  try {
365
- this.deviceStatus = (
366
- await this.platform.axios.get(
367
- `${settings.DeviceURL}/${this.device.deviceId}/status`,
368
- )
369
- ).data;
370
- this.debugLog(
371
- `Motion Sensor: ${
372
- this.accessory.displayName
373
- } refreshStatus: ${JSON.stringify(this.deviceStatus)}`,
374
- );
300
+ this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
301
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
375
302
  if (this.deviceStatus.body.moveDetected === undefined) {
376
- this.errorLog(
377
- `Motion Sensor: ${
378
- this.accessory.displayName
379
- } refreshStatus: ${JSON.stringify(this.deviceStatus)}`,
380
- );
303
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
381
304
  }
382
305
  this.moveDetected = this.deviceStatus.body.moveDetected;
383
306
  this.brightness = this.deviceStatus.body.brightness;
384
307
  this.parseStatus();
385
308
  this.updateHomeKitCharacteristics();
386
309
  } catch (e: any) {
387
- this.errorLog(
388
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`,
389
- );
390
- if (this.deviceLogging.includes("debug")) {
391
- this.errorLog(
392
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
393
- ` Error Message: ${JSON.stringify(e.message)}`,
394
- );
310
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
311
+ if (this.deviceLogging === 'debug') {
312
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
313
+ + ` Error Message: ${JSON.stringify(e.message)}`);
314
+ }
315
+ if (this.platform.debugMode) {
316
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
317
+ + ` Error: ${JSON.stringify(e)}`);
395
318
  }
396
319
  this.apiError(e);
397
320
  }
@@ -403,167 +326,103 @@ export class Motion {
403
326
  */
404
327
  updateHomeKitCharacteristics() {
405
328
  if (this.MotionDetected === undefined) {
406
- this.debugLog(
407
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
408
- );
329
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
409
330
  } else {
410
- this.service.updateCharacteristic(
411
- this.platform.Characteristic.MotionDetected,
412
- this.MotionDetected,
413
- );
414
- this.debugLog(
415
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`,
416
- );
331
+ this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
332
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
417
333
  }
418
334
  if (this.device.ble) {
419
335
  if (this.BatteryLevel === undefined) {
420
- this.debugLog(
421
- `Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`,
422
- );
336
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
423
337
  } else {
424
- this.batteryService?.updateCharacteristic(
425
- this.platform.Characteristic.BatteryLevel,
426
- this.BatteryLevel,
427
- );
428
- this.debugLog(
429
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`,
430
- );
338
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
339
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
431
340
  }
432
341
  if (this.StatusLowBattery === undefined) {
433
- this.debugLog(
434
- `Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`,
435
- );
342
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
436
343
  } else {
437
- this.batteryService?.updateCharacteristic(
438
- this.platform.Characteristic.StatusLowBattery,
439
- this.StatusLowBattery,
440
- );
441
- this.debugLog(
442
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`,
443
- );
344
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
345
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
444
346
  }
445
347
  if (this.CurrentAmbientLightLevel === undefined) {
446
- this.debugLog(
447
- `Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
448
- );
348
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
449
349
  } else {
450
- this.lightSensorService?.updateCharacteristic(
451
- this.platform.Characteristic.CurrentAmbientLightLevel,
452
- this.CurrentAmbientLightLevel,
453
- );
454
- this.debugLog(
455
- `Motion Sensor: ${this.accessory.displayName}` +
456
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
457
- );
350
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
351
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName}`
352
+ + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
458
353
  }
459
354
  }
460
355
  }
461
356
 
462
357
  public apiError(e: any) {
463
- this.service.updateCharacteristic(
464
- this.platform.Characteristic.MotionDetected,
465
- e,
466
- );
358
+ this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
467
359
  if (this.device.ble) {
468
- this.batteryService?.updateCharacteristic(
469
- this.platform.Characteristic.BatteryLevel,
470
- e,
471
- );
472
- this.batteryService?.updateCharacteristic(
473
- this.platform.Characteristic.StatusLowBattery,
474
- e,
475
- );
476
- this.lightSensorService?.updateCharacteristic(
477
- this.platform.Characteristic.CurrentAmbientLightLevel,
478
- e,
479
- );
360
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
361
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
362
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
480
363
  }
481
364
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
482
365
  }
483
366
 
484
- config(device: settings.device & settings.devicesConfig) {
367
+ config(device: device & devicesConfig) {
485
368
  let config = {};
486
369
  if (device.motion) {
487
370
  config = device.motion;
488
371
  }
489
372
  if (device.ble !== undefined) {
490
- config["ble"] = device.ble;
373
+ config['ble'] = device.ble;
491
374
  }
492
375
  if (device.logging !== undefined) {
493
- config["logging"] = device.logging;
376
+ config['logging'] = device.logging;
494
377
  }
495
378
  if (device.refreshRate !== undefined) {
496
- config["refreshRate"] = device.refreshRate;
379
+ config['refreshRate'] = device.refreshRate;
497
380
  }
498
381
  if (device.scanDuration !== undefined) {
499
- config["scanDuration"] = device.scanDuration;
382
+ config['scanDuration'] = device.scanDuration;
500
383
  }
501
384
  if (Object.entries(config).length !== 0) {
502
- this.warnLog(
503
- `Motion Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(
504
- config,
505
- )}`,
506
- );
385
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
507
386
  }
508
387
  }
509
388
 
510
- refreshRate(device: settings.device & settings.devicesConfig) {
389
+ refreshRate(device: device & devicesConfig) {
511
390
  if (device.refreshRate) {
512
- this.deviceRefreshRate = this.accessory.context.refreshRate =
513
- device.refreshRate;
514
- this.debugLog(
515
- `Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`,
516
- );
391
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
392
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
517
393
  } else if (this.platform.config.options!.refreshRate) {
518
- this.deviceRefreshRate = this.accessory.context.refreshRate =
519
- this.platform.config.options!.refreshRate;
520
- this.debugLog(
521
- `Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`,
522
- );
394
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
395
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
523
396
  }
524
397
  }
525
398
 
526
- scan(device: settings.device & settings.devicesConfig) {
399
+ scan(device: device & devicesConfig) {
527
400
  if (device.scanDuration) {
528
- this.scanDuration = this.accessory.context.scanDuration =
529
- device.scanDuration;
401
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
530
402
  if (device.ble) {
531
- this.debugLog(
532
- `Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`,
533
- );
403
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
534
404
  }
535
405
  } else {
536
406
  this.scanDuration = this.accessory.context.scanDuration = 1;
537
407
  if (this.device.ble) {
538
- this.debugLog(
539
- `Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`,
540
- );
408
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
541
409
  }
542
410
  }
543
411
  }
544
412
 
545
- logs(device: settings.device & settings.devicesConfig) {
413
+ logs(device: device & devicesConfig) {
546
414
  if (this.platform.debugMode) {
547
- this.deviceLogging = this.accessory.context.logging = "debugMode";
548
- this.debugLog(
549
- `Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`,
550
- );
415
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
416
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
551
417
  } else if (device.logging) {
552
418
  this.deviceLogging = this.accessory.context.logging = device.logging;
553
- this.debugLog(
554
- `Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`,
555
- );
419
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
556
420
  } else if (this.platform.config.options?.logging) {
557
- this.deviceLogging = this.accessory.context.logging =
558
- this.platform.config.options?.logging;
559
- this.debugLog(
560
- `Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`,
561
- );
421
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
422
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
562
423
  } else {
563
- this.deviceLogging = this.accessory.context.logging = "standard";
564
- this.debugLog(
565
- `Motion Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`,
566
- );
424
+ this.deviceLogging = this.accessory.context.logging = 'standard';
425
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
567
426
  }
568
427
  }
569
428
 
@@ -586,30 +445,30 @@ export class Motion {
586
445
  }
587
446
 
588
447
  /**
589
- * Logging for Device
590
- */
591
- infoLog(...log: any[]): void {
448
+ * Logging for Device
449
+ */
450
+ infoLog(...log: any[]) {
592
451
  if (this.enablingDeviceLogging()) {
593
452
  this.platform.log.info(String(...log));
594
453
  }
595
454
  }
596
455
 
597
- warnLog(...log: any[]): void {
456
+ warnLog(...log: any[]) {
598
457
  if (this.enablingDeviceLogging()) {
599
458
  this.platform.log.warn(String(...log));
600
459
  }
601
460
  }
602
461
 
603
- errorLog(...log: any[]): void {
462
+ errorLog(...log: any[]) {
604
463
  if (this.enablingDeviceLogging()) {
605
464
  this.platform.log.error(String(...log));
606
465
  }
607
466
  }
608
467
 
609
- debugLog(...log: any[]): void {
468
+ debugLog(...log: any[]) {
610
469
  if (this.enablingDeviceLogging()) {
611
- if (this.deviceLogging === "debug") {
612
- this.platform.log.info("[DEBUG]", String(...log));
470
+ if (this.deviceLogging === 'debug') {
471
+ this.platform.log.info('[DEBUG]', String(...log));
613
472
  } else {
614
473
  this.platform.log.debug(String(...log));
615
474
  }
@@ -617,8 +476,6 @@ export class Motion {
617
476
  }
618
477
 
619
478
  enablingDeviceLogging(): boolean {
620
- return (
621
- this.deviceLogging.includes("debug") || this.deviceLogging === "standard"
622
- );
479
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
623
480
  }
624
481
  }