@switchbot/homebridge-switchbot 1.7.1-beta.2 → 1.7.1-beta.6

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 (47) hide show
  1. package/CHANGELOG.md +5 -2
  2. package/dist/devices/bots.d.ts +2 -2
  3. package/dist/devices/bots.d.ts.map +1 -1
  4. package/dist/devices/bots.js +10 -28
  5. package/dist/devices/bots.js.map +1 -1
  6. package/dist/devices/colorbulb.d.ts +5 -1
  7. package/dist/devices/colorbulb.d.ts.map +1 -1
  8. package/dist/devices/colorbulb.js +12 -8
  9. package/dist/devices/colorbulb.js.map +1 -1
  10. package/dist/devices/contact.d.ts +4 -2
  11. package/dist/devices/contact.d.ts.map +1 -1
  12. package/dist/devices/contact.js +13 -34
  13. package/dist/devices/contact.js.map +1 -1
  14. package/dist/devices/curtains.d.ts +5 -3
  15. package/dist/devices/curtains.d.ts.map +1 -1
  16. package/dist/devices/curtains.js +19 -32
  17. package/dist/devices/curtains.js.map +1 -1
  18. package/dist/devices/humidifiers.d.ts +7 -2
  19. package/dist/devices/humidifiers.d.ts.map +1 -1
  20. package/dist/devices/humidifiers.js +22 -35
  21. package/dist/devices/humidifiers.js.map +1 -1
  22. package/dist/devices/meters.d.ts +12 -12
  23. package/dist/devices/meters.d.ts.map +1 -1
  24. package/dist/devices/meters.js +49 -100
  25. package/dist/devices/meters.js.map +1 -1
  26. package/dist/devices/motion.d.ts +3 -2
  27. package/dist/devices/motion.d.ts.map +1 -1
  28. package/dist/devices/motion.js +14 -33
  29. package/dist/devices/motion.js.map +1 -1
  30. package/dist/devices/plugs.d.ts +2 -1
  31. package/dist/devices/plugs.d.ts.map +1 -1
  32. package/dist/devices/plugs.js +2 -1
  33. package/dist/devices/plugs.js.map +1 -1
  34. package/dist/platform.d.ts +2 -1
  35. package/dist/platform.d.ts.map +1 -1
  36. package/dist/platform.js +14 -1
  37. package/dist/platform.js.map +1 -1
  38. package/package.json +5 -5
  39. package/src/devices/bots.ts +12 -30
  40. package/src/devices/colorbulb.ts +17 -9
  41. package/src/devices/contact.ts +17 -35
  42. package/src/devices/curtains.ts +24 -34
  43. package/src/devices/humidifiers.ts +29 -36
  44. package/src/devices/meters.ts +63 -110
  45. package/src/devices/motion.ts +17 -34
  46. package/src/devices/plugs.ts +4 -2
  47. package/src/platform.ts +14 -1
@@ -2,7 +2,7 @@ import { interval, Subject } from 'rxjs';
2
2
  import { skipWhile } from 'rxjs/operators';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -23,6 +23,8 @@ export class Motion {
23
23
 
24
24
  // OpenAPI Others
25
25
  deviceStatus!: deviceStatusResponse;
26
+ moveDetected: deviceStatus['moveDetected'];
27
+ brightness: deviceStatus['brightness'];
26
28
 
27
29
  // BLE Others
28
30
  connected?: boolean;
@@ -113,7 +115,7 @@ export class Motion {
113
115
  this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
114
116
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
115
117
  accessory.removeService(this.batteryService!);
116
- } else if (!this.batteryService) {
118
+ } else if (device.ble && !this.batteryService) {
117
119
  this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
118
120
  (this.batteryService =
119
121
  this.accessory.getService(this.platform.Service.Battery) ||
@@ -215,7 +217,7 @@ export class Motion {
215
217
  private async BLEparseStatus() {
216
218
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
217
219
  // Movement
218
- this.MotionDetected = Boolean(this.movement);
220
+ this.MotionDetected = this.movement!;
219
221
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
220
222
  // Light Level
221
223
  if (!this.device.motion?.hide_lightsensor) {
@@ -236,7 +238,7 @@ export class Motion {
236
238
  if (this.battery === undefined) {
237
239
  this.battery = 100;
238
240
  }
239
- this.BatteryLevel = Number(this.battery);
241
+ this.BatteryLevel = this.battery!;
240
242
  if (this.BatteryLevel < 10) {
241
243
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
242
244
  } else {
@@ -252,15 +254,13 @@ export class Motion {
252
254
  if (this.platform.config.credentials?.openToken) {
253
255
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
254
256
  // Motion State
255
- if (typeof this.deviceStatus.body.moveDetected === 'boolean') {
256
- this.MotionDetected = this.deviceStatus.body.moveDetected;
257
- }
257
+ this.MotionDetected = this.moveDetected!;
258
258
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
259
259
  // Light Level
260
260
  if (!this.device.motion?.hide_lightsensor) {
261
261
  this.set_minLux = this.minLux();
262
262
  this.set_maxLux = this.maxLux();
263
- switch (this.deviceStatus.body.brightness) {
263
+ switch (this.brightness) {
264
264
  case 'dim':
265
265
  this.CurrentAmbientLightLevel = this.set_minLux;
266
266
  break;
@@ -285,34 +285,12 @@ export class Motion {
285
285
  }
286
286
  }
287
287
 
288
- public async connectBLE() {
289
- let switchbot: any;
290
- try {
291
- // eslint-disable-next-line @typescript-eslint/no-var-requires
292
- const Switchbot = require('node-switchbot');
293
- switchbot = new Switchbot();
294
- // Convert to BLE Address
295
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
296
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
297
- } catch (e: any) {
298
- switchbot = false;
299
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
300
- if (this.deviceLogging === 'debug') {
301
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
302
- + ` Error Message: ${JSON.stringify(e.message)}`);
303
- }
304
- if (this.platform.debugMode) {
305
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
306
- + ` Error: ${JSON.stringify(e)}`);
307
- }
308
- }
309
- return switchbot;
310
- }
311
-
312
288
  private async BLERefreshStatus() {
313
289
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
314
- // eslint-disable-next-line @typescript-eslint/no-var-requires
315
- const switchbot = await this.connectBLE();
290
+ const switchbot = await this.platform.connectBLE();
291
+ // Convert to BLE Address
292
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
293
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
316
294
  // Start to monitor advertisement packets
317
295
  if (switchbot !== false) {
318
296
  switchbot.startScan({
@@ -385,6 +363,11 @@ export class Motion {
385
363
  try {
386
364
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
387
365
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
366
+ if (this.deviceStatus.body.moveDetected === undefined) {
367
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
368
+ }
369
+ this.moveDetected = this.deviceStatus.body.moveDetected;
370
+ this.brightness = this.deviceStatus.body.brightness;
388
371
  this.parseStatus();
389
372
  this.updateHomeKitCharacteristics();
390
373
  } catch (e: any) {
@@ -4,7 +4,7 @@ import { interval, Subject } from 'rxjs';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
6
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { DeviceURL, device, devicesConfig, deviceStatusResponse, payload } from '../settings';
7
+ import { DeviceURL, device, devicesConfig, deviceStatusResponse, payload, deviceStatus } from '../settings';
8
8
 
9
9
  export class Plug {
10
10
  // Services
@@ -15,6 +15,7 @@ export class Plug {
15
15
  OnCached!: CharacteristicValue;
16
16
 
17
17
  // OpenAPI Others
18
+ power: deviceStatus['power'];
18
19
  deviceStatus!: deviceStatusResponse;
19
20
 
20
21
  // Config
@@ -164,7 +165,7 @@ export class Plug {
164
165
  }
165
166
 
166
167
  parseStatus() {
167
- switch (this.deviceStatus.body.power) {
168
+ switch (this.power) {
168
169
  case 'on':
169
170
  this.On = true;
170
171
  break;
@@ -178,6 +179,7 @@ export class Plug {
178
179
  try {
179
180
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
180
181
  this.debugLog(`Plug: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
182
+ this.power = this.deviceStatus.body.power;
181
183
  this.parseStatus();
182
184
  this.updateHomeKitCharacteristics();
183
185
  } catch (e: any) {
package/src/platform.ts CHANGED
@@ -192,8 +192,21 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
192
192
  }
193
193
  }
194
194
 
195
+ connectBLE() {
196
+ let Switchbot: new () => any;
197
+ let switchbot: any;
198
+ try {
199
+ Switchbot = require('node-switchbot');
200
+ switchbot = new Switchbot();
201
+ } catch (e) {
202
+ switchbot = false;
203
+ this.errorLog(`Was 'node-switchbot' found: ${switchbot}`);
204
+ }
205
+ return switchbot;
206
+ }
207
+
195
208
  /**
196
- * this method discovers the Locations
209
+ * this method discovers devices
197
210
  */
198
211
  async discoverDevices() {
199
212
  try {