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