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