@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
@@ -6,10 +6,15 @@ import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
7
  import { convertUnits } from '../utils.js';
8
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 { meterServiceData } from '../types/bledevicestatus.js';
15
+ import type { meterStatus } from '../types/devicestatus.js';
16
+ import type { meterWebhookContext } 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 Meter extends deviceBase {
15
20
  // Services
@@ -136,76 +141,63 @@ export class Meter extends deviceBase {
136
141
  // Retrieve initial values and update Homekit
137
142
  this.updateHomeKitCharacteristics();
138
143
 
144
+ //regisiter webhook event handler
145
+ this.registerWebhook();
146
+
139
147
  // Start an update interval
140
148
  interval(this.deviceRefreshRate * 1000)
141
149
  .pipe(skipWhile(() => this.meterUpdateInProgress))
142
150
  .subscribe(async () => {
143
151
  await this.refreshStatus();
144
152
  });
145
-
146
- //regisiter webhook event handler
147
- this.registerWebhook(accessory, device);
148
153
  }
149
154
 
150
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
151
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
152
-
153
- // BatteryLevel
154
- this.Battery.BatteryLevel = Number(serviceData.battery);
155
- if (this.Battery.BatteryLevel < 15) {
156
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
157
- } else {
158
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
159
- }
160
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
161
-
155
+ async BLEparseStatus(serviceData: meterServiceData): Promise<void> {
156
+ await this.debugLog('BLEparseStatus');
162
157
  // CurrentRelativeHumidity
163
- if (!this.device.meter?.hide_humidity) {
164
- this.HumiditySensor!.CurrentRelativeHumidity = serviceData.humidity!;
165
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
158
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
159
+ this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
160
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
166
161
  }
167
-
168
162
  // Current Temperature
169
- if (!this.device.meter?.hide_temperature) {
170
- const celcius = serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
171
- this.TemperatureSensor!.CurrentTemperature = celcius;
172
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
163
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
164
+ const CELSIUS = serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
165
+ this.TemperatureSensor.CurrentTemperature = CELSIUS;
166
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
173
167
  }
168
+ // BatteryLevel
169
+ this.Battery.BatteryLevel = Number(serviceData.battery);
170
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
171
+ // StatusLowBattery
172
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
173
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
174
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
174
175
  }
175
176
 
176
- async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
177
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
178
-
179
- // Battery
180
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
181
- if (this.Battery.BatteryLevel < 10) {
182
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
183
- } else {
184
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
185
- }
186
- if (Number.isNaN(this.Battery.BatteryLevel)) {
187
- this.Battery.BatteryLevel = 100;
188
- }
189
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
190
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
191
-
177
+ async openAPIparseStatus(deviceStatus: meterStatus): Promise<void> {
178
+ await this.debugLog('openAPIparseStatus');
192
179
  // CurrentRelativeHumidity
193
- if (!this.device.meter?.hide_humidity) {
194
- this.HumiditySensor!.CurrentRelativeHumidity = deviceStatus.body.humidity!;
195
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
180
+ if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
181
+ this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
182
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
196
183
  }
197
-
198
184
  // CurrentTemperature
199
- if (!this.device.meter?.hide_temperature) {
200
- this.TemperatureSensor!.CurrentTemperature = deviceStatus.body.temperature!;
201
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
185
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
186
+ this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
187
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
202
188
  }
203
-
204
- // Firmware Version
205
- const version = deviceStatus.body.version?.toString();
206
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
207
- if (deviceStatus.body.version) {
208
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
189
+ // BatteryLevel
190
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
191
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
192
+ // StatusLowBattery
193
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
194
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
195
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
196
+ // FirmwareVersion
197
+ const version = deviceStatus.version.toString();
198
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
199
+ if (deviceStatus.version) {
200
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
209
201
  this.accessory
210
202
  .getService(this.hap.Service.AccessoryInformation)!
211
203
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -213,7 +205,29 @@ export class Meter extends deviceBase {
213
205
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
214
206
  .updateValue(deviceVersion);
215
207
  this.accessory.context.deviceVersion = deviceVersion;
216
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
208
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
209
+ }
210
+ }
211
+
212
+ async parseStatusWebhook(context: meterWebhookContext): Promise<void> {
213
+ await this.debugLog('parseStatusWebhook');
214
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
215
+ this.device.meter?.convertUnitTo)}, ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
216
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
217
+ // Check if the scale is not CELSIUS
218
+ if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
219
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
220
+ + ' if displaying incorrectly in HomeKit.');
221
+ }
222
+ // CurrentRelativeHumidity
223
+ if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
224
+ this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
225
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
226
+ }
227
+ // CurrentTemperature
228
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
229
+ this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
230
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
217
231
  }
218
232
  }
219
233
 
@@ -222,20 +236,19 @@ export class Meter extends deviceBase {
222
236
  */
223
237
  async refreshStatus(): Promise<void> {
224
238
  if (!this.device.enableCloudService && this.OpenAPI) {
225
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
239
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
226
240
  } else if (this.BLE) {
227
241
  await this.BLERefreshStatus();
228
242
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
229
243
  await this.openAPIRefreshStatus();
230
244
  } else {
231
245
  await this.offlineOff();
232
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
233
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
246
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
234
247
  }
235
248
  }
236
249
 
237
250
  async BLERefreshStatus(): Promise<void> {
238
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
251
+ await this.debugLog('BLERefreshStatus');
239
252
  const switchbot = await this.switchbotBLE();
240
253
 
241
254
  if (switchbot === undefined) {
@@ -244,13 +257,13 @@ export class Meter extends deviceBase {
244
257
  // Start to monitor advertisement packets
245
258
  (async () => {
246
259
  // Start to monitor advertisement packets
247
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
260
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
248
261
  // Update HomeKit
249
- if (serviceData.model !== '' && serviceData.modelName !== '') {
262
+ if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
250
263
  await this.BLEparseStatus(serviceData);
251
264
  await this.updateHomeKitCharacteristics();
252
265
  } else {
253
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
266
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
254
267
  await this.BLERefreshConnection(switchbot);
255
268
  }
256
269
  })();
@@ -258,55 +271,39 @@ export class Meter extends deviceBase {
258
271
  }
259
272
 
260
273
  async openAPIRefreshStatus(): Promise<void> {
261
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
274
+ await this.debugLog('openAPIRefreshStatus');
262
275
  try {
263
276
  const { body, statusCode } = await this.deviceRefreshStatus();
264
277
  const deviceStatus: any = await body.json();
265
- await this.refreshStatusCodes(statusCode, deviceStatus);;
266
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
267
- await this.successfulRefreshStatus(statusCode, deviceStatus);
268
- await this.openAPIparseStatus(deviceStatus);
278
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
279
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
280
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
281
+ await this.openAPIparseStatus(deviceStatus.body);
269
282
  await this.updateHomeKitCharacteristics();
270
283
  } else {
271
- await this.statusCodes(statusCode, deviceStatus);
284
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
285
+ await this.debugWarnLog(statusCode, deviceStatus);
272
286
  }
273
287
  } catch (e: any) {
274
- this.apiError(e);
275
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
276
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
288
+ await this.apiError(e);
289
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
277
290
  }
278
291
  }
279
292
 
280
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
281
- if (device.webhook) {
282
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
283
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
293
+ async registerWebhook() {
294
+ if (this.device.webhook) {
295
+ await this.debugLog('is listening webhook.');
296
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: meterWebhookContext) => {
284
297
  try {
285
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
286
- const { temperature, humidity } = context;
287
- const { CurrentTemperature } = this.TemperatureSensor ?? { CurrentTemperature: undefined };
288
- const { CurrentRelativeHumidity } = this.HumiditySensor ?? { CurrentRelativeHumidity: undefined };
289
- if (context.scale !== 'CELCIUS' && device.meter?.convertUnitTo === undefined) {
290
- this.warnLog(`${device.deviceType}: ${accessory.displayName} received Webhook scale: `
291
- + `${context.scale}, instead of CELCIUS. Use the *convertUnitsTo* config under Meter settings, if displaying incorrectly in HomeKit.`);
292
- }
293
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
294
- '(scale, temperature, humidity) = '
295
- + `Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.meter?.convertUnitTo)}, ${humidity}), `
296
- + `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
297
- if (!device.meter?.hide_humidity) {
298
- this.HumiditySensor!.CurrentRelativeHumidity = humidity;
299
- }
300
- if (!device.meter?.hide_temperature) {
301
- this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.meter?.convertUnitTo);
302
- }
303
- this.updateHomeKitCharacteristics();
298
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
299
+ await this.parseStatusWebhook(context);
300
+ await this.updateHomeKitCharacteristics();
304
301
  } catch (e: any) {
305
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
302
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
306
303
  }
307
304
  };
308
305
  } else {
309
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
306
+ await this.debugLog('is not listening webhook.');
310
307
  }
311
308
  }
312
309
 
@@ -314,113 +311,54 @@ export class Meter extends deviceBase {
314
311
  * Updates the status for each of the HomeKit Characteristics
315
312
  */
316
313
  async updateHomeKitCharacteristics(): Promise<void> {
317
- const mqttmessage: string[] = [];
318
- const entry = { time: Math.round(new Date().valueOf() / 1000) };
319
-
320
314
  // CurrentRelativeHumidity
321
- if (!this.device.meter?.hide_humidity) {
322
- if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
323
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
324
- + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
325
- } else {
326
- this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
327
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
328
- this.HumiditySensor!.CurrentRelativeHumidity);
329
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
330
- + ` updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
331
- if (this.device.mqttURL) {
332
- mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
333
- }
334
- if (this.device.history) {
335
- entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
336
- }
337
- }
315
+ if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
316
+ await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity,
317
+ this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity');
338
318
  }
339
-
340
319
  // CurrentTemperature
341
- if (!this.device.meter?.hide_temperature) {
342
- if (this.TemperatureSensor!.CurrentTemperature === undefined) {
343
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
344
- } else {
345
- this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
346
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
347
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
348
- + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
349
- if (this.device.mqttURL) {
350
- mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
351
- }
352
- if (this.device.history) {
353
- entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
354
- }
355
- }
320
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
321
+ await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature,
322
+ this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature');
356
323
  }
357
-
358
324
  // BatteryLevel
359
- if (this.Battery.BatteryLevel === undefined) {
360
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
361
- } else {
362
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
363
- this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
364
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
365
- if (this.device.mqttURL) {
366
- mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
367
- }
368
- }
369
- if (this.Battery.StatusLowBattery === undefined) {
370
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
371
- } else {
372
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
373
- this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
374
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
375
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
376
- if (this.device.mqttURL) {
377
- mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
378
- }
379
- }
380
-
381
- // MQTT Publish
382
- if (this.device.mqttURL) {
383
- this.mqttPublish(`{${mqttmessage.join(',')}}`);
384
- }
385
-
386
- // History Service
387
- if (!this.device.meter?.hide_humidity && (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0)) {
388
- // reject unreliable data
389
- if (this.device.history) {
390
- this.historyService?.addEntry(entry);
391
- }
392
- }
325
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
326
+ this.Battery.BatteryLevel, 'BatteryLevel');
327
+ // StatusLowBattery
328
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
329
+ this.Battery.StatusLowBattery, 'StatusLowBattery');
393
330
  }
394
331
 
395
332
  async BLERefreshConnection(switchbot: any): Promise<void> {
396
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
397
- + ` ${JSON.stringify(switchbot)}`);
333
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
398
334
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
399
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
335
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
400
336
  await this.openAPIRefreshStatus();
401
337
  }
402
338
  }
403
339
 
404
340
  async offlineOff(): Promise<void> {
405
341
  if (this.device.offline) {
406
- if (!this.device.meter?.hide_humidity) {
407
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
342
+ if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
343
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
408
344
  }
409
- if (!this.device.meter?.hide_temperature) {
410
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
345
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
346
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
411
347
  }
412
348
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100);
349
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery,
350
+ this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL);
413
351
  }
414
352
  }
415
353
 
416
354
  async apiError(e: any): Promise<void> {
417
- if (!this.device.meter?.hide_humidity) {
418
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
355
+ if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
356
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
419
357
  }
420
- if (!this.device.meter?.hide_temperature) {
421
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
358
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
359
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
422
360
  }
423
- this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
424
- this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
361
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
362
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
425
363
  }
426
364
  }