@switchbot/homebridge-switchbot 3.5.1-beta.9 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/config.schema.json +19 -3
  3. package/dist/device/blindtilt.d.ts +6 -4
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +60 -46
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +6 -3
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +59 -25
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +6 -3
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +47 -29
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +6 -3
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +53 -36
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +6 -3
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +45 -31
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +6 -4
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +41 -27
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +6 -3
  28. package/dist/device/device.d.ts.map +1 -1
  29. package/dist/device/device.js +59 -152
  30. package/dist/device/device.js.map +1 -1
  31. package/dist/device/fan.d.ts +6 -3
  32. package/dist/device/fan.d.ts.map +1 -1
  33. package/dist/device/fan.js +53 -32
  34. package/dist/device/fan.js.map +1 -1
  35. package/dist/device/hub.d.ts +6 -3
  36. package/dist/device/hub.d.ts.map +1 -1
  37. package/dist/device/hub.js +49 -30
  38. package/dist/device/hub.js.map +1 -1
  39. package/dist/device/humidifier.d.ts +6 -3
  40. package/dist/device/humidifier.d.ts.map +1 -1
  41. package/dist/device/humidifier.js +47 -28
  42. package/dist/device/humidifier.js.map +1 -1
  43. package/dist/device/iosensor.d.ts +6 -3
  44. package/dist/device/iosensor.d.ts.map +1 -1
  45. package/dist/device/iosensor.js +44 -27
  46. package/dist/device/iosensor.js.map +1 -1
  47. package/dist/device/lightstrip.d.ts +6 -3
  48. package/dist/device/lightstrip.d.ts.map +1 -1
  49. package/dist/device/lightstrip.js +46 -28
  50. package/dist/device/lightstrip.js.map +1 -1
  51. package/dist/device/lock.d.ts +6 -3
  52. package/dist/device/lock.d.ts.map +1 -1
  53. package/dist/device/lock.js +38 -25
  54. package/dist/device/lock.js.map +1 -1
  55. package/dist/device/meter.d.ts +6 -3
  56. package/dist/device/meter.d.ts.map +1 -1
  57. package/dist/device/meter.js +43 -27
  58. package/dist/device/meter.js.map +1 -1
  59. package/dist/device/meterplus.d.ts +6 -3
  60. package/dist/device/meterplus.d.ts.map +1 -1
  61. package/dist/device/meterplus.js +42 -27
  62. package/dist/device/meterplus.js.map +1 -1
  63. package/dist/device/motion.d.ts +6 -3
  64. package/dist/device/motion.d.ts.map +1 -1
  65. package/dist/device/motion.js +37 -24
  66. package/dist/device/motion.js.map +1 -1
  67. package/dist/device/plug.d.ts +6 -3
  68. package/dist/device/plug.d.ts.map +1 -1
  69. package/dist/device/plug.js +30 -17
  70. package/dist/device/plug.js.map +1 -1
  71. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  72. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  73. package/dist/device/robotvacuumcleaner.js +37 -22
  74. package/dist/device/robotvacuumcleaner.js.map +1 -1
  75. package/dist/device/waterdetector.d.ts +6 -3
  76. package/dist/device/waterdetector.d.ts.map +1 -1
  77. package/dist/device/waterdetector.js +43 -26
  78. package/dist/device/waterdetector.js.map +1 -1
  79. package/dist/homebridge-ui/public/index.html +2 -2
  80. package/dist/irdevice/airconditioner.d.ts +1 -3
  81. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  82. package/dist/irdevice/airconditioner.js +67 -177
  83. package/dist/irdevice/airconditioner.js.map +1 -1
  84. package/dist/irdevice/airpurifier.d.ts +0 -1
  85. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  86. package/dist/irdevice/airpurifier.js +24 -53
  87. package/dist/irdevice/airpurifier.js.map +1 -1
  88. package/dist/irdevice/camera.d.ts.map +1 -1
  89. package/dist/irdevice/camera.js +12 -17
  90. package/dist/irdevice/camera.js.map +1 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +22 -38
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +22 -10
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +94 -100
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts.map +1 -1
  99. package/dist/irdevice/light.js +20 -38
  100. package/dist/irdevice/light.js.map +1 -1
  101. package/dist/irdevice/other.d.ts.map +1 -1
  102. package/dist/irdevice/other.js +79 -77
  103. package/dist/irdevice/other.js.map +1 -1
  104. package/dist/irdevice/tv.d.ts.map +1 -1
  105. package/dist/irdevice/tv.js +38 -53
  106. package/dist/irdevice/tv.js.map +1 -1
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +11 -16
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts.map +1 -1
  111. package/dist/irdevice/waterheater.js +11 -16
  112. package/dist/irdevice/waterheater.js.map +1 -1
  113. package/dist/platform.d.ts +4 -6
  114. package/dist/platform.d.ts.map +1 -1
  115. package/dist/platform.js +564 -665
  116. package/dist/platform.js.map +1 -1
  117. package/dist/settings.d.ts +10 -8
  118. package/dist/settings.d.ts.map +1 -1
  119. package/dist/settings.js.map +1 -1
  120. package/dist/types/bledevicestatus.d.ts +2 -1
  121. package/dist/types/bledevicestatus.d.ts.map +1 -1
  122. package/dist/types/devicestatus.d.ts +1 -1
  123. package/dist/types/devicestatus.d.ts.map +1 -1
  124. package/package.json +11 -11
  125. package/src/device/blindtilt.ts +64 -48
  126. package/src/device/bot.ts +79 -26
  127. package/src/device/ceilinglight.ts +57 -31
  128. package/src/device/colorbulb.ts +57 -43
  129. package/src/device/contact.ts +49 -31
  130. package/src/device/curtain.ts +51 -28
  131. package/src/device/device.ts +61 -135
  132. package/src/device/fan.ts +67 -33
  133. package/src/device/hub.ts +62 -33
  134. package/src/device/humidifier.ts +60 -33
  135. package/src/device/iosensor.ts +51 -30
  136. package/src/device/lightstrip.ts +52 -29
  137. package/src/device/lock.ts +55 -26
  138. package/src/device/meter.ts +60 -29
  139. package/src/device/meterplus.ts +56 -29
  140. package/src/device/motion.ts +50 -26
  141. package/src/device/plug.ts +38 -18
  142. package/src/device/robotvacuumcleaner.ts +53 -24
  143. package/src/device/waterdetector.ts +62 -28
  144. package/src/homebridge-ui/public/index.html +2 -2
  145. package/src/irdevice/airconditioner.ts +76 -165
  146. package/src/irdevice/airpurifier.ts +29 -49
  147. package/src/irdevice/camera.ts +13 -16
  148. package/src/irdevice/fan.ts +25 -35
  149. package/src/irdevice/irdevice.ts +97 -96
  150. package/src/irdevice/light.ts +21 -35
  151. package/src/irdevice/other.ts +79 -77
  152. package/src/irdevice/tv.ts +40 -51
  153. package/src/irdevice/vacuumcleaner.ts +12 -15
  154. package/src/irdevice/waterheater.ts +12 -15
  155. package/src/platform.ts +566 -696
  156. package/src/settings.ts +11 -8
  157. package/src/types/bledevicestatus.ts +4 -2
  158. package/src/types/devicestatus.ts +1 -1
  159. package/.github/npm-version-script.cjs +0 -81
@@ -21,6 +21,15 @@ export class Plug extends deviceBase {
21
21
  On: CharacteristicValue;
22
22
  };
23
23
 
24
+ // OpenAPI
25
+ deviceStatus!: plugStatus | plugMiniStatus;
26
+
27
+ //Webhook
28
+ webhookContext!: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext;
29
+
30
+ // BLE
31
+ serviceData!: plugMiniUSServiceData | plugMiniJPServiceData;
32
+
24
33
  // Updates
25
34
  plugUpdateInProgress!: boolean;
26
35
  doPlugUpdate!: Subject<void>;
@@ -31,6 +40,9 @@ export class Plug extends deviceBase {
31
40
  device: device & devicesConfig,
32
41
  ) {
33
42
  super(platform, accessory, device);
43
+ // Set category
44
+ accessory.category = this.hap.Categories.OUTLET;
45
+
34
46
  // this is subject we use to track when we need to POST changes to the SwitchBot API
35
47
  this.doPlugUpdate = new Subject();
36
48
  this.plugUpdateInProgress = false;
@@ -54,12 +66,11 @@ export class Plug extends deviceBase {
54
66
  .onSet(this.OnSet.bind(this));
55
67
 
56
68
  // Retrieve initial values and updateHomekit
69
+ this.debugLog('Retrieve initial values and update Homekit');
57
70
  this.refreshStatus();
58
71
 
59
- // Update Homekit
60
- this.updateHomeKitCharacteristics();
61
-
62
72
  //regisiter webhook event handler
73
+ this.debugLog('Registering Webhook Event Handler');
63
74
  this.registerWebhook();
64
75
 
65
76
  // Start an update interval
@@ -89,22 +100,27 @@ export class Plug extends deviceBase {
89
100
  });
90
101
  }
91
102
 
92
- async BLEparseStatus(serviceData: plugMiniUSServiceData | plugMiniJPServiceData): Promise<void> {
103
+ async BLEparseStatus(): Promise<void> {
93
104
  await this.debugLog('BLEparseStatus');
105
+ await this.debugLog(`(powerState) = BLE: (${this.serviceData.state}), current:(${this.Outlet.On})`);
106
+
94
107
  // On
95
- this.Outlet.On = serviceData.state === 'on' ? true : false;
108
+ this.Outlet.On = this.serviceData.state === 'on' ? true : false;
96
109
  await this.debugLog(`On: ${this.Outlet.On}`);
97
110
  }
98
111
 
99
- async openAPIparseStatus(deviceStatus: plugStatus | plugMiniStatus) {
112
+ async openAPIparseStatus() {
100
113
  await this.debugLog('openAPIparseStatus');
114
+ await this.debugLog(`(powerState) = OpenAPI: (${this.deviceStatus.power}), current:(${this.Outlet.On})`);
115
+
101
116
  // On
102
- this.Outlet.On = deviceStatus.power === 'on' ? true : false;
117
+ this.Outlet.On = this.deviceStatus.power === 'on' ? true : false;
103
118
  await this.debugLog(`On: ${this.Outlet.On}`);
119
+
104
120
  // Firmware Version
105
- const version = deviceStatus.version.toString();
106
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
107
- if (deviceStatus.version) {
121
+ if (this.deviceStatus.version) {
122
+ const version = this.deviceStatus.version.toString();
123
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
108
124
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
109
125
  this.accessory
110
126
  .getService(this.hap.Service.AccessoryInformation)!
@@ -112,16 +128,17 @@ export class Plug extends deviceBase {
112
128
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
113
129
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
114
130
  .updateValue(deviceVersion);
115
- this.accessory.context.deviceVersion = deviceVersion;
116
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
131
+ this.accessory.context.version = deviceVersion;
132
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
117
133
  }
118
134
  }
119
135
 
120
- async parseStatusWebhook(context: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext): Promise<void> {
136
+ async parseStatusWebhook(): Promise<void> {
121
137
  await this.debugLog('parseStatusWebhook');
122
- await this.debugLog(`(powerState) = Webhook: (${context.powerState}), current:(${this.Outlet.On})`);
138
+ await this.debugLog(`(powerState) = Webhook: (${this.webhookContext.powerState}), current:(${this.Outlet.On})`);
139
+
123
140
  // On
124
- this.Outlet.On = context.powerState === 'ON' ? true : false;
141
+ this.Outlet.On = this.webhookContext.powerState === 'ON' ? true : false;
125
142
  await this.debugLog(`On: ${this.Outlet.On}`);
126
143
  }
127
144
 
@@ -155,7 +172,8 @@ export class Plug extends deviceBase {
155
172
  // Update HomeKit
156
173
  if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
157
174
  && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
158
- await this.BLEparseStatus(serviceData);
175
+ this.serviceData = serviceData;
176
+ await this.BLEparseStatus();
159
177
  await this.updateHomeKitCharacteristics();
160
178
  } else {
161
179
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -173,7 +191,8 @@ export class Plug extends deviceBase {
173
191
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
174
192
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
175
193
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
176
- await this.openAPIparseStatus(deviceStatus.body);
194
+ this.deviceStatus = deviceStatus.body;
195
+ await this.openAPIparseStatus();
177
196
  await this.updateHomeKitCharacteristics();
178
197
  } else {
179
198
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -192,7 +211,8 @@ export class Plug extends deviceBase {
192
211
  | plugMiniJPWebhookContext) => {
193
212
  try {
194
213
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
195
- await this.parseStatusWebhook(context);
214
+ this.webhookContext = context;
215
+ await this.parseStatusWebhook();
196
216
  await this.updateHomeKitCharacteristics();
197
217
  } catch (e: any) {
198
218
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -35,6 +35,15 @@ export class RobotVacuumCleaner extends deviceBase {
35
35
  ChargingState: CharacteristicValue;
36
36
  };
37
37
 
38
+ // OpenAPI
39
+ deviceStatus!: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status;
40
+
41
+ //Webhook
42
+ webhookContext!: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext;
43
+
44
+ // BLE
45
+ serviceData!: robotVacuumCleanerServiceData;
46
+
38
47
  // Updates
39
48
  robotVacuumCleanerUpdateInProgress!: boolean;
40
49
  doRobotVacuumCleanerUpdate!: Subject<void>;
@@ -45,6 +54,9 @@ export class RobotVacuumCleaner extends deviceBase {
45
54
  device: device & devicesConfig,
46
55
  ) {
47
56
  super(platform, accessory, device);
57
+ // Set category
58
+ accessory.category = this.hap.Categories.OTHER;
59
+
48
60
  // this is subject we use to track when we need to POST changes to the SwitchBot API
49
61
  this.doRobotVacuumCleanerUpdate = new Subject();
50
62
  this.robotVacuumCleanerUpdateInProgress = false;
@@ -115,12 +127,11 @@ export class RobotVacuumCleaner extends deviceBase {
115
127
  });
116
128
 
117
129
  // Retrieve initial values and updateHomekit
130
+ this.debugLog('Retrieve initial values and update Homekit');
118
131
  this.refreshStatus();
119
132
 
120
- // Update Homekit
121
- this.updateHomeKitCharacteristics();
122
-
123
133
  //regisiter webhook event handler
134
+ this.debugLog('Registering Webhook Event Handler');
124
135
  this.registerWebhook();
125
136
 
126
137
  // Start an update interval
@@ -150,39 +161,51 @@ export class RobotVacuumCleaner extends deviceBase {
150
161
  });
151
162
  }
152
163
 
153
- async BLEparseStatus(serviceData: robotVacuumCleanerServiceData): Promise<void> {
164
+ async BLEparseStatus(): Promise<void> {
154
165
  await this.debugLog('BLEparseStatus');
166
+ await this.debugLog(`(state, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.battery}),`
167
+ + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel})`);
168
+
155
169
  // On
156
- this.LightBulb.On = serviceData.state === 'on' ? true : false;
170
+ this.LightBulb.On = this.serviceData.state === 'on' ? true : false;
157
171
  await this.debugLog(`On: ${this.LightBulb.On}`);
172
+
158
173
  // BatteryLevel
159
- this.Battery.BatteryLevel = Number(serviceData.battery);
174
+ this.Battery.BatteryLevel = this.serviceData.battery;
160
175
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
176
+
161
177
  // StatusLowBattery
162
178
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
163
179
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
164
180
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
165
181
  }
166
182
 
167
- async openAPIparseStatus(deviceStatus: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status) {
183
+ async openAPIparseStatus() {
168
184
  await this.debugLog('openAPIparseStatus');
185
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = API: (${this.deviceStatus.onlineStatus}, ${this.deviceStatus.battery},`
186
+ + ` ${this.deviceStatus.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
187
+
169
188
  // On
170
- this.LightBulb.On = deviceStatus.onlineStatus === 'online' ? true : false;
189
+ this.LightBulb.On = this.deviceStatus.onlineStatus === 'online' ? true : false;
171
190
  await this.debugLog(`On: ${this.LightBulb.On}`);
191
+
172
192
  // BatteryLevel
173
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
193
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
174
194
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
195
+
175
196
  // StatusLowBattery
176
197
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
177
198
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
178
199
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
200
+
179
201
  // ChargingState
180
- this.Battery.ChargingState = deviceStatus.workingStatus === 'Charging'
202
+ this.Battery.ChargingState = this.deviceStatus.workingStatus === 'Charging'
181
203
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
182
204
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
205
+
183
206
  // Firmware Version
184
- if (deviceStatus.version) {
185
- const version = deviceStatus.version.toString();
207
+ if (this.deviceStatus.version) {
208
+ const version = this.deviceStatus.version.toString();
186
209
  await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
187
210
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
188
211
  this.accessory
@@ -191,28 +214,31 @@ export class RobotVacuumCleaner extends deviceBase {
191
214
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
192
215
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
193
216
  .updateValue(deviceVersion);
194
- this.accessory.context.deviceVersion = deviceVersion;
195
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
217
+ this.accessory.context.version = deviceVersion;
218
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
196
219
  }
197
220
  }
198
221
 
199
- async parseStatusWebhook(context: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext
200
- | floorCleaningRobotS10WebhookContext): Promise<void> {
222
+ async parseStatusWebhook(): Promise<void> {
201
223
  await this.debugLog('parseStatusWebhook');
202
- await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${context.onlineStatus}, ${context.battery}, ${context.workingStatus}),`
203
- + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
224
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${this.webhookContext.onlineStatus}, ${this.webhookContext.battery},`
225
+ + ` ${this.webhookContext.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
226
+
204
227
  // On
205
- this.LightBulb.On = context.onlineStatus === 'online' ? true : false;
228
+ this.LightBulb.On = this.webhookContext.onlineStatus === 'online' ? true : false;
206
229
  await this.debugLog(`On: ${this.LightBulb.On}`);
230
+
207
231
  // BatteryLevel
208
- this.Battery.BatteryLevel = Number(context.battery);
232
+ this.Battery.BatteryLevel = this.webhookContext.battery;
209
233
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
234
+
210
235
  // StatusLowBattery
211
236
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
212
237
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
213
238
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
239
+
214
240
  // ChargingState
215
- this.Battery.ChargingState = context.workingStatus === 'Charging'
241
+ this.Battery.ChargingState = this.webhookContext.workingStatus === 'Charging'
216
242
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
217
243
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
218
244
  }
@@ -246,7 +272,8 @@ export class RobotVacuumCleaner extends deviceBase {
246
272
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as robotVacuumCleanerServiceData;
247
273
  // Update HomeKit
248
274
  if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
249
- await this.BLEparseStatus(serviceData);
275
+ this.serviceData = serviceData;
276
+ await this.BLEparseStatus();
250
277
  await this.updateHomeKitCharacteristics();
251
278
  } else {
252
279
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -264,7 +291,8 @@ export class RobotVacuumCleaner extends deviceBase {
264
291
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
265
292
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
266
293
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
267
- await this.openAPIparseStatus(deviceStatus.body);
294
+ this.deviceStatus = deviceStatus.body;
295
+ await this.openAPIparseStatus();
268
296
  await this.updateHomeKitCharacteristics();
269
297
  } else {
270
298
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -283,7 +311,8 @@ export class RobotVacuumCleaner extends deviceBase {
283
311
  | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext) => {
284
312
  try {
285
313
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
286
- await this.parseStatusWebhook(context);
314
+ this.webhookContext = context;
315
+ await this.parseStatusWebhook();
287
316
  await this.updateHomeKitCharacteristics();
288
317
  } catch (e: any) {
289
318
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -36,6 +36,15 @@ export class WaterDetector extends deviceBase {
36
36
  LeakDetected: CharacteristicValue;
37
37
  };
38
38
 
39
+ // OpenAPI
40
+ deviceStatus!: waterLeakDetectorStatus;
41
+
42
+ //Webhook
43
+ webhookContext!: waterLeakDetectorWebhookContext;
44
+
45
+ // BLE
46
+ serviceData!: waterLeakDetectorServiceData;
47
+
39
48
  // Updates
40
49
  WaterDetectorUpdateInProgress!: boolean;
41
50
  doWaterDetectorUpdate: Subject<void>;
@@ -46,6 +55,9 @@ export class WaterDetector extends deviceBase {
46
55
  device: device & devicesConfig,
47
56
  ) {
48
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.SENSOR;
60
+
49
61
  // this is subject we use to track when we need to POST changes to the SwitchBot API
50
62
  this.doWaterDetectorUpdate = new Subject();
51
63
  this.WaterDetectorUpdateInProgress = false;
@@ -79,11 +91,11 @@ export class WaterDetector extends deviceBase {
79
91
  // Initialize Leak Sensor Service
80
92
  if (device.waterdetector?.hide_leak) {
81
93
  if (this.LeakSensor) {
82
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leak Sensor Service`);
94
+ this.debugLog('Removing Leak Sensor Service');
83
95
  this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor) as Service;
84
96
  accessory.removeService(this.LeakSensor.Service);
85
97
  } else {
86
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Leak Sensor Service Not Found`);
98
+ this.debugLog('Leak Sensor Service Not Found');
87
99
  }
88
100
  } else {
89
101
  accessory.context.LeakSensor = accessory.context.LeakSensor ?? {};
@@ -106,13 +118,11 @@ export class WaterDetector extends deviceBase {
106
118
  }
107
119
 
108
120
  // Retrieve initial values and updateHomekit
109
- this.debugLog('retrieve initial values and Update Homekit');
121
+ this.debugLog('Retrieve initial values and update Homekit');
110
122
  this.refreshStatus();
111
123
 
112
- // Retrieve initial values and updateHomekit
113
- this.updateHomeKitCharacteristics();
114
-
115
124
  //regisiter webhook event handler
125
+ this.debugLog('Registering Webhook Event Handler');
116
126
  this.registerWebhook();
117
127
 
118
128
  // Start an update interval
@@ -124,18 +134,24 @@ export class WaterDetector extends deviceBase {
124
134
  });
125
135
  }
126
136
 
127
- async BLEparseStatus(serviceData: waterLeakDetectorServiceData): Promise<void> {
137
+ async BLEparseStatus(): Promise<void> {
128
138
  await this.debugLog('BLEparseStatus');
139
+ await this.debugLog(`(state, status, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.status}, ${this.serviceData.battery}),`
140
+ + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
141
+
142
+ //LeakSensor
129
143
  if (this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
144
+
130
145
  // StatusActive
131
- this.LeakSensor.StatusActive = serviceData.state;
146
+ this.LeakSensor.StatusActive = this.serviceData.state;
132
147
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
148
+
133
149
  // LeakDetected
134
- this.LeakSensor.LeakDetected = serviceData.status;
150
+ this.LeakSensor.LeakDetected = this.serviceData.status;
135
151
  this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
136
152
  }
137
153
  // BatteryLevel
138
- this.Battery.BatteryLevel = Number(serviceData.battery);
154
+ this.Battery.BatteryLevel = this.serviceData.battery;
139
155
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
140
156
  // StatusLowBattery
141
157
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
@@ -143,27 +159,36 @@ export class WaterDetector extends deviceBase {
143
159
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
144
160
  }
145
161
 
146
- async openAPIparseStatus(deviceStatus: waterLeakDetectorStatus): Promise<void> {
162
+ async openAPIparseStatus(): Promise<void> {
147
163
  await this.debugLog('openAPIparseStatus');
164
+ await this.debugLog(`(status, battery) = OpenAPI: (${this.deviceStatus.status}, ${this.deviceStatus.battery}),`
165
+ + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
166
+
167
+ //LeakSensor
148
168
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
169
+
149
170
  // StatusActive
150
- this.LeakSensor.StatusActive = deviceStatus.battery === 0 ? false : true;
171
+ this.LeakSensor.StatusActive = this.deviceStatus.battery === 0 ? false : true;
151
172
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
173
+
152
174
  // LeakDetected
153
- this.LeakSensor.LeakDetected = deviceStatus.status;
175
+ this.LeakSensor.LeakDetected = this.deviceStatus.status;
154
176
  this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
155
177
  }
178
+
156
179
  // BatteryLevel
157
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
180
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
158
181
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
182
+
159
183
  // StatusLowBattery
160
184
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
161
185
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
162
186
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
187
+
163
188
  // FirmwareVersion
164
- const version = deviceStatus.version.toString();
165
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
166
- if (deviceStatus.version) {
189
+ if (this.deviceStatus.version) {
190
+ const version = this.deviceStatus.version.toString();
191
+ await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`);
167
192
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
168
193
  this.accessory
169
194
  .getService(this.hap.Service.AccessoryInformation)!
@@ -171,26 +196,32 @@ export class WaterDetector extends deviceBase {
171
196
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
172
197
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
173
198
  .updateValue(deviceVersion);
174
- this.accessory.context.deviceVersion = deviceVersion;
175
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
199
+ this.accessory.context.version = deviceVersion;
200
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
176
201
  }
177
202
  }
178
203
 
179
- async parseStatusWebhook(context: waterLeakDetectorWebhookContext): Promise<void> {
204
+ async parseStatusWebhook(): Promise<void> {
180
205
  await this.debugLog('parseStatusWebhook');
181
- await this.debugLog(`(detectionState, battery) = Webhook: (${context.detectionState}, ${context.battery}),`
206
+ await this.debugLog(`(detectionState, battery) = Webhook: (${this.webhookContext.detectionState}, ${this.webhookContext.battery}),`
182
207
  + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
208
+
209
+ //LeakSensor
183
210
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
211
+
184
212
  // StatusActive
185
- this.LeakSensor.StatusActive = context.detectionState ? true : false;
213
+ this.LeakSensor.StatusActive = this.webhookContext.detectionState ? true : false;
186
214
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
215
+
187
216
  // LeakDetected
188
- this.LeakSensor.LeakDetected = context.detectionState;
217
+ this.LeakSensor.LeakDetected = this.webhookContext.detectionState;
189
218
  await this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
190
219
  }
220
+
191
221
  // BatteryLevel
192
- this.Battery.BatteryLevel = Number(context.battery);
222
+ this.Battery.BatteryLevel = this.webhookContext.battery;
193
223
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
224
+
194
225
  // StatusLowBattery
195
226
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
196
227
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
@@ -223,10 +254,11 @@ export class WaterDetector extends deviceBase {
223
254
  // Start to monitor advertisement packets
224
255
  (async () => {
225
256
  // Start to monitor advertisement packets
226
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as waterLeakDetectorServiceData;
257
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData;
227
258
  // Update HomeKit
228
259
  if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
229
- await this.BLEparseStatus(serviceData);
260
+ this.serviceData = serviceData;
261
+ await this.BLEparseStatus();
230
262
  await this.updateHomeKitCharacteristics();
231
263
  } else {
232
264
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -244,7 +276,8 @@ export class WaterDetector extends deviceBase {
244
276
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
245
277
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
246
278
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
247
- await this.openAPIparseStatus(deviceStatus.body);
279
+ this.deviceStatus = deviceStatus.body;
280
+ await this.openAPIparseStatus();
248
281
  await this.updateHomeKitCharacteristics();
249
282
  } else {
250
283
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -262,7 +295,8 @@ export class WaterDetector extends deviceBase {
262
295
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: waterLeakDetectorWebhookContext) => {
263
296
  try {
264
297
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
265
- await this.parseStatusWebhook(context);
298
+ this.webhookContext = context;
299
+ await this.parseStatusWebhook();
266
300
  await this.updateHomeKitCharacteristics();
267
301
  } catch (e: any) {
268
302
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -51,7 +51,7 @@
51
51
  <tbody>
52
52
  <tr>
53
53
  <th scope="row">Device ID</th>
54
- <td id="deviceID"></td>
54
+ <td id="deviceId"></td>
55
55
  </tr>
56
56
  <tr>
57
57
  <th scope="row">Model</th>
@@ -166,7 +166,7 @@
166
166
  const thisAcc = cachedAccessories.find((x) => x.UUID === UUID);
167
167
  const context = thisAcc.context;
168
168
  document.getElementById('displayName').innerHTML = thisAcc.displayName;
169
- document.getElementById('deviceID').innerHTML = context.deviceID;
169
+ document.getElementById('deviceId').innerHTML = context.deviceId;
170
170
  document.getElementById('model').innerHTML = context.model;
171
171
  document.getElementById('version').innerHTML = context.version;
172
172
  document.getElementById('deviceType').innerHTML = context.deviceType;