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

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