@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/dist/device/blindtilt.d.ts +15 -6
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +203 -255
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts +9 -4
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +196 -218
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts +9 -4
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +202 -247
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +10 -5
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +329 -378
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.d.ts +9 -4
  18. package/dist/device/contact.d.ts.map +1 -1
  19. package/dist/device/contact.js +99 -142
  20. package/dist/device/contact.js.map +1 -1
  21. package/dist/device/curtain.d.ts +19 -7
  22. package/dist/device/curtain.d.ts.map +1 -1
  23. package/dist/device/curtain.js +203 -264
  24. package/dist/device/curtain.js.map +1 -1
  25. package/dist/device/device.d.ts +28 -32
  26. package/dist/device/device.d.ts.map +1 -1
  27. package/dist/device/device.js +186 -225
  28. package/dist/device/device.js.map +1 -1
  29. package/dist/device/fan.d.ts +9 -4
  30. package/dist/device/fan.d.ts.map +1 -1
  31. package/dist/device/fan.js +198 -227
  32. package/dist/device/fan.js.map +1 -1
  33. package/dist/device/hub.d.ts +9 -5
  34. package/dist/device/hub.d.ts.map +1 -1
  35. package/dist/device/hub.js +106 -231
  36. package/dist/device/hub.js.map +1 -1
  37. package/dist/device/humidifier.d.ts +12 -4
  38. package/dist/device/humidifier.d.ts.map +1 -1
  39. package/dist/device/humidifier.js +192 -232
  40. package/dist/device/humidifier.js.map +1 -1
  41. package/dist/device/iosensor.d.ts +9 -4
  42. package/dist/device/iosensor.d.ts.map +1 -1
  43. package/dist/device/iosensor.js +91 -157
  44. package/dist/device/iosensor.js.map +1 -1
  45. package/dist/device/lightstrip.d.ts +10 -5
  46. package/dist/device/lightstrip.d.ts.map +1 -1
  47. package/dist/device/lightstrip.js +287 -367
  48. package/dist/device/lightstrip.js.map +1 -1
  49. package/dist/device/lock.d.ts +10 -5
  50. package/dist/device/lock.d.ts.map +1 -1
  51. package/dist/device/lock.js +171 -216
  52. package/dist/device/lock.js.map +1 -1
  53. package/dist/device/meter.d.ts +9 -4
  54. package/dist/device/meter.d.ts.map +1 -1
  55. package/dist/device/meter.js +93 -158
  56. package/dist/device/meter.js.map +1 -1
  57. package/dist/device/meterplus.d.ts +9 -4
  58. package/dist/device/meterplus.d.ts.map +1 -1
  59. package/dist/device/meterplus.js +94 -155
  60. package/dist/device/meterplus.js.map +1 -1
  61. package/dist/device/motion.d.ts +9 -4
  62. package/dist/device/motion.d.ts.map +1 -1
  63. package/dist/device/motion.js +84 -142
  64. package/dist/device/motion.js.map +1 -1
  65. package/dist/device/plug.d.ts +9 -4
  66. package/dist/device/plug.d.ts.map +1 -1
  67. package/dist/device/plug.js +99 -124
  68. package/dist/device/plug.js.map +1 -1
  69. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  70. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  71. package/dist/device/robotvacuumcleaner.js +170 -253
  72. package/dist/device/robotvacuumcleaner.js.map +1 -1
  73. package/dist/device/waterdetector.d.ts +12 -4
  74. package/dist/device/waterdetector.d.ts.map +1 -1
  75. package/dist/device/waterdetector.js +102 -128
  76. package/dist/device/waterdetector.js.map +1 -1
  77. package/dist/irdevice/airconditioner.d.ts +2 -1
  78. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  79. package/dist/irdevice/airconditioner.js.map +1 -1
  80. package/dist/irdevice/airpurifier.d.ts +2 -1
  81. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts +2 -1
  84. package/dist/irdevice/camera.d.ts.map +1 -1
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +2 -1
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js.map +1 -1
  89. package/dist/irdevice/irdevice.d.ts +2 -1
  90. package/dist/irdevice/irdevice.d.ts.map +1 -1
  91. package/dist/irdevice/irdevice.js +1 -1
  92. package/dist/irdevice/irdevice.js.map +1 -1
  93. package/dist/irdevice/light.d.ts +2 -1
  94. package/dist/irdevice/light.d.ts.map +1 -1
  95. package/dist/irdevice/light.js.map +1 -1
  96. package/dist/irdevice/other.d.ts +2 -1
  97. package/dist/irdevice/other.d.ts.map +1 -1
  98. package/dist/irdevice/other.js.map +1 -1
  99. package/dist/irdevice/tv.d.ts +2 -1
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js.map +1 -1
  102. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  105. package/dist/irdevice/waterheater.d.ts +2 -1
  106. package/dist/irdevice/waterheater.d.ts.map +1 -1
  107. package/dist/irdevice/waterheater.js.map +1 -1
  108. package/dist/platform.d.ts +5 -7
  109. package/dist/platform.d.ts.map +1 -1
  110. package/dist/platform.js +51 -34
  111. package/dist/platform.js.map +1 -1
  112. package/dist/settings.d.ts +49 -191
  113. package/dist/settings.d.ts.map +1 -1
  114. package/dist/settings.js +19 -0
  115. package/dist/settings.js.map +1 -1
  116. package/dist/types/bledevicestatus.d.ts +279 -0
  117. package/dist/types/bledevicestatus.d.ts.map +1 -0
  118. package/dist/types/bledevicestatus.js +2 -0
  119. package/dist/types/bledevicestatus.js.map +1 -0
  120. package/dist/types/devicelist.d.ts +89 -0
  121. package/dist/types/devicelist.d.ts.map +1 -0
  122. package/dist/types/devicelist.js +6 -0
  123. package/dist/types/devicelist.js.map +1 -0
  124. package/dist/types/deviceresponse.d.ts +13 -0
  125. package/dist/types/deviceresponse.d.ts.map +1 -0
  126. package/dist/types/deviceresponse.js +2 -0
  127. package/dist/types/deviceresponse.js.map +1 -0
  128. package/dist/types/devicestatus.d.ts +149 -0
  129. package/dist/types/devicestatus.d.ts.map +1 -0
  130. package/dist/types/devicestatus.js +3 -0
  131. package/dist/types/devicestatus.js.map +1 -0
  132. package/dist/types/devicewebhookstatus.d.ts +156 -0
  133. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  134. package/dist/types/devicewebhookstatus.js +2 -0
  135. package/dist/types/devicewebhookstatus.js.map +1 -0
  136. package/dist/types/irdevicelist.d.ts +10 -0
  137. package/dist/types/irdevicelist.d.ts.map +1 -0
  138. package/dist/types/irdevicelist.js +2 -0
  139. package/dist/types/irdevicelist.js.map +1 -0
  140. package/dist/types/pushbody.d.ts +6 -0
  141. package/dist/types/pushbody.d.ts.map +1 -0
  142. package/dist/types/pushbody.js +2 -0
  143. package/dist/types/pushbody.js.map +1 -0
  144. package/dist/utils.d.ts +25 -0
  145. package/dist/utils.d.ts.map +1 -1
  146. package/dist/utils.js +28 -0
  147. package/dist/utils.js.map +1 -1
  148. package/package.json +10 -10
  149. package/src/device/blindtilt.ts +223 -263
  150. package/src/device/bot.ts +208 -221
  151. package/src/device/ceilinglight.ts +210 -257
  152. package/src/device/colorbulb.ts +334 -397
  153. package/src/device/contact.ts +111 -146
  154. package/src/device/curtain.ts +220 -283
  155. package/src/device/device.ts +194 -245
  156. package/src/device/fan.ts +214 -230
  157. package/src/device/hub.ts +126 -246
  158. package/src/device/humidifier.ts +216 -243
  159. package/src/device/iosensor.ts +114 -166
  160. package/src/device/lightstrip.ts +302 -377
  161. package/src/device/lock.ts +187 -221
  162. package/src/device/meter.ts +116 -177
  163. package/src/device/meterplus.ts +117 -174
  164. package/src/device/motion.ts +99 -144
  165. package/src/device/plug.ts +110 -128
  166. package/src/device/robotvacuumcleaner.ts +189 -252
  167. package/src/device/waterdetector.ts +120 -138
  168. package/src/irdevice/airconditioner.ts +2 -1
  169. package/src/irdevice/airpurifier.ts +2 -1
  170. package/src/irdevice/camera.ts +2 -1
  171. package/src/irdevice/fan.ts +2 -1
  172. package/src/irdevice/irdevice.ts +3 -2
  173. package/src/irdevice/light.ts +2 -1
  174. package/src/irdevice/other.ts +2 -1
  175. package/src/irdevice/tv.ts +2 -1
  176. package/src/irdevice/vacuumcleaner.ts +2 -1
  177. package/src/irdevice/waterheater.ts +2 -1
  178. package/src/platform.ts +52 -34
  179. package/src/settings.ts +30 -239
  180. package/src/types/bledevicestatus.ts +305 -0
  181. package/src/types/devicelist.ts +124 -0
  182. package/src/types/deviceresponse.ts +15 -0
  183. package/src/types/devicestatus.ts +178 -0
  184. package/src/types/devicewebhookstatus.ts +196 -0
  185. package/src/types/irdevicelist.ts +15 -0
  186. package/src/types/pushbody.ts +9 -0
  187. package/src/utils.ts +33 -0
@@ -3,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,94 +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
339
  await this.apiError(e);
345
- await this.openAPIRefreshError(e);
340
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
346
341
  }
347
342
  }
348
343
 
349
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
350
- if (device.webhook) {
351
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
352
- 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) => {
353
348
  try {
354
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
355
- const { powerState, brightness, color, colorTemperature } = context;
356
- const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
357
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
358
- '(powerState, brightness, color, colorTemperature) = ' +
359
- `Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), `
360
- + `current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
361
-
362
- // On
363
- this.LightBulb.On = powerState === 'ON' ? true : false;
364
- if (accessory.context.Brightness !== this.LightBulb.On) {
365
- this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
366
- } else {
367
- this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
368
- }
369
-
370
- // Brightness
371
- this.LightBulb.Brightness = brightness;
372
- if (accessory.context.Brightness !== this.LightBulb.Brightness) {
373
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
374
- } else {
375
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
376
- }
377
-
378
- this.debugLog(`${device.deviceType}: ${accessory.displayName} color: ${JSON.stringify(color)}`);
379
- const [red, green, blue] = color!.split(':');
380
- this.debugLog(`${device.deviceType}: ${accessory.displayName} red: ${JSON.stringify(red)}`);
381
- this.debugLog(`${device.deviceType}: ${accessory.displayName} green: ${JSON.stringify(green)}`);
382
- this.debugLog(`${device.deviceType}: ${accessory.displayName} blue: ${JSON.stringify(blue)}`);
383
-
384
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
385
- this.debugLog(
386
- `${device.deviceType}: ${accessory.displayName}`
387
- + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
388
-
389
- // Hue
390
- this.LightBulb.Hue = hue;
391
- if (accessory.context.Hue !== this.LightBulb.Hue) {
392
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
393
- } else {
394
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
395
- }
396
-
397
- // Saturation
398
- this.LightBulb.Saturation = saturation;
399
- if (accessory.context.Saturation !== this.LightBulb.Saturation) {
400
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
401
- } else {
402
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
403
- }
404
-
405
- this.LightBulb.ColorTemperature = colorTemperature;
406
- if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
407
- this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
408
- } else {
409
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
410
- }
411
- this.updateHomeKitCharacteristics();
349
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
350
+ await this.parseStatusWebhook(context);
351
+ await this.updateHomeKitCharacteristics();
412
352
  } catch (e: any) {
413
- this.errorLog(`${device.deviceType}: ${accessory.displayName} `
414
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
353
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
415
354
  }
416
355
  };
417
356
  } else {
418
- this.debugWarnLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
357
+ await this.debugLog('is not listening webhook.');
419
358
  }
420
359
  }
421
360
 
@@ -432,14 +371,40 @@ export class ColorBulb extends deviceBase {
432
371
  */
433
372
  async pushChanges(): Promise<void> {
434
373
  if (!this.device.enableCloudService && this.OpenAPI) {
435
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
374
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
436
375
  } else if (this.BLE) {
437
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
+ }
438
390
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
439
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
+ }
440
405
  } else {
441
406
  await this.offlineOff();
442
- await this.pushChangeDisabled();
407
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
443
408
  }
444
409
  // Refresh the status from the API
445
410
  interval(15000)
@@ -451,239 +416,209 @@ export class ColorBulb extends deviceBase {
451
416
  }
452
417
 
453
418
  async BLEpushChanges(): Promise<void> {
454
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
419
+ await this.debugLog('BLEpushChanges');
455
420
  if (this.LightBulb.On !== this.accessory.context.On) {
456
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
457
- + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
458
- const switchbot = await this.platform.connectBLE();
459
- // 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);
460
423
  await this.convertBLEAddress();
461
- switchbot
462
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
463
- .then(async (device_list: any) => {
464
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
465
- return await this.retryBLE({
466
- max: await this.maxRetryBLE(),
467
- fn: async () => {
468
- if (this.LightBulb.On) {
469
- return await device_list[0].turnOn({ id: this.device.bleMac });
470
- } else {
471
- return await device_list[0].turnOff({ id: this.device.bleMac });
472
- }
473
- },
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();
474
448
  });
475
- })
476
- .then(() => {
477
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
478
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
479
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
480
- this.LightBulb.On = false;
481
- })
482
- .catch(async (e: any) => {
483
- this.apiError(e);
484
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
485
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
486
- await this.BLEPushConnection();
487
- });
488
- // Push Brightness Update
489
- if (this.LightBulb.On) {
490
- await this.BLEpushBrightnessChanges();
491
- }
492
- // Push ColorTemperature Update
493
- if (this.LightBulb.On) {
494
- await this.BLEpushColorTemperatureChanges();
495
- }
496
- // Push Hue & Saturation Update
497
- if (this.LightBulb.On) {
498
- await this.BLEpushRGBChanges();
449
+ } else {
450
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
451
+ await this.BLEPushConnection();
499
452
  }
500
- await this.updateHomeKitCharacteristics();
501
453
  } else {
502
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, On: ${this.LightBulb.On},`
503
- + ` OnCached: ${this.accessory.context.On}`);
454
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
504
455
  }
505
456
  }
506
457
 
507
458
  async BLEpushBrightnessChanges(): Promise<void> {
508
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
459
+ await this.debugLog('BLEpushBrightnessChanges');
509
460
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
510
- const switchbot = await this.platform.connectBLE();
511
- // Convert to BLE Address
461
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
512
462
  await this.convertBLEAddress();
513
- switchbot
514
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
515
- .then(async (device_list: any) => {
516
- this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
517
- return await device_list[0].setBrightness(this.LightBulb.Brightness);
518
- })
519
- .then(() => {
520
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
521
- this.LightBulb.On = false;
522
- })
523
- .catch(async (e: any) => {
524
- this.apiError(e);
525
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
526
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
527
- await this.BLEPushConnection();
528
- });
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
+ }
529
483
  } else {
530
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.`
531
- + `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}`);
532
486
  }
533
487
  }
534
488
 
535
489
  async BLEpushColorTemperatureChanges(): Promise<void> {
536
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushColorTemperatureChanges`);
490
+ await this.debugLog('BLEpushColorTemperatureChanges');
537
491
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
538
- const switchbot = await this.platform.connectBLE();
539
- // Convert to BLE Address
492
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
540
493
  await this.convertBLEAddress();
541
- switchbot
542
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
543
- .then(async (device_list: any) => {
544
- this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
545
- return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
546
- })
547
- .then(() => {
548
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
549
- this.LightBulb.On = false;
550
- })
551
- .catch(async (e: any) => {
552
- this.apiError(e);
553
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushColorTemperatureChanges with `
554
- + `${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
555
- await this.BLEPushConnection();
556
- });
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
+ }
557
515
  } else {
558
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushColorTemperatureChanges.`
559
- + `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}`);
560
518
  }
561
519
  }
562
520
 
563
521
  async BLEpushRGBChanges(): Promise<void> {
564
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
565
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
566
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
567
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
568
-
569
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
570
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
571
-
572
- const switchbot = await this.platform.connectBLE();
573
- // 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);
574
528
  await this.convertBLEAddress();
575
- switchbot
576
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
577
- .then(async (device_list: any) => {
578
- this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
579
- return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
580
- })
581
- .then(() => {
582
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
583
- this.LightBulb.On = false;
584
- })
585
- .catch(async (e: any) => {
586
- this.apiError(e);
587
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
588
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
589
- await this.BLEPushConnection();
590
- });
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
+ }
591
550
  } else {
592
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
593
- + `${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}`);
594
553
  }
595
554
  }
596
555
 
597
556
  async openAPIpushChanges(): Promise<void> {
598
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
557
+ await this.debugLog('openAPIpushChanges');
599
558
  if (this.LightBulb.On !== this.accessory.context.On) {
600
- let command = '';
601
- if (this.LightBulb.On) {
602
- command = 'turnOn';
603
- } else {
604
- command = 'turnOff';
605
- }
559
+ const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
606
560
  const bodyChange = JSON.stringify({
607
561
  command: `${command}`,
608
562
  parameter: 'default',
609
563
  commandType: 'command',
610
564
  });
611
- 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)}`);
612
566
  try {
613
567
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
614
568
  const deviceStatus: any = await body.json();
615
- await this.pushStatusCodes(statusCode, deviceStatus);
616
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
617
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
618
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
619
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
620
- + `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();
621
573
  } else {
622
- this.statusCode(statusCode);
623
- this.statusCode(deviceStatus.statusCode);
574
+ await this.statusCode(statusCode);
575
+ await this.statusCode(deviceStatus.statusCode);
624
576
  }
625
577
  } catch (e: any) {
626
- this.apiError(e);
627
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
628
- + ` 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)}`);
629
580
  }
630
581
  } else {
631
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges,`
632
- + `On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
633
- }
634
- // Push Hue & Saturation Update
635
- if (this.LightBulb.On) {
636
- await this.pushHueSaturationChanges();
637
- }
638
- // Push ColorTemperature Update
639
- if (this.LightBulb.On) {
640
- await this.pushColorTemperatureChanges();
641
- }
642
- // Push Brightness Update
643
- if (this.LightBulb.On) {
644
- await this.pushBrightnessChanges();
582
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
645
583
  }
646
584
  }
647
585
 
648
586
  async pushHueSaturationChanges(): Promise<void> {
649
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
650
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
651
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
652
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
653
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
654
- 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])}`);
655
592
  const bodyChange = JSON.stringify({
656
593
  command: 'setColor',
657
594
  parameter: `${red}:${green}:${blue}`,
658
595
  commandType: 'command',
659
596
  });
660
- 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)}`);
661
598
  try {
662
599
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
663
600
  const deviceStatus: any = await body.json();
664
- await this.pushStatusCodes(statusCode, deviceStatus);
665
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
666
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
667
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
668
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
669
- + `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();
670
605
  } else {
671
- this.statusCode(statusCode);
672
- this.statusCode(deviceStatus.statusCode);
606
+ await this.statusCode(statusCode);
607
+ await this.statusCode(deviceStatus.statusCode);
673
608
  }
674
609
  } catch (e: any) {
675
- this.apiError(e);
676
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
677
- + ` 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)}`);
678
613
  }
679
614
  } else {
680
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
681
- + `${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}`);
682
617
  }
683
618
  }
684
619
 
685
620
  async pushColorTemperatureChanges(): Promise<void> {
686
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
621
+ await this.debugLog('pushColorTemperatureChanges');
687
622
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
688
623
  const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
689
624
  this.accessory.context.kelvin = kelvin;
@@ -692,61 +627,57 @@ export class ColorBulb extends deviceBase {
692
627
  parameter: `${kelvin}`,
693
628
  commandType: 'command',
694
629
  });
695
- 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)}`);
696
631
  try {
697
632
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
698
633
  const deviceStatus: any = await body.json();
699
- await this.pushStatusCodes(statusCode, deviceStatus);
700
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
701
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
702
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
703
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
704
- + `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();
705
638
  } else {
706
- this.statusCode(statusCode);
707
- this.statusCode(deviceStatus.statusCode);
639
+ await this.statusCode(statusCode);
640
+ await this.statusCode(deviceStatus.statusCode);
708
641
  }
709
642
  } catch (e: any) {
710
- this.apiError(e);
711
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
712
- + ` 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)}`);
713
646
  }
714
647
  } else {
715
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
716
- + `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}`);
717
650
  }
718
651
  }
719
652
 
720
653
  async pushBrightnessChanges(): Promise<void> {
721
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
654
+ await this.debugLog('pushBrightnessChanges');
722
655
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
723
656
  const bodyChange = JSON.stringify({
724
657
  command: 'setBrightness',
725
658
  parameter: `${this.LightBulb.Brightness}`,
726
659
  commandType: 'command',
727
660
  });
728
- 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)}`);
729
662
  try {
730
663
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
731
664
  const deviceStatus: any = await body.json();
732
- await this.pushStatusCodes(statusCode, deviceStatus);
733
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
734
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
735
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
736
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
737
- + `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();
738
669
  } else {
739
- this.statusCode(statusCode);
740
- this.statusCode(deviceStatus.statusCode);
670
+ await this.statusCode(statusCode);
671
+ await this.statusCode(deviceStatus.statusCode);
741
672
  }
742
673
  } catch (e: any) {
743
- this.apiError(e);
744
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
745
- + ` 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)}`);
746
677
  }
747
678
  } else {
748
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.`
749
- + `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}`);
750
681
  }
751
682
  }
752
683
 
@@ -754,10 +685,10 @@ export class ColorBulb extends deviceBase {
754
685
  * Handle requests to set the value of the "On" characteristic
755
686
  */
756
687
  async OnSet(value: CharacteristicValue): Promise<void> {
757
- if (this.LightBulb.On === this.accessory.context.On) {
758
- 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}`);
759
690
  } else {
760
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
691
+ await this.debugLog(`No Changes, On: ${value}`);
761
692
  }
762
693
 
763
694
  this.LightBulb.On = value;
@@ -768,12 +699,14 @@ export class ColorBulb extends deviceBase {
768
699
  * Handle requests to set the value of the "Brightness" characteristic
769
700
  */
770
701
  async BrightnessSet(value: CharacteristicValue): Promise<void> {
771
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
772
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
773
- } else if (this.LightBulb.On) {
774
- 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}`);
775
704
  } else {
776
- 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
+ }
777
710
  }
778
711
 
779
712
  this.LightBulb.Brightness = value;
@@ -784,12 +717,14 @@ export class ColorBulb extends deviceBase {
784
717
  * Handle requests to set the value of the "ColorTemperature" characteristic
785
718
  */
786
719
  async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
787
- if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
788
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
789
- } else if (this.LightBulb.On) {
790
- 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}`);
791
722
  } else {
792
- 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
+ }
793
728
  }
794
729
 
795
730
  const minKelvin = 2000;
@@ -817,12 +752,14 @@ export class ColorBulb extends deviceBase {
817
752
  * Handle requests to set the value of the "Hue" characteristic
818
753
  */
819
754
  async HueSet(value: CharacteristicValue): Promise<void> {
820
- if (this.LightBulb.Hue === this.accessory.context.Hue) {
821
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
822
- } else if (this.LightBulb.On) {
823
- 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}`);
824
757
  } else {
825
- 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
+ }
826
763
  }
827
764
 
828
765
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
@@ -835,12 +772,14 @@ export class ColorBulb extends deviceBase {
835
772
  * Handle requests to set the value of the "Saturation" characteristic
836
773
  */
837
774
  async SaturationSet(value: CharacteristicValue): Promise<void> {
838
- if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
839
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
840
- } else if (this.LightBulb.On) {
841
- 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}`);
842
777
  } else {
843
- 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
+ }
844
783
  }
845
784
 
846
785
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
@@ -870,25 +809,23 @@ export class ColorBulb extends deviceBase {
870
809
  async adaptiveLighting(device: device & devicesConfig): Promise<void> {
871
810
  if (device.colorbulb?.adaptiveLightingShift) {
872
811
  this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
873
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
874
812
  } else {
875
813
  this.adaptiveLightingShift = 0;
876
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
877
814
  }
815
+ await this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
878
816
  }
879
817
 
880
818
  async BLEPushConnection() {
881
819
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
882
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
820
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
883
821
  await this.openAPIpushChanges();
884
822
  }
885
823
  }
886
824
 
887
825
  async BLERefreshConnection(switchbot: any): Promise<void> {
888
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
889
- + ` ${JSON.stringify(switchbot)}`);
826
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
890
827
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
891
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
828
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
892
829
  await this.openAPIRefreshStatus();
893
830
  }
894
831
  }
@@ -899,7 +836,7 @@ export class ColorBulb extends deviceBase {
899
836
  }
900
837
  }
901
838
 
902
- apiError(e: any): void {
839
+ async apiError(e: any): Promise<void> {
903
840
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
904
841
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
905
842
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);