@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.3

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