@switchbot/homebridge-switchbot 2.14.1-beta.0 → 3.0.0-beta.1

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 (147) hide show
  1. package/.github/{npm-version-script.js → npm-version-script.cjs} +1 -1
  2. package/.github/workflows/dependabot.yml +10 -11
  3. package/CHANGELOG.md +11 -2
  4. package/LICENSE +1 -1
  5. package/dist/device/blindtilt.d.ts +10 -6
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +152 -91
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot.d.ts +10 -6
  10. package/dist/device/bot.d.ts.map +1 -1
  11. package/dist/device/bot.js +234 -184
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/ceilinglight.d.ts +10 -6
  14. package/dist/device/ceilinglight.d.ts.map +1 -1
  15. package/dist/device/ceilinglight.js +110 -64
  16. package/dist/device/ceilinglight.js.map +1 -1
  17. package/dist/device/colorbulb.d.ts +10 -6
  18. package/dist/device/colorbulb.d.ts.map +1 -1
  19. package/dist/device/colorbulb.js +121 -69
  20. package/dist/device/colorbulb.js.map +1 -1
  21. package/dist/device/contact.d.ts +10 -6
  22. package/dist/device/contact.d.ts.map +1 -1
  23. package/dist/device/contact.js +111 -66
  24. package/dist/device/contact.js.map +1 -1
  25. package/dist/device/curtain.d.ts +10 -6
  26. package/dist/device/curtain.d.ts.map +1 -1
  27. package/dist/device/curtain.js +165 -107
  28. package/dist/device/curtain.js.map +1 -1
  29. package/dist/device/hub.d.ts +10 -6
  30. package/dist/device/hub.d.ts.map +1 -1
  31. package/dist/device/hub.js +88 -51
  32. package/dist/device/hub.js.map +1 -1
  33. package/dist/device/humidifier.d.ts +10 -6
  34. package/dist/device/humidifier.d.ts.map +1 -1
  35. package/dist/device/humidifier.js +133 -90
  36. package/dist/device/humidifier.js.map +1 -1
  37. package/dist/device/iosensor.d.ts +10 -6
  38. package/dist/device/iosensor.d.ts.map +1 -1
  39. package/dist/device/iosensor.js +109 -66
  40. package/dist/device/iosensor.js.map +1 -1
  41. package/dist/device/lightstrip.d.ts +10 -6
  42. package/dist/device/lightstrip.d.ts.map +1 -1
  43. package/dist/device/lightstrip.js +110 -65
  44. package/dist/device/lightstrip.js.map +1 -1
  45. package/dist/device/lock.d.ts +11 -7
  46. package/dist/device/lock.d.ts.map +1 -1
  47. package/dist/device/lock.js +125 -84
  48. package/dist/device/lock.js.map +1 -1
  49. package/dist/device/meter.d.ts +10 -6
  50. package/dist/device/meter.d.ts.map +1 -1
  51. package/dist/device/meter.js +103 -63
  52. package/dist/device/meter.js.map +1 -1
  53. package/dist/device/meterplus.d.ts +10 -6
  54. package/dist/device/meterplus.d.ts.map +1 -1
  55. package/dist/device/meterplus.js +103 -63
  56. package/dist/device/meterplus.js.map +1 -1
  57. package/dist/device/motion.d.ts +10 -6
  58. package/dist/device/motion.d.ts.map +1 -1
  59. package/dist/device/motion.js +95 -54
  60. package/dist/device/motion.js.map +1 -1
  61. package/dist/device/plug.d.ts +10 -6
  62. package/dist/device/plug.d.ts.map +1 -1
  63. package/dist/device/plug.js +68 -40
  64. package/dist/device/plug.js.map +1 -1
  65. package/dist/device/robotvacuumcleaner.d.ts +10 -6
  66. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  67. package/dist/device/robotvacuumcleaner.js +86 -52
  68. package/dist/device/robotvacuumcleaner.js.map +1 -1
  69. package/dist/homebridge-ui/server.js +5 -10
  70. package/dist/homebridge-ui/server.js.map +1 -1
  71. package/dist/index.d.ts +1 -4
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +9 -5
  74. package/dist/index.js.map +1 -1
  75. package/dist/irdevice/airconditioner.d.ts +9 -5
  76. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  77. package/dist/irdevice/airconditioner.js +105 -71
  78. package/dist/irdevice/airconditioner.js.map +1 -1
  79. package/dist/irdevice/airpurifier.d.ts +9 -5
  80. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  81. package/dist/irdevice/airpurifier.js +78 -46
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts +9 -5
  84. package/dist/irdevice/camera.d.ts.map +1 -1
  85. package/dist/irdevice/camera.js +41 -25
  86. package/dist/irdevice/camera.js.map +1 -1
  87. package/dist/irdevice/fan.d.ts +9 -5
  88. package/dist/irdevice/fan.d.ts.map +1 -1
  89. package/dist/irdevice/fan.js +62 -39
  90. package/dist/irdevice/fan.js.map +1 -1
  91. package/dist/irdevice/light.d.ts +9 -5
  92. package/dist/irdevice/light.d.ts.map +1 -1
  93. package/dist/irdevice/light.js +67 -45
  94. package/dist/irdevice/light.js.map +1 -1
  95. package/dist/irdevice/other.d.ts +9 -5
  96. package/dist/irdevice/other.d.ts.map +1 -1
  97. package/dist/irdevice/other.js +176 -150
  98. package/dist/irdevice/other.js.map +1 -1
  99. package/dist/irdevice/tv.d.ts +9 -5
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js +81 -62
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts +9 -5
  104. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  105. package/dist/irdevice/vacuumcleaner.js +41 -25
  106. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  107. package/dist/irdevice/waterheater.d.ts +9 -5
  108. package/dist/irdevice/waterheater.d.ts.map +1 -1
  109. package/dist/irdevice/waterheater.js +48 -32
  110. package/dist/irdevice/waterheater.js.map +1 -1
  111. package/dist/platform.d.ts +8 -9
  112. package/dist/platform.d.ts.map +1 -1
  113. package/dist/platform.js +151 -162
  114. package/dist/platform.js.map +1 -1
  115. package/dist/settings.js +7 -14
  116. package/dist/settings.js.map +1 -1
  117. package/dist/utils.js +1 -5
  118. package/dist/utils.js.map +1 -1
  119. package/package.json +20 -9
  120. package/src/device/blindtilt.ts +84 -72
  121. package/src/device/bot.ts +191 -180
  122. package/src/device/ceilinglight.ts +57 -44
  123. package/src/device/colorbulb.ts +53 -40
  124. package/src/device/contact.ts +71 -60
  125. package/src/device/curtain.ts +98 -86
  126. package/src/device/hub.ts +57 -45
  127. package/src/device/humidifier.ts +88 -77
  128. package/src/device/iosensor.ts +67 -55
  129. package/src/device/lightstrip.ts +56 -43
  130. package/src/device/lock.ts +91 -81
  131. package/src/device/meter.ts +63 -51
  132. package/src/device/meterplus.ts +67 -55
  133. package/src/device/motion.ts +58 -47
  134. package/src/device/plug.ts +38 -27
  135. package/src/device/robotvacuumcleaner.ts +49 -38
  136. package/src/index.ts +10 -7
  137. package/src/irdevice/airconditioner.ts +71 -63
  138. package/src/irdevice/airpurifier.ts +48 -40
  139. package/src/irdevice/camera.ts +28 -20
  140. package/src/irdevice/fan.ts +42 -34
  141. package/src/irdevice/light.ts +48 -40
  142. package/src/irdevice/other.ts +164 -156
  143. package/src/irdevice/tv.ts +66 -58
  144. package/src/irdevice/vacuumcleaner.ts +28 -20
  145. package/src/irdevice/waterheater.ts +35 -27
  146. package/src/platform.ts +65 -47
  147. package/tsconfig.json +11 -13
@@ -1,10 +1,10 @@
1
1
  import { request } from 'undici';
2
- import { sleep } from '../utils';
2
+ import { sleep } from '../utils.js';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { skipWhile } from 'rxjs/operators';
5
- import { SwitchBotPlatform } from '../platform';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '../settings';
5
+ import { SwitchBotPlatform } from '../platform.js';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices, SwitchBotPlatformConfig } from '../settings.js';
8
8
 
9
9
  /**
10
10
  * Platform Accessory
@@ -12,6 +12,10 @@ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '.
12
12
  * Each accessory may expose multiple services of different service types.
13
13
  */
14
14
  export class Motion {
15
+ public readonly api: API;
16
+ public readonly log: Logging;
17
+ public readonly config!: SwitchBotPlatformConfig;
18
+ protected readonly hap: HAP;
15
19
  // Services
16
20
  batteryService: Service;
17
21
  motionSensorService!: Service;
@@ -51,20 +55,27 @@ export class Motion {
51
55
  doMotionUpdate!: Subject<void>;
52
56
 
53
57
  // Connection
54
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
55
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
58
+ private readonly OpenAPI: boolean;
59
+ private readonly BLE: boolean;
56
60
 
57
61
  constructor(
58
62
  private readonly platform: SwitchBotPlatform,
59
63
  private accessory: PlatformAccessory,
60
64
  public device: device & devicesConfig,
61
65
  ) {
66
+ this.api = this.platform.api;
67
+ this.log = this.platform.log;
68
+ this.config = this.platform.config;
69
+ this.hap = this.api.hap;
70
+ // Connection
71
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
72
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
62
73
  // default placeholders
63
- this.logs(device);
74
+ this.deviceLogs(device);
64
75
  this.scan(device);
65
76
  this.refreshRate(device);
66
77
  this.context();
67
- this.config(device);
78
+ this.deviceConfig(device);
68
79
 
69
80
  // this is subject we use to track when we need to POST changes to the SwitchBot API
70
81
  this.doMotionUpdate = new Subject();
@@ -75,21 +86,21 @@ export class Motion {
75
86
 
76
87
  // set accessory information
77
88
  accessory
78
- .getService(this.platform.Service.AccessoryInformation)!
79
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
80
- .setCharacteristic(this.platform.Characteristic.Model, 'W1101500')
81
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
82
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
89
+ .getService(this.hap.Service.AccessoryInformation)!
90
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
91
+ .setCharacteristic(this.hap.Characteristic.Model, 'W1101500')
92
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
93
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
83
94
 
84
95
  // get the Battery service if it exists, otherwise create a new Motion service
85
96
  // you can create multiple services for each accessory
86
97
  const motionSensorService = `${accessory.displayName} Motion Sensor`;
87
- (this.motionSensorService = accessory.getService(this.platform.Service.MotionSensor)
88
- || accessory.addService(this.platform.Service.MotionSensor)), motionSensorService;
98
+ (this.motionSensorService = accessory.getService(this.hap.Service.MotionSensor)
99
+ || accessory.addService(this.hap.Service.MotionSensor)), motionSensorService;
89
100
 
90
- this.motionSensorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
91
- if (!this.motionSensorService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
92
- this.motionSensorService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
101
+ this.motionSensorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
102
+ if (!this.motionSensorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
103
+ this.motionSensorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
93
104
  }
94
105
  // each service must implement at-minimum the "required characteristics" for the given service type
95
106
  // see https://developers.homebridge.io/#/service/MotionSensor
@@ -97,30 +108,30 @@ export class Motion {
97
108
  // Light Sensor Service
98
109
  if (device.motion?.hide_lightsensor) {
99
110
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
100
- this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
111
+ this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor);
101
112
  accessory.removeService(this.lightSensorService!);
102
113
  } else if (!this.lightSensorService) {
103
114
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
104
115
  const lightSensorService = `${accessory.displayName} Light Sensor`;
105
- (this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor)
106
- || this.accessory.addService(this.platform.Service.LightSensor)), lightSensorService;
116
+ (this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
117
+ || this.accessory.addService(this.hap.Service.LightSensor)), lightSensorService;
107
118
 
108
- this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
109
- this.lightSensorService.setCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
119
+ this.lightSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Light Sensor`);
120
+ this.lightSensorService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
110
121
  } else {
111
122
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
112
123
  }
113
124
 
114
125
  // Battery Service
115
126
  const batteryService = `${accessory.displayName} Battery`;
116
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
117
- || accessory.addService(this.platform.Service.Battery)), batteryService;
127
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
128
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
118
129
 
119
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
120
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
121
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
130
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
131
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
132
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
122
133
  }
123
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
134
+ this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
124
135
 
125
136
  // Retrieve initial values and updateHomekit
126
137
  this.updateHomeKitCharacteristics();
@@ -141,14 +152,14 @@ export class Motion {
141
152
  const { detectionState } = context;
142
153
  const { MotionDetected } = this;
143
154
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
144
- '(detectionState) = ' +
145
- `Webhook:(${detectionState}), ` +
146
- `current:(${MotionDetected})`);
155
+ '(detectionState) = ' +
156
+ `Webhook:(${detectionState}), ` +
157
+ `current:(${MotionDetected})`);
147
158
  this.MotionDetected = detectionState === 'DETECTED' ? true : false;
148
159
  this.updateHomeKitCharacteristics();
149
160
  } catch (e: any) {
150
161
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
151
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
162
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
152
163
  }
153
164
  };
154
165
  }
@@ -206,9 +217,9 @@ export class Motion {
206
217
  }
207
218
  this.BatteryLevel = this.BLE_BatteryLevel!;
208
219
  if (this.BatteryLevel < 10) {
209
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
220
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
210
221
  } else {
211
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
222
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
212
223
  }
213
224
  this.debugLog(
214
225
  `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
@@ -238,9 +249,9 @@ export class Motion {
238
249
  // Battery
239
250
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
240
251
  if (this.BatteryLevel < 10) {
241
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
252
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
242
253
  } else {
243
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
254
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
244
255
  }
245
256
  if (Number.isNaN(this.BatteryLevel)) {
246
257
  this.BatteryLevel = 100;
@@ -394,21 +405,21 @@ export class Motion {
394
405
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
395
406
  } else {
396
407
  this.accessory.context.MotionDetected = this.MotionDetected;
397
- this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
408
+ this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionDetected);
398
409
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
399
410
  }
400
411
  if (this.BatteryLevel === undefined) {
401
412
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
402
413
  } else {
403
414
  this.accessory.context.BatteryLevel = this.BatteryLevel;
404
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
415
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
405
416
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
406
417
  }
407
418
  if (this.StatusLowBattery === undefined) {
408
419
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
409
420
  } else {
410
421
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
411
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
422
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
412
423
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
413
424
  }
414
425
  if (this.BLE) {
@@ -416,7 +427,7 @@ export class Motion {
416
427
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
417
428
  } else {
418
429
  this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
419
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
430
+ this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
420
431
  this.debugLog(
421
432
  `${this.device.deviceType}: ${this.accessory.displayName}` +
422
433
  ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
@@ -563,11 +574,11 @@ export class Motion {
563
574
  }
564
575
 
565
576
  async apiError(e: any): Promise<void> {
566
- this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
567
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
568
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
577
+ this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
578
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
579
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
569
580
  if (this.BLE) {
570
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
581
+ this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
571
582
  }
572
583
  }
573
584
 
@@ -589,7 +600,7 @@ export class Motion {
589
600
  }
590
601
  }
591
602
 
592
- async config(device: device & devicesConfig): Promise<void> {
603
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
593
604
  let config = {};
594
605
  if (device.motion) {
595
606
  config = device.motion;
@@ -617,7 +628,7 @@ export class Motion {
617
628
  }
618
629
  }
619
630
 
620
- async logs(device: device & devicesConfig): Promise<void> {
631
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
621
632
  if (this.platform.debugMode) {
622
633
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
623
634
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -1,12 +1,16 @@
1
1
  import { request } from 'undici';
2
- import { sleep } from '../utils';
2
+ import { sleep } from '../utils.js';
3
3
  import { interval, Subject } from 'rxjs';
4
- import { SwitchBotPlatform } from '../platform';
4
+ import { SwitchBotPlatform } from '../platform.js';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, deviceStatus, ad, serviceData, Devices } from '../settings';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, deviceStatus, ad, serviceData, Devices, SwitchBotPlatformConfig } from '../settings.js';
8
8
 
9
9
  export class Plug {
10
+ public readonly api: API;
11
+ public readonly log: Logging;
12
+ public readonly config!: SwitchBotPlatformConfig;
13
+ protected readonly hap: HAP;
10
14
  // Services
11
15
  outletService: Service;
12
16
 
@@ -32,20 +36,27 @@ export class Plug {
32
36
  doPlugUpdate!: Subject<void>;
33
37
 
34
38
  // Connection
35
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
36
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
39
+ private readonly OpenAPI: boolean;
40
+ private readonly BLE: boolean;
37
41
 
38
42
  constructor(
39
43
  private readonly platform: SwitchBotPlatform,
40
44
  private accessory: PlatformAccessory,
41
45
  public device: device & devicesConfig,
42
46
  ) {
47
+ this.api = this.platform.api;
48
+ this.log = this.platform.log;
49
+ this.config = this.platform.config;
50
+ this.hap = this.api.hap;
51
+ // Connection
52
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
53
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
43
54
  // default placeholders
44
- this.logs(device);
55
+ this.deviceLogs(device);
45
56
  this.scan(device);
46
57
  this.refreshRate(device);
47
58
  this.context();
48
- this.config(device);
59
+ this.deviceConfig(device);
49
60
 
50
61
  // this is subject we use to track when we need to POST changes to the SwitchBot API
51
62
  this.doPlugUpdate = new Subject();
@@ -56,27 +67,27 @@ export class Plug {
56
67
 
57
68
  // set accessory information
58
69
  accessory
59
- .getService(this.platform.Service.AccessoryInformation)!
60
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
61
- .setCharacteristic(this.platform.Characteristic.Model, this.model(device))
62
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
63
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
70
+ .getService(this.hap.Service.AccessoryInformation)!
71
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
72
+ .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
73
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
74
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
64
75
 
65
76
  // get the Outlet service if it exists, otherwise create a new Outlet service
66
77
  // you can create multiple services for each accessory
67
78
  const outletService = `${accessory.displayName} ${device.deviceType}`;
68
- (this.outletService = accessory.getService(this.platform.Service.Outlet)
69
- || accessory.addService(this.platform.Service.Outlet)), outletService;
79
+ (this.outletService = accessory.getService(this.hap.Service.Outlet)
80
+ || accessory.addService(this.hap.Service.Outlet)), outletService;
70
81
 
71
- this.outletService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
72
- if (!this.outletService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
73
- this.outletService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
82
+ this.outletService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
83
+ if (!this.outletService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
84
+ this.outletService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
74
85
  }
75
86
  // each service must implement at-minimum the "required characteristics" for the given service type
76
87
  // see https://developers.homebridge.io/#/service/Outlet
77
88
 
78
89
  // create handlers for required characteristics
79
- this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
90
+ this.outletService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
80
91
 
81
92
  // Update Homekit
82
93
  this.updateHomeKitCharacteristics();
@@ -97,14 +108,14 @@ export class Plug {
97
108
  const { powerState } = context;
98
109
  const { On } = this;
99
110
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
100
- '(powerState) = ' +
101
- `Webhook:(${powerState}), ` +
102
- `current:(${On})`);
111
+ '(powerState) = ' +
112
+ `Webhook:(${powerState}), ` +
113
+ `current:(${On})`);
103
114
  this.On = powerState === 'ON' ? true : false;
104
115
  this.updateHomeKitCharacteristics();
105
116
  } catch (e: any) {
106
117
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
107
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
118
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
108
119
  }
109
120
  };
110
121
  }
@@ -442,7 +453,7 @@ export class Plug {
442
453
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
443
454
  } else {
444
455
  this.accessory.context.On = this.On;
445
- this.outletService.updateCharacteristic(this.platform.Characteristic.On, this.On);
456
+ this.outletService.updateCharacteristic(this.hap.Characteristic.On, this.On);
446
457
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
447
458
  }
448
459
  }
@@ -595,7 +606,7 @@ export class Plug {
595
606
  }
596
607
 
597
608
  async apiError(e: any): Promise<void> {
598
- this.outletService.updateCharacteristic(this.platform.Characteristic.On, e);
609
+ this.outletService.updateCharacteristic(this.hap.Characteristic.On, e);
599
610
  }
600
611
 
601
612
  async context() {
@@ -616,7 +627,7 @@ export class Plug {
616
627
  }
617
628
  }
618
629
 
619
- async config(device: device & devicesConfig): Promise<void> {
630
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
620
631
  let config = {};
621
632
  if (device.plug) {
622
633
  config = device.plug;
@@ -647,7 +658,7 @@ export class Plug {
647
658
  }
648
659
  }
649
660
 
650
- async logs(device: device & devicesConfig): Promise<void> {
661
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
651
662
  if (this.platform.debugMode) {
652
663
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
653
664
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -1,12 +1,16 @@
1
1
  import { request } from 'undici';
2
- import { sleep } from '../utils';
2
+ import { sleep } from '../utils.js';
3
3
  import { interval, Subject } from 'rxjs';
4
- import { SwitchBotPlatform } from '../platform';
4
+ import { SwitchBotPlatform } from '../platform.js';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, deviceStatus, ad, serviceData, Devices } from '../settings';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, deviceStatus, ad, serviceData, Devices, SwitchBotPlatformConfig } from '../settings.js';
8
8
 
9
9
  export class RobotVacuumCleaner {
10
+ public readonly api: API;
11
+ public readonly log: Logging;
12
+ public readonly config!: SwitchBotPlatformConfig;
13
+ protected readonly hap: HAP;
10
14
  // Services
11
15
  batteryService: Service;
12
16
  robotVacuumCleanerService: Service;
@@ -39,20 +43,27 @@ export class RobotVacuumCleaner {
39
43
  doRobotVacuumCleanerUpdate!: Subject<void>;
40
44
 
41
45
  // Connection
42
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
43
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
46
+ private readonly OpenAPI: boolean;
47
+ private readonly BLE: boolean;
44
48
 
45
49
  constructor(
46
50
  private readonly platform: SwitchBotPlatform,
47
51
  private accessory: PlatformAccessory,
48
52
  public device: device & devicesConfig,
49
53
  ) {
54
+ this.api = this.platform.api;
55
+ this.log = this.platform.log;
56
+ this.config = this.platform.config;
57
+ this.hap = this.api.hap;
58
+ // Connection
59
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
60
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
50
61
  // default placeholders
51
- this.logs(device);
62
+ this.deviceLogs(device);
52
63
  this.scan(device);
53
64
  this.refreshRate(device);
54
65
  this.context();
55
- this.config(device);
66
+ this.deviceConfig(device);
56
67
 
57
68
  // this is subject we use to track when we need to POST changes to the SwitchBot API
58
69
  this.doRobotVacuumCleanerUpdate = new Subject();
@@ -63,31 +74,31 @@ export class RobotVacuumCleaner {
63
74
 
64
75
  // set accessory information
65
76
  accessory
66
- .getService(this.platform.Service.AccessoryInformation)!
67
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
68
- .setCharacteristic(this.platform.Characteristic.Model, this.model(device))
69
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
70
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
77
+ .getService(this.hap.Service.AccessoryInformation)!
78
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
79
+ .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
80
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
81
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
71
82
 
72
83
  // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
73
84
  // you can create multiple services for each accessory
74
85
  const robotVacuumCleanerService = `${accessory.displayName} ${device.deviceType}`;
75
- (this.robotVacuumCleanerService = accessory.getService(this.platform.Service.Lightbulb)
76
- || accessory.addService(this.platform.Service.Lightbulb)), robotVacuumCleanerService;
86
+ (this.robotVacuumCleanerService = accessory.getService(this.hap.Service.Lightbulb)
87
+ || accessory.addService(this.hap.Service.Lightbulb)), robotVacuumCleanerService;
77
88
 
78
- this.robotVacuumCleanerService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
79
- if (!this.robotVacuumCleanerService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
80
- this.robotVacuumCleanerService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
89
+ this.robotVacuumCleanerService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
90
+ if (!this.robotVacuumCleanerService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
91
+ this.robotVacuumCleanerService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
81
92
  }
82
93
  // each service must implement at-minimum the "required characteristics" for the given service type
83
94
  // see https://developers.homebridge.io/#/service/Lightbulb
84
95
 
85
96
  // create handlers for required characteristics
86
- this.robotVacuumCleanerService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
97
+ this.robotVacuumCleanerService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
87
98
 
88
99
  // handle Brightness events using the Brightness characteristic
89
100
  this.robotVacuumCleanerService
90
- .getCharacteristic(this.platform.Characteristic.Brightness)
101
+ .getCharacteristic(this.hap.Characteristic.Brightness)
91
102
  .setProps({
92
103
  minStep: 25,
93
104
  minValue: 0,
@@ -102,12 +113,12 @@ export class RobotVacuumCleaner {
102
113
 
103
114
  // Battery Service
104
115
  const batteryService = `${accessory.displayName} Battery`;
105
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
106
- || accessory.addService(this.platform.Service.Battery)), batteryService;
116
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
117
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
107
118
 
108
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
109
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
110
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
119
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
120
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
121
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
111
122
  }
112
123
 
113
124
  // Update Homekit
@@ -129,15 +140,15 @@ export class RobotVacuumCleaner {
129
140
  const { onlineStatus, battery } = context;
130
141
  const { On, BatteryLevel } = this;
131
142
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
132
- '(onlineStatus, battery) = ' +
133
- `Webhook:(${onlineStatus}, ${battery}), ` +
134
- `current:(${On}, ${BatteryLevel})`);
143
+ '(onlineStatus, battery) = ' +
144
+ `Webhook:(${onlineStatus}, ${battery}), ` +
145
+ `current:(${On}, ${BatteryLevel})`);
135
146
  this.On = onlineStatus === 'online' ? true : false;
136
147
  this.BatteryLevel = battery;
137
148
  this.updateHomeKitCharacteristics();
138
149
  } catch (e: any) {
139
150
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
140
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
151
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
141
152
  }
142
153
  };
143
154
  }
@@ -215,9 +226,9 @@ export class RobotVacuumCleaner {
215
226
  // Battery
216
227
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
217
228
  if (this.BatteryLevel < 15) {
218
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
229
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
219
230
  } else {
220
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
231
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
221
232
  }
222
233
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
223
234
 
@@ -583,7 +594,7 @@ export class RobotVacuumCleaner {
583
594
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
584
595
  } else {
585
596
  this.accessory.context.On = this.On;
586
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.On, this.On);
597
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, this.On);
587
598
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
588
599
  }
589
600
  // Brightness
@@ -591,7 +602,7 @@ export class RobotVacuumCleaner {
591
602
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
592
603
  } else {
593
604
  this.accessory.context.Brightness = this.Brightness;
594
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
605
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
595
606
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
596
607
  }
597
608
  // BatteryLevel
@@ -599,7 +610,7 @@ export class RobotVacuumCleaner {
599
610
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
600
611
  } else {
601
612
  this.accessory.context.BatteryLevel = this.BatteryLevel;
602
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
613
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
603
614
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
604
615
  }
605
616
  // StatusLowBattery
@@ -607,7 +618,7 @@ export class RobotVacuumCleaner {
607
618
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
608
619
  } else {
609
620
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
610
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
621
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
611
622
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
612
623
  }
613
624
  }
@@ -759,7 +770,7 @@ export class RobotVacuumCleaner {
759
770
  }
760
771
 
761
772
  async apiError(e: any): Promise<void> {
762
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.On, e);
773
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, e);
763
774
  }
764
775
 
765
776
  async context() {
@@ -785,7 +796,7 @@ export class RobotVacuumCleaner {
785
796
  }
786
797
  }
787
798
 
788
- async config(device: device & devicesConfig): Promise<void> {
799
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
789
800
  let config = {};
790
801
  if (device.plug) {
791
802
  config = device.plug;
@@ -816,7 +827,7 @@ export class RobotVacuumCleaner {
816
827
  }
817
828
  }
818
829
 
819
- async logs(device: device & devicesConfig): Promise<void> {
830
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
820
831
  if (this.platform.debugMode) {
821
832
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
822
833
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
package/src/index.ts CHANGED
@@ -1,10 +1,13 @@
1
+ /* Copyright(C) 2021-2023, SwitchBot (https://github.com/SwitchBot). All rights reserved.
2
+ *
3
+ * index.ts: @switchbot/homebridge-switchbot plugin registration.
4
+ */
5
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
1
6
  import { API } from 'homebridge';
2
- import { PLATFORM_NAME } from './settings';
3
- import { SwitchBotPlatform } from './platform';
7
+ import { SwitchBotPlatform } from './platform.js';
4
8
 
5
- /**
6
- * This method registers the platform with Homebridge
7
- */
8
- export = (api: API): void => {
9
- api.registerPlatform(PLATFORM_NAME, SwitchBotPlatform);
9
+ // Register our platform with homebridge.
10
+ export default (api: API): void => {
11
+
12
+ api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SwitchBotPlatform);
10
13
  };