@switchbot/homebridge-switchbot 0.1.2-beta.0 → 0.1.2-beta.4

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 (66) hide show
  1. package/.github/workflows/issue-validate.yml +1 -1
  2. package/dist/devices/bots.d.ts +1 -0
  3. package/dist/devices/bots.d.ts.map +1 -1
  4. package/dist/devices/bots.js +70 -22
  5. package/dist/devices/bots.js.map +1 -1
  6. package/dist/devices/bulb.d.ts +10 -1
  7. package/dist/devices/bulb.d.ts.map +1 -1
  8. package/dist/devices/bulb.js +12 -1
  9. package/dist/devices/bulb.js.map +1 -1
  10. package/dist/devices/contact.d.ts +13 -3
  11. package/dist/devices/contact.d.ts.map +1 -1
  12. package/dist/devices/contact.js +77 -2
  13. package/dist/devices/contact.js.map +1 -1
  14. package/dist/devices/curtains.d.ts +19 -8
  15. package/dist/devices/curtains.d.ts.map +1 -1
  16. package/dist/devices/curtains.js +96 -19
  17. package/dist/devices/curtains.js.map +1 -1
  18. package/dist/devices/humidifiers.d.ts.map +1 -1
  19. package/dist/devices/humidifiers.js +2 -2
  20. package/dist/devices/humidifiers.js.map +1 -1
  21. package/dist/devices/meters.d.ts +13 -4
  22. package/dist/devices/meters.d.ts.map +1 -1
  23. package/dist/devices/meters.js +28 -9
  24. package/dist/devices/meters.js.map +1 -1
  25. package/dist/devices/motion.d.ts +11 -3
  26. package/dist/devices/motion.d.ts.map +1 -1
  27. package/dist/devices/motion.js +67 -1
  28. package/dist/devices/motion.js.map +1 -1
  29. package/dist/devices/plugs.d.ts +11 -1
  30. package/dist/devices/plugs.d.ts.map +1 -1
  31. package/dist/devices/plugs.js +67 -1
  32. package/dist/devices/plugs.js.map +1 -1
  33. package/dist/irdevices/airconditioners.js +2 -2
  34. package/dist/irdevices/airconditioners.js.map +1 -1
  35. package/dist/irdevices/airpurifiers.js +2 -2
  36. package/dist/irdevices/airpurifiers.js.map +1 -1
  37. package/dist/irdevices/cameras.js +1 -1
  38. package/dist/irdevices/cameras.js.map +1 -1
  39. package/dist/irdevices/fans.js +1 -1
  40. package/dist/irdevices/fans.js.map +1 -1
  41. package/dist/irdevices/lights.js +3 -3
  42. package/dist/irdevices/lights.js.map +1 -1
  43. package/dist/irdevices/others.js +1 -1
  44. package/dist/irdevices/others.js.map +1 -1
  45. package/dist/irdevices/tvs.d.ts.map +1 -1
  46. package/dist/irdevices/tvs.js +2 -3
  47. package/dist/irdevices/tvs.js.map +1 -1
  48. package/dist/irdevices/vacuumcleaners.js +1 -1
  49. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  50. package/package.json +1 -1
  51. package/src/devices/bots.ts +85 -35
  52. package/src/devices/bulb.ts +32 -2
  53. package/src/devices/contact.ts +101 -9
  54. package/src/devices/curtains.ts +135 -35
  55. package/src/devices/humidifiers.ts +7 -2
  56. package/src/devices/meters.ts +50 -14
  57. package/src/devices/motion.ts +90 -7
  58. package/src/devices/plugs.ts +89 -4
  59. package/src/irdevices/airconditioners.ts +2 -2
  60. package/src/irdevices/airpurifiers.ts +2 -2
  61. package/src/irdevices/cameras.ts +1 -1
  62. package/src/irdevices/fans.ts +1 -1
  63. package/src/irdevices/lights.ts +3 -3
  64. package/src/irdevices/others.ts +1 -1
  65. package/src/irdevices/tvs.ts +2 -4
  66. package/src/irdevices/vacuumcleaners.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
1
+ import { Service, PlatformAccessory, CharacteristicValue, MacAddress } from 'homebridge';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { debounceTime, skipWhile, tap } from 'rxjs/operators';
@@ -6,12 +6,31 @@ import { DeviceURL, device, deviceStatusResponse } from '../settings';
6
6
  import { AxiosResponse } from 'axios';
7
7
 
8
8
  export class Plug {
9
+ // Services
9
10
  private service: Service;
10
11
 
12
+ // Characteristic Values
11
13
  On!: CharacteristicValue;
12
14
  OutletInUse!: CharacteristicValue;
15
+
16
+ // Others
13
17
  deviceStatus!: deviceStatusResponse;
18
+ switchbot!: {
19
+ discover: (
20
+ arg0:
21
+ {
22
+ duration: any;
23
+ model: string;
24
+ quick: boolean;
25
+ id: MacAddress;
26
+ }
27
+ ) => Promise<any>;
28
+ wait: (
29
+ arg0: number
30
+ ) => any;
31
+ };
14
32
 
33
+ // Updates
15
34
  plugUpdateInProgress!: boolean;
16
35
  doPlugUpdate;
17
36
 
@@ -24,6 +43,19 @@ export class Plug {
24
43
  this.On = false;
25
44
  this.OutletInUse = true;
26
45
 
46
+ // BLE Connections
47
+ if (this.platform.config.options?.ble?.includes(this.device.deviceId!)) {
48
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
49
+ const SwitchBot = require('node-switchbot');
50
+ this.switchbot = new SwitchBot();
51
+ const colon = device.deviceId!.match(/.{1,2}/g);
52
+ const bleMac = colon!.join(':'); //returns 1A:23:B4:56:78:9A;
53
+ this.device.bleMac = bleMac.toLowerCase();
54
+ if (this.platform.config.options.debug) {
55
+ this.platform.log.warn(this.device.bleMac.toLowerCase());
56
+ }
57
+ }
58
+
27
59
  // this is subject we use to track when we need to POST changes to the SwitchBot API
28
60
  this.doPlugUpdate = new Subject();
29
61
  this.plugUpdateInProgress = false;
@@ -42,7 +74,7 @@ export class Plug {
42
74
  // you can create multiple services for each accessory
43
75
  (this.service =
44
76
  accessory.getService(this.platform.Service.Outlet) ||
45
- accessory.addService(this.platform.Service.Outlet)), '%s %s', device.deviceName, device.deviceType;
77
+ accessory.addService(this.platform.Service.Outlet)), `${device.deviceName} ${device.deviceType}`;
46
78
 
47
79
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
48
80
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -103,6 +135,60 @@ export class Plug {
103
135
  }
104
136
 
105
137
  async refreshStatus() {
138
+ if (this.platform.config.options?.ble?.includes(this.device.deviceId!)) {
139
+ this.platform.log.warn('BLE DEVICE-REFRESH');
140
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
141
+ const Switchbot = require('node-switchbot');
142
+ const switchbot = new Switchbot();
143
+ const colon = this.device.deviceId!.match(/.{1,2}/g);
144
+ const bleMac = colon!.join(':'); //returns 1A:23:B4:56:78:9A;
145
+ this.device.bleMac = bleMac.toLowerCase();
146
+ if (this.platform.config.options.debug) {
147
+ this.platform.log.warn(this.device.bleMac!);
148
+ }
149
+ switchbot.onadvertisement = (ad: any) => {
150
+ this.platform.log.info(JSON.stringify(ad, null, ' '));
151
+ this.platform.log.warn('ad:', JSON.stringify(ad));
152
+ };
153
+ switchbot
154
+ .startScan({
155
+ id: this.device.bleMac,
156
+ })
157
+ .then(() => {
158
+ return switchbot.wait(this.platform.config.options!.refreshRate! * 1000);
159
+ })
160
+ .then(() => {
161
+ switchbot.stopScan();
162
+ })
163
+ .catch(async (error: any) => {
164
+ this.platform.log.error(error);
165
+ await this.openAPIRefreshStatus();
166
+ });
167
+ setInterval(() => {
168
+ this.platform.log.info('Start scan ' + this.device.deviceName + '(' + this.device.bleMac + ')');
169
+ switchbot
170
+ .startScan({
171
+ mode: 'T',
172
+ id: bleMac,
173
+ })
174
+ .then(() => {
175
+ return switchbot.wait(this.platform.config.options!.refreshRate! * 1000);
176
+ })
177
+ .then(() => {
178
+ switchbot.stopScan();
179
+ this.platform.log.info('Stop scan ' + this.device.deviceName + '(' + this.device.bleMac + ')');
180
+ })
181
+ .catch(async (error: any) => {
182
+ this.platform.log.error(error);
183
+ await this.openAPIRefreshStatus();
184
+ });
185
+ }, this.platform.config.options!.refreshRate! * 60000);
186
+ } else {
187
+ await this.openAPIRefreshStatus();
188
+ }
189
+ }
190
+
191
+ private async openAPIRefreshStatus() {
106
192
  try {
107
193
  this.platform.debug('Plug - Reading', `${DeviceURL}/${this.device.deviceId}/status`);
108
194
  const deviceStatus: deviceStatusResponse = (
@@ -118,8 +204,7 @@ export class Plug {
118
204
  this.platform.log.error(
119
205
  `Plug - Failed to refresh status of ${this.device.deviceName}`,
120
206
  JSON.stringify(e.message),
121
- this.platform.debug(`Plug ${this.accessory.displayName} - ${JSON.stringify(e)}`),
122
- );
207
+ this.platform.debug(`Plug ${this.accessory.displayName} - ${JSON.stringify(e)}`));
123
208
  this.apiError(e);
124
209
  }
125
210
  }
@@ -45,7 +45,7 @@ export class AirConditioner {
45
45
  // you can create multiple services for each accessory
46
46
  (this.service =
47
47
  accessory.getService(this.platform.Service.HeaterCooler) ||
48
- accessory.addService(this.platform.Service.HeaterCooler)), '%s %s', device.deviceName, device.remoteType;
48
+ accessory.addService(this.platform.Service.HeaterCooler)), `${device.deviceName} ${device.remoteType}`;
49
49
 
50
50
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
51
51
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -261,7 +261,7 @@ export class AirConditioner {
261
261
  this.CurrentARMode = this.CurrentMode || 1;
262
262
  this.CurrentARFanSpeed = this.CurrentFanSpeed || 1;
263
263
  this.ARActive = this.Active === 1 ? 'on' : 'off';
264
- payload.parameter = '%s,%s,%s,%s', this.CurrentARTemp, this.CurrentARMode, this.CurrentARFanSpeed, this.ARActive;
264
+ payload.parameter = `${this.CurrentARTemp},${this.CurrentARMode},${this.CurrentARFanSpeed},${this.ARActive}`;
265
265
 
266
266
 
267
267
  if (this.Active === 1) {
@@ -44,7 +44,7 @@ export class AirPurifier {
44
44
  // you can create multiple services for each accessory
45
45
  (this.service =
46
46
  accessory.getService(this.platform.Service.AirPurifier) ||
47
- accessory.addService(this.platform.Service.AirPurifier)), '%s %s', device.deviceName, device.remoteType;
47
+ accessory.addService(this.platform.Service.AirPurifier)), `${device.deviceName} ${device.remoteType}`;
48
48
 
49
49
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
50
50
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -159,7 +159,7 @@ export class AirPurifier {
159
159
  this.CurrentAPMode = this.CurrentMode || 1;
160
160
  this.CurrentAPFanSpeed = this.CurrentFanSpeed || 1;
161
161
  this.APActive = this.Active === 1 ? 'on' : 'off';
162
- payload.parameter = '%s,%s,%s,%s', this.CurrentAPTemp, this.CurrentAPMode, this.CurrentAPFanSpeed, this.APActive;
162
+ payload.parameter = `${this.CurrentAPTemp},${this.CurrentAPMode},${this.CurrentAPFanSpeed},${this.APActive}`;
163
163
 
164
164
  if (this.Active === 1) {
165
165
  if ((this.CurrentTemperature || 24) < (this.LastTemperature || 30)) {
@@ -29,7 +29,7 @@ export class Camera {
29
29
  // you can create multiple services for each accessory
30
30
  (this.service =
31
31
  accessory.getService(this.platform.Service.Switch) ||
32
- accessory.addService(this.platform.Service.Switch)), '%s %s', device.deviceName, device.remoteType;
32
+ accessory.addService(this.platform.Service.Switch)), `${device.deviceName} ${device.remoteType}`;
33
33
 
34
34
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
35
35
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -37,7 +37,7 @@ export class Fan {
37
37
  // you can create multiple services for each accessory
38
38
  (this.service =
39
39
  accessory.getService(this.platform.Service.Fanv2) ||
40
- accessory.addService(this.platform.Service.Fanv2)), '%s %s', device.deviceName, device.remoteType;
40
+ accessory.addService(this.platform.Service.Fanv2)), `${device.deviceName} ${device.remoteType}`;
41
41
 
42
42
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
43
43
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -29,7 +29,7 @@ export class Light {
29
29
  // you can create multiple services for each accessory
30
30
  (this.service =
31
31
  accessory.getService(this.platform.Service.Lightbulb) ||
32
- accessory.addService(this.platform.Service.Lightbulb)), '${} ${}', device.deviceName, device.remoteType;
32
+ accessory.addService(this.platform.Service.Lightbulb)), `${device.deviceName} ${device.remoteType}`;
33
33
 
34
34
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
35
35
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -46,7 +46,7 @@ export class Light {
46
46
  /* this.service
47
47
  .getCharacteristic(this.platform.Characteristic.Brightness)
48
48
  .on(CharacteristicEventTypes.SET, (value: any, callback: CharacteristicGetCallback) => {
49
- this.platform.debug('${} ${} Set Brightness: ${}', device.remoteType, accessory.displayName, value);
49
+ this.platform.debug(`${this.device.remoteType} ${this.accessory.displayName} Set Brightness: ${value}`);
50
50
  this.Brightness = value;
51
51
  if (value > this.Brightness) {
52
52
  this.pushLightBrightnessUpChanges();
@@ -59,7 +59,7 @@ export class Light {
59
59
  }
60
60
 
61
61
  private OnSet(value: CharacteristicValue) {
62
- this.platform.debug('${} ${} Set On: ${}', this.device.remoteType, this.accessory.displayName, value);
62
+ this.platform.debug(`${this.device.remoteType} ${this.accessory.displayName} Set On: ${value}`);
63
63
  this.On = value;
64
64
  if (this.On) {
65
65
  this.pushLightOnChanges();
@@ -31,7 +31,7 @@ export class Others {
31
31
  if (!this.service && this.platform.config.options?.other?.deviceType === 'Fan') {
32
32
  this.service = accessory.addService(
33
33
  this.platform.Service.Fanv2,
34
- `${device.deviceName} ${device.remoteType} Temperature Sensor`,
34
+ `${device.deviceName} ${device.remoteType} Fan`,
35
35
  );
36
36
  this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
37
37
 
@@ -53,7 +53,7 @@ export class TV {
53
53
  // you can create multiple services for each accessory
54
54
  (this.service =
55
55
  accessory.getService(this.platform.Service.Television) ||
56
- accessory.addService(this.platform.Service.Television)), '%s %s', device.deviceName, device.remoteType;
56
+ accessory.addService(this.platform.Service.Television)), `${device.deviceName} ${device.remoteType}`;
57
57
 
58
58
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
59
59
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -63,8 +63,6 @@ export class TV {
63
63
  // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
64
64
  this.service.getCharacteristic(this.platform.Characteristic.ConfiguredName);
65
65
 
66
- //this.service.setCharacteristic(this.platform.Characteristic.Name, `${device.deviceName} ${device.remoteType}`);
67
-
68
66
  // set sleep discovery characteristic
69
67
  this.service.setCharacteristic(
70
68
  this.platform.Characteristic.SleepDiscoveryMode,
@@ -88,7 +86,7 @@ export class TV {
88
86
  // create a new Television Speaker service
89
87
  (this.speakerService =
90
88
  accessory.getService(this.platform.Service.TelevisionSpeaker) ||
91
- accessory.addService(this.platform.Service.TelevisionSpeaker)), '%s %s Speaker', device.deviceName, device.remoteType;
89
+ accessory.addService(this.platform.Service.TelevisionSpeaker)), `${device.deviceName} ${device.remoteType} Speaker`;
92
90
 
93
91
  this.speakerService
94
92
  .setCharacteristic(this.platform.Characteristic.Active, this.platform.Characteristic.Active.ACTIVE)
@@ -48,7 +48,7 @@ export class VacuumCleaner {
48
48
  }
49
49
 
50
50
  private OnSet(value: CharacteristicValue) {
51
- this.platform.debug('${} ${} Set On: ${}', this.device.remoteType, this.accessory.displayName, value);
51
+ this.platform.debug(`${this.device.remoteType} ${this.accessory.displayName} Set On: ${value}`);
52
52
  this.On = value;
53
53
  if (this.On) {
54
54
  this.pushOnChanges();