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

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