@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10

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 (197) hide show
  1. package/README.md +16 -0
  2. package/dist/device/blindtilt.d.ts +15 -6
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +224 -370
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +9 -4
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +197 -221
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +10 -5
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +214 -292
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +10 -5
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +335 -421
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +9 -4
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +121 -200
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +20 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +229 -403
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +41 -22
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +212 -181
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +9 -4
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +204 -249
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +9 -5
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +107 -233
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +12 -4
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +193 -235
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +9 -4
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +92 -159
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +10 -5
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +288 -370
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +10 -5
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +172 -219
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +9 -4
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +94 -160
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +9 -4
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +95 -157
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +9 -4
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +85 -144
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +9 -4
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +100 -127
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +172 -259
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +12 -4
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +106 -131
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +2 -1
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +2 -3
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +2 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +2 -3
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +2 -1
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +2 -3
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +2 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +2 -3
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +3 -1
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +5 -1
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts +2 -1
  99. package/dist/irdevice/light.d.ts.map +1 -1
  100. package/dist/irdevice/light.js +2 -3
  101. package/dist/irdevice/light.js.map +1 -1
  102. package/dist/irdevice/other.d.ts +2 -1
  103. package/dist/irdevice/other.d.ts.map +1 -1
  104. package/dist/irdevice/other.js +2 -3
  105. package/dist/irdevice/other.js.map +1 -1
  106. package/dist/irdevice/tv.d.ts +2 -1
  107. package/dist/irdevice/tv.d.ts.map +1 -1
  108. package/dist/irdevice/tv.js +2 -3
  109. package/dist/irdevice/tv.js.map +1 -1
  110. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  111. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  112. package/dist/irdevice/vacuumcleaner.js +2 -3
  113. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  114. package/dist/irdevice/waterheater.d.ts +2 -1
  115. package/dist/irdevice/waterheater.d.ts.map +1 -1
  116. package/dist/irdevice/waterheater.js +2 -3
  117. package/dist/irdevice/waterheater.js.map +1 -1
  118. package/dist/platform.d.ts +14 -14
  119. package/dist/platform.d.ts.map +1 -1
  120. package/dist/platform.js +102 -86
  121. package/dist/platform.js.map +1 -1
  122. package/dist/settings.d.ts +49 -190
  123. package/dist/settings.d.ts.map +1 -1
  124. package/dist/settings.js +19 -0
  125. package/dist/settings.js.map +1 -1
  126. package/dist/types/bledevicestatus.d.ts +279 -0
  127. package/dist/types/bledevicestatus.d.ts.map +1 -0
  128. package/dist/types/bledevicestatus.js +2 -0
  129. package/dist/types/bledevicestatus.js.map +1 -0
  130. package/dist/types/devicelist.d.ts +89 -0
  131. package/dist/types/devicelist.d.ts.map +1 -0
  132. package/dist/types/devicelist.js +6 -0
  133. package/dist/types/devicelist.js.map +1 -0
  134. package/dist/types/deviceresponse.d.ts +13 -0
  135. package/dist/types/deviceresponse.d.ts.map +1 -0
  136. package/dist/types/deviceresponse.js +2 -0
  137. package/dist/types/deviceresponse.js.map +1 -0
  138. package/dist/types/devicestatus.d.ts +149 -0
  139. package/dist/types/devicestatus.d.ts.map +1 -0
  140. package/dist/types/devicestatus.js +3 -0
  141. package/dist/types/devicestatus.js.map +1 -0
  142. package/dist/types/devicewebhookstatus.d.ts +156 -0
  143. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  144. package/dist/types/devicewebhookstatus.js +2 -0
  145. package/dist/types/devicewebhookstatus.js.map +1 -0
  146. package/dist/types/irdevicelist.d.ts +10 -0
  147. package/dist/types/irdevicelist.d.ts.map +1 -0
  148. package/dist/types/irdevicelist.js +2 -0
  149. package/dist/types/irdevicelist.js.map +1 -0
  150. package/dist/types/pushbody.d.ts +6 -0
  151. package/dist/types/pushbody.d.ts.map +1 -0
  152. package/dist/types/pushbody.js +2 -0
  153. package/dist/types/pushbody.js.map +1 -0
  154. package/dist/utils.d.ts +25 -0
  155. package/dist/utils.d.ts.map +1 -1
  156. package/dist/utils.js +28 -0
  157. package/dist/utils.js.map +1 -1
  158. package/package.json +11 -11
  159. package/src/device/blindtilt.ts +254 -376
  160. package/src/device/bot.ts +209 -224
  161. package/src/device/ceilinglight.ts +227 -297
  162. package/src/device/colorbulb.ts +345 -435
  163. package/src/device/contact.ts +139 -200
  164. package/src/device/curtain.ts +254 -414
  165. package/src/device/device.ts +221 -190
  166. package/src/device/fan.ts +222 -253
  167. package/src/device/hub.ts +127 -248
  168. package/src/device/humidifier.ts +217 -246
  169. package/src/device/iosensor.ts +115 -168
  170. package/src/device/lightstrip.ts +303 -380
  171. package/src/device/lock.ts +188 -224
  172. package/src/device/meter.ts +117 -179
  173. package/src/device/meterplus.ts +118 -176
  174. package/src/device/motion.ts +100 -146
  175. package/src/device/plug.ts +111 -131
  176. package/src/device/robotvacuumcleaner.ts +191 -258
  177. package/src/device/waterdetector.ts +124 -141
  178. package/src/irdevice/airconditioner.ts +4 -4
  179. package/src/irdevice/airpurifier.ts +4 -4
  180. package/src/irdevice/camera.ts +4 -4
  181. package/src/irdevice/fan.ts +4 -4
  182. package/src/irdevice/irdevice.ts +8 -2
  183. package/src/irdevice/light.ts +4 -4
  184. package/src/irdevice/other.ts +4 -4
  185. package/src/irdevice/tv.ts +4 -4
  186. package/src/irdevice/vacuumcleaner.ts +4 -4
  187. package/src/irdevice/waterheater.ts +4 -4
  188. package/src/platform.ts +104 -87
  189. package/src/settings.ts +30 -238
  190. package/src/types/bledevicestatus.ts +305 -0
  191. package/src/types/devicelist.ts +124 -0
  192. package/src/types/deviceresponse.ts +15 -0
  193. package/src/types/devicestatus.ts +178 -0
  194. package/src/types/devicewebhookstatus.ts +196 -0
  195. package/src/types/irdevicelist.ts +15 -0
  196. package/src/types/pushbody.ts +9 -0
  197. package/src/utils.ts +33 -0
package/src/device/hub.ts CHANGED
@@ -5,11 +5,16 @@
5
5
  import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
7
  import { convertUnits } from '../utils.js';
8
- import { Subject, interval, skipWhile } from 'rxjs';
8
+ import { Subject, interval, skipWhile} from 'rxjs';
9
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
9
10
 
11
+ import type { devicesConfig } from '../settings.js';
12
+ import type { device } from '../types/devicelist.js';
10
13
  import type { SwitchBotPlatform } from '../platform.js';
14
+ import type { hub2ServiceData } from '../types/bledevicestatus.js';
15
+ import type { hub2Status } from '../types/devicestatus.js';
16
+ import type { hub2WebhookContext } from '../types/devicewebhookstatus.js';
11
17
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
12
- import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
13
18
 
14
19
  export class Hub extends deviceBase {
15
20
  // Services
@@ -139,6 +144,9 @@ export class Hub extends deviceBase {
139
144
  // Retrieve initial values and update Homekit
140
145
  this.updateHomeKitCharacteristics();
141
146
 
147
+ //regisiter webhook event handler
148
+ this.registerWebhook();
149
+
142
150
  // Start an update interval
143
151
  interval(this.deviceRefreshRate * 1000)
144
152
  .pipe(skipWhile(() => this.hubUpdateInProgress))
@@ -146,51 +154,58 @@ export class Hub extends deviceBase {
146
154
  await this.refreshStatus();
147
155
  });
148
156
 
149
- //regisiter webhook event handler
150
- this.registerWebhook(accessory, device);
151
-
152
157
  }
153
158
 
154
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
155
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
156
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(serviceData)}`);
159
+ async BLEparseStatus(serviceData: hub2ServiceData): Promise<void> {
160
+ await this.debugLog('BLEparseStatus');
161
+ // CurrentTemperature
162
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
163
+ this.TemperatureSensor.CurrentTemperature = serviceData.temperature;
164
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
165
+ }
166
+ // CurrentRelativeHumidity
167
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
168
+ this.HumiditySensor!.CurrentRelativeHumidity = Number(serviceData.humidity);
169
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
170
+ }
171
+ // CurrentAmbientLightLevel
172
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
173
+ const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
174
+ const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
175
+ const lightLevel = Number(serviceData.lightLevel);
176
+ this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
177
+ await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
178
+ }
157
179
  }
158
180
 
159
- async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
160
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
181
+ async openAPIparseStatus(deviceStatus: hub2Status): Promise<void> {
182
+ await this.debugLog('openAPIparseStatus');
161
183
  // CurrentRelativeHumidity
162
- if (!this.device.hub?.hide_humidity) {
163
- this.HumiditySensor!.CurrentRelativeHumidity = Number(deviceStatus.body.humidity);
164
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
184
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
185
+ this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
186
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
165
187
  }
166
188
 
167
189
  // CurrentTemperature
168
- if (!this.device.hub?.hide_temperature) {
169
- this.TemperatureSensor!.CurrentTemperature = Number(deviceStatus.body.temperature);
170
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
190
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
191
+ this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
192
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
171
193
  }
172
194
 
173
- // Brightness
174
- if (!this.device.hub?.hide_lightsensor) {
175
- if (!this.device.curtain?.hide_lightsensor) {
176
- const set_minLux = this.device.curtain?.set_minLux ?? 1;
177
- const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
178
- const spaceBetweenLevels = 19;
179
- const lightLevel = deviceStatus.body.lightLevel;
180
- await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
181
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${deviceStatus.body.lightLevel},`
182
- + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
183
- }
184
- if (!this.device.hub?.hide_lightsensor) {
185
- this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
186
- }
195
+ // LightSensor
196
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
197
+ const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
198
+ const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
199
+ const lightLevel = Number(deviceStatus.lightLevel);
200
+ this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
201
+ await this.debugLog(`LightLevel: ${deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
187
202
  }
188
203
 
189
204
  // Firmware Version
190
- const version = deviceStatus.body.version?.toString();
191
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
192
- if (deviceStatus.body.version) {
193
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
205
+ const version = deviceStatus.version.toString();
206
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
207
+ if (deviceStatus.version) {
208
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
194
209
  this.accessory
195
210
  .getService(this.hap.Service.AccessoryInformation)!
196
211
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -198,7 +213,36 @@ export class Hub extends deviceBase {
198
213
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
199
214
  .updateValue(deviceVersion);
200
215
  this.accessory.context.deviceVersion = deviceVersion;
201
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
216
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
217
+ }
218
+ }
219
+
220
+ async parseStatusWebhook(context: hub2WebhookContext): Promise<void> {
221
+ await this.debugLog('parseStatusWebhook');
222
+ await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
223
+ this.device.hub?.convertUnitTo)}, ${context.humidity}, ${context.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature},`
224
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`);
225
+ // Check if the scale is not CELSIUS
226
+ if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
227
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
228
+ + ' if displaying incorrectly in HomeKit.');
229
+ }
230
+ // CurrentRelativeHumidity
231
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
232
+ this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
233
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
234
+ }
235
+ // CurrentTemperature
236
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
237
+ this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
238
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
239
+ }
240
+ // CurrentAmbientLightLevel
241
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
242
+ const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
243
+ const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
244
+ this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(context.lightLevel, set_minLux, set_maxLux, 19);
245
+ await this.debugLog(`CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
202
246
  }
203
247
  }
204
248
 
@@ -207,20 +251,19 @@ export class Hub extends deviceBase {
207
251
  */
208
252
  async refreshStatus(): Promise<void> {
209
253
  if (!this.device.enableCloudService && this.OpenAPI) {
210
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
254
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
211
255
  } else if (this.BLE) {
212
256
  await this.BLERefreshStatus();
213
257
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
214
258
  await this.openAPIRefreshStatus();
215
259
  } else {
216
260
  await this.offlineOff();
217
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
218
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
261
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
219
262
  }
220
263
  }
221
264
 
222
265
  async BLERefreshStatus(): Promise<void> {
223
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
266
+ await this.debugLog('BLERefreshStatus');
224
267
  const switchbot = await this.switchbotBLE();
225
268
 
226
269
  if (switchbot === undefined) {
@@ -229,13 +272,13 @@ export class Hub extends deviceBase {
229
272
  // Start to monitor advertisement packets
230
273
  (async () => {
231
274
  // Start to monitor advertisement packets
232
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
275
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as hub2ServiceData;
233
276
  // Update HomeKit
234
- if (serviceData.model !== '' && serviceData.modelName !== '') {
277
+ if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
235
278
  await this.BLEparseStatus(serviceData);
236
279
  await this.updateHomeKitCharacteristics();
237
280
  } else {
238
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
281
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
239
282
  await this.BLERefreshConnection(switchbot);
240
283
  }
241
284
  })();
@@ -243,61 +286,39 @@ export class Hub extends deviceBase {
243
286
  }
244
287
 
245
288
  async openAPIRefreshStatus(): Promise<void> {
246
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
289
+ await this.debugLog('openAPIRefreshStatus');
247
290
  try {
248
291
  const { body, statusCode } = await this.deviceRefreshStatus();
249
292
  const deviceStatus: any = await body.json();
250
- await this.refreshStatusCodes(statusCode, deviceStatus);;
251
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
252
- await this.successfulRefreshStatus(statusCode, deviceStatus);
253
- await this.openAPIparseStatus(deviceStatus);
293
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
294
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
295
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
296
+ await this.openAPIparseStatus(deviceStatus.body);
254
297
  await this.updateHomeKitCharacteristics();
255
298
  } else {
256
- await this.statusCodes(statusCode, deviceStatus);
299
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
300
+ await this.debugWarnLog(statusCode, deviceStatus);
257
301
  }
258
302
  } catch (e: any) {
259
- this.apiError(e);
260
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
261
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
303
+ await this.apiError(e);
304
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
262
305
  }
263
306
  }
264
307
 
265
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
266
- if (device.webhook) {
267
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
268
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
308
+ async registerWebhook(): Promise<void> {
309
+ if (this.device.webhook) {
310
+ await this.debugLog('is listening webhook.');
311
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
269
312
  try {
270
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
271
- const { temperature, humidity, lightLevel } = context;
272
- const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
273
- const { CurrentAmbientLightLevel } = this.LightSensor || { CurrentAmbientLightLevel: undefined };
274
- const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
275
- if (context.scale !== 'CELCIUS' && device.hub?.convertUnitTo === undefined) {
276
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received a non-CELCIUS Webhook scale: `
277
- + `${context.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`);
278
- }
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
280
- '(scale, temperature, humidity, lightLevel) = ' +
281
- `Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.hub?.convertUnitTo)}, ${humidity}, ${lightLevel}), `
282
- + `current:(${CurrentTemperature}, ${CurrentRelativeHumidity}, ${CurrentAmbientLightLevel})`);
283
- if (!this.device.hub?.hide_humidity) {
284
- this.HumiditySensor!.CurrentRelativeHumidity = humidity;
285
- }
286
- if (!this.device.hub?.hide_temperature) {
287
- this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.hub?.convertUnitTo);
288
- }
289
- if (!this.device.hub?.hide_lightsensor) {
290
- const set_minLux = this.device.curtain?.set_minLux ?? 1;
291
- const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
292
- const spaceBetweenLevels = 19;
293
- await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
294
- }
295
- this.updateHomeKitCharacteristics();
313
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
314
+ await this.parseStatusWebhook(context);
315
+ await this.updateHomeKitCharacteristics();
296
316
  } catch (e: any) {
297
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
298
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
317
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
299
318
  }
300
319
  };
320
+ }else {
321
+ await this.debugLog('is not listening webhook.');
301
322
  }
302
323
  }
303
324
 
@@ -306,198 +327,56 @@ export class Hub extends deviceBase {
306
327
  */
307
328
 
308
329
  async updateHomeKitCharacteristics(): Promise<void> {
309
- const mqttmessage: string[] = [];
310
- const entry = { time: Math.round(new Date().valueOf() / 1000) };
311
-
312
330
  // CurrentRelativeHumidity
313
- if (!this.device.hub?.hide_humidity) {
314
- if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
315
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
316
- + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
317
- } else {
318
- if (this.device.mqttURL) {
319
- mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
320
- }
321
- if (this.device.history) {
322
- entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
323
- }
324
- this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
325
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
326
- this.HumiditySensor!.CurrentRelativeHumidity);
327
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
328
- + `updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
329
- }
331
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
332
+ await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity,
333
+ this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity');
330
334
  }
331
-
332
335
  // CurrentTemperature
333
- if (!this.device.hub?.hide_temperature) {
334
- if (this.TemperatureSensor!.CurrentTemperature === undefined) {
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
336
- } else {
337
- if (this.device.mqttURL) {
338
- mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
339
- }
340
- if (this.device.history) {
341
- entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
342
- }
343
- this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
344
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
345
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
346
- + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
347
- }
336
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
337
+ await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature,
338
+ this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature');
348
339
  }
349
-
350
340
  // CurrentAmbientLightLevel
351
- if (!this.device.hub?.hide_lightsensor) {
352
- if (this.LightSensor!.CurrentAmbientLightLevel === undefined) {
353
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
354
- + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
355
- } else {
356
- if (this.device.mqttURL) {
357
- mqttmessage.push(`"light": ${this.LightSensor!.CurrentAmbientLightLevel}`);
358
- }
359
- if (this.device.history) {
360
- entry['lux'] = this.LightSensor!.CurrentAmbientLightLevel;
361
- }
362
- this.accessory.context.CurrentAmbientLightLevel = this.LightSensor!.CurrentAmbientLightLevel;
363
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
364
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
365
- + `updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
366
- }
367
- }
368
-
369
- // MQTT
370
- if (this.device.mqttURL) {
371
- this.mqttPublish(`{${mqttmessage.join(',')}}`);
372
- }
373
- if (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0) {
374
- // reject unreliable data
375
- if (this.device.history) {
376
- this.historyService?.addEntry(entry);
377
- }
341
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
342
+ await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel,
343
+ this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel');
378
344
  }
379
345
  }
380
346
 
381
347
  async BLERefreshConnection(switchbot: any): Promise<void> {
382
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
383
- + ` ${JSON.stringify(switchbot)}`);
348
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
384
349
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
385
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
350
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
386
351
  await this.openAPIRefreshStatus();
387
352
  }
388
353
  }
389
354
 
390
355
  async offlineOff(): Promise<void> {
391
356
  if (this.device.offline) {
392
- if (!this.device.hub?.hide_temperature) {
393
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature);
357
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
358
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature);
394
359
  }
395
- if (!this.device.hub?.hide_humidity) {
396
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
360
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
361
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
397
362
  this.accessory.context.CurrentRelativeHumidity);
398
363
  }
399
- if (!this.device.hub?.hide_lightsensor) {
400
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel,
364
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
365
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel,
401
366
  this.accessory.context.CurrentAmbientLightLevel);
402
367
  }
403
368
  }
404
369
  }
405
370
 
406
371
  async apiError(e: any): Promise<void> {
407
- if (!this.device.hub?.hide_temperature) {
408
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
409
- }
410
- if (!this.device.hub?.hide_humidity) {
411
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
372
+ if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
373
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
412
374
  }
413
- if (!this.device.hub?.hide_lightsensor) {
414
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
375
+ if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
376
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
415
377
  }
416
- }
417
-
418
- async getLightLevel(lightLevel: any, set_minLux: number, set_maxLux: number, spaceBetweenLevels: number) {
419
- switch (lightLevel) {
420
- case 1:
421
- this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
422
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
423
- break;
424
- case 2:
425
- this.LightSensor!.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
426
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel},`
427
- + ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
428
- break;
429
- case 3:
430
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
431
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
432
- break;
433
- case 4:
434
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
435
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
436
- break;
437
- case 5:
438
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
439
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
440
- break;
441
- case 6:
442
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
443
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
444
- break;
445
- case 7:
446
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
447
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
448
- break;
449
- case 8:
450
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
451
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
452
- break;
453
- case 9:
454
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
455
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
456
- break;
457
- case 10:
458
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 9;
459
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
460
- break;
461
- case 11:
462
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 10;
463
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
464
- break;
465
- case 12:
466
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 11;
467
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
468
- break;
469
- case 13:
470
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 12;
471
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
472
- break;
473
- case 14:
474
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 13;
475
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
476
- break;
477
- case 15:
478
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 14;
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
480
- break;
481
- case 16:
482
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 15;
483
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
484
- break;
485
- case 17:
486
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 16;
487
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
488
- break;
489
- case 18:
490
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 17;
491
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
492
- break;
493
- case 19:
494
- this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 18;
495
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
496
- break;
497
- case 20:
498
- default:
499
- this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
500
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
378
+ if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
379
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
501
380
  }
502
381
  }
503
382
  }