@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
@@ -3,8 +3,8 @@
3
3
  * robotvacuumcleaner.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
- import { Subject, interval, skipWhile } from 'rxjs';
7
- import { debounceTime, take, tap } from 'rxjs/operators';
6
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
7
+ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
8
8
  export class RobotVacuumCleaner extends deviceBase {
9
9
  platform;
10
10
  // Services
@@ -81,14 +81,14 @@ export class RobotVacuumCleaner extends deviceBase {
81
81
  this.refreshStatus();
82
82
  // Update Homekit
83
83
  this.updateHomeKitCharacteristics();
84
+ //regisiter webhook event handler
85
+ this.registerWebhook();
84
86
  // Start an update interval
85
87
  interval(this.deviceRefreshRate * 1000)
86
88
  .pipe(skipWhile(() => this.robotVacuumCleanerUpdateInProgress))
87
89
  .subscribe(async () => {
88
90
  await this.refreshStatus();
89
91
  });
90
- //regisiter webhook event handler
91
- this.registerWebhook(accessory, device);
92
92
  // Watch for Plug change events
93
93
  // We put in a debounce of 100ms so we don't make duplicate calls
94
94
  this.doRobotVacuumCleanerUpdate
@@ -100,66 +100,46 @@ export class RobotVacuumCleaner extends deviceBase {
100
100
  await this.pushChanges();
101
101
  }
102
102
  catch (e) {
103
- this.apiError(e);
104
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
105
- + ` Error Message: ${JSON.stringify(e.message)}`);
103
+ await this.apiError(e);
104
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
106
105
  }
107
106
  this.robotVacuumCleanerUpdateInProgress = false;
108
107
  });
109
108
  }
110
109
  async BLEparseStatus(serviceData) {
111
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
112
- // Battery
110
+ await this.debugLog('BLEparseStatus');
111
+ // On
112
+ this.LightBulb.On = serviceData.state === 'on' ? true : false;
113
+ await this.debugLog(`On: ${this.LightBulb.On}`);
114
+ // BatteryLevel
113
115
  this.Battery.BatteryLevel = Number(serviceData.battery);
114
- if (this.Battery.BatteryLevel < 10) {
115
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
116
- }
117
- else {
118
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
119
- }
120
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
121
- // State
122
- switch (serviceData.state) {
123
- case 'on':
124
- this.LightBulb.On = true;
125
- break;
126
- default:
127
- this.LightBulb.On = false;
128
- }
129
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
116
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
117
+ // StatusLowBattery
118
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
119
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
120
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
130
121
  }
131
122
  async openAPIparseStatus(deviceStatus) {
132
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
133
- switch (deviceStatus.body.power) {
134
- case 'on':
135
- this.LightBulb.On = true;
136
- break;
137
- default:
138
- this.LightBulb.On = false;
139
- }
140
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
123
+ await this.debugLog('openAPIparseStatus');
124
+ // On
125
+ this.LightBulb.On = deviceStatus.onlineStatus === 'online' ? true : false;
126
+ await this.debugLog(`On: ${this.LightBulb.On}`);
141
127
  // BatteryLevel
142
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
128
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
129
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
143
130
  // StatusLowBattery
144
- if (this.Battery.BatteryLevel < 10) {
145
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
146
- }
147
- else {
148
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
149
- }
150
- if (Number.isNaN(this.Battery.BatteryLevel)) {
151
- this.Battery.BatteryLevel = 100;
152
- }
131
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
132
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
133
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
153
134
  // ChargingState
154
- this.Battery.ChargingState = deviceStatus.body.workingStatus === 'Charging'
135
+ this.Battery.ChargingState = deviceStatus.workingStatus === 'Charging'
155
136
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
156
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
157
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}, ChargingState: ${this.Battery.ChargingState}`);
137
+ await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
158
138
  // Firmware Version
159
- const version = deviceStatus.body.version?.toString();
160
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
161
- if (deviceStatus.body.version) {
162
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
139
+ if (deviceStatus.version) {
140
+ const version = deviceStatus.version.toString();
141
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
142
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
163
143
  this.accessory
164
144
  .getService(this.hap.Service.AccessoryInformation)
165
145
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -167,15 +147,34 @@ export class RobotVacuumCleaner extends deviceBase {
167
147
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
168
148
  .updateValue(deviceVersion);
169
149
  this.accessory.context.deviceVersion = deviceVersion;
170
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
150
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
171
151
  }
172
152
  }
153
+ async parseStatusWebhook(context) {
154
+ await this.debugLog('parseStatusWebhook');
155
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${context.onlineStatus}, ${context.battery}, ${context.workingStatus}),`
156
+ + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
157
+ // On
158
+ this.LightBulb.On = context.onlineStatus === 'online' ? true : false;
159
+ await this.debugLog(`On: ${this.LightBulb.On}`);
160
+ // BatteryLevel
161
+ this.Battery.BatteryLevel = Number(context.battery);
162
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
163
+ // StatusLowBattery
164
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
165
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
166
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
167
+ // ChargingState
168
+ this.Battery.ChargingState = context.workingStatus === 'Charging'
169
+ ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
170
+ await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
171
+ }
173
172
  /**
174
173
  * Asks the SwitchBot API for the latest device information
175
174
  */
176
175
  async refreshStatus() {
177
176
  if (!this.device.enableCloudService && this.OpenAPI) {
178
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
177
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
179
178
  }
180
179
  else if (this.BLE) {
181
180
  await this.BLERefreshStatus();
@@ -185,12 +184,11 @@ export class RobotVacuumCleaner extends deviceBase {
185
184
  }
186
185
  else {
187
186
  await this.offlineOff();
188
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
189
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
187
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
190
188
  }
191
189
  }
192
190
  async BLERefreshStatus() {
193
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
191
+ await this.debugLog('BLERefreshStatus');
194
192
  const switchbot = await this.switchbotBLE();
195
193
  if (switchbot === undefined) {
196
194
  await this.BLERefreshConnection(switchbot);
@@ -201,62 +199,55 @@ export class RobotVacuumCleaner extends deviceBase {
201
199
  // Start to monitor advertisement packets
202
200
  const serviceData = await this.monitorAdvertisementPackets(switchbot);
203
201
  // Update HomeKit
204
- if (serviceData.model !== '' && serviceData.modelName !== '') {
202
+ if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
205
203
  await this.BLEparseStatus(serviceData);
206
204
  await this.updateHomeKitCharacteristics();
207
205
  }
208
206
  else {
209
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
207
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
210
208
  await this.BLERefreshConnection(switchbot);
211
209
  }
212
210
  })();
213
211
  }
214
212
  }
215
213
  async openAPIRefreshStatus() {
216
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
214
+ await this.debugLog('openAPIRefreshStatus');
217
215
  try {
218
216
  const { body, statusCode } = await this.deviceRefreshStatus();
219
217
  const deviceStatus = await body.json();
220
- await this.refreshStatusCodes(statusCode, deviceStatus);
218
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
221
219
  ;
222
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
223
- await this.successfulRefreshStatus(statusCode, deviceStatus);
224
- await this.openAPIparseStatus(deviceStatus);
220
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
221
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
222
+ await this.openAPIparseStatus(deviceStatus.body);
225
223
  await this.updateHomeKitCharacteristics();
226
224
  }
227
225
  else {
228
- await this.statusCodes(statusCode, deviceStatus);
226
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
227
+ await this.debugWarnLog(statusCode, deviceStatus);
229
228
  }
230
229
  }
231
230
  catch (e) {
232
231
  await this.apiError(e);
233
- await this.openAPIRefreshError(e);
232
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
234
233
  }
235
234
  }
236
- async registerWebhook(accessory, device) {
237
- if (device.webhook) {
238
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
239
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
235
+ async registerWebhook() {
236
+ if (this.device.webhook) {
237
+ await this.debugLog('is listening webhook.');
238
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
240
239
  try {
241
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
242
- const { onlineStatus, battery, workingStatus } = context;
243
- const { On } = this.LightBulb;
244
- const { BatteryLevel, ChargingState } = this.Battery;
245
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (onlineStatus, battery, workingStatus) = `
246
- + `Webhook: (${onlineStatus}, ${battery}, ${workingStatus}), current: (${On}, ${BatteryLevel}, ${ChargingState})`);
247
- this.LightBulb.On = onlineStatus === 'online' ? true : false;
248
- this.Battery.ChargingState = workingStatus === 'Charging'
249
- ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
250
- this.Battery.BatteryLevel = battery;
251
- this.updateHomeKitCharacteristics();
240
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
241
+ await this.parseStatusWebhook(context);
242
+ await this.updateHomeKitCharacteristics();
252
243
  }
253
244
  catch (e) {
254
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
245
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
255
246
  }
256
247
  };
257
248
  }
258
249
  else {
259
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
250
+ await this.debugLog('is not listening webhook.');
260
251
  }
261
252
  }
262
253
  /**
@@ -269,7 +260,7 @@ export class RobotVacuumCleaner extends deviceBase {
269
260
  */
270
261
  async pushChanges() {
271
262
  if (!this.device.enableCloudService && this.OpenAPI) {
272
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
263
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
273
264
  }
274
265
  else if (this.BLE) {
275
266
  await this.BLEpushChanges();
@@ -279,7 +270,7 @@ export class RobotVacuumCleaner extends deviceBase {
279
270
  }
280
271
  else {
281
272
  await this.offlineOff();
282
- await this.pushChangeDisabled();
273
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
283
274
  }
284
275
  // Refresh the status from the API
285
276
  interval(15000)
@@ -290,160 +281,122 @@ export class RobotVacuumCleaner extends deviceBase {
290
281
  });
291
282
  }
292
283
  async BLEpushChanges() {
293
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
284
+ await this.debugLog('BLEpushChanges');
294
285
  if (this.LightBulb.On !== this.accessory.context.On) {
295
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
296
- + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
297
- const switchbot = await this.platform.connectBLE();
298
- // Convert to BLE Address
286
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
299
287
  await this.convertBLEAddress();
300
- switchbot
301
- .discover({
302
- model: '?',
303
- id: this.device.bleMac,
304
- })
305
- .then(async (device_list) => {
306
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
307
- return await this.retryBLE({
308
- max: await this.maxRetryBLE(),
309
- fn: async () => {
310
- if (this.LightBulb.On) {
311
- return await device_list[0].turnOn({ id: this.device.bleMac });
312
- }
313
- else {
314
- return await device_list[0].turnOff({ id: this.device.bleMac });
315
- }
316
- },
288
+ if (switchbot !== false) {
289
+ switchbot
290
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
291
+ .then(async (device_list) => {
292
+ await this.infoLog(`On: ${this.LightBulb.On}`);
293
+ return await this.retryBLE({
294
+ max: await this.maxRetryBLE(),
295
+ fn: async () => {
296
+ if (this.LightBulb.On) {
297
+ return await device_list[0].turnOn({ id: this.device.bleMac });
298
+ }
299
+ else {
300
+ return await device_list[0].turnOff({ id: this.device.bleMac });
301
+ }
302
+ },
303
+ });
304
+ })
305
+ .then(async () => {
306
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`);
307
+ await this.updateHomeKitCharacteristics();
308
+ })
309
+ .catch(async (e) => {
310
+ await this.apiError(e);
311
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
312
+ await this.BLEPushConnection();
317
313
  });
318
- })
319
- .then(() => {
320
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
321
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
322
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
323
- this.LightBulb.On = false;
324
- })
325
- .catch(async (e) => {
326
- this.apiError(e);
327
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
328
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
314
+ }
315
+ else {
316
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
329
317
  await this.BLEPushConnection();
330
- });
318
+ }
331
319
  }
332
320
  else {
333
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
334
- + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
321
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
335
322
  }
336
323
  }
337
324
  async openAPIpushChanges() {
338
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
325
+ await this.debugLog('openAPIpushChanges');
339
326
  if (this.LightBulb.On !== this.accessory.context.On) {
340
- const bodyChange = await this.commands();
341
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
327
+ const command = this.LightBulb.On ? 'start' : 'dock';
328
+ const bodyChange = JSON.stringify({
329
+ command: `${command}`,
330
+ parameter: 'default',
331
+ commandType: 'command',
332
+ });
333
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
342
334
  try {
343
335
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
344
336
  const deviceStatus = await body.json();
345
- await this.pushStatusCodes(statusCode, deviceStatus);
346
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
347
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
348
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
349
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
350
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
337
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
338
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
339
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
340
+ await this.updateHomeKitCharacteristics();
351
341
  }
352
342
  else {
353
- this.statusCode(statusCode);
354
- this.statusCode(deviceStatus.statusCode);
343
+ await this.statusCode(statusCode);
344
+ await this.statusCode(deviceStatus.statusCode);
355
345
  }
356
346
  }
357
347
  catch (e) {
358
348
  await this.apiError(e);
359
- await this.pushChangeError('openAPIpushChanges', e);
349
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
360
350
  }
361
351
  }
362
352
  else {
363
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges, On: ${this.LightBulb.On}, `
364
- + `OnCached: ${this.accessory.context.On}`);
353
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
365
354
  }
366
355
  }
367
356
  async openAPIpushBrightnessChanges() {
368
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushBrightnessChanges`);
369
- const bodyChange = await this.brightnessCommands();
370
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
371
- try {
372
- const { body, statusCode } = await this.pushChangeRequest(bodyChange);
373
- const deviceStatus = await body.json();
374
- await this.refreshStatusCodes(statusCode, deviceStatus);
375
- ;
376
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
377
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
378
- await this.updateHomeKitCharacteristics();
357
+ await this.debugLog('openAPIpushBrightnessChanges');
358
+ if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
359
+ const command = this.LightBulb.Brightness === 0 ? 'dock' : 'PowLevel';
360
+ const parameter = this.LightBulb.Brightness === 25 ? '0' : this.LightBulb.Brightness === 50 ? '1' : this.LightBulb.Brightness === 75
361
+ ? '2' : this.LightBulb.Brightness === 100 ? '3' : 'default';
362
+ const bodyChange = JSON.stringify({
363
+ command: `${command}`,
364
+ parameter: `${parameter}`,
365
+ commandType: 'command',
366
+ });
367
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
368
+ try {
369
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
370
+ const deviceStatus = await body.json();
371
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
372
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
373
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
374
+ await this.updateHomeKitCharacteristics();
375
+ }
376
+ else {
377
+ await this.statusCode(statusCode);
378
+ await this.statusCode(deviceStatus.statusCode);
379
+ }
379
380
  }
380
- else {
381
- await this.statusCode(statusCode);
382
- await this.statusCode(deviceStatus.statusCode);
381
+ catch (e) {
382
+ await this.apiError(e);
383
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
383
384
  }
384
385
  }
385
- catch (e) {
386
- await this.apiError(e);
387
- await this.pushChangeError('openAPIpushBrightnessChanges', e);
388
- }
389
- }
390
- async commands() {
391
- let command;
392
- let parameter;
393
- if (this.LightBulb.On) {
394
- command = 'start';
395
- parameter = 'default';
396
- }
397
386
  else {
398
- command = 'dock';
399
- parameter = 'default';
387
+ await this.debugLog(`No changes (openAPIpushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
388
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
400
389
  }
401
- const body = JSON.stringify({
402
- command: `${command}`,
403
- parameter: `${parameter}`,
404
- commandType: 'command',
405
- });
406
- return body;
407
- }
408
- async brightnessCommands() {
409
- let command;
410
- let parameter;
411
- if (this.LightBulb.Brightness === 25) {
412
- command = 'PowLevel';
413
- parameter = '0';
414
- }
415
- else if (this.LightBulb.Brightness === 50) {
416
- command = 'PowLevel';
417
- parameter = '1';
418
- }
419
- else if (this.LightBulb.Brightness === 75) {
420
- command = 'PowLevel';
421
- parameter = '2';
422
- }
423
- else if (this.LightBulb.Brightness === 100) {
424
- command = 'PowLevel';
425
- parameter = '3';
426
- }
427
- else {
428
- command = 'dock';
429
- parameter = 'default';
430
- }
431
- const bodyChange = JSON.stringify({
432
- command: `${command}`,
433
- parameter: `${parameter}`,
434
- commandType: 'command',
435
- });
436
- return bodyChange;
437
390
  }
438
391
  /**
439
392
  * Handle requests to set the value of the "On" characteristic
440
393
  */
441
394
  async OnSet(value) {
442
- if (this.LightBulb.On === this.accessory.context.On) {
443
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
395
+ if (this.LightBulb.On !== this.accessory.context.On) {
396
+ this.infoLog(`Set On: ${value}`);
444
397
  }
445
398
  else {
446
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
399
+ this.debugLog(`No Changes, On: ${value}`);
447
400
  }
448
401
  this.LightBulb.On = value;
449
402
  this.doRobotVacuumCleanerUpdate.next();
@@ -452,78 +405,42 @@ export class RobotVacuumCleaner extends deviceBase {
452
405
  * Handle requests to set the value of the "Brightness" characteristic
453
406
  */
454
407
  async BrightnessSet(value) {
455
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
456
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
457
- }
458
- else if (this.LightBulb.On) {
459
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
408
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
409
+ await this.infoLog(`Set Brightness: ${value}`);
460
410
  }
461
411
  else {
462
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
412
+ if (this.LightBulb.On) {
413
+ this.debugLog(`No Changes, Brightness: ${value}`);
414
+ }
415
+ else {
416
+ this.debugLog(`Brightness: ${value}, On: ${this.LightBulb.On}`);
417
+ }
463
418
  }
464
419
  this.LightBulb.Brightness = value;
465
420
  this.doRobotVacuumCleanerUpdate.next();
466
421
  }
467
422
  async updateHomeKitCharacteristics() {
468
423
  // On
469
- if (this.LightBulb.On === undefined) {
470
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
471
- }
472
- else {
473
- this.accessory.context.On = this.LightBulb.On;
474
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
475
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
476
- }
424
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On');
477
425
  // Brightness
478
- if (this.LightBulb.Brightness === undefined) {
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
480
- }
481
- else {
482
- this.accessory.context.Brightness = this.LightBulb.Brightness;
483
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
484
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
485
- }
426
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness, this.LightBulb.Brightness, 'Brightness');
486
427
  // BatteryLevel
487
- if (this.Battery.BatteryLevel === undefined) {
488
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
489
- }
490
- else {
491
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
492
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
494
- }
428
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel');
495
429
  // StatusLowBattery
496
- if (this.Battery.StatusLowBattery === undefined) {
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
498
- }
499
- else {
500
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
501
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
502
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
503
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
504
- }
430
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery');
505
431
  // ChargingState
506
- if (this.Battery.ChargingState === undefined) {
507
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
508
- }
509
- else {
510
- this.accessory.context.ChargingState = this.Battery.ChargingState;
511
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
512
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
513
- + ` ChargingState: ${this.Battery.ChargingState}`);
514
- }
432
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.ChargingState, this.Battery.ChargingState, 'ChargingState');
515
433
  }
516
434
  async BLEPushConnection() {
517
435
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
518
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
436
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
519
437
  await this.openAPIpushChanges();
520
438
  }
521
439
  }
522
440
  async BLERefreshConnection(switchbot) {
523
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
524
- + ` ${JSON.stringify(switchbot)}`);
441
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
525
442
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
526
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
443
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
527
444
  await this.openAPIRefreshStatus();
528
445
  }
529
446
  }