@switchbot/homebridge-switchbot 2.0.0-beta.2 → 2.0.0-beta.21

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