@switchbot/homebridge-switchbot 2.0.0-beta.4 → 2.0.0-beta.41

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 (119) hide show
  1. package/CHANGELOG.md +12 -2
  2. package/README.md +3 -2
  3. package/config.schema.json +108 -20
  4. package/dist/device/bot.d.ts +34 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +657 -648
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/ceilinglight.d.ts +137 -0
  9. package/dist/device/ceilinglight.d.ts.map +1 -0
  10. package/dist/device/ceilinglight.js +1127 -0
  11. package/dist/device/ceilinglight.js.map +1 -0
  12. package/dist/device/colorbulb.d.ts +36 -19
  13. package/dist/device/colorbulb.d.ts.map +1 -1
  14. package/dist/device/colorbulb.js +486 -309
  15. package/dist/device/colorbulb.js.map +1 -1
  16. package/dist/device/contact.d.ts +18 -8
  17. package/dist/device/contact.d.ts.map +1 -1
  18. package/dist/device/contact.js +306 -237
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +23 -16
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +566 -502
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/humidifier.d.ts +24 -18
  25. package/dist/device/humidifier.d.ts.map +1 -1
  26. package/dist/device/humidifier.js +453 -369
  27. package/dist/device/humidifier.js.map +1 -1
  28. package/dist/device/lock.d.ts +27 -8
  29. package/dist/device/lock.d.ts.map +1 -1
  30. package/dist/device/lock.js +305 -148
  31. package/dist/device/lock.js.map +1 -1
  32. package/dist/device/meter.d.ts +22 -16
  33. package/dist/device/meter.d.ts.map +1 -1
  34. package/dist/device/meter.js +367 -267
  35. package/dist/device/meter.js.map +1 -1
  36. package/dist/device/meterplus.d.ts +24 -12
  37. package/dist/device/meterplus.d.ts.map +1 -1
  38. package/dist/device/meterplus.js +381 -213
  39. package/dist/device/meterplus.js.map +1 -1
  40. package/dist/device/motion.d.ts +18 -8
  41. package/dist/device/motion.d.ts.map +1 -1
  42. package/dist/device/motion.js +279 -216
  43. package/dist/device/motion.js.map +1 -1
  44. package/dist/device/plug.d.ts +27 -17
  45. package/dist/device/plug.d.ts.map +1 -1
  46. package/dist/device/plug.js +295 -251
  47. package/dist/device/plug.js.map +1 -1
  48. package/dist/device/striplight.d.ts +43 -19
  49. package/dist/device/striplight.d.ts.map +1 -1
  50. package/dist/device/striplight.js +506 -262
  51. package/dist/device/striplight.js.map +1 -1
  52. package/dist/irdevice/airconditioner.d.ts +9 -12
  53. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  54. package/dist/irdevice/airconditioner.js +278 -230
  55. package/dist/irdevice/airconditioner.js.map +1 -1
  56. package/dist/irdevice/airpurifier.d.ts +5 -3
  57. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  58. package/dist/irdevice/airpurifier.js +154 -104
  59. package/dist/irdevice/airpurifier.js.map +1 -1
  60. package/dist/irdevice/camera.d.ts +11 -8
  61. package/dist/irdevice/camera.d.ts.map +1 -1
  62. package/dist/irdevice/camera.js +133 -86
  63. package/dist/irdevice/camera.js.map +1 -1
  64. package/dist/irdevice/fan.d.ts +10 -7
  65. package/dist/irdevice/fan.d.ts.map +1 -1
  66. package/dist/irdevice/fan.js +160 -109
  67. package/dist/irdevice/fan.js.map +1 -1
  68. package/dist/irdevice/light.d.ts +11 -8
  69. package/dist/irdevice/light.d.ts.map +1 -1
  70. package/dist/irdevice/light.js +137 -103
  71. package/dist/irdevice/light.js.map +1 -1
  72. package/dist/irdevice/other.d.ts +11 -8
  73. package/dist/irdevice/other.d.ts.map +1 -1
  74. package/dist/irdevice/other.js +145 -98
  75. package/dist/irdevice/other.js.map +1 -1
  76. package/dist/irdevice/tv.d.ts +12 -9
  77. package/dist/irdevice/tv.d.ts.map +1 -1
  78. package/dist/irdevice/tv.js +143 -93
  79. package/dist/irdevice/tv.js.map +1 -1
  80. package/dist/irdevice/vacuumcleaner.d.ts +9 -10
  81. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  82. package/dist/irdevice/vacuumcleaner.js +132 -89
  83. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  84. package/dist/irdevice/waterheater.d.ts +8 -9
  85. package/dist/irdevice/waterheater.d.ts.map +1 -1
  86. package/dist/irdevice/waterheater.js +132 -89
  87. package/dist/irdevice/waterheater.js.map +1 -1
  88. package/dist/platform.d.ts +15 -12
  89. package/dist/platform.d.ts.map +1 -1
  90. package/dist/platform.js +574 -454
  91. package/dist/platform.js.map +1 -1
  92. package/dist/settings.d.ts +21 -11
  93. package/dist/settings.d.ts.map +1 -1
  94. package/dist/settings.js.map +1 -1
  95. package/homebridge-ui/public/index.html +1 -0
  96. package/package.json +13 -11
  97. package/src/device/bot.ts +656 -659
  98. package/src/device/ceilinglight.ts +1208 -0
  99. package/src/device/colorbulb.ts +490 -344
  100. package/src/device/contact.ts +309 -241
  101. package/src/device/curtain.ts +571 -516
  102. package/src/device/humidifier.ts +456 -403
  103. package/src/device/lock.ts +314 -149
  104. package/src/device/meter.ts +378 -280
  105. package/src/device/meterplus.ts +391 -219
  106. package/src/device/motion.ts +285 -222
  107. package/src/device/plug.ts +299 -260
  108. package/src/device/striplight.ts +522 -289
  109. package/src/irdevice/airconditioner.ts +282 -241
  110. package/src/irdevice/airpurifier.ts +151 -103
  111. package/src/irdevice/camera.ts +131 -85
  112. package/src/irdevice/fan.ts +157 -106
  113. package/src/irdevice/light.ts +135 -103
  114. package/src/irdevice/other.ts +143 -97
  115. package/src/irdevice/tv.ts +142 -93
  116. package/src/irdevice/vacuumcleaner.ts +131 -89
  117. package/src/irdevice/waterheater.ts +130 -89
  118. package/src/platform.ts +567 -476
  119. package/src/settings.ts +22 -11
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ColorBulb = void 0;
7
7
  const https_1 = __importDefault(require("https"));
8
8
  const crypto_1 = __importDefault(require("crypto"));
9
+ const super_stringify_1 = __importDefault(require("super-stringify"));
9
10
  const rxjs_1 = require("rxjs");
10
11
  const operators_1 = require("rxjs/operators");
11
12
  const settings_1 = require("../settings");
@@ -19,24 +20,16 @@ class ColorBulb {
19
20
  this.platform = platform;
20
21
  this.accessory = accessory;
21
22
  this.device = device;
23
+ // Connection
24
+ this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
25
+ this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
22
26
  // default placeholders
23
27
  this.logs(device);
24
28
  this.scan(device);
25
29
  this.refreshRate(device);
26
30
  this.adaptiveLighting(device);
27
31
  this.config(device);
28
- if (this.On === undefined) {
29
- this.On = false;
30
- }
31
- else {
32
- this.On = this.accessory.context.On;
33
- }
34
- this.Hue = 0;
35
- this.Brightness = 0;
36
- this.Saturation = 0;
37
- this.ColorTemperature = 140;
38
- this.minKelvin = 2000;
39
- this.maxKelvin = 9000;
32
+ this.context();
40
33
  // this is subject we use to track when we need to POST changes to the SwitchBot API
41
34
  this.doColorBulbUpdate = new rxjs_1.Subject();
42
35
  this.colorBulbUpdateInProgress = false;
@@ -59,7 +52,7 @@ class ColorBulb {
59
52
  this.accessory.removeService(this.lightBulbService);
60
53
  this.lightBulbService = this.accessory.addService(this.platform.Service.Lightbulb);
61
54
  this.accessory.context.adaptiveLighting = false;
62
- this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
55
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
63
56
  }
64
57
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
65
58
  // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
@@ -83,7 +76,6 @@ class ColorBulb {
83
76
  })
84
77
  .onSet(this.BrightnessSet.bind(this));
85
78
  // handle ColorTemperature events using the ColorTemperature characteristic
86
- this.debugLog(`Color Bulb: ${accessory.displayName} Add ColorTemperature Characteristic`);
87
79
  this.lightBulbService
88
80
  .getCharacteristic(this.platform.Characteristic.ColorTemperature)
89
81
  .setProps({
@@ -119,14 +111,14 @@ class ColorBulb {
119
111
  return this.Saturation;
120
112
  })
121
113
  .onSet(this.SaturationSet.bind(this));
122
- this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
114
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
123
115
  if (this.adaptiveLightingShift !== -1) {
124
116
  this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.lightBulbService, {
125
117
  customTemperatureAdjustment: this.adaptiveLightingShift,
126
118
  });
127
119
  this.accessory.configureController(this.AdaptiveLightingController);
128
120
  this.accessory.context.adaptiveLighting = true;
129
- this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
121
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
130
122
  ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
131
123
  }
132
124
  // Update Homekit
@@ -148,25 +140,36 @@ class ColorBulb {
148
140
  await this.pushChanges();
149
141
  }
150
142
  catch (e) {
151
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges`);
152
- if (this.deviceLogging.includes('debug')) {
153
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
154
- }
155
143
  this.apiError(e);
144
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
145
+ + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
156
146
  }
157
147
  this.colorBulbUpdateInProgress = false;
148
+ // Refresh the status from the API
149
+ (0, rxjs_1.interval)(15000)
150
+ .pipe((0, operators_1.skipWhile)(() => this.colorBulbUpdateInProgress))
151
+ .pipe((0, operators_1.take)(1))
152
+ .subscribe(async () => {
153
+ await this.refreshStatus();
154
+ });
158
155
  });
159
156
  }
157
+ /**
158
+ * Parse the device status from the SwitchBot api
159
+ */
160
160
  async parseStatus() {
161
- if (this.SwitchToOpenAPI || !this.device.ble) {
161
+ /*if (this.BLE) {
162
+ await this.BLEparseStatus();
163
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
162
164
  await this.openAPIparseStatus();
163
165
  }
164
166
  else {
165
- await this.BLEparseStatus();
167
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
168
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
166
169
  }
167
170
  }
168
171
  async BLEparseStatus() {
169
- this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE parseStatus`);
172
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
170
173
  // State
171
174
  switch (this.state) {
172
175
  case 'on':
@@ -175,9 +178,10 @@ class ColorBulb {
175
178
  default:
176
179
  this.On = false;
177
180
  }
178
- this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
181
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
179
182
  }
180
183
  async openAPIparseStatus() {
184
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
181
185
  switch (this.power) {
182
186
  case 'on':
183
187
  this.On = true;
@@ -185,52 +189,59 @@ class ColorBulb {
185
189
  default:
186
190
  this.On = false;
187
191
  }
188
- this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
192
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
189
193
  // Brightness
190
194
  this.Brightness = Number(this.brightness);
191
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
195
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
192
196
  // Color, Hue & Brightness
193
197
  if (this.color) {
194
- this.debugLog(`Color Bulb: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${(0, super_stringify_1.default)(this.color)}`);
195
199
  const [red, green, blue] = this.color.split(':');
196
- this.debugLog(`Color Bulb: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
197
- this.debugLog(`Color Bulb: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
198
- this.debugLog(`Color Bulb: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
200
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${(0, super_stringify_1.default)(red)}`);
201
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${(0, super_stringify_1.default)(green)}`);
202
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${(0, super_stringify_1.default)(blue)}`);
199
203
  const [hue, saturation] = (0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue));
200
- this.debugLog(`Color Bulb: ${this.accessory.displayName} hs: ${JSON.stringify((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
204
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
205
+ + ` hs: ${(0, super_stringify_1.default)((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
201
206
  // Hue
202
207
  this.Hue = hue;
203
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
208
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
204
209
  // Saturation
205
210
  this.Saturation = saturation;
206
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
211
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
207
212
  }
208
213
  // ColorTemperature
209
214
  if (!Number.isNaN(this.colorTemperature)) {
210
- this.debugLog(`Color Bulb: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.colorTemperature}`);
215
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.colorTemperature}`);
211
216
  const mired = Math.round(1000000 / this.colorTemperature);
212
217
  this.ColorTemperature = Number(mired);
213
218
  this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
214
- this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
219
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
215
220
  }
216
221
  }
222
+ /**
223
+ * Asks the SwitchBot API for the latest device information
224
+ */
217
225
  async refreshStatus() {
218
- if (this.device.ble) {
219
- await this.BLERefreshStatus();
226
+ /*if (this.BLE) {
227
+ await this.BLERefreshStatus();
228
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
229
+ await this.openAPIRefreshStatus();
220
230
  }
221
231
  else {
222
- await this.openAPIRefreshStatus();
232
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
233
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
223
234
  }
224
235
  }
225
236
  async BLERefreshStatus() {
226
- this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE refreshStatus`);
237
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
227
238
  const switchbot = await this.platform.connectBLE();
228
239
  // Convert to BLE Address
229
240
  this.device.bleMac = this.device
230
241
  .deviceId.match(/.{1,2}/g)
231
242
  .join(':')
232
243
  .toLowerCase();
233
- this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
244
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
234
245
  this.getCustomBLEAddress(switchbot);
235
246
  // Start to monitor advertisement packets
236
247
  if (switchbot !== false) {
@@ -243,13 +254,9 @@ class ColorBulb {
243
254
  // Set an event hander
244
255
  switchbot.onadvertisement = (ad) => {
245
256
  this.address = ad.address;
246
- if (this.deviceLogging.includes('debug')) {
247
- this.infoLog(this.address);
248
- this.infoLog(this.device.bleMac);
249
- this.infoLog(`Color Bulb: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
250
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
251
- }
252
- this.errorLog(`Color Bulb: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
257
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
258
+ + ` BLE Address Found: ${this.address}`);
259
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
253
260
  this.serviceData = ad.serviceData;
254
261
  //this.state = ad.serviceData.state;
255
262
  //this.delay = ad.serviceData.delay;
@@ -258,19 +265,19 @@ class ColorBulb {
258
265
  //this.wifiRssi = ad.serviceData.wifiRssi;
259
266
  //this.overload = ad.serviceData.overload;
260
267
  //this.currentPower = ad.serviceData.currentPower;
261
- this.debugLog(`Color Bulb: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
268
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
262
269
  /*this.debugLog(
263
- `Color Bulb: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
270
+ `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
264
271
  `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
265
272
  `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
266
273
  );*/
267
274
  if (this.serviceData) {
268
275
  this.connected = true;
269
- this.debugLog(`Color Bulb: ${this.accessory.displayName} connected: ${this.connected}`);
276
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
270
277
  }
271
278
  else {
272
279
  this.connected = false;
273
- this.debugLog(`Color Bulb: ${this.accessory.displayName} connected: ${this.connected}`);
280
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
274
281
  }
275
282
  };
276
283
  // Wait 2 seconds
@@ -280,69 +287,33 @@ class ColorBulb {
280
287
  // Stop to monitor
281
288
  switchbot.stopScan();
282
289
  if (this.connected) {
283
- this.parseStatus();
290
+ this.BLEparseStatus();
284
291
  this.updateHomeKitCharacteristics();
285
292
  }
286
293
  else {
287
- this.errorLog(`Color Bulb: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
288
- if (this.platform.config.credentials?.token) {
289
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using OpenAPI Connection`);
290
- this.SwitchToOpenAPI = true;
291
- await this.openAPIRefreshStatus();
292
- }
294
+ await this.BLERefreshConnection(switchbot);
293
295
  }
294
296
  })
295
297
  .catch(async (e) => {
296
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
297
- if (this.deviceLogging.includes('debug')) {
298
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
299
- }
300
- if (this.platform.config.credentials?.token) {
301
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using OpenAPI Connection`);
302
- this.SwitchToOpenAPI = true;
303
- await this.openAPIRefreshStatus();
304
- }
305
298
  this.apiError(e);
299
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
300
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
301
+ await this.BLERefreshConnection(switchbot);
306
302
  });
307
303
  }
308
304
  else {
309
- await this.BLEconnection(switchbot);
310
- }
311
- }
312
- async getCustomBLEAddress(switchbot) {
313
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
314
- this.debugLog(`Color Bulb: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
315
- (async () => {
316
- // Start to monitor advertisement packets
317
- await switchbot.startScan({
318
- model: 'u',
319
- });
320
- // Set an event handler
321
- switchbot.onadvertisement = (ad) => {
322
- this.warnLog(`Color Bulb: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
323
- };
324
- await switchbot.wait(10000);
325
- // Stop to monitor
326
- switchbot.stopScan();
327
- })();
328
- }
329
- }
330
- async BLEconnection(switchbot) {
331
- this.errorLog(`Color Bulb: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
332
- if (this.platform.config.credentials?.token) {
333
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using OpenAPI Connection`);
334
- this.SwitchToOpenAPI = true;
335
- await this.openAPIRefreshStatus();
305
+ await this.BLERefreshConnection(switchbot);
336
306
  }
337
307
  }
338
308
  async openAPIRefreshStatus() {
309
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
339
310
  try {
340
311
  const t = Date.now();
341
312
  const nonce = 'requestID';
342
313
  const data = this.platform.config.credentials?.token + t + nonce;
343
314
  const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
344
315
  const sign = signTerm.toString('base64');
345
- this.debugLog(`Color Bulb: ${this.accessory.displayName} sign: ${sign}`);
316
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
346
317
  const options = {
347
318
  hostname: settings_1.HostDomain,
348
319
  port: 443,
@@ -357,39 +328,38 @@ class ColorBulb {
357
328
  },
358
329
  };
359
330
  const req = https_1.default.request(options, (res) => {
360
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Refresh statusCode: ${res.statusCode}`);
331
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
332
+ this.statusCode({ res });
361
333
  let rawData = '';
362
334
  res.on('data', (d) => {
363
335
  rawData += d;
364
- this.debugLog(`Color Bulb: ${this.accessory.displayName} d: ${d}`);
336
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
365
337
  });
366
338
  res.on('end', () => {
367
339
  try {
368
340
  this.deviceStatus = JSON.parse(rawData);
369
- this.debugLog(`Color Bulb: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
341
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
370
342
  this.power = this.deviceStatus.body.power;
371
343
  this.color = this.deviceStatus.body.color;
372
344
  this.brightness = this.deviceStatus.body.brightness;
373
345
  this.colorTemperature = this.deviceStatus.body.colorTemperature;
374
- this.parseStatus();
346
+ this.openAPIparseStatus();
375
347
  this.updateHomeKitCharacteristics();
376
348
  }
377
349
  catch (e) {
378
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
350
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
379
351
  }
380
352
  });
381
353
  });
382
354
  req.on('error', (e) => {
383
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
355
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
384
356
  });
385
357
  req.end();
386
358
  }
387
359
  catch (e) {
388
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
389
- if (this.deviceLogging.includes('debug')) {
390
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
391
- }
392
360
  this.apiError(e);
361
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
362
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
393
363
  }
394
364
  }
395
365
  /**
@@ -404,8 +374,61 @@ class ColorBulb {
404
374
  *
405
375
  */
406
376
  async pushChanges() {
377
+ /*if (this.BLE) {
378
+ await this.BLEpushChanges();
379
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
380
+ await this.openAPIpushChanges();
381
+ }
382
+ else {
383
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
384
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
385
+ }
386
+ (0, rxjs_1.interval)(5000)
387
+ .pipe((0, operators_1.take)(1))
388
+ .subscribe(async () => {
389
+ await this.refreshStatus();
390
+ });
391
+ }
392
+ async BLEpushChanges() {
393
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
394
+ if (this.On !== this.accessory.context.On) {
395
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
396
+ const switchbot = await this.platform.connectBLE();
397
+ // Convert to BLE Address
398
+ this.device.bleMac = this.device
399
+ .deviceId.match(/.{1,2}/g)
400
+ .join(':')
401
+ .toLowerCase();
402
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
403
+ switchbot
404
+ .discover({
405
+ model: 'u',
406
+ id: this.device.bleMac,
407
+ })
408
+ .then((device_list) => {
409
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
410
+ return this.turnOnOff(device_list);
411
+ })
412
+ .then(() => {
413
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
414
+ this.On = false;
415
+ })
416
+ .catch(async (e) => {
417
+ this.apiError(e);
418
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
419
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
420
+ await this.BLEPushConnection();
421
+ });
422
+ }
423
+ else {
424
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
425
+ + `OnCached: ${this.accessory.context.On}`);
426
+ }
427
+ }
428
+ async openAPIpushChanges() {
407
429
  try {
408
- if (this.On !== this.OnCached) {
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
431
+ if (this.On !== this.accessory.context.On) {
409
432
  // Make Push On request to the API
410
433
  const t = Date.now();
411
434
  const nonce = 'requestID';
@@ -414,7 +437,7 @@ class ColorBulb {
414
437
  .update(Buffer.from(data, 'utf-8'))
415
438
  .digest();
416
439
  const sign = signTerm.toString('base64');
417
- this.debugLog(`Color Bulb: ${this.accessory.displayName} sign: ${sign}`);
440
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
418
441
  let command = '';
419
442
  if (this.On) {
420
443
  command = 'turnOn';
@@ -422,12 +445,12 @@ class ColorBulb {
422
445
  else {
423
446
  command = 'turnOff';
424
447
  }
425
- const body = JSON.stringify({
448
+ const body = (0, super_stringify_1.default)({
426
449
  'command': `${command}`,
427
450
  'parameter': 'default',
428
451
  'commandType': 'command',
429
452
  });
430
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
453
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
431
454
  const options = {
432
455
  hostname: settings_1.HostDomain,
433
456
  port: 443,
@@ -443,28 +466,30 @@ class ColorBulb {
443
466
  },
444
467
  };
445
468
  const req = https_1.default.request(options, res => {
446
- this.debugLog(`Color Bulb: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
469
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
447
470
  this.statusCode({ res });
448
471
  res.on('data', d => {
449
- this.debugLog(`Color Bulb: ${this.accessory.displayName} d: ${d}`);
472
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
450
473
  });
451
474
  });
452
475
  req.on('error', (e) => {
453
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
476
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
454
477
  });
455
478
  req.write(body);
456
479
  req.end();
457
- this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(req)}`);
458
- this.OnCached = this.On;
459
- this.accessory.context.On = this.OnCached;
480
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges: ${(0, super_stringify_1.default)(req)}`);
481
+ }
482
+ else {
483
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
484
+ + `OnCached: ${this.accessory.context.On}`);
460
485
  }
461
486
  // Push Brightness Update
462
487
  if (this.On) {
463
- //await this.pushBrightnessChanges();
488
+ await this.pushBrightnessChanges();
464
489
  }
465
490
  // Push ColorTemperature Update
466
491
  if (this.On) {
467
- //await this.pushColorTemperatureChanges();
492
+ await this.pushColorTemperatureChanges();
468
493
  }
469
494
  // Push Hue & Saturation Update
470
495
  if (this.On) {
@@ -472,20 +497,19 @@ class ColorBulb {
472
497
  }
473
498
  }
474
499
  catch (e) {
475
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
476
- if (this.deviceLogging.includes('debug')) {
477
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
478
- }
479
500
  this.apiError(e);
501
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
502
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
480
503
  }
481
504
  }
482
505
  async pushHueSaturationChanges() {
483
506
  try {
484
- if (this.Hue !== this.HueCached || this.Saturation !== this.SaturationCached) {
485
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
486
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
507
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
508
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
509
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
487
511
  const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
488
- this.debugLog(`Color Bulb: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
489
513
  // Make Push Hue/Saturation request to the API
490
514
  const t = Date.now();
491
515
  const nonce = 'requestID';
@@ -494,13 +518,13 @@ class ColorBulb {
494
518
  .update(Buffer.from(data, 'utf-8'))
495
519
  .digest();
496
520
  const sign = signTerm.toString('base64');
497
- this.debugLog(`Color Bulb: ${this.accessory.displayName} sign: ${sign}`);
498
- const body = JSON.stringify({
521
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
522
+ const body = (0, super_stringify_1.default)({
499
523
  'command': 'setColor',
500
524
  'parameter': `${red}:${green}:${blue}`,
501
525
  'commandType': 'command',
502
526
  });
503
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
527
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
504
528
  const options = {
505
529
  hostname: settings_1.HostDomain,
506
530
  port: 443,
@@ -516,36 +540,33 @@ class ColorBulb {
516
540
  },
517
541
  };
518
542
  const req = https_1.default.request(options, res => {
519
- this.debugLog(`Color Bulb: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
543
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
520
544
  res.on('data', d => {
521
- this.debugLog(`Color Bulb: ${this.accessory.displayName} d: ${d}`);
545
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
522
546
  });
523
547
  });
524
548
  req.on('error', (e) => {
525
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
549
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
526
550
  });
527
551
  req.write(body);
528
552
  req.end();
529
- this.debugLog(`Color Bulb: ${this.accessory.displayName} pushHueSaturationChanges: ${JSON.stringify(req)}`);
530
- this.HueCached = this.Hue;
531
- this.SaturationCached = this.Saturation;
553
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${(0, super_stringify_1.default)(req)}`);
532
554
  }
533
555
  else {
534
- this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.` +
535
- `Hue: ${this.Hue}, HueCached: ${this.HueCached}, Saturation: ${this.Saturation}, SaturationCached: ${this.SaturationCached}`);
556
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
557
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
536
558
  }
537
559
  }
538
560
  catch (e) {
539
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
540
- if (this.deviceLogging.includes('debug')) {
541
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
542
- }
543
561
  this.apiError(e);
562
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
563
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
544
564
  }
545
565
  }
546
566
  async pushColorTemperatureChanges() {
547
567
  try {
548
- if (this.ColorTemperature !== this.ColorTemperatureCached) {
568
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
569
+ if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
549
570
  const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
550
571
  this.cacheKelvin = kelvin;
551
572
  // Make Push On request to the API
@@ -556,13 +577,13 @@ class ColorBulb {
556
577
  .update(Buffer.from(data, 'utf-8'))
557
578
  .digest();
558
579
  const sign = signTerm.toString('base64');
559
- this.debugLog(`Color Bulb: ${this.accessory.displayName} sign: ${sign}`);
560
- const body = JSON.stringify({
580
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
581
+ const body = (0, super_stringify_1.default)({
561
582
  'command': 'setColorTemperature',
562
583
  'parameter': `${kelvin}`,
563
584
  'commandType': 'command',
564
585
  });
565
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
586
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
566
587
  const options = {
567
588
  hostname: settings_1.HostDomain,
568
589
  port: 443,
@@ -578,35 +599,34 @@ class ColorBulb {
578
599
  },
579
600
  };
580
601
  const req = https_1.default.request(options, res => {
581
- this.debugLog(`Color Bulb: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
602
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
582
603
  res.on('data', d => {
583
- this.debugLog(`Color Bulb: ${this.accessory.displayName} d: ${d}`);
604
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
584
605
  });
585
606
  });
586
607
  req.on('error', (e) => {
587
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
608
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
588
609
  });
589
610
  req.write(body);
590
611
  req.end();
591
- this.debugLog(`Color Bulb: ${this.accessory.displayName} pushColorTemperatureChanges: ${JSON.stringify(req)}`);
592
- this.ColorTemperatureCached = this.ColorTemperature;
612
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges: ${(0, super_stringify_1.default)(req)}`);
613
+ this.accessory.context.ColorTemperature = this.ColorTemperature;
593
614
  }
594
615
  else {
595
- this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.` +
596
- `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.ColorTemperatureCached}`);
616
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
617
+ `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
597
618
  }
598
619
  }
599
620
  catch (e) {
600
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
601
- if (this.deviceLogging.includes('debug')) {
602
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
603
- }
604
621
  this.apiError(e);
622
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
623
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
605
624
  }
606
625
  }
607
626
  async pushBrightnessChanges() {
608
627
  try {
609
- if (this.Brightness !== this.BrightnessCached) {
628
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
629
+ if (this.Brightness !== this.accessory.context.Brightness) {
610
630
  // Make Push On request to the API
611
631
  const t = Date.now();
612
632
  const nonce = 'requestID';
@@ -615,13 +635,13 @@ class ColorBulb {
615
635
  .update(Buffer.from(data, 'utf-8'))
616
636
  .digest();
617
637
  const sign = signTerm.toString('base64');
618
- this.debugLog(`Color Bulb: ${this.accessory.displayName} sign: ${sign}`);
619
- const body = JSON.stringify({
638
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
639
+ const body = (0, super_stringify_1.default)({
620
640
  'command': 'setBrightness',
621
641
  'parameter': `${this.Brightness}`,
622
642
  'commandType': 'command',
623
643
  });
624
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
644
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
625
645
  const options = {
626
646
  hostname: settings_1.HostDomain,
627
647
  port: 443,
@@ -637,107 +657,290 @@ class ColorBulb {
637
657
  },
638
658
  };
639
659
  const req = https_1.default.request(options, res => {
640
- this.debugLog(`Color Bulb: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
660
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
641
661
  res.on('data', d => {
642
- this.debugLog(`Color Bulb: ${this.accessory.displayName} d: ${d}`);
662
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
643
663
  });
644
664
  });
645
665
  req.on('error', (e) => {
646
- this.errorLog(`Color Bulb: ${this.accessory.displayName} error message: ${e.message}`);
666
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
647
667
  });
648
668
  req.write(body);
649
669
  req.end();
650
- this.debugLog(`Color Bulb: ${this.accessory.displayName} pushBrightnessChanges: ${JSON.stringify(req)}`);
651
- this.BrightnessCached = this.Brightness;
670
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${(0, super_stringify_1.default)(req)}`);
652
671
  }
653
672
  else {
654
- this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.` + `Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}`);
673
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
674
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
655
675
  }
656
676
  }
657
677
  catch (e) {
658
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
659
- if (this.deviceLogging.includes('debug')) {
660
- this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
661
- }
662
678
  this.apiError(e);
679
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
680
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
681
+ }
682
+ }
683
+ /**
684
+ * Handle requests to set the value of the "On" characteristic
685
+ */
686
+ async OnSet(value) {
687
+ if (this.On === this.accessory.context.On) {
688
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
689
+ }
690
+ else {
691
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
692
+ }
693
+ this.On = value;
694
+ this.doColorBulbUpdate.next();
695
+ }
696
+ /**
697
+ * Handle requests to set the value of the "Brightness" characteristic
698
+ */
699
+ async BrightnessSet(value) {
700
+ if (this.Brightness === this.accessory.context.Brightness) {
701
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
702
+ }
703
+ else if (this.On) {
704
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
705
+ }
706
+ else {
707
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
663
708
  }
709
+ this.Brightness = value;
710
+ this.doColorBulbUpdate.next();
711
+ }
712
+ /**
713
+ * Handle requests to set the value of the "ColorTemperature" characteristic
714
+ */
715
+ async ColorTemperatureSet(value) {
716
+ if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
717
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
718
+ }
719
+ else if (this.On) {
720
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
721
+ }
722
+ else {
723
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
724
+ }
725
+ // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
726
+ const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
727
+ // Check and increase/decrease kelvin to range of device
728
+ const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
729
+ if (!this.accessory.context.On || this.cacheKelvin === k) {
730
+ return;
731
+ }
732
+ // Updating the hue/sat to the corresponding values mimics native adaptive lighting
733
+ const hs = (0, settings_1.m2hs)(value);
734
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
735
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
736
+ this.ColorTemperature = value;
737
+ this.doColorBulbUpdate.next();
738
+ }
739
+ /**
740
+ * Handle requests to set the value of the "Hue" characteristic
741
+ */
742
+ async HueSet(value) {
743
+ if (this.Hue === this.accessory.context.Hue) {
744
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
745
+ }
746
+ else if (this.On) {
747
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
748
+ }
749
+ else {
750
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
751
+ }
752
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
753
+ this.Hue = value;
754
+ this.doColorBulbUpdate.next();
755
+ }
756
+ /**
757
+ * Handle requests to set the value of the "Saturation" characteristic
758
+ */
759
+ async SaturationSet(value) {
760
+ if (this.Saturation === this.accessory.context.Saturation) {
761
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
762
+ }
763
+ else if (this.On) {
764
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
765
+ }
766
+ else {
767
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
768
+ }
769
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
770
+ this.Saturation = value;
771
+ this.doColorBulbUpdate.next();
664
772
  }
665
773
  async updateHomeKitCharacteristics() {
666
774
  if (this.On === undefined) {
667
- this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
775
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
668
776
  }
669
777
  else {
778
+ this.accessory.context.On = this.On;
670
779
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, this.On);
671
- this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
780
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
672
781
  }
673
782
  if (this.Brightness === undefined) {
674
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
783
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
675
784
  }
676
785
  else {
786
+ this.accessory.context.Brightness = this.Brightness;
677
787
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
678
- this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
788
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
679
789
  }
680
790
  if (this.ColorTemperature === undefined) {
681
- this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
791
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
682
792
  }
683
793
  else {
794
+ this.accessory.context.ColorTemperature = this.ColorTemperature;
684
795
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, this.ColorTemperature);
685
- this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
796
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
686
797
  }
687
798
  if (this.Hue === undefined) {
688
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
799
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
689
800
  }
690
801
  else {
802
+ this.accessory.context.Hue = this.Hue;
691
803
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, this.Hue);
692
- this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
804
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
693
805
  }
694
806
  if (this.Saturation === undefined) {
695
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
807
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
696
808
  }
697
809
  else {
810
+ this.accessory.context.Saturation = this.Saturation;
698
811
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, this.Saturation);
699
- this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
812
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
700
813
  }
701
814
  }
702
- apiError(e) {
703
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
704
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
705
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
706
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
707
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
708
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
815
+ async adaptiveLighting(device) {
816
+ if (device.colorbulb?.adaptiveLightingShift) {
817
+ this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
818
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
819
+ }
820
+ else {
821
+ this.adaptiveLightingShift = 0;
822
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
823
+ }
824
+ }
825
+ async getCustomBLEAddress(switchbot) {
826
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
827
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
828
+ (async () => {
829
+ // Start to monitor advertisement packets
830
+ await switchbot.startScan({
831
+ model: 'u',
832
+ });
833
+ // Set an event handler
834
+ switchbot.onadvertisement = (ad) => {
835
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
836
+ };
837
+ await switchbot.wait(10000);
838
+ // Stop to monitor
839
+ switchbot.stopScan();
840
+ })();
841
+ }
842
+ }
843
+ async BLEPushConnection() {
844
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
845
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
846
+ await this.openAPIpushChanges();
847
+ }
848
+ }
849
+ async BLERefreshConnection(switchbot) {
850
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
851
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
852
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
853
+ await this.openAPIRefreshStatus();
854
+ }
855
+ }
856
+ async turnOnOff(device_list) {
857
+ return await this.retry({
858
+ max: await this.maxRetry(),
859
+ fn: () => {
860
+ if (this.On) {
861
+ return device_list[0].turnOn({ id: this.device.bleMac });
862
+ }
863
+ else {
864
+ return device_list[0].turnOff({ id: this.device.bleMac });
865
+ }
866
+ },
867
+ });
868
+ }
869
+ async retry({ max, fn }) {
870
+ return fn().catch(async (err) => {
871
+ if (max === 0) {
872
+ throw err;
873
+ }
874
+ this.infoLog(err);
875
+ this.infoLog('Retrying');
876
+ await this.switchbot.wait(1000);
877
+ return this.retry({ max: max - 1, fn });
878
+ });
879
+ }
880
+ async maxRetry() {
881
+ let maxRetry;
882
+ if (this.device.bot?.maxRetry) {
883
+ maxRetry = this.device.bot?.maxRetry;
884
+ }
885
+ else {
886
+ maxRetry = 5;
887
+ }
888
+ return maxRetry;
889
+ }
890
+ minStep(device) {
891
+ if (device.colorbulb?.set_minStep) {
892
+ this.set_minStep = device.colorbulb?.set_minStep;
893
+ }
894
+ else {
895
+ this.set_minStep = 1;
896
+ }
897
+ return this.set_minStep;
898
+ }
899
+ async scan(device) {
900
+ if (device.scanDuration) {
901
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
902
+ if (this.BLE) {
903
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
904
+ }
905
+ }
906
+ else {
907
+ this.scanDuration = this.accessory.context.scanDuration = 1;
908
+ if (this.BLE) {
909
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
910
+ }
911
+ }
709
912
  }
710
913
  async statusCode({ res }) {
711
914
  switch (res.statusCode) {
712
915
  case 151:
713
- this.errorLog(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
916
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
714
917
  break;
715
918
  case 152:
716
- this.errorLog(`Color Bulb: ${this.accessory.displayName} Device not found.`);
919
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
717
920
  break;
718
921
  case 160:
719
- this.errorLog(`Color Bulb: ${this.accessory.displayName} Command is not supported.`);
922
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
720
923
  break;
721
924
  case 161:
722
- this.errorLog(`Color Bulb: ${this.accessory.displayName} Device is offline.`);
925
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
723
926
  this.offlineOff();
724
927
  break;
725
928
  case 171:
726
- this.errorLog(`Color Bulb: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
929
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
727
930
  this.offlineOff();
728
931
  break;
729
932
  case 190:
730
- this.errorLog(`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
731
- ` Or command: ${JSON.stringify(res)} format is invalid`);
933
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
934
+ ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
732
935
  break;
733
936
  case 100:
734
937
  if (this.platform.debugMode) {
735
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Command successfully sent.`);
938
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
736
939
  }
737
940
  break;
738
941
  default:
739
942
  if (this.platform.debugMode) {
740
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Unknown statusCode.`);
943
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
741
944
  }
742
945
  }
743
946
  }
@@ -745,68 +948,87 @@ class ColorBulb {
745
948
  if (this.device.offline) {
746
949
  this.On = false;
747
950
  this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
951
+ await this.updateHomeKitCharacteristics();
748
952
  }
749
953
  }
750
- /**
751
- * Handle requests to set the value of the "On" characteristic
752
- */
753
- async OnSet(value) {
754
- this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${value}`);
755
- this.On = value;
756
- this.doColorBulbUpdate.next();
757
- }
758
- /**
759
- * Handle requests to set the value of the "Brightness" characteristic
760
- */
761
- async BrightnessSet(value) {
762
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${value}`);
763
- this.Brightness = value;
764
- this.doColorBulbUpdate.next();
954
+ apiError(e) {
955
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
956
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
957
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
958
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
959
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
765
960
  }
766
- /**
767
- * Handle requests to set the value of the "ColorTemperature" characteristic
768
- */
769
- async ColorTemperatureSet(value) {
770
- this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
771
- // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
772
- const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
773
- // Check and increase/decrease kelvin to range of device
774
- const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
775
- if (!this.OnCached || this.cacheKelvin === k) {
776
- return;
961
+ FirmwareRevision(accessory, device) {
962
+ let FirmwareRevision;
963
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
964
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
965
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
966
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
967
+ if (accessory.context.FirmwareRevision) {
968
+ FirmwareRevision = accessory.context.FirmwareRevision;
777
969
  }
778
- // Updating the hue/sat to the corresponding values mimics native adaptive lighting
779
- const hs = (0, settings_1.m2hs)(value);
780
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
781
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
782
- this.ColorTemperature = value;
783
- this.doColorBulbUpdate.next();
970
+ else if (device.firmware) {
971
+ FirmwareRevision = device.firmware;
972
+ }
973
+ else {
974
+ FirmwareRevision = this.platform.version;
975
+ }
976
+ return FirmwareRevision;
784
977
  }
785
- /**
786
- * Handle requests to set the value of the "Hue" characteristic
787
- */
788
- async HueSet(value) {
789
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
790
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
791
- this.Hue = value;
792
- this.doColorBulbUpdate.next();
978
+ context() {
979
+ if (this.On === undefined) {
980
+ this.On = false;
981
+ }
982
+ else {
983
+ this.On = this.accessory.context.On;
984
+ }
985
+ if (this.Hue === undefined) {
986
+ this.Hue = 0;
987
+ }
988
+ else {
989
+ this.Hue = this.accessory.context.Hue;
990
+ }
991
+ if (this.Brightness === undefined) {
992
+ this.Brightness = 0;
993
+ }
994
+ else {
995
+ this.Brightness = this.accessory.context.Brightness;
996
+ }
997
+ if (this.Brightness === undefined) {
998
+ this.Saturation = 0;
999
+ }
1000
+ else {
1001
+ this.Saturation = this.accessory.context.Saturation;
1002
+ }
1003
+ if (this.ColorTemperature === undefined) {
1004
+ this.ColorTemperature = 140;
1005
+ }
1006
+ else {
1007
+ this.ColorTemperature = this.accessory.context.ColorTemperature;
1008
+ }
1009
+ this.minKelvin = 2000;
1010
+ this.maxKelvin = 9000;
793
1011
  }
794
- /**
795
- * Handle requests to set the value of the "Saturation" characteristic
796
- */
797
- async SaturationSet(value) {
798
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
799
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
800
- this.Saturation = value;
801
- this.doColorBulbUpdate.next();
1012
+ async refreshRate(device) {
1013
+ if (device.refreshRate) {
1014
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1015
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1016
+ }
1017
+ else if (this.platform.config.options.refreshRate) {
1018
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1019
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1020
+ }
802
1021
  }
803
1022
  async config(device) {
804
1023
  let config = {};
805
1024
  if (device.colorbulb) {
806
1025
  config = device.colorbulb;
807
1026
  }
808
- if (device.ble) {
809
- config['ble'] = device.ble;
1027
+ if (device.connectionType !== undefined) {
1028
+ config['connectionType'] = device.connectionType;
1029
+ }
1030
+ if (device.external !== undefined) {
1031
+ config['external'] = device.external;
810
1032
  }
811
1033
  if (device.logging !== undefined) {
812
1034
  config['logging'] = device.logging;
@@ -821,85 +1043,26 @@ class ColorBulb {
821
1043
  config['offline'] = device.offline;
822
1044
  }
823
1045
  if (Object.entries(config).length !== 0) {
824
- this.infoLog(`Color Bulb: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
825
- }
826
- }
827
- async refreshRate(device) {
828
- if (device.refreshRate) {
829
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
830
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
831
- }
832
- else if (this.platform.config.options.refreshRate) {
833
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
834
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
835
- }
836
- }
837
- async scan(device) {
838
- if (device.scanDuration) {
839
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
840
- if (device.ble) {
841
- this.debugLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
842
- }
843
- }
844
- else {
845
- this.scanDuration = this.accessory.context.scanDuration = 1;
846
- if (this.device.ble) {
847
- this.debugLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
848
- }
1046
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
849
1047
  }
850
1048
  }
851
1049
  async logs(device) {
852
1050
  if (this.platform.debugMode) {
853
1051
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
854
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1052
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
855
1053
  }
856
1054
  else if (device.logging) {
857
1055
  this.deviceLogging = this.accessory.context.logging = device.logging;
858
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1056
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
859
1057
  }
860
1058
  else if (this.platform.config.options?.logging) {
861
1059
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
862
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1060
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
863
1061
  }
864
1062
  else {
865
1063
  this.deviceLogging = this.accessory.context.logging = 'standard';
866
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
867
- }
868
- }
869
- FirmwareRevision(accessory, device) {
870
- let FirmwareRevision;
871
- this.debugLog(`Color Bulb: ${this.accessory.displayName} accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
872
- this.debugLog(`Color Bulb: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
873
- this.debugLog(`Color Bulb: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
874
- if (accessory.context.FirmwareRevision) {
875
- FirmwareRevision = accessory.context.FirmwareRevision;
1064
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
876
1065
  }
877
- else if (device.firmware) {
878
- FirmwareRevision = device.firmware;
879
- }
880
- else {
881
- FirmwareRevision = this.platform.version;
882
- }
883
- return FirmwareRevision;
884
- }
885
- async adaptiveLighting(device) {
886
- if (device.colorbulb?.adaptiveLightingShift) {
887
- this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
888
- this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
889
- }
890
- else {
891
- this.adaptiveLightingShift = 0;
892
- this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
893
- }
894
- }
895
- minStep(device) {
896
- if (device.colorbulb?.set_minStep) {
897
- this.set_minStep = device.colorbulb?.set_minStep;
898
- }
899
- else {
900
- this.set_minStep = 1;
901
- }
902
- return this.set_minStep;
903
1066
  }
904
1067
  /**
905
1068
  * Logging for Device
@@ -914,11 +1077,25 @@ class ColorBulb {
914
1077
  this.platform.log.warn(String(...log));
915
1078
  }
916
1079
  }
1080
+ debugWarnLog(...log) {
1081
+ if (this.enablingDeviceLogging()) {
1082
+ if (this.deviceLogging?.includes('debug')) {
1083
+ this.platform.log.warn('[DEBUG]', String(...log));
1084
+ }
1085
+ }
1086
+ }
917
1087
  errorLog(...log) {
918
1088
  if (this.enablingDeviceLogging()) {
919
1089
  this.platform.log.error(String(...log));
920
1090
  }
921
1091
  }
1092
+ debugErrorLog(...log) {
1093
+ if (this.enablingDeviceLogging()) {
1094
+ if (this.deviceLogging?.includes('debug')) {
1095
+ this.platform.log.error('[DEBUG]', String(...log));
1096
+ }
1097
+ }
1098
+ }
922
1099
  debugLog(...log) {
923
1100
  if (this.enablingDeviceLogging()) {
924
1101
  if (this.deviceLogging === 'debug') {