@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
@@ -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,63 +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
- this.apiError(e);
233
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
234
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
231
+ await this.apiError(e);
232
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
235
233
  }
236
234
  }
237
- async registerWebhook(accessory, device) {
238
- if (device.webhook) {
239
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
240
- 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) => {
241
239
  try {
242
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
243
- const { onlineStatus, battery, workingStatus } = context;
244
- const { On } = this.LightBulb;
245
- const { BatteryLevel, ChargingState } = this.Battery;
246
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (onlineStatus, battery, workingStatus) = `
247
- + `Webhook: (${onlineStatus}, ${battery}, ${workingStatus}), current: (${On}, ${BatteryLevel}, ${ChargingState})`);
248
- this.LightBulb.On = onlineStatus === 'online' ? true : false;
249
- this.Battery.ChargingState = workingStatus === 'Charging'
250
- ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
251
- this.Battery.BatteryLevel = battery;
252
- this.updateHomeKitCharacteristics();
240
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
241
+ await this.parseStatusWebhook(context);
242
+ await this.updateHomeKitCharacteristics();
253
243
  }
254
244
  catch (e) {
255
- 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}`);
256
246
  }
257
247
  };
258
248
  }
259
249
  else {
260
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
250
+ await this.debugLog('is not listening webhook.');
261
251
  }
262
252
  }
263
253
  /**
@@ -270,7 +260,7 @@ export class RobotVacuumCleaner extends deviceBase {
270
260
  */
271
261
  async pushChanges() {
272
262
  if (!this.device.enableCloudService && this.OpenAPI) {
273
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
263
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
274
264
  }
275
265
  else if (this.BLE) {
276
266
  await this.BLEpushChanges();
@@ -280,8 +270,7 @@ export class RobotVacuumCleaner extends deviceBase {
280
270
  }
281
271
  else {
282
272
  await this.offlineOff();
283
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
284
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
273
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
285
274
  }
286
275
  // Refresh the status from the API
287
276
  interval(15000)
@@ -292,162 +281,122 @@ export class RobotVacuumCleaner extends deviceBase {
292
281
  });
293
282
  }
294
283
  async BLEpushChanges() {
295
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
284
+ await this.debugLog('BLEpushChanges');
296
285
  if (this.LightBulb.On !== this.accessory.context.On) {
297
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
298
- + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
299
- const switchbot = await this.platform.connectBLE();
300
- // Convert to BLE Address
286
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
301
287
  await this.convertBLEAddress();
302
- switchbot
303
- .discover({
304
- model: '?',
305
- id: this.device.bleMac,
306
- })
307
- .then(async (device_list) => {
308
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
309
- return await this.retryBLE({
310
- max: await this.maxRetryBLE(),
311
- fn: async () => {
312
- if (this.LightBulb.On) {
313
- return await device_list[0].turnOn({ id: this.device.bleMac });
314
- }
315
- else {
316
- return await device_list[0].turnOff({ id: this.device.bleMac });
317
- }
318
- },
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();
319
313
  });
320
- })
321
- .then(() => {
322
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
323
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
324
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
325
- this.LightBulb.On = false;
326
- })
327
- .catch(async (e) => {
328
- this.apiError(e);
329
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
330
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
314
+ }
315
+ else {
316
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
331
317
  await this.BLEPushConnection();
332
- });
318
+ }
333
319
  }
334
320
  else {
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
336
- + ` 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}`);
337
322
  }
338
323
  }
339
324
  async openAPIpushChanges() {
340
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
325
+ await this.debugLog('openAPIpushChanges');
341
326
  if (this.LightBulb.On !== this.accessory.context.On) {
342
- const bodyChange = await this.commands();
343
- 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)}`);
344
334
  try {
345
335
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
346
336
  const deviceStatus = await body.json();
347
- await this.pushStatusCodes(statusCode, deviceStatus);
348
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
349
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
350
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
351
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
352
- + `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();
353
341
  }
354
342
  else {
355
- this.statusCode(statusCode);
356
- this.statusCode(deviceStatus.statusCode);
343
+ await this.statusCode(statusCode);
344
+ await this.statusCode(deviceStatus.statusCode);
357
345
  }
358
346
  }
359
347
  catch (e) {
360
- this.apiError(e);
361
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
362
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
348
+ await this.apiError(e);
349
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
363
350
  }
364
351
  }
365
352
  else {
366
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges, On: ${this.LightBulb.On}, `
367
- + `OnCached: ${this.accessory.context.On}`);
353
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
368
354
  }
369
355
  }
370
356
  async openAPIpushBrightnessChanges() {
371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushBrightnessChanges`);
372
- const bodyChange = await this.brightnessCommands();
373
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
374
- try {
375
- const { body, statusCode } = await this.pushChangeRequest(bodyChange);
376
- const deviceStatus = await body.json();
377
- await this.refreshStatusCodes(statusCode, deviceStatus);
378
- ;
379
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
380
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
381
- 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
+ }
382
380
  }
383
- else {
384
- await this.statusCode(statusCode);
385
- 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)}`);
386
384
  }
387
385
  }
388
- catch (e) {
389
- this.apiError(e);
390
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
391
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
392
- }
393
- }
394
- async commands() {
395
- let command;
396
- let parameter;
397
- if (this.LightBulb.On) {
398
- command = 'start';
399
- parameter = 'default';
400
- }
401
386
  else {
402
- command = 'dock';
403
- parameter = 'default';
387
+ await this.debugLog(`No changes (openAPIpushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
388
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
404
389
  }
405
- const body = JSON.stringify({
406
- command: `${command}`,
407
- parameter: `${parameter}`,
408
- commandType: 'command',
409
- });
410
- return body;
411
- }
412
- async brightnessCommands() {
413
- let command;
414
- let parameter;
415
- if (this.LightBulb.Brightness === 25) {
416
- command = 'PowLevel';
417
- parameter = '0';
418
- }
419
- else if (this.LightBulb.Brightness === 50) {
420
- command = 'PowLevel';
421
- parameter = '1';
422
- }
423
- else if (this.LightBulb.Brightness === 75) {
424
- command = 'PowLevel';
425
- parameter = '2';
426
- }
427
- else if (this.LightBulb.Brightness === 100) {
428
- command = 'PowLevel';
429
- parameter = '3';
430
- }
431
- else {
432
- command = 'dock';
433
- parameter = 'default';
434
- }
435
- const bodyChange = JSON.stringify({
436
- command: `${command}`,
437
- parameter: `${parameter}`,
438
- commandType: 'command',
439
- });
440
- return bodyChange;
441
390
  }
442
391
  /**
443
392
  * Handle requests to set the value of the "On" characteristic
444
393
  */
445
394
  async OnSet(value) {
446
- if (this.LightBulb.On === this.accessory.context.On) {
447
- 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}`);
448
397
  }
449
398
  else {
450
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
399
+ this.debugLog(`No Changes, On: ${value}`);
451
400
  }
452
401
  this.LightBulb.On = value;
453
402
  this.doRobotVacuumCleanerUpdate.next();
@@ -456,78 +405,42 @@ export class RobotVacuumCleaner extends deviceBase {
456
405
  * Handle requests to set the value of the "Brightness" characteristic
457
406
  */
458
407
  async BrightnessSet(value) {
459
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
460
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
461
- }
462
- else if (this.LightBulb.On) {
463
- 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}`);
464
410
  }
465
411
  else {
466
- 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
+ }
467
418
  }
468
419
  this.LightBulb.Brightness = value;
469
420
  this.doRobotVacuumCleanerUpdate.next();
470
421
  }
471
422
  async updateHomeKitCharacteristics() {
472
423
  // On
473
- if (this.LightBulb.On === undefined) {
474
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
475
- }
476
- else {
477
- this.accessory.context.On = this.LightBulb.On;
478
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
480
- }
424
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On');
481
425
  // Brightness
482
- if (this.LightBulb.Brightness === undefined) {
483
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
484
- }
485
- else {
486
- this.accessory.context.Brightness = this.LightBulb.Brightness;
487
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
488
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
489
- }
426
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness, this.LightBulb.Brightness, 'Brightness');
490
427
  // BatteryLevel
491
- if (this.Battery.BatteryLevel === undefined) {
492
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
493
- }
494
- else {
495
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
496
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
498
- }
428
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel');
499
429
  // StatusLowBattery
500
- if (this.Battery.StatusLowBattery === undefined) {
501
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
502
- }
503
- else {
504
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
505
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
506
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
507
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
508
- }
430
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery');
509
431
  // ChargingState
510
- if (this.Battery.ChargingState === undefined) {
511
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
512
- }
513
- else {
514
- this.accessory.context.ChargingState = this.Battery.ChargingState;
515
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
516
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
517
- + ` ChargingState: ${this.Battery.ChargingState}`);
518
- }
432
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.ChargingState, this.Battery.ChargingState, 'ChargingState');
519
433
  }
520
434
  async BLEPushConnection() {
521
435
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
522
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
436
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
523
437
  await this.openAPIpushChanges();
524
438
  }
525
439
  }
526
440
  async BLERefreshConnection(switchbot) {
527
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
528
- + ` ${JSON.stringify(switchbot)}`);
441
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
529
442
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
530
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
443
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
531
444
  await this.openAPIRefreshStatus();
532
445
  }
533
446
  }