@switchbot/homebridge-switchbot 2.0.0-beta.3 → 2.0.0-beta.30

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