@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/README.md +16 -0
  2. package/dist/device/blindtilt.d.ts +15 -6
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +224 -370
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +9 -4
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +197 -221
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +10 -5
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +214 -292
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +10 -5
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +335 -421
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +9 -4
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +121 -200
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +20 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +229 -403
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +41 -22
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +212 -181
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +9 -4
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +204 -249
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +9 -5
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +107 -233
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +12 -4
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +193 -235
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +9 -4
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +92 -159
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +10 -5
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +288 -370
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +10 -5
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +172 -219
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +9 -4
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +94 -160
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +9 -4
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +95 -157
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +9 -4
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +85 -144
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +9 -4
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +100 -127
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +172 -259
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +12 -4
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +106 -131
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +2 -1
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +2 -3
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +2 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +2 -3
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +2 -1
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +2 -3
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +2 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +2 -3
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +3 -1
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +5 -1
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts +2 -1
  99. package/dist/irdevice/light.d.ts.map +1 -1
  100. package/dist/irdevice/light.js +2 -3
  101. package/dist/irdevice/light.js.map +1 -1
  102. package/dist/irdevice/other.d.ts +2 -1
  103. package/dist/irdevice/other.d.ts.map +1 -1
  104. package/dist/irdevice/other.js +2 -3
  105. package/dist/irdevice/other.js.map +1 -1
  106. package/dist/irdevice/tv.d.ts +2 -1
  107. package/dist/irdevice/tv.d.ts.map +1 -1
  108. package/dist/irdevice/tv.js +2 -3
  109. package/dist/irdevice/tv.js.map +1 -1
  110. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  111. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  112. package/dist/irdevice/vacuumcleaner.js +2 -3
  113. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  114. package/dist/irdevice/waterheater.d.ts +2 -1
  115. package/dist/irdevice/waterheater.d.ts.map +1 -1
  116. package/dist/irdevice/waterheater.js +2 -3
  117. package/dist/irdevice/waterheater.js.map +1 -1
  118. package/dist/platform.d.ts +14 -14
  119. package/dist/platform.d.ts.map +1 -1
  120. package/dist/platform.js +102 -86
  121. package/dist/platform.js.map +1 -1
  122. package/dist/settings.d.ts +49 -190
  123. package/dist/settings.d.ts.map +1 -1
  124. package/dist/settings.js +19 -0
  125. package/dist/settings.js.map +1 -1
  126. package/dist/types/bledevicestatus.d.ts +279 -0
  127. package/dist/types/bledevicestatus.d.ts.map +1 -0
  128. package/dist/types/bledevicestatus.js +2 -0
  129. package/dist/types/bledevicestatus.js.map +1 -0
  130. package/dist/types/devicelist.d.ts +89 -0
  131. package/dist/types/devicelist.d.ts.map +1 -0
  132. package/dist/types/devicelist.js +6 -0
  133. package/dist/types/devicelist.js.map +1 -0
  134. package/dist/types/deviceresponse.d.ts +13 -0
  135. package/dist/types/deviceresponse.d.ts.map +1 -0
  136. package/dist/types/deviceresponse.js +2 -0
  137. package/dist/types/deviceresponse.js.map +1 -0
  138. package/dist/types/devicestatus.d.ts +149 -0
  139. package/dist/types/devicestatus.d.ts.map +1 -0
  140. package/dist/types/devicestatus.js +3 -0
  141. package/dist/types/devicestatus.js.map +1 -0
  142. package/dist/types/devicewebhookstatus.d.ts +156 -0
  143. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  144. package/dist/types/devicewebhookstatus.js +2 -0
  145. package/dist/types/devicewebhookstatus.js.map +1 -0
  146. package/dist/types/irdevicelist.d.ts +10 -0
  147. package/dist/types/irdevicelist.d.ts.map +1 -0
  148. package/dist/types/irdevicelist.js +2 -0
  149. package/dist/types/irdevicelist.js.map +1 -0
  150. package/dist/types/pushbody.d.ts +6 -0
  151. package/dist/types/pushbody.d.ts.map +1 -0
  152. package/dist/types/pushbody.js +2 -0
  153. package/dist/types/pushbody.js.map +1 -0
  154. package/dist/utils.d.ts +25 -0
  155. package/dist/utils.d.ts.map +1 -1
  156. package/dist/utils.js +28 -0
  157. package/dist/utils.js.map +1 -1
  158. package/package.json +11 -11
  159. package/src/device/blindtilt.ts +254 -376
  160. package/src/device/bot.ts +209 -224
  161. package/src/device/ceilinglight.ts +227 -297
  162. package/src/device/colorbulb.ts +345 -435
  163. package/src/device/contact.ts +139 -200
  164. package/src/device/curtain.ts +254 -414
  165. package/src/device/device.ts +221 -190
  166. package/src/device/fan.ts +222 -253
  167. package/src/device/hub.ts +127 -248
  168. package/src/device/humidifier.ts +217 -246
  169. package/src/device/iosensor.ts +115 -168
  170. package/src/device/lightstrip.ts +303 -380
  171. package/src/device/lock.ts +188 -224
  172. package/src/device/meter.ts +117 -179
  173. package/src/device/meterplus.ts +118 -176
  174. package/src/device/motion.ts +100 -146
  175. package/src/device/plug.ts +111 -131
  176. package/src/device/robotvacuumcleaner.ts +191 -258
  177. package/src/device/waterdetector.ts +124 -141
  178. package/src/irdevice/airconditioner.ts +4 -4
  179. package/src/irdevice/airpurifier.ts +4 -4
  180. package/src/irdevice/camera.ts +4 -4
  181. package/src/irdevice/fan.ts +4 -4
  182. package/src/irdevice/irdevice.ts +8 -2
  183. package/src/irdevice/light.ts +4 -4
  184. package/src/irdevice/other.ts +4 -4
  185. package/src/irdevice/tv.ts +4 -4
  186. package/src/irdevice/vacuumcleaner.ts +4 -4
  187. package/src/irdevice/waterheater.ts +4 -4
  188. package/src/platform.ts +104 -87
  189. package/src/settings.ts +30 -238
  190. package/src/types/bledevicestatus.ts +305 -0
  191. package/src/types/devicelist.ts +124 -0
  192. package/src/types/deviceresponse.ts +15 -0
  193. package/src/types/devicestatus.ts +178 -0
  194. package/src/types/devicewebhookstatus.ts +196 -0
  195. package/src/types/irdevicelist.ts +15 -0
  196. package/src/types/pushbody.ts +9 -0
  197. package/src/utils.ts +33 -0
@@ -4,8 +4,8 @@
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
10
  * Platform Accessory
11
11
  * An instance of this class is created for each accessory your platform registers
@@ -24,6 +24,7 @@ export class StripLight extends deviceBase {
24
24
  constructor(platform, accessory, device) {
25
25
  super(platform, accessory, device);
26
26
  this.platform = platform;
27
+ // Adaptive Lighting
27
28
  this.adaptiveLighting(device);
28
29
  // this is subject we use to track when we need to POST changes to the SwitchBot API
29
30
  this.doStripLightUpdate = new Subject();
@@ -45,7 +46,7 @@ export class StripLight extends deviceBase {
45
46
  this.accessory.removeService(this.LightBulb.Service);
46
47
  this.LightBulb.Service = this.accessory.addService(this.hap.Service.Lightbulb);
47
48
  this.accessory.context.adaptiveLighting = false;
48
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
49
+ this.debugLog(`adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
49
50
  }
50
51
  if (this.adaptiveLightingShift !== -1) {
51
52
  this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
@@ -53,10 +54,10 @@ export class StripLight extends deviceBase {
53
54
  });
54
55
  this.accessory.configureController(this.AdaptiveLightingController);
55
56
  this.accessory.context.adaptiveLighting = true;
56
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
57
+ this.debugLog(`adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
57
58
  + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
58
59
  }
59
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
60
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
60
61
  // Initialize LightBulb Characteristics
61
62
  this.LightBulb.Service
62
63
  .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
@@ -118,14 +119,14 @@ export class StripLight extends deviceBase {
118
119
  this.refreshStatus();
119
120
  // Update Homekit
120
121
  this.updateHomeKitCharacteristics();
122
+ //regisiter webhook event handler
123
+ this.registerWebhook();
121
124
  // Start an update interval
122
125
  interval(this.deviceRefreshRate * 1000)
123
126
  .pipe(skipWhile(() => this.stripLightUpdateInProgress))
124
127
  .subscribe(async () => {
125
128
  await this.refreshStatus();
126
129
  });
127
- //regisiter webhook event handler
128
- this.registerWebhook(accessory, device);
129
130
  // Watch for Bulb change events
130
131
  // We put in a debounce of 1000ms so we don't make duplicate calls
131
132
  this.doStripLightUpdate
@@ -137,83 +138,56 @@ export class StripLight extends deviceBase {
137
138
  await this.pushChanges();
138
139
  }
139
140
  catch (e) {
140
- this.apiError(e);
141
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
142
- + ` Error Message: ${JSON.stringify(e.message)}`);
141
+ await this.apiError(e);
142
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
143
143
  }
144
144
  this.stripLightUpdateInProgress = false;
145
145
  });
146
146
  }
147
147
  async BLEparseStatus(serviceData) {
148
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
149
- // State
150
- switch (serviceData.state) {
151
- case 'on':
152
- this.LightBulb.On = true;
153
- break;
154
- default:
155
- this.LightBulb.On = false;
156
- }
157
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
148
+ await this.debugLog('BLEparseStatus');
149
+ // On
150
+ this.LightBulb.On = serviceData.power;
151
+ await this.debugLog(`On: ${this.LightBulb.On}`);
158
152
  // Brightness
159
- this.LightBulb.Brightness = Number(serviceData.brightness);
160
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
153
+ this.LightBulb.Brightness = serviceData.brightness;
154
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
161
155
  // Color, Hue & Brightness
162
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
163
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
164
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
165
- const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
166
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
167
- + ` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`);
156
+ await this.debugLog(`red: ${serviceData.red}, green: ${serviceData.green}, blue: ${serviceData.blue}`);
157
+ const [hue, saturation] = rgb2hs(serviceData.red, serviceData.green, serviceData.blue);
158
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(serviceData.red, serviceData.green, serviceData.blue))}`);
168
159
  // Hue
169
160
  this.LightBulb.Hue = hue;
170
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
161
+ this.debugLog(`Hue: ${this.LightBulb.Hue}`);
171
162
  // Saturation
172
163
  this.LightBulb.Saturation = saturation;
173
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
174
- // ColorTemperature
175
- if (serviceData.color_temperature) {
176
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
177
- this.LightBulb.ColorTemperature = serviceData.color_temperature;
178
- this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
179
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
180
- }
164
+ this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
181
165
  }
182
166
  async openAPIparseStatus(deviceStatus) {
183
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
184
- switch (deviceStatus.body.power) {
185
- case 'on':
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}`);
167
+ await this.debugLog('openAPIparseStatus');
168
+ // On
169
+ this.LightBulb.On = deviceStatus.power === 'on' ? true : false;
170
+ await this.debugLog(`On: ${this.LightBulb.On}`);
192
171
  // Brightness
193
- this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
172
+ this.LightBulb.Brightness = deviceStatus.brightness;
173
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
195
174
  // Color, Hue & Brightness
196
- if (deviceStatus.body.color) {
197
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
198
- const [red, green, blue] = deviceStatus.body.color.split(':');
199
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
200
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
201
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
202
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
203
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
204
- + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
205
- // Hue
206
- this.LightBulb.Hue = hue;
207
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
208
- // Saturation
209
- this.LightBulb.Saturation = saturation;
210
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
211
- }
175
+ await this.debugLog(`color: ${JSON.stringify(deviceStatus.color)}`);
176
+ const [red, green, blue] = deviceStatus.color.split(':');
177
+ await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
178
+ const [hue, saturation] = rgb2hs(red, green, blue);
179
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
180
+ // Hue
181
+ this.LightBulb.Hue = hue;
182
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
183
+ // Saturation
184
+ this.LightBulb.Saturation = saturation;
185
+ await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
212
186
  // Firmware Version
213
- const version = deviceStatus.body.version?.toString();
214
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
215
- if (deviceStatus.body.version) {
216
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
187
+ const version = deviceStatus.version.toString();
188
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
189
+ if (deviceStatus.version) {
190
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
217
191
  this.accessory
218
192
  .getService(this.hap.Service.AccessoryInformation)
219
193
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -221,15 +195,38 @@ export class StripLight extends deviceBase {
221
195
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
222
196
  .updateValue(deviceVersion);
223
197
  this.accessory.context.deviceVersion = deviceVersion;
224
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
198
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
225
199
  }
226
200
  }
201
+ async parseStatusWebhook(context) {
202
+ await this.debugLog('parseStatusWebhook');
203
+ await this.debugLog(`(powerState, brightness, color) = Webhook:(${context.powerState}, ${context.brightness}, ${context.color}),`
204
+ + ` current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.Hue}, ${this.LightBulb.Saturation})`);
205
+ // On
206
+ this.LightBulb.On = context.powerState === 'ON' ? true : false;
207
+ await this.debugLog(`On: ${this.LightBulb.On}`);
208
+ // Brightness
209
+ this.LightBulb.Brightness = context.brightness;
210
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
211
+ // Color, Hue & Brightness
212
+ await this.debugLog(`color: ${JSON.stringify(context.color)}`);
213
+ const [red, green, blue] = context.color.split(':');
214
+ await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
215
+ const [hue, saturation] = rgb2hs(red, green, blue);
216
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
217
+ // Hue
218
+ this.LightBulb.Hue = hue;
219
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
220
+ // Saturation
221
+ this.LightBulb.Saturation = saturation;
222
+ await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
223
+ }
227
224
  /**
228
225
  * Asks the SwitchBot API for the latest device information
229
226
  */
230
227
  async refreshStatus() {
231
228
  if (!this.device.enableCloudService && this.OpenAPI) {
232
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
229
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
233
230
  }
234
231
  else if (this.BLE) {
235
232
  await this.BLERefreshStatus();
@@ -239,12 +236,11 @@ export class StripLight extends deviceBase {
239
236
  }
240
237
  else {
241
238
  await this.offlineOff();
242
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
243
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
239
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
244
240
  }
245
241
  }
246
242
  async BLERefreshStatus() {
247
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
243
+ await this.debugLog('BLERefreshStatus');
248
244
  const switchbot = await this.switchbotBLE();
249
245
  if (switchbot === undefined) {
250
246
  await this.BLERefreshConnection(switchbot);
@@ -255,106 +251,55 @@ export class StripLight extends deviceBase {
255
251
  // Start to monitor advertisement packets
256
252
  const serviceData = await this.monitorAdvertisementPackets(switchbot);
257
253
  // Update HomeKit
258
- if (serviceData.model !== '' && serviceData.modelName !== '') {
254
+ if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
259
255
  await this.BLEparseStatus(serviceData);
260
256
  await this.updateHomeKitCharacteristics();
261
257
  }
262
258
  else {
263
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
259
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
264
260
  await this.BLERefreshConnection(switchbot);
265
261
  }
266
262
  })();
267
263
  }
268
264
  }
269
265
  async openAPIRefreshStatus() {
270
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
266
+ await this.debugLog('openAPIRefreshStatus');
271
267
  try {
272
268
  const { body, statusCode } = await this.deviceRefreshStatus();
273
269
  const deviceStatus = await body.json();
274
- await this.refreshStatusCodes(statusCode, deviceStatus);
270
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
275
271
  ;
276
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
277
- await this.successfulRefreshStatus(statusCode, deviceStatus);
278
- await this.openAPIparseStatus(deviceStatus);
272
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
273
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
274
+ await this.openAPIparseStatus(deviceStatus.body);
279
275
  await this.updateHomeKitCharacteristics();
280
276
  }
281
277
  else {
282
- await this.statusCodes(statusCode, deviceStatus);
278
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
279
+ await this.debugWarnLog(statusCode, deviceStatus);
283
280
  }
284
281
  }
285
282
  catch (e) {
286
- this.apiError(e);
287
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
288
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
283
+ await this.apiError(e);
284
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
289
285
  }
290
286
  }
291
- async registerWebhook(accessory, device) {
292
- if (device.webhook) {
293
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
294
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
287
+ async registerWebhook() {
288
+ if (this.device.webhook) {
289
+ await this.debugLog('is listening webhook.');
290
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
295
291
  try {
296
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
297
- const { powerState, brightness, color, colorTemperature } = context;
298
- const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
299
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState, brightness, color, colorTemperature) = Webhook: (${powerState},`
300
- + ` ${brightness}, ${color}, ${colorTemperature}), current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
301
- // On
302
- this.LightBulb.On = powerState === 'ON' ? true : false;
303
- if (accessory.context.Brightness !== this.LightBulb.On) {
304
- this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
305
- }
306
- else {
307
- this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
308
- }
309
- // Brightness
310
- this.LightBulb.Brightness = brightness;
311
- if (accessory.context.Brightness !== this.LightBulb.Brightness) {
312
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
313
- }
314
- else {
315
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
316
- }
317
- this.debugLog(`${device.deviceType}: ${accessory.displayName} color: ${JSON.stringify(color)}`);
318
- const [red, green, blue] = color.split(':');
319
- this.debugLog(`${device.deviceType}: ${accessory.displayName} red: ${JSON.stringify(red)}`);
320
- this.debugLog(`${device.deviceType}: ${accessory.displayName} green: ${JSON.stringify(green)}`);
321
- this.debugLog(`${device.deviceType}: ${accessory.displayName} blue: ${JSON.stringify(blue)}`);
322
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
323
- this.debugLog(`${device.deviceType}: ${accessory.displayName}`
324
- + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
325
- // Hue
326
- this.LightBulb.Hue = hue;
327
- if (accessory.context.Hue !== this.LightBulb.Hue) {
328
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
329
- }
330
- else {
331
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
332
- }
333
- // Saturation
334
- this.LightBulb.Saturation = saturation;
335
- if (accessory.context.Saturation !== this.LightBulb.Saturation) {
336
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
337
- }
338
- else {
339
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
340
- }
341
- // ColorTemperature
342
- this.LightBulb.ColorTemperature = colorTemperature;
343
- if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
344
- this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
345
- }
346
- else {
347
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
348
- }
349
- this.updateHomeKitCharacteristics();
292
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
293
+ await this.parseStatusWebhook(context);
294
+ await this.updateHomeKitCharacteristics();
350
295
  }
351
296
  catch (e) {
352
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
297
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
353
298
  }
354
299
  };
355
300
  }
356
301
  else {
357
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
302
+ await this.debugLog('is not listening webhook.');
358
303
  }
359
304
  }
360
305
  /**
@@ -369,18 +314,48 @@ export class StripLight extends deviceBase {
369
314
  */
370
315
  async pushChanges() {
371
316
  if (!this.device.enableCloudService && this.OpenAPI) {
372
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
317
+ this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
373
318
  }
374
319
  else if (this.BLE) {
375
320
  await this.BLEpushChanges();
321
+ if (this.LightBulb.On) {
322
+ // Push Brightness Update
323
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
324
+ await this.BLEpushBrightnessChanges();
325
+ // Push Hue & Saturation Update
326
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
327
+ await this.BLEpushRGBChanges();
328
+ // Set ColorTemperature
329
+ if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
330
+ const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
331
+ this.accessory.context.kelvin = kelvin;
332
+ }
333
+ else {
334
+ await this.debugLog(`No pushColorTemperatureChanges, ColorTemperature: ${this.LightBulb.ColorTemperature},`
335
+ + ` ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
336
+ }
337
+ }
338
+ else {
339
+ await this.debugLog('BLE (Brightness), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
340
+ }
376
341
  }
377
342
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
378
343
  await this.openAPIpushChanges();
344
+ if (this.LightBulb.On) {
345
+ // Push Brightness Update
346
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
347
+ await this.pushBrightnessChanges();
348
+ // Push Hue & Saturation Update
349
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
350
+ await this.pushHueSaturationChanges();
351
+ }
352
+ else {
353
+ await this.debugLog('openAPI (Brightness), (ColorTemperature), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
354
+ }
379
355
  }
380
356
  else {
381
357
  await this.offlineOff();
382
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
383
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
358
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
384
359
  }
385
360
  // Refresh the status from the API
386
361
  interval(15000)
@@ -391,145 +366,131 @@ export class StripLight extends deviceBase {
391
366
  });
392
367
  }
393
368
  async BLEpushChanges() {
394
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
369
+ await this.debugLog('BLEpushChanges');
395
370
  if (this.LightBulb.On !== this.accessory.context.On) {
396
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
397
- + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
398
- const switchbot = await this.platform.connectBLE();
399
- // Convert to BLE Address
371
+ await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
372
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
400
373
  await this.convertBLEAddress();
401
- switchbot
402
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
403
- .then(async (device_list) => {
404
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
405
- return await this.retryBLE({
406
- max: await this.maxRetryBLE(),
407
- fn: async () => {
408
- if (this.LightBulb.On) {
409
- return await device_list[0].turnOn({ id: this.device.bleMac });
410
- }
411
- else {
412
- return await device_list[0].turnOff({ id: this.device.bleMac });
413
- }
414
- },
374
+ if (switchbot !== false) {
375
+ switchbot
376
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
377
+ .then(async (device_list) => {
378
+ await this.infoLog(`On: ${this.LightBulb.On}`);
379
+ return await this.retryBLE({
380
+ max: await this.maxRetryBLE(),
381
+ fn: async () => {
382
+ if (this.LightBulb.On) {
383
+ return await device_list[0].turnOn({ id: this.device.bleMac });
384
+ }
385
+ else {
386
+ return await device_list[0].turnOff({ id: this.device.bleMac });
387
+ }
388
+ },
389
+ });
390
+ })
391
+ .then(async () => {
392
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`);
393
+ await this.updateHomeKitCharacteristics();
394
+ })
395
+ .catch(async (e) => {
396
+ await this.apiError(e);
397
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
398
+ await this.BLEPushConnection();
415
399
  });
416
- })
417
- .then(() => {
418
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
419
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
420
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
421
- this.LightBulb.On = false;
422
- })
423
- .catch(async (e) => {
424
- this.apiError(e);
425
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
426
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
427
- await this.BLEPushConnection();
428
- });
429
- // Push Brightness Update
430
- if (this.LightBulb.On) {
431
- await this.BLEpushBrightnessChanges();
432
- }
433
- // Push Hue & Saturation Update
434
- if (this.LightBulb.On) {
435
- await this.BLEpushRGBChanges();
436
- }
437
- if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
438
- const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
439
- this.accessory.context.kelvin = kelvin;
440
400
  }
441
401
  else {
442
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
443
- + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
402
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
403
+ await this.BLEPushConnection();
444
404
  }
445
405
  }
446
406
  else {
447
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
448
- + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
407
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
449
408
  }
450
409
  }
451
410
  async BLEpushBrightnessChanges() {
452
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
411
+ await this.debugLog('BLEpushBrightnessChanges');
453
412
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
454
- const switchbot = await this.platform.connectBLE();
455
- // Convert to BLE Address
413
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
456
414
  await this.convertBLEAddress();
457
- switchbot
458
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
459
- .then(async (device_list) => {
460
- this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
461
- return await device_list[0].setBrightness(this.LightBulb.Brightness);
462
- })
463
- .then(() => {
464
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
465
- this.LightBulb.On = false;
466
- })
467
- .catch(async (e) => {
468
- this.apiError(e);
469
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
470
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
415
+ if (switchbot !== false) {
416
+ switchbot
417
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
418
+ .then(async (device_list) => {
419
+ await this.infoLog(`Brightness: ${this.LightBulb.Brightness}`);
420
+ return await device_list[0].setBrightness(this.LightBulb.Brightness);
421
+ })
422
+ .then(async () => {
423
+ await this.successLog(`Brightness: ${this.LightBulb.Brightness} sent over SwitchBot BLE, sent successfully`);
424
+ await this.updateHomeKitCharacteristics();
425
+ })
426
+ .catch(async (e) => {
427
+ await this.apiError(e);
428
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
429
+ await this.BLEPushConnection();
430
+ });
431
+ }
432
+ else {
433
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
471
434
  await this.BLEPushConnection();
472
- });
435
+ }
473
436
  }
474
437
  else {
475
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.`
476
- + `Brightness: ${this.LightBulb.Brightness}, `
477
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
438
+ await this.debugLog(`No changes (BLEpushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
439
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
478
440
  }
479
441
  }
480
442
  async BLEpushRGBChanges() {
481
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
482
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
483
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
484
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
485
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
486
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
487
- const switchbot = await this.platform.connectBLE();
488
- // Convert to BLE Address
443
+ await this.debugLog('BLEpushRGBChanges');
444
+ if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
445
+ await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
446
+ const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation);
447
+ await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
448
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
489
449
  await this.convertBLEAddress();
490
- switchbot
491
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
492
- .then(async (device_list) => {
493
- this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
494
- return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
495
- })
496
- .then(() => {
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
498
- this.LightBulb.On = false;
499
- })
500
- .catch(async (e) => {
501
- this.apiError(e);
502
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
503
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
450
+ if (switchbot !== false) {
451
+ switchbot
452
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
453
+ .then(async (device_list) => {
454
+ await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
455
+ return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
456
+ })
457
+ .then(async () => {
458
+ await this.successLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)} sent over SwitchBot BLE, sent successfully`);
459
+ await this.updateHomeKitCharacteristics();
460
+ })
461
+ .catch(async (e) => {
462
+ await this.apiError(e);
463
+ await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection,`
464
+ + ` Error Message: ${JSON.stringify(e.message)}`);
465
+ await this.BLEPushConnection();
466
+ });
467
+ }
468
+ else {
469
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
504
470
  await this.BLEPushConnection();
505
- });
471
+ }
506
472
  }
507
473
  else {
508
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
509
- + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
474
+ await this.debugLog(`No changes (BLEpushRGBChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
475
+ + ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
510
476
  }
511
477
  }
512
478
  async openAPIpushChanges() {
513
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
479
+ await this.debugLog('openAPIpushChanges');
514
480
  if (this.LightBulb.On !== this.accessory.context.On) {
515
481
  const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
516
- /*if (this.LightBulb.On) {
517
- command = 'turnOn';
518
- } else {
519
- command = 'turnOff';
520
- }*/
521
482
  const bodyChange = JSON.stringify({
522
483
  command: `${command}`,
523
484
  parameter: 'default',
524
485
  commandType: 'command',
525
486
  });
526
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
487
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
527
488
  try {
528
489
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
529
490
  const deviceStatus = await body.json();
530
- await this.pushStatusCodes(statusCode, deviceStatus);
531
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
532
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
491
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
492
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
493
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
533
494
  await this.updateHomeKitCharacteristics();
534
495
  }
535
496
  else {
@@ -538,45 +499,32 @@ export class StripLight extends deviceBase {
538
499
  }
539
500
  }
540
501
  catch (e) {
541
- this.apiError(e);
542
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
543
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
502
+ await this.apiError(e);
503
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
544
504
  }
545
505
  }
546
506
  else {
547
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
548
- + `On: ${this.LightBulb.On}, `
549
- + `OnCached: ${this.accessory.context.On}`);
550
- }
551
- // Push Hue & Saturation Update
552
- if (this.LightBulb.On) {
553
- await this.pushHueSaturationChanges();
554
- }
555
- // Push Brightness Update
556
- if (this.LightBulb.On) {
557
- await this.pushBrightnessChanges();
507
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
558
508
  }
559
509
  }
560
510
  async pushHueSaturationChanges() {
561
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
562
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
563
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
564
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
565
- const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
566
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
567
- // Make Push On request to the API
511
+ await this.debugLog('pushHueSaturationChanges');
512
+ if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
513
+ await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
514
+ const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation);
515
+ await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
568
516
  const bodyChange = JSON.stringify({
569
517
  command: 'setColor',
570
518
  parameter: `${red}:${green}:${blue}`,
571
519
  commandType: 'command',
572
520
  });
573
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
521
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
574
522
  try {
575
523
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
576
524
  const deviceStatus = await body.json();
577
- await this.pushStatusCodes(statusCode, deviceStatus);
578
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
579
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
525
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
526
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
527
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
580
528
  await this.updateHomeKitCharacteristics();
581
529
  }
582
530
  else {
@@ -585,31 +533,31 @@ export class StripLight extends deviceBase {
585
533
  }
586
534
  }
587
535
  catch (e) {
588
- this.apiError(e);
589
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
590
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
536
+ await this.apiError(e);
537
+ await this.errorLog(`failed pushHueSaturationChanges with ${this.device.connectionType} Connection,`
538
+ + ` Error Message: ${JSON.stringify(e.message)}`);
591
539
  }
592
540
  }
593
541
  else {
594
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
595
- + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
542
+ await this.debugLog(`No changes (pushHueSaturationChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
543
+ + ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
596
544
  }
597
545
  }
598
546
  async pushBrightnessChanges() {
599
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
547
+ await this.debugLog('pushBrightnessChanges');
600
548
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
601
549
  const bodyChange = JSON.stringify({
602
550
  command: 'setBrightness',
603
551
  parameter: `${this.LightBulb.Brightness}`,
604
552
  commandType: 'command',
605
553
  });
606
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
554
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
607
555
  try {
608
556
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
609
557
  const deviceStatus = await body.json();
610
- await this.pushStatusCodes(statusCode, deviceStatus);
611
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
612
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
558
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
559
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
560
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
613
561
  await this.updateHomeKitCharacteristics();
614
562
  }
615
563
  else {
@@ -618,26 +566,25 @@ export class StripLight extends deviceBase {
618
566
  }
619
567
  }
620
568
  catch (e) {
621
- this.apiError(e);
622
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
623
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
569
+ await this.apiError(e);
570
+ await this.errorLog(`failed pushBrightnessChanges with ${this.device.connectionType} Connection,`
571
+ + ` Error Message: ${JSON.stringify(e.message)}`);
624
572
  }
625
573
  }
626
574
  else {
627
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,`
628
- + `Brightness: ${this.LightBulb.Brightness}, `
629
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
575
+ await this.debugLog(`No changes (pushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
576
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
630
577
  }
631
578
  }
632
579
  /**
633
580
  * Handle requests to set the value of the "On" characteristic
634
581
  */
635
582
  async OnSet(value) {
636
- if (this.LightBulb.On === this.accessory.context.On) {
637
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
583
+ if (this.LightBulb.On !== this.accessory.context.On) {
584
+ await this.infoLog(`Set On: ${value}`);
638
585
  }
639
586
  else {
640
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
587
+ await this.debugLog(`No Changes, On: ${value}`);
641
588
  }
642
589
  this.LightBulb.On = value;
643
590
  this.doStripLightUpdate.next();
@@ -646,14 +593,16 @@ export class StripLight extends deviceBase {
646
593
  * Handle requests to set the value of the "Brightness" characteristic
647
594
  */
648
595
  async BrightnessSet(value) {
649
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
650
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
651
- }
652
- else if (this.LightBulb.On) {
653
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
596
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
597
+ await this.infoLog(`Set Brightness: ${value}`);
654
598
  }
655
599
  else {
656
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
600
+ if (this.LightBulb.On) {
601
+ this.debugLog(`No Changes, Brightness: ${value}`);
602
+ }
603
+ else {
604
+ this.debugLog(`Set Brightness: ${value}, On: ${this.LightBulb.On}`);
605
+ }
657
606
  }
658
607
  this.LightBulb.Brightness = value;
659
608
  this.doStripLightUpdate.next();
@@ -662,14 +611,16 @@ export class StripLight extends deviceBase {
662
611
  * Handle requests to set the value of the "ColorTemperature" characteristic
663
612
  */
664
613
  async ColorTemperatureSet(value) {
665
- if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
666
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
667
- }
668
- else if (this.LightBulb.On) {
669
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
614
+ if (this.LightBulb.On && (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature)) {
615
+ this.infoLog(`Set ColorTemperature: ${value}`);
670
616
  }
671
617
  else {
672
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
618
+ if (this.LightBulb.On) {
619
+ this.debugLog(`No Changes, ColorTemperature: ${value}`);
620
+ }
621
+ else {
622
+ this.debugLog(`Set ColorTemperature: ${value}, On: ${this.LightBulb.On}`);
623
+ }
673
624
  }
674
625
  const minKelvin = 2000;
675
626
  const maxKelvin = 9000;
@@ -691,14 +642,16 @@ export class StripLight extends deviceBase {
691
642
  * Handle requests to set the value of the "Hue" characteristic
692
643
  */
693
644
  async HueSet(value) {
694
- if (this.LightBulb.Hue === this.accessory.context.Hue) {
695
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
696
- }
697
- else if (this.LightBulb.On) {
698
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
645
+ if (this.LightBulb.On && (this.LightBulb.Hue !== this.accessory.context.Hue)) {
646
+ this.infoLog(`Set Hue: ${value}`);
699
647
  }
700
648
  else {
701
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
649
+ if (this.LightBulb.On) {
650
+ this.debugLog(`No Changes, Hue: ${value}`);
651
+ }
652
+ else {
653
+ this.debugLog(`Set Hue: ${value}, On: ${this.LightBulb.On}`);
654
+ }
702
655
  }
703
656
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
704
657
  this.LightBulb.Hue = value;
@@ -708,14 +661,16 @@ export class StripLight extends deviceBase {
708
661
  * Handle requests to set the value of the "Saturation" characteristic
709
662
  */
710
663
  async SaturationSet(value) {
711
- if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
712
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
713
- }
714
- else if (this.LightBulb.On) {
715
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
664
+ if (this.LightBulb.On && (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
665
+ this.infoLog(`Set Saturation: ${value}`);
716
666
  }
717
667
  else {
718
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
668
+ if (this.LightBulb.On) {
669
+ this.debugLog(`No Changes, Saturation: ${value}`);
670
+ }
671
+ else {
672
+ this.debugLog(`Set Saturation: ${value}, On: ${this.LightBulb.On}`);
673
+ }
719
674
  }
720
675
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
721
676
  this.LightBulb.Saturation = value;
@@ -723,76 +678,39 @@ export class StripLight extends deviceBase {
723
678
  }
724
679
  async updateHomeKitCharacteristics() {
725
680
  // On
726
- if (this.LightBulb.On === undefined) {
727
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
728
- }
729
- else {
730
- this.accessory.context.On = this.LightBulb.On;
731
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
732
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
733
- }
681
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On');
734
682
  // Brightness
735
- if (this.LightBulb.Brightness === undefined) {
736
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
737
- }
738
- else {
739
- this.accessory.context.Brightness = this.LightBulb.Brightness;
740
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
741
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
742
- }
683
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness, this.LightBulb.Brightness, 'Brightness');
743
684
  // ColorTemperature
744
- if (this.LightBulb.ColorTemperature === undefined) {
745
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
746
- }
747
- else {
748
- this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
749
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
750
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
751
- + ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
752
- }
685
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature, 'ColorTemperature');
753
686
  // Hue
754
- if (this.LightBulb.Hue === undefined) {
755
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
756
- }
757
- else {
758
- this.accessory.context.Hue = this.LightBulb.Hue;
759
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
760
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
761
- }
687
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Hue, this.LightBulb.Hue, 'Hue');
762
688
  // Saturation
763
- if (this.LightBulb.Saturation === undefined) {
764
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
689
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Saturation, this.LightBulb.Saturation, 'Saturation');
690
+ }
691
+ async adaptiveLighting(device) {
692
+ if (device.striplight?.adaptiveLightingShift) {
693
+ this.adaptiveLightingShift = device.striplight.adaptiveLightingShift;
694
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
765
695
  }
766
696
  else {
767
- this.accessory.context.Saturation = this.LightBulb.Saturation;
768
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
769
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
697
+ this.adaptiveLightingShift = 0;
698
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
770
699
  }
771
700
  }
772
701
  async BLEPushConnection() {
773
702
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
774
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
703
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
775
704
  await this.openAPIpushChanges();
776
705
  }
777
706
  }
778
707
  async BLERefreshConnection(switchbot) {
779
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
780
- + ` ${JSON.stringify(switchbot)}`);
708
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
781
709
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
782
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
710
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
783
711
  await this.openAPIRefreshStatus();
784
712
  }
785
713
  }
786
- async adaptiveLighting(device) {
787
- if (device.striplight?.adaptiveLightingShift) {
788
- this.adaptiveLightingShift = device.striplight.adaptiveLightingShift;
789
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
790
- }
791
- else {
792
- this.adaptiveLightingShift = 0;
793
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
794
- }
795
- }
796
714
  async offlineOff() {
797
715
  if (this.device.offline) {
798
716
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);