@switchbot/homebridge-switchbot 1.7.1-beta.5 → 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 (40) hide show
  1. package/dist/devices/bots.d.ts +2 -2
  2. package/dist/devices/bots.d.ts.map +1 -1
  3. package/dist/devices/bots.js +10 -28
  4. package/dist/devices/bots.js.map +1 -1
  5. package/dist/devices/colorbulb.d.ts +5 -1
  6. package/dist/devices/colorbulb.d.ts.map +1 -1
  7. package/dist/devices/colorbulb.js +12 -8
  8. package/dist/devices/colorbulb.js.map +1 -1
  9. package/dist/devices/contact.d.ts +4 -2
  10. package/dist/devices/contact.d.ts.map +1 -1
  11. package/dist/devices/contact.js +13 -34
  12. package/dist/devices/contact.js.map +1 -1
  13. package/dist/devices/curtains.d.ts.map +1 -1
  14. package/dist/devices/curtains.js +3 -4
  15. package/dist/devices/curtains.js.map +1 -1
  16. package/dist/devices/humidifiers.d.ts +7 -2
  17. package/dist/devices/humidifiers.d.ts.map +1 -1
  18. package/dist/devices/humidifiers.js +22 -35
  19. package/dist/devices/humidifiers.js.map +1 -1
  20. package/dist/devices/meters.d.ts +12 -12
  21. package/dist/devices/meters.d.ts.map +1 -1
  22. package/dist/devices/meters.js +49 -100
  23. package/dist/devices/meters.js.map +1 -1
  24. package/dist/devices/motion.d.ts +0 -1
  25. package/dist/devices/motion.d.ts.map +1 -1
  26. package/dist/devices/motion.js +5 -27
  27. package/dist/devices/motion.js.map +1 -1
  28. package/dist/devices/plugs.d.ts +2 -1
  29. package/dist/devices/plugs.d.ts.map +1 -1
  30. package/dist/devices/plugs.js +2 -1
  31. package/dist/devices/plugs.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/devices/bots.ts +12 -30
  34. package/src/devices/colorbulb.ts +17 -9
  35. package/src/devices/contact.ts +17 -35
  36. package/src/devices/curtains.ts +3 -4
  37. package/src/devices/humidifiers.ts +29 -36
  38. package/src/devices/meters.ts +63 -110
  39. package/src/devices/motion.ts +5 -27
  40. package/src/devices/plugs.ts +4 -2
@@ -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, Units, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -11,23 +11,24 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
11
11
  */
12
12
  export class Meter {
13
13
  // Services
14
- private service: Service;
15
- temperatureservice?: Service;
16
- humidityservice?: Service;
14
+ private batteryService?: Service;
15
+ private temperatureservice?: Service;
16
+ private humidityservice?: Service;
17
17
 
18
18
  // Characteristic Values
19
19
  CurrentRelativeHumidity!: CharacteristicValue;
20
20
  CurrentRelativeHumidityCached!: CharacteristicValue;
21
- CurrentTemperature!: CharacteristicValue;
22
- CurrentTemperatureCached!: CharacteristicValue;
23
- BatteryLevel!: CharacteristicValue;
24
- BatteryLevelCached!: CharacteristicValue;
25
- ChargingState!: CharacteristicValue;
26
- StatusLowBattery!: CharacteristicValue;
21
+ CurrentTemperature?: CharacteristicValue;
22
+ CurrentTemperatureCached?: CharacteristicValue;
23
+ BatteryLevel?: CharacteristicValue;
24
+ ChargingState?: CharacteristicValue;
25
+ StatusLowBattery?: CharacteristicValue;
27
26
  Active!: CharacteristicValue;
28
27
  WaterLevel!: CharacteristicValue;
29
28
 
30
29
  // OpenAPI Others
30
+ Temperature: deviceStatus['temperature'];
31
+ Humidity: deviceStatus['humidity'];
31
32
  deviceStatus!: deviceStatusResponse;
32
33
 
33
34
  // BLE Others
@@ -57,17 +58,6 @@ export class Meter {
57
58
  this.logs();
58
59
  this.scan();
59
60
  this.refreshRate();
60
- if (this.BatteryLevel === undefined) {
61
- this.BatteryLevel = 100;
62
- } else {
63
- this.BatteryLevel = this.accessory.context.BatteryLevel;
64
- }
65
- if (this.BatteryLevel < 15) {
66
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
67
- } else {
68
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
69
- }
70
- this.ChargingState = this.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
71
61
  if (this.CurrentRelativeHumidity === undefined) {
72
62
  this.CurrentRelativeHumidity = 0;
73
63
  } else {
@@ -97,26 +87,6 @@ export class Meter {
97
87
  .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-METERTH-S1')
98
88
  .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
99
89
 
100
- // get the Battery service if it exists, otherwise create a new Battery service
101
- // you can create multiple services for each accessory
102
- (this.service =
103
- accessory.getService(this.platform.Service.Battery) ||
104
- accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
105
-
106
- // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
107
- // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
108
- // accessory.getService('NAME') ?? accessory.addService(this.platform.Service.Battery, 'NAME', 'USER_DEFINED_SUBTYPE');
109
-
110
- // set the service name, this is what is displayed as the default name on the Home app
111
- // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
112
- this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
113
-
114
- // each service must implement at-minimum the "required characteristics" for the given service type
115
- // see https://developers.homebridge.io/#/service/Battery
116
-
117
- // create handlers for required characteristics
118
- this.service.setCharacteristic(this.platform.Characteristic.ChargingState, 2);
119
-
120
90
  // Temperature Sensor Service
121
91
  if (device.meter?.hide_temperature) {
122
92
  this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
@@ -128,7 +98,7 @@ export class Meter {
128
98
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
129
99
  this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
130
100
 
131
- this.service.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
101
+ this.temperatureservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
132
102
 
133
103
  this.temperatureservice
134
104
  .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
@@ -140,7 +110,7 @@ export class Meter {
140
110
  minStep: 0.1,
141
111
  })
142
112
  .onGet(() => {
143
- return this.CurrentTemperature;
113
+ return this.CurrentTemperature!;
144
114
  });
145
115
  } else {
146
116
  this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
@@ -157,7 +127,7 @@ export class Meter {
157
127
  this.accessory.getService(this.platform.Service.HumiditySensor) ||
158
128
  this.accessory.addService(this.platform.Service.HumiditySensor)), `${accessory.displayName} Humidity Sensor`;
159
129
 
160
- this.service.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
130
+ this.humidityservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
161
131
 
162
132
  this.humidityservice
163
133
  .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
@@ -171,6 +141,27 @@ export class Meter {
171
141
  this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
172
142
  }
173
143
 
144
+ // Battery Service
145
+ if (!device.ble) {
146
+ this.debugLog(`Meter: ${accessory.displayName} Removing Battery Service`);
147
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
148
+ accessory.removeService(this.batteryService!);
149
+ } else if (device.ble && !this.batteryService) {
150
+ this.debugLog(`Meter: ${accessory.displayName} Add Battery Service`);
151
+ (this.batteryService =
152
+ this.accessory.getService(this.platform.Service.Battery) ||
153
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
154
+
155
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
156
+
157
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
158
+
159
+ } else {
160
+ this.debugLog(`Meter: ${accessory.displayName} Battery Service Not Added`);
161
+ }
162
+
163
+
164
+
174
165
  // Retrieve initial values and updateHomekit
175
166
  this.updateHomeKitCharacteristics();
176
167
 
@@ -252,9 +243,9 @@ export class Meter {
252
243
  }
253
244
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
254
245
 
246
+ // Humidity
255
247
  if (!this.device.meter?.hide_humidity) {
256
- // Humidity
257
- this.CurrentRelativeHumidity = Number(this.humidity);
248
+ this.CurrentRelativeHumidity = this.humidity!;
258
249
  this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
259
250
  }
260
251
 
@@ -272,25 +263,15 @@ export class Meter {
272
263
  }
273
264
  if (this.platform.config.credentials?.openToken) {
274
265
  this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
275
- if (this.deviceStatus.body) {
276
- this.BatteryLevel = 100;
277
- } else {
278
- this.BatteryLevel = 10;
279
- }
280
- if (this.BatteryLevel < 15) {
281
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
282
- } else {
283
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
284
- }
285
266
  // Current Relative Humidity
286
267
  if (!this.device.meter?.hide_humidity) {
287
- this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
268
+ this.CurrentRelativeHumidity = this.Humidity!;
288
269
  this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
289
270
  }
290
271
 
291
272
  // Current Temperature
292
273
  if (!this.device.meter?.hide_temperature) {
293
- this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
274
+ this.CurrentTemperature = this.Temperature!;
294
275
  this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
295
276
  }
296
277
  }
@@ -307,34 +288,12 @@ export class Meter {
307
288
  }
308
289
  }
309
290
 
310
- private async connectBLE() {
311
- let switchbot: any;
312
- try {
313
- // eslint-disable-next-line @typescript-eslint/no-var-requires
314
- const Switchbot = require('node-switchbot');
315
- switchbot = new Switchbot();
316
- // Convert to BLE Address
317
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
318
- this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
319
- } catch (e: any) {
320
- switchbot = false;
321
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
322
- if (this.deviceLogging === 'debug') {
323
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
324
- + ` Error Message: ${JSON.stringify(e.message)}`);
325
- }
326
- if (this.platform.debugMode) {
327
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
328
- + ` Error: ${JSON.stringify(e)}`);
329
- }
330
- }
331
- return switchbot;
332
- }
333
-
334
291
  private async BLErefreshStatus() {
335
292
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
336
- // eslint-disable-next-line @typescript-eslint/no-var-requires
337
- const switchbot = await this.connectBLE();
293
+ const switchbot = await this.platform.connectBLE();
294
+ // Convert to BLE Address
295
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
296
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
338
297
  // Start to monitor advertisement packets
339
298
  if (switchbot !== false) {
340
299
  switchbot.startScan({
@@ -366,12 +325,6 @@ export class Meter {
366
325
  // Stop to monitor
367
326
  switchbot.stopScan();
368
327
  if (this.connected) {
369
- this.CurrentTemperatureCached = this.temperature;
370
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
371
- this.CurrentRelativeHumidityCached = this.humidity!;
372
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
373
- this.BatteryLevelCached = this.battery!;
374
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
375
328
  this.parseStatus();
376
329
  this.updateHomeKitCharacteristics();
377
330
  } else {
@@ -414,12 +367,8 @@ export class Meter {
414
367
  try {
415
368
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
416
369
  this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
417
- this.CurrentTemperatureCached = this.deviceStatus.body.humidity!;
418
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
419
- this.CurrentRelativeHumidityCached = this.deviceStatus.body.humidity!;
420
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
421
- this.BatteryLevelCached = 100;
422
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
370
+ this.Humidity = this.deviceStatus.body.humidity!;
371
+ this.Temperature = this.deviceStatus.body.temperature!;
423
372
  this.parseStatus();
424
373
  this.updateHomeKitCharacteristics();
425
374
  } catch (e: any) {
@@ -441,18 +390,6 @@ export class Meter {
441
390
  * Updates the status for each of the HomeKit Characteristics
442
391
  */
443
392
  updateHomeKitCharacteristics() {
444
- if (this.StatusLowBattery === undefined) {
445
- this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
446
- } else {
447
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
448
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
449
- }
450
- if (this.BatteryLevel === undefined) {
451
- this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
452
- } else {
453
- this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
454
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
455
- }
456
393
  if (!this.device.meter?.hide_humidity) {
457
394
  if (this.CurrentRelativeHumidity === undefined) {
458
395
  this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
@@ -469,17 +406,33 @@ export class Meter {
469
406
  this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
470
407
  }
471
408
  }
409
+ if (this.device.ble) {
410
+ if (this.BatteryLevel === undefined) {
411
+ this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
412
+ } else {
413
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
414
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
415
+ }
416
+ if (this.StatusLowBattery === undefined) {
417
+ this.debugLog(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
418
+ } else {
419
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
420
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
421
+ }
422
+ }
472
423
  }
473
424
 
474
425
  public apiError(e: any) {
475
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
476
- this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
477
426
  if (!this.device.meter?.hide_humidity) {
478
427
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
479
428
  }
480
429
  if (!this.device.meter?.hide_temperature) {
481
430
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
482
431
  }
432
+ if (this.device.ble) {
433
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
434
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
435
+ }
483
436
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
484
437
  }
485
438
 
@@ -115,7 +115,7 @@ export class Motion {
115
115
  this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
116
116
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
117
117
  accessory.removeService(this.batteryService!);
118
- } else if (!this.batteryService) {
118
+ } else if (device.ble && !this.batteryService) {
119
119
  this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
120
120
  (this.batteryService =
121
121
  this.accessory.getService(this.platform.Service.Battery) ||
@@ -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({
@@ -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) {