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

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