@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,9 +3,9 @@
3
3
  * blindtilt.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
- import { interval, Subject } from 'rxjs';
7
6
  import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
8
- import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
7
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
+ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
9
  /**
10
10
  * Platform Accessory
11
11
  * An instance of this class is created for each accessory your platform registers
@@ -46,7 +46,7 @@ export class ColorBulb extends deviceBase {
46
46
  accessory.removeService(this.LightBulb.Service);
47
47
  this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
48
48
  accessory.context.adaptiveLighting = false;
49
- this.debugLog(`${device.deviceType}: ${accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting}`);
49
+ this.debugLog(`adaptiveLighting: ${accessory.context.adaptiveLighting}`);
50
50
  }
51
51
  if (this.adaptiveLightingShift !== -1) {
52
52
  this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
@@ -115,14 +115,14 @@ export class ColorBulb extends deviceBase {
115
115
  this.refreshStatus();
116
116
  // Update Homekit
117
117
  this.updateHomeKitCharacteristics();
118
+ //regisiter webhook event handler
119
+ this.registerWebhook();
118
120
  // Start an update interval
119
121
  interval(this.deviceRefreshRate * 1000)
120
122
  .pipe(skipWhile(() => this.colorBulbUpdateInProgress))
121
123
  .subscribe(async () => {
122
124
  await this.refreshStatus();
123
125
  });
124
- //regisiter webhook event handler
125
- this.registerWebhook(accessory, device);
126
126
  // Watch for Bulb change events
127
127
  // We put in a debounce of 100ms so we don't make duplicate calls
128
128
  this.doColorBulbUpdate
@@ -134,9 +134,8 @@ export class ColorBulb extends deviceBase {
134
134
  await this.pushChanges();
135
135
  }
136
136
  catch (e) {
137
- this.apiError(e);
138
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
139
- + ` Error Message: ${JSON.stringify(e.message)}`);
137
+ await this.apiError(e);
138
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
140
139
  }
141
140
  this.colorBulbUpdateInProgress = false;
142
141
  });
@@ -145,86 +144,59 @@ export class ColorBulb extends deviceBase {
145
144
  * Parse the device status from the SwitchBotBLE API
146
145
  */
147
146
  async BLEparseStatus(serviceData) {
148
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
149
- // State
150
- switch (serviceData.power) {
151
- case true:
152
- this.LightBulb.On = true;
153
- break;
154
- default:
155
- this.LightBulb.On = false;
156
- }
157
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
147
+ await this.debugLog('BLEparseStatus');
148
+ // On
149
+ this.LightBulb.On = serviceData.power;
150
+ await this.debugLog(`On: ${this.LightBulb.On}`);
158
151
  // Brightness
159
- this.LightBulb.Brightness = Number(serviceData.brightness);
160
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
152
+ this.LightBulb.Brightness = serviceData.brightness;
153
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
161
154
  // Color, Hue & Brightness
162
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
163
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
164
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
165
- const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
166
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
167
- + ` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`);
155
+ await this.debugLog(`red: ${serviceData.red}, green: ${serviceData.green}, blue: ${serviceData.blue}`);
156
+ const [hue, saturation] = rgb2hs(serviceData.red, serviceData.green, serviceData.blue);
157
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(serviceData.red, serviceData.green, serviceData.blue))}`);
168
158
  // Hue
169
159
  this.LightBulb.Hue = hue;
170
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
160
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
171
161
  // Saturation
172
162
  this.LightBulb.Saturation = saturation;
173
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
163
+ await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
174
164
  // ColorTemperature
175
- if (serviceData.color_temperature) {
176
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
177
- this.LightBulb.ColorTemperature = serviceData.color_temperature;
178
- this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
179
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
180
- }
165
+ this.LightBulb.ColorTemperature = Math.max(Math.min(serviceData.color_temperature, 500), 140);
166
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
181
167
  }
182
168
  /**
183
169
  * Parse the device status from the SwitchBot OpenAPI
184
170
  */
185
171
  async openAPIparseStatus(deviceStatus) {
186
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
187
- switch (deviceStatus.body.power) {
188
- case 'on':
189
- this.LightBulb.On = true;
190
- break;
191
- default:
192
- this.LightBulb.On = false;
193
- }
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
172
+ await this.debugLog('openAPIparseStatus');
173
+ // On
174
+ this.LightBulb.On = deviceStatus.power === 'on' ? true : false;
175
+ await this.debugLog(`On: ${this.LightBulb.On}`);
195
176
  // Brightness
196
- this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
197
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
177
+ this.LightBulb.Brightness = deviceStatus.brightness;
178
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
198
179
  // Color, Hue & Brightness
199
- if (deviceStatus.body.color) {
200
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
201
- const [red, green, blue] = deviceStatus.body.color.split(':');
202
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
203
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
204
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
205
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
206
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
207
- + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
208
- // Hue
209
- this.LightBulb.Hue = hue;
210
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
211
- // Saturation
212
- this.LightBulb.Saturation = saturation;
213
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
214
- }
180
+ await this.debugLog(`color: ${JSON.stringify(deviceStatus.color)}`);
181
+ const [red, green, blue] = deviceStatus.color.split(':');
182
+ await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
183
+ const [hue, saturation] = rgb2hs(red, green, blue);
184
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
185
+ // Hue
186
+ this.LightBulb.Hue = hue;
187
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
188
+ // Saturation
189
+ this.LightBulb.Saturation = saturation;
190
+ await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
215
191
  // ColorTemperature
216
- if (!Number.isNaN(deviceStatus.body.colorTemperature)) {
217
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${deviceStatus.body.colorTemperature}`);
218
- const mired = Math.round(1000000 / deviceStatus.body.colorTemperature);
219
- this.LightBulb.ColorTemperature = Number(mired);
220
- this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
221
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
222
- }
192
+ const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
193
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
194
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
223
195
  // Firmware Version
224
- const version = deviceStatus.body.version?.toString();
225
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
226
- if (deviceStatus.body.version) {
227
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
196
+ const version = deviceStatus.version.toString();
197
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
198
+ if (deviceStatus.version) {
199
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
228
200
  this.accessory
229
201
  .getService(this.hap.Service.AccessoryInformation)
230
202
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -232,15 +204,42 @@ export class ColorBulb extends deviceBase {
232
204
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
233
205
  .updateValue(deviceVersion);
234
206
  this.accessory.context.deviceVersion = deviceVersion;
235
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
207
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
236
208
  }
237
209
  }
210
+ async parseStatusWebhook(context) {
211
+ await this.debugLog('parseStatusWebhook');
212
+ await this.debugLog(`(powerState, brightness, color, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness}, ${context.color},`
213
+ + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.Hue},`
214
+ + ` ${this.LightBulb.Saturation}, ${this.LightBulb.ColorTemperature})`);
215
+ // On
216
+ this.LightBulb.On = context.powerState === 'ON' ? true : false;
217
+ await this.debugLog(`On: ${this.LightBulb.On}`);
218
+ // Brightness
219
+ this.LightBulb.Brightness = context.brightness;
220
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
221
+ // Color, Hue & Brightness
222
+ await this.debugLog(`color: ${JSON.stringify(context.color)}`);
223
+ const [red, green, blue] = context.color.split(':');
224
+ await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
225
+ const [hue, saturation] = rgb2hs(red, green, blue);
226
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
227
+ // Hue
228
+ this.LightBulb.Hue = hue;
229
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
230
+ // Saturation
231
+ this.LightBulb.Saturation = saturation;
232
+ await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
233
+ // ColorTemperature
234
+ this.LightBulb.ColorTemperature = context.colorTemperature;
235
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
236
+ }
238
237
  /**
239
238
  * Asks the SwitchBot API for the latest device information
240
239
  */
241
240
  async refreshStatus() {
242
241
  if (!this.device.enableCloudService && this.OpenAPI) {
243
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
242
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
244
243
  }
245
244
  else if (this.BLE) {
246
245
  await this.BLERefreshStatus();
@@ -250,12 +249,11 @@ export class ColorBulb extends deviceBase {
250
249
  }
251
250
  else {
252
251
  await this.offlineOff();
253
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
254
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
252
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
255
253
  }
256
254
  }
257
255
  async BLERefreshStatus() {
258
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
256
+ await this.debugLog('BLERefreshStatus');
259
257
  const switchbot = await this.switchbotBLE();
260
258
  if (switchbot === undefined) {
261
259
  await this.BLERefreshConnection(switchbot);
@@ -266,108 +264,55 @@ export class ColorBulb extends deviceBase {
266
264
  // Start to monitor advertisement packets
267
265
  const serviceData = await this.monitorAdvertisementPackets(switchbot);
268
266
  // Update HomeKit
269
- if (serviceData.model !== '' && serviceData.modelName !== '') {
267
+ if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
270
268
  await this.BLEparseStatus(serviceData);
271
269
  await this.updateHomeKitCharacteristics();
272
270
  }
273
271
  else {
274
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
272
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
275
273
  await this.BLERefreshConnection(switchbot);
276
274
  }
277
275
  })();
278
276
  }
279
277
  }
280
278
  async openAPIRefreshStatus() {
281
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
279
+ await this.debugLog('openAPIRefreshStatus');
282
280
  try {
283
281
  const { body, statusCode } = await this.deviceRefreshStatus();
284
282
  const deviceStatus = await body.json();
285
- await this.refreshStatusCodes(statusCode, deviceStatus);
283
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
286
284
  ;
287
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
288
- await this.successfulRefreshStatus(statusCode, deviceStatus);
289
- await this.openAPIparseStatus(deviceStatus);
285
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
286
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
287
+ await this.openAPIparseStatus(deviceStatus.body);
290
288
  await this.updateHomeKitCharacteristics();
291
289
  }
292
290
  else {
293
- await this.statusCodes(statusCode, deviceStatus);
291
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
292
+ await this.debugWarnLog(statusCode, deviceStatus);
294
293
  }
295
294
  }
296
295
  catch (e) {
297
- this.apiError(e);
298
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
299
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
296
+ await this.apiError(e);
297
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
300
298
  }
301
299
  }
302
- async registerWebhook(accessory, device) {
303
- if (device.webhook) {
304
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
305
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
300
+ async registerWebhook() {
301
+ if (this.device.webhook) {
302
+ await this.debugLog('is listening webhook.');
303
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
306
304
  try {
307
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
308
- const { powerState, brightness, color, colorTemperature } = context;
309
- const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
310
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
311
- '(powerState, brightness, color, colorTemperature) = ' +
312
- `Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), `
313
- + `current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
314
- // On
315
- this.LightBulb.On = powerState === 'ON' ? true : false;
316
- if (accessory.context.Brightness !== this.LightBulb.On) {
317
- this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
318
- }
319
- else {
320
- this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
321
- }
322
- // Brightness
323
- this.LightBulb.Brightness = brightness;
324
- if (accessory.context.Brightness !== this.LightBulb.Brightness) {
325
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
326
- }
327
- else {
328
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
329
- }
330
- this.debugLog(`${device.deviceType}: ${accessory.displayName} color: ${JSON.stringify(color)}`);
331
- const [red, green, blue] = color.split(':');
332
- this.debugLog(`${device.deviceType}: ${accessory.displayName} red: ${JSON.stringify(red)}`);
333
- this.debugLog(`${device.deviceType}: ${accessory.displayName} green: ${JSON.stringify(green)}`);
334
- this.debugLog(`${device.deviceType}: ${accessory.displayName} blue: ${JSON.stringify(blue)}`);
335
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
336
- this.debugLog(`${device.deviceType}: ${accessory.displayName}`
337
- + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
338
- // Hue
339
- this.LightBulb.Hue = hue;
340
- if (accessory.context.Hue !== this.LightBulb.Hue) {
341
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
342
- }
343
- else {
344
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
345
- }
346
- // Saturation
347
- this.LightBulb.Saturation = saturation;
348
- if (accessory.context.Saturation !== this.LightBulb.Saturation) {
349
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
350
- }
351
- else {
352
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
353
- }
354
- this.LightBulb.ColorTemperature = colorTemperature;
355
- if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
356
- this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
357
- }
358
- else {
359
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
360
- }
361
- this.updateHomeKitCharacteristics();
305
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
306
+ await this.parseStatusWebhook(context);
307
+ await this.updateHomeKitCharacteristics();
362
308
  }
363
309
  catch (e) {
364
- this.errorLog(`${device.deviceType}: ${accessory.displayName} `
365
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
310
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
366
311
  }
367
312
  };
368
313
  }
369
314
  else {
370
- this.debugWarnLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
315
+ await this.debugLog('is not listening webhook.');
371
316
  }
372
317
  }
373
318
  /**
@@ -383,18 +328,45 @@ export class ColorBulb extends deviceBase {
383
328
  */
384
329
  async pushChanges() {
385
330
  if (!this.device.enableCloudService && this.OpenAPI) {
386
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
331
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
387
332
  }
388
333
  else if (this.BLE) {
389
334
  await this.BLEpushChanges();
335
+ if (this.LightBulb.On) {
336
+ // Push Brightness Update
337
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
338
+ await this.BLEpushBrightnessChanges();
339
+ // Push ColorTemperature Update
340
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
341
+ await this.BLEpushColorTemperatureChanges();
342
+ // Push Hue & Saturation Update
343
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
344
+ await this.BLEpushRGBChanges();
345
+ }
346
+ else {
347
+ await this.debugLog('BLE (Brightness), (ColorTemperature), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
348
+ }
390
349
  }
391
350
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
392
351
  await this.openAPIpushChanges();
352
+ if (this.LightBulb.On) {
353
+ // Push Brightness Update
354
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
355
+ await this.pushBrightnessChanges();
356
+ // Push ColorTemperature Update
357
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
358
+ await this.pushColorTemperatureChanges();
359
+ // Push Hue & Saturation Update
360
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
361
+ await this.pushHueSaturationChanges();
362
+ }
363
+ else {
364
+ await this.debugLog('openAPI (Brightness), (ColorTemperature), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
365
+ }
393
366
  }
394
367
  else {
395
368
  await this.offlineOff();
396
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
397
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
369
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
398
370
  }
399
371
  // Refresh the status from the API
400
372
  interval(15000)
@@ -405,242 +377,218 @@ export class ColorBulb extends deviceBase {
405
377
  });
406
378
  }
407
379
  async BLEpushChanges() {
408
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
380
+ await this.debugLog('BLEpushChanges');
409
381
  if (this.LightBulb.On !== this.accessory.context.On) {
410
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
411
- + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
412
- const switchbot = await this.platform.connectBLE();
413
- // Convert to BLE Address
382
+ await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
383
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
414
384
  await this.convertBLEAddress();
415
- switchbot
416
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
417
- .then(async (device_list) => {
418
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
419
- return await this.retryBLE({
420
- max: await this.maxRetryBLE(),
421
- fn: async () => {
422
- if (this.LightBulb.On) {
423
- return await device_list[0].turnOn({ id: this.device.bleMac });
424
- }
425
- else {
426
- return await device_list[0].turnOff({ id: this.device.bleMac });
427
- }
428
- },
385
+ if (switchbot !== false) {
386
+ switchbot
387
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
388
+ .then(async (device_list) => {
389
+ await this.infoLog(`On: ${this.LightBulb.On}`);
390
+ return await this.retryBLE({
391
+ max: await this.maxRetryBLE(),
392
+ fn: async () => {
393
+ if (this.LightBulb.On) {
394
+ return await device_list[0].turnOn({ id: this.device.bleMac });
395
+ }
396
+ else {
397
+ return await device_list[0].turnOff({ id: this.device.bleMac });
398
+ }
399
+ },
400
+ });
401
+ })
402
+ .then(async () => {
403
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`);
404
+ await this.updateHomeKitCharacteristics();
405
+ })
406
+ .catch(async (e) => {
407
+ await this.apiError(e);
408
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
409
+ await this.BLEPushConnection();
429
410
  });
430
- })
431
- .then(() => {
432
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
433
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
434
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
435
- this.LightBulb.On = false;
436
- })
437
- .catch(async (e) => {
438
- this.apiError(e);
439
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
440
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
441
- await this.BLEPushConnection();
442
- });
443
- // Push Brightness Update
444
- if (this.LightBulb.On) {
445
- await this.BLEpushBrightnessChanges();
446
411
  }
447
- // Push ColorTemperature Update
448
- if (this.LightBulb.On) {
449
- await this.BLEpushColorTemperatureChanges();
450
- }
451
- // Push Hue & Saturation Update
452
- if (this.LightBulb.On) {
453
- await this.BLEpushRGBChanges();
412
+ else {
413
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
414
+ await this.BLEPushConnection();
454
415
  }
455
416
  }
456
417
  else {
457
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, On: ${this.LightBulb.On},`
458
- + ` OnCached: ${this.accessory.context.On}`);
418
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
459
419
  }
460
420
  }
461
421
  async BLEpushBrightnessChanges() {
462
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
422
+ await this.debugLog('BLEpushBrightnessChanges');
463
423
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
464
- const switchbot = await this.platform.connectBLE();
465
- // Convert to BLE Address
424
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
466
425
  await this.convertBLEAddress();
467
- switchbot
468
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
469
- .then(async (device_list) => {
470
- this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
471
- return await device_list[0].setBrightness(this.LightBulb.Brightness);
472
- })
473
- .then(() => {
474
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
475
- this.LightBulb.On = false;
476
- })
477
- .catch(async (e) => {
478
- this.apiError(e);
479
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
480
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
426
+ if (switchbot !== false) {
427
+ switchbot
428
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
429
+ .then(async (device_list) => {
430
+ await this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
431
+ return await device_list[0].setBrightness(this.LightBulb.Brightness);
432
+ })
433
+ .then(async () => {
434
+ await this.successLog(`Brightness: ${this.LightBulb.Brightness} sent over SwitchBot BLE, sent successfully`);
435
+ await this.updateHomeKitCharacteristics();
436
+ })
437
+ .catch(async (e) => {
438
+ await this.apiError(e);
439
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
440
+ await this.BLEPushConnection();
441
+ });
442
+ }
443
+ else {
444
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
481
445
  await this.BLEPushConnection();
482
- });
446
+ }
483
447
  }
484
448
  else {
485
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.`
486
- + `Brightness: ${this.LightBulb.Brightness}, BrightnessCached: ${this.accessory.context.Brightness}`);
449
+ await this.debugLog(`No changes (BLEpushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
450
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
487
451
  }
488
452
  }
489
453
  async BLEpushColorTemperatureChanges() {
490
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushColorTemperatureChanges`);
454
+ await this.debugLog('BLEpushColorTemperatureChanges');
491
455
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
492
- const switchbot = await this.platform.connectBLE();
493
- // Convert to BLE Address
456
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
494
457
  await this.convertBLEAddress();
495
- switchbot
496
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
497
- .then(async (device_list) => {
498
- this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
499
- return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
500
- })
501
- .then(() => {
502
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
503
- this.LightBulb.On = false;
504
- })
505
- .catch(async (e) => {
506
- this.apiError(e);
507
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushColorTemperatureChanges with `
508
- + `${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
458
+ if (switchbot !== false) {
459
+ switchbot
460
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
461
+ .then(async (device_list) => {
462
+ await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
463
+ return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
464
+ })
465
+ .then(async () => {
466
+ await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`);
467
+ await this.updateHomeKitCharacteristics();
468
+ })
469
+ .catch(async (e) => {
470
+ await this.apiError(e);
471
+ await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection,`
472
+ + ` Error Message: ${JSON.stringify(e.message)}`);
473
+ await this.BLEPushConnection();
474
+ });
475
+ }
476
+ else {
477
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
509
478
  await this.BLEPushConnection();
510
- });
479
+ }
511
480
  }
512
481
  else {
513
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushColorTemperatureChanges.`
514
- + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
482
+ await this.debugLog(`No changes (BLEpushColorTemperatureChanges), ColorTemperature: ${this.LightBulb.ColorTemperature},`
483
+ + ` ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
515
484
  }
516
485
  }
517
486
  async BLEpushRGBChanges() {
518
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
519
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
520
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
521
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
522
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
523
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
524
- const switchbot = await this.platform.connectBLE();
525
- // Convert to BLE Address
487
+ await this.debugLog('BLEpushRGBChanges');
488
+ if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
489
+ await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
490
+ const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation);
491
+ await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
492
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
526
493
  await this.convertBLEAddress();
527
- switchbot
528
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
529
- .then(async (device_list) => {
530
- this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
531
- return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
532
- })
533
- .then(() => {
534
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
535
- this.LightBulb.On = false;
536
- })
537
- .catch(async (e) => {
538
- this.apiError(e);
539
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
540
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
494
+ if (switchbot !== false) {
495
+ switchbot
496
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
497
+ .then(async (device_list) => {
498
+ await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
499
+ return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
500
+ })
501
+ .then(async () => {
502
+ await this.successLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)} sent over SwitchBot BLE, sent successfully`);
503
+ await this.updateHomeKitCharacteristics();
504
+ })
505
+ .catch(async (e) => {
506
+ await this.apiError(e);
507
+ await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection,`
508
+ + ` Error Message: ${JSON.stringify(e.message)}`);
509
+ await this.BLEPushConnection();
510
+ });
511
+ }
512
+ else {
513
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
541
514
  await this.BLEPushConnection();
542
- });
515
+ }
543
516
  }
544
517
  else {
545
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
546
- + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
518
+ await this.debugLog(`No changes (BLEpushRGBChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
519
+ + ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
547
520
  }
548
521
  }
549
522
  async openAPIpushChanges() {
550
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
523
+ await this.debugLog('openAPIpushChanges');
551
524
  if (this.LightBulb.On !== this.accessory.context.On) {
552
- let command = '';
553
- if (this.LightBulb.On) {
554
- command = 'turnOn';
555
- }
556
- else {
557
- command = 'turnOff';
558
- }
525
+ const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
559
526
  const bodyChange = JSON.stringify({
560
527
  command: `${command}`,
561
528
  parameter: 'default',
562
529
  commandType: 'command',
563
530
  });
564
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
531
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
565
532
  try {
566
533
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
567
534
  const deviceStatus = await body.json();
568
- await this.pushStatusCodes(statusCode, deviceStatus);
569
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
570
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
571
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
572
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
573
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
535
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
536
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
537
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
538
+ await this.updateHomeKitCharacteristics();
574
539
  }
575
540
  else {
576
- this.statusCode(statusCode);
577
- this.statusCode(deviceStatus.statusCode);
541
+ await this.statusCode(statusCode);
542
+ await this.statusCode(deviceStatus.statusCode);
578
543
  }
579
544
  }
580
545
  catch (e) {
581
- this.apiError(e);
582
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
583
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
546
+ await this.apiError(e);
547
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
584
548
  }
585
549
  }
586
550
  else {
587
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges,`
588
- + `On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
589
- }
590
- // Push Hue & Saturation Update
591
- if (this.LightBulb.On) {
592
- await this.pushHueSaturationChanges();
593
- }
594
- // Push ColorTemperature Update
595
- if (this.LightBulb.On) {
596
- await this.pushColorTemperatureChanges();
597
- }
598
- // Push Brightness Update
599
- if (this.LightBulb.On) {
600
- await this.pushBrightnessChanges();
551
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
601
552
  }
602
553
  }
603
554
  async pushHueSaturationChanges() {
604
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
605
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
606
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
607
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
608
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
609
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
555
+ await this.debugLog('pushHueSaturationChanges');
556
+ if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
557
+ await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
558
+ const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation);
559
+ await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
610
560
  const bodyChange = JSON.stringify({
611
561
  command: 'setColor',
612
562
  parameter: `${red}:${green}:${blue}`,
613
563
  commandType: 'command',
614
564
  });
615
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
565
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
616
566
  try {
617
567
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
618
568
  const deviceStatus = await body.json();
619
- await this.pushStatusCodes(statusCode, deviceStatus);
620
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
621
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
622
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
623
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
624
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
569
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
570
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
571
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
572
+ await this.updateHomeKitCharacteristics();
625
573
  }
626
574
  else {
627
- this.statusCode(statusCode);
628
- this.statusCode(deviceStatus.statusCode);
575
+ await this.statusCode(statusCode);
576
+ await this.statusCode(deviceStatus.statusCode);
629
577
  }
630
578
  }
631
579
  catch (e) {
632
- this.apiError(e);
633
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
634
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
580
+ await this.apiError(e);
581
+ await this.errorLog(`failed pushHueSaturationChanges with ${this.device.connectionType} Connection,`
582
+ + ` Error Message: ${JSON.stringify(e.message)}`);
635
583
  }
636
584
  }
637
585
  else {
638
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
639
- + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
586
+ await this.debugLog(`No changes (pushHueSaturationChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
587
+ + ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
640
588
  }
641
589
  }
642
590
  async pushColorTemperatureChanges() {
643
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
591
+ await this.debugLog('pushColorTemperatureChanges');
644
592
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
645
593
  const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
646
594
  this.accessory.context.kelvin = kelvin;
@@ -649,77 +597,73 @@ export class ColorBulb extends deviceBase {
649
597
  parameter: `${kelvin}`,
650
598
  commandType: 'command',
651
599
  });
652
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
600
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
653
601
  try {
654
602
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
655
603
  const deviceStatus = await body.json();
656
- await this.pushStatusCodes(statusCode, deviceStatus);
657
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
658
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
659
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
660
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
661
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
604
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
605
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
606
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
607
+ await this.updateHomeKitCharacteristics();
662
608
  }
663
609
  else {
664
- this.statusCode(statusCode);
665
- this.statusCode(deviceStatus.statusCode);
610
+ await this.statusCode(statusCode);
611
+ await this.statusCode(deviceStatus.statusCode);
666
612
  }
667
613
  }
668
614
  catch (e) {
669
- this.apiError(e);
670
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
671
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
615
+ await this.apiError(e);
616
+ await this.errorLog(`failed pushColorTemperatureChanges with ${this.device.connectionType} Connection,`
617
+ + ` Error Message: ${JSON.stringify(e.message)}`);
672
618
  }
673
619
  }
674
620
  else {
675
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
676
- + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
621
+ await this.debugLog(`No changes (pushColorTemperatureChanges), ColorTemperature: ${this.LightBulb.ColorTemperature},`
622
+ + ` ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
677
623
  }
678
624
  }
679
625
  async pushBrightnessChanges() {
680
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
626
+ await this.debugLog('pushBrightnessChanges');
681
627
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
682
628
  const bodyChange = JSON.stringify({
683
629
  command: 'setBrightness',
684
630
  parameter: `${this.LightBulb.Brightness}`,
685
631
  commandType: 'command',
686
632
  });
687
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
633
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
688
634
  try {
689
635
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
690
636
  const deviceStatus = await body.json();
691
- await this.pushStatusCodes(statusCode, deviceStatus);
692
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
693
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
694
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
695
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
696
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
637
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
638
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
639
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
640
+ await this.updateHomeKitCharacteristics();
697
641
  }
698
642
  else {
699
- this.statusCode(statusCode);
700
- this.statusCode(deviceStatus.statusCode);
643
+ await this.statusCode(statusCode);
644
+ await this.statusCode(deviceStatus.statusCode);
701
645
  }
702
646
  }
703
647
  catch (e) {
704
- this.apiError(e);
705
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
706
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
648
+ await this.apiError(e);
649
+ await this.errorLog(`failed pushBrightnessChanges with ${this.device.connectionType} Connection,`
650
+ + ` Error Message: ${JSON.stringify(e.message)}`);
707
651
  }
708
652
  }
709
653
  else {
710
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.`
711
- + `Brightness: ${this.LightBulb.Brightness}, BrightnessCached: ${this.accessory.context.Brightness}`);
654
+ await this.debugLog(`No changes (pushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
655
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
712
656
  }
713
657
  }
714
658
  /**
715
659
  * Handle requests to set the value of the "On" characteristic
716
660
  */
717
661
  async OnSet(value) {
718
- if (this.LightBulb.On === this.accessory.context.On) {
719
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
662
+ if (this.LightBulb.On !== this.accessory.context.On) {
663
+ await this.infoLog(`Set On: ${value}`);
720
664
  }
721
665
  else {
722
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
666
+ await this.debugLog(`No Changes, On: ${value}`);
723
667
  }
724
668
  this.LightBulb.On = value;
725
669
  this.doColorBulbUpdate.next();
@@ -728,14 +672,16 @@ export class ColorBulb extends deviceBase {
728
672
  * Handle requests to set the value of the "Brightness" characteristic
729
673
  */
730
674
  async BrightnessSet(value) {
731
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
732
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
733
- }
734
- else if (this.LightBulb.On) {
735
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
675
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
676
+ await this.infoLog(`Set Brightness: ${value}`);
736
677
  }
737
678
  else {
738
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
679
+ if (this.LightBulb.On) {
680
+ this.debugLog(`No Changes, Brightness: ${value}`);
681
+ }
682
+ else {
683
+ this.debugLog(`Brightness: ${value}, On: ${this.LightBulb.On}`);
684
+ }
739
685
  }
740
686
  this.LightBulb.Brightness = value;
741
687
  this.doColorBulbUpdate.next();
@@ -744,14 +690,16 @@ export class ColorBulb extends deviceBase {
744
690
  * Handle requests to set the value of the "ColorTemperature" characteristic
745
691
  */
746
692
  async ColorTemperatureSet(value) {
747
- if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
748
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
749
- }
750
- else if (this.LightBulb.On) {
751
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
693
+ if (this.LightBulb.On && (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature)) {
694
+ this.infoLog(`Set ColorTemperature: ${value}`);
752
695
  }
753
696
  else {
754
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
697
+ if (this.LightBulb.On) {
698
+ this.debugLog(`No Changes, ColorTemperature: ${value}`);
699
+ }
700
+ else {
701
+ this.debugLog(`Set ColorTemperature: ${value}, On: ${this.LightBulb.On}`);
702
+ }
755
703
  }
756
704
  const minKelvin = 2000;
757
705
  const maxKelvin = 9000;
@@ -773,14 +721,16 @@ export class ColorBulb extends deviceBase {
773
721
  * Handle requests to set the value of the "Hue" characteristic
774
722
  */
775
723
  async HueSet(value) {
776
- if (this.LightBulb.Hue === this.accessory.context.Hue) {
777
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
778
- }
779
- else if (this.LightBulb.On) {
780
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
724
+ if (this.LightBulb.On && (this.LightBulb.Hue !== this.accessory.context.Hue)) {
725
+ this.infoLog(`Set Hue: ${value}`);
781
726
  }
782
727
  else {
783
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
728
+ if (this.LightBulb.On) {
729
+ this.debugLog(`No Changes, Hue: ${value}`);
730
+ }
731
+ else {
732
+ this.debugLog(`Set Hue: ${value}, On: ${this.LightBulb.On}`);
733
+ }
784
734
  }
785
735
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
786
736
  this.LightBulb.Hue = value;
@@ -790,14 +740,16 @@ export class ColorBulb extends deviceBase {
790
740
  * Handle requests to set the value of the "Saturation" characteristic
791
741
  */
792
742
  async SaturationSet(value) {
793
- if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
794
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
795
- }
796
- else if (this.LightBulb.On) {
797
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
743
+ if (this.LightBulb.On && (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
744
+ this.infoLog(`Set Saturation: ${value}`);
798
745
  }
799
746
  else {
800
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
747
+ if (this.LightBulb.On) {
748
+ this.debugLog(`No Changes, Saturation: ${value}`);
749
+ }
750
+ else {
751
+ this.debugLog(`Set Saturation: ${value}, On: ${this.LightBulb.On}`);
752
+ }
801
753
  }
802
754
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
803
755
  this.LightBulb.Saturation = value;
@@ -805,73 +757,35 @@ export class ColorBulb extends deviceBase {
805
757
  }
806
758
  async updateHomeKitCharacteristics() {
807
759
  // On
808
- if (this.LightBulb.On === undefined) {
809
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
810
- }
811
- else {
812
- this.accessory.context.On = this.LightBulb.On;
813
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
814
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
815
- }
760
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On');
816
761
  // Brightness
817
- if (this.LightBulb.Brightness === undefined) {
818
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
819
- }
820
- else {
821
- this.accessory.context.Brightness = this.LightBulb.Brightness;
822
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
823
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
824
- }
762
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness, this.LightBulb.Brightness, 'Brightness');
825
763
  // ColorTemperature
826
- if (this.LightBulb.ColorTemperature === undefined) {
827
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
828
- }
829
- else {
830
- this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
831
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
832
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
833
- + ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
834
- }
764
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature, 'ColorTemperature');
835
765
  // Hue
836
- if (this.LightBulb.Hue === undefined) {
837
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
838
- }
839
- else {
840
- this.accessory.context.Hue = this.LightBulb.Hue;
841
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
842
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
843
- }
766
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Hue, this.LightBulb.Hue, 'Hue');
844
767
  // Saturation
845
- if (this.LightBulb.Saturation === undefined) {
846
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
847
- }
848
- else {
849
- this.accessory.context.Saturation = this.LightBulb.Saturation;
850
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
851
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
852
- }
768
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Saturation, this.LightBulb.Saturation, 'Saturation');
853
769
  }
854
770
  async adaptiveLighting(device) {
855
771
  if (device.colorbulb?.adaptiveLightingShift) {
856
772
  this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
857
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
858
773
  }
859
774
  else {
860
775
  this.adaptiveLightingShift = 0;
861
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
862
776
  }
777
+ await this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
863
778
  }
864
779
  async BLEPushConnection() {
865
780
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
866
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
781
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
867
782
  await this.openAPIpushChanges();
868
783
  }
869
784
  }
870
785
  async BLERefreshConnection(switchbot) {
871
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
872
- + ` ${JSON.stringify(switchbot)}`);
786
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
873
787
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
874
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
788
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
875
789
  await this.openAPIRefreshStatus();
876
790
  }
877
791
  }
@@ -880,7 +794,7 @@ export class ColorBulb extends deviceBase {
880
794
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
881
795
  }
882
796
  }
883
- apiError(e) {
797
+ async apiError(e) {
884
798
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
885
799
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
886
800
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);