@switchbot/homebridge-switchbot 3.5.1-beta.1 → 3.5.1-beta.11

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 +203 -255
  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 +196 -218
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +9 -4
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +203 -249
  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 +329 -378
  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 +99 -142
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +19 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +203 -264
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +28 -32
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +186 -225
  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 +198 -227
  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 +106 -231
  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 +192 -232
  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 +91 -157
  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 +287 -367
  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 +171 -216
  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 +93 -158
  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 +94 -155
  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 +84 -142
  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 +99 -124
  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 +170 -253
  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 +105 -129
  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 +45 -51
  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 +17 -17
  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 +9 -9
  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 +19 -19
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +24 -11
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +90 -68
  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 +15 -17
  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 +76 -76
  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 +35 -35
  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 +9 -9
  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 +9 -9
  117. package/dist/irdevice/waterheater.js.map +1 -1
  118. package/dist/platform.d.ts +15 -15
  119. package/dist/platform.d.ts.map +1 -1
  120. package/dist/platform.js +288 -271
  121. package/dist/platform.js.map +1 -1
  122. package/dist/settings.d.ts +49 -191
  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 +10 -10
  159. package/src/device/blindtilt.ts +224 -264
  160. package/src/device/bot.ts +208 -221
  161. package/src/device/ceilinglight.ts +211 -261
  162. package/src/device/colorbulb.ts +334 -397
  163. package/src/device/contact.ts +111 -146
  164. package/src/device/curtain.ts +220 -283
  165. package/src/device/device.ts +194 -245
  166. package/src/device/fan.ts +214 -230
  167. package/src/device/hub.ts +126 -246
  168. package/src/device/humidifier.ts +216 -243
  169. package/src/device/iosensor.ts +114 -166
  170. package/src/device/lightstrip.ts +302 -377
  171. package/src/device/lock.ts +187 -221
  172. package/src/device/meter.ts +116 -177
  173. package/src/device/meterplus.ts +117 -174
  174. package/src/device/motion.ts +99 -144
  175. package/src/device/plug.ts +110 -128
  176. package/src/device/robotvacuumcleaner.ts +189 -252
  177. package/src/device/waterdetector.ts +123 -139
  178. package/src/irdevice/airconditioner.ts +47 -52
  179. package/src/irdevice/airpurifier.ts +19 -18
  180. package/src/irdevice/camera.ts +11 -10
  181. package/src/irdevice/fan.ts +21 -20
  182. package/src/irdevice/irdevice.ts +95 -70
  183. package/src/irdevice/light.ts +17 -18
  184. package/src/irdevice/other.ts +78 -77
  185. package/src/irdevice/tv.ts +37 -36
  186. package/src/irdevice/vacuumcleaner.ts +11 -10
  187. package/src/irdevice/waterheater.ts +11 -10
  188. package/src/platform.ts +290 -272
  189. package/src/settings.ts +30 -239
  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,39 @@ 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!);
241
-
242
- this.LightBulb.ColorTemperature = Number(mired);
243
-
244
- 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}`);
209
+ if (!Number.isNaN(deviceStatus.colorTemperature)) {
210
+ await this.debugLog(`OpenAPI ColorTemperature: ${deviceStatus.colorTemperature}`);
211
+ const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
212
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
213
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
246
214
  }
247
215
 
248
216
  // 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';
217
+ const version = deviceStatus.version.toString();
218
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
219
+ if (deviceStatus.version) {
220
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
253
221
  this.accessory
254
222
  .getService(this.hap.Service.AccessoryInformation)!
255
223
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -257,29 +225,43 @@ export class CeilingLight extends deviceBase {
257
225
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
258
226
  .updateValue(deviceVersion);
259
227
  this.accessory.context.deviceVersion = deviceVersion;
260
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
228
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
261
229
  }
262
230
  }
263
231
 
232
+ async parseStatusWebhook(context: ceilingLightWebhookContext | ceilingLightProWebhookContext): Promise<void> {
233
+ await this.debugLog('parseStatusWebhook');
234
+ await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness},`
235
+ + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
236
+ // On
237
+ this.LightBulb.On = context.powerState === 'ON' ? true : false;
238
+ await this.debugLog(`On: ${this.LightBulb.On}`);
239
+ // Brightness
240
+ this.LightBulb.Brightness = context.brightness;
241
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
242
+ // ColorTemperature
243
+ this.LightBulb.ColorTemperature = Math.max(Math.min(context.colorTemperature, 500), 140);
244
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
245
+ }
246
+
264
247
  /**
265
248
  * Asks the SwitchBot API for the latest device information
266
249
  */
267
250
  async refreshStatus(): Promise<void> {
268
251
  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();*/
252
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
253
+ } else if (this.BLE) {
254
+ await this.BLERefreshStatus();
272
255
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
273
256
  await this.openAPIRefreshStatus();
274
257
  } else {
275
258
  await this.offlineOff();
276
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
277
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
259
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
278
260
  }
279
261
  }
280
262
 
281
263
  async BLERefreshStatus(): Promise<void> {
282
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
264
+ await this.debugLog('BLERefreshStatus');
283
265
  const switchbot = await this.switchbotBLE();
284
266
 
285
267
  if (switchbot === undefined) {
@@ -287,13 +269,14 @@ export class CeilingLight extends deviceBase {
287
269
  } else {
288
270
  (async () => {
289
271
  // Start to monitor advertisement packets
290
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
272
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
291
273
  // Update HomeKit
292
- if (serviceData.model !== '' && serviceData.modelName !== '') {
274
+ if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
275
+ && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
293
276
  await this.BLEparseStatus(serviceData);
294
277
  await this.updateHomeKitCharacteristics();
295
278
  } else {
296
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
279
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
297
280
  await this.BLERefreshConnection(switchbot);
298
281
  }
299
282
  })();
@@ -301,68 +284,39 @@ export class CeilingLight extends deviceBase {
301
284
  }
302
285
 
303
286
  async openAPIRefreshStatus(): Promise<void> {
304
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
287
+ await this.debugLog('openAPIRefreshStatus');
305
288
  try {
306
289
  const { body, statusCode } = await this.deviceRefreshStatus();
307
290
  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);
291
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
292
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
293
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
294
+ await this.openAPIparseStatus(deviceStatus.body);
312
295
  await this.updateHomeKitCharacteristics();
313
296
  } else {
314
- await this.statusCodes(statusCode, deviceStatus);
297
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
298
+ await this.debugWarnLog(statusCode, deviceStatus);
315
299
  }
316
300
  } catch (e: any) {
317
301
  await this.apiError(e);
318
- await this.openAPIRefreshError(e);
302
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
319
303
  }
320
304
  }
321
305
 
322
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
323
- if (device.webhook) {
324
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
325
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
306
+ async registerWebhook() {
307
+ if (this.device.webhook) {
308
+ await this.debugLog('is listening webhook.');
309
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
326
310
  try {
327
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
328
- const { powerState, brightness, colorTemperature } = context;
329
- const { On, Brightness, ColorTemperature } = this.LightBulb;
330
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
331
- '(powerState, brightness, colorTemperature) = ' +
332
- `Webhook:(${powerState}, ${brightness}, ${colorTemperature}), `
333
- + `current:(${On}, ${Brightness}, ${ColorTemperature})`);
334
-
335
- // On
336
- this.LightBulb.On = powerState === 'ON' ? true : false;
337
- if (accessory.context.Brightness !== this.LightBulb.On) {
338
- this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
339
- } else {
340
- this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
341
- }
342
-
343
- // Brightness
344
- this.LightBulb.Brightness = brightness;
345
- if (accessory.context.Brightness !== this.LightBulb.Brightness) {
346
- this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
347
- } else {
348
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
349
- }
350
-
351
- // ColorTemperature
352
- this.LightBulb.ColorTemperature = colorTemperature;
353
- if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
354
- this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
355
- } else {
356
- this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
357
- }
358
- this.updateHomeKitCharacteristics();
311
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
312
+ await this.parseStatusWebhook(context);
313
+ await this.updateHomeKitCharacteristics();
359
314
  } catch (e: any) {
360
- this.errorLog(`${device.deviceType}: ${accessory.displayName} `
361
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
315
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
362
316
  }
363
317
  };
364
318
  } else {
365
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
319
+ await this.debugLog('is not listening webhook.');
366
320
  }
367
321
  }
368
322
 
@@ -379,14 +333,28 @@ export class CeilingLight extends deviceBase {
379
333
  */
380
334
  async pushChanges(): Promise<void> {
381
335
  if (!this.device.enableCloudService && this.OpenAPI) {
382
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
336
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
383
337
  } else if (this.BLE) {
384
338
  await this.BLEpushChanges();
385
339
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
386
340
  await this.openAPIpushChanges();
341
+ if (this.LightBulb.On) {
342
+ await this.debugLog(`On: ${this.LightBulb.On}`);
343
+ // Push Brightness Update
344
+ await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
345
+ await this.pushBrightnessChanges();
346
+ // Push ColorTemperature Update
347
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
348
+ await this.pushColorTemperatureChanges();
349
+ // Push Hue & Saturation Update
350
+ await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
351
+ await this.pushHueSaturationChanges();
352
+ } else {
353
+ await this.debugLog('BLE (Brightness), (ColorTemperature), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
354
+ }
387
355
  } else {
388
356
  await this.offlineOff();
389
- await this.pushChangeDisabled();
357
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
390
358
  }
391
359
  // Refresh the status from the API
392
360
  interval(15000)
@@ -398,134 +366,112 @@ export class CeilingLight extends deviceBase {
398
366
  }
399
367
 
400
368
  async BLEpushChanges(): Promise<void> {
401
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
369
+ await this.debugLog('BLEpushChanges');
402
370
  if (this.LightBulb.On !== this.accessory.context.On) {
403
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
404
- + ` BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
405
- const switchbot = await this.platform.connectBLE();
406
- // Convert to BLE Address
371
+ await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
372
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
407
373
  await this.convertBLEAddress();
408
- switchbot
409
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
410
- .then(async (device_list: any) => {
411
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
412
- return await this.retryBLE({
413
- max: await this.maxRetryBLE(),
414
- fn: async () => {
415
- if (this.LightBulb.On) {
416
- return await device_list[0].turnOn({ id: this.device.bleMac });
417
- } else {
418
- return await device_list[0].turnOff({ id: this.device.bleMac });
419
- }
420
- },
374
+ if (switchbot !== false) {
375
+ switchbot
376
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
377
+ .then(async (device_list: any) => {
378
+ this.infoLog(`On: ${this.LightBulb.On}`);
379
+ return await this.retryBLE({
380
+ max: await this.maxRetryBLE(),
381
+ fn: async () => {
382
+ if (this.LightBulb.On) {
383
+ return await device_list[0].turnOn({ id: this.device.bleMac });
384
+ } else {
385
+ return await device_list[0].turnOff({ id: this.device.bleMac });
386
+ }
387
+ },
388
+ });
389
+ })
390
+ .then(async () => {
391
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`);
392
+ this.LightBulb.On = false;
393
+ })
394
+ .catch(async (e: any) => {
395
+ await this.apiError(e);
396
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
397
+ await this.BLEPushConnection();
421
398
  });
422
- })
423
- .then(() => {
424
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
425
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
426
- + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
427
- this.LightBulb.On = false;
428
- })
429
- .catch(async (e: any) => {
430
- this.apiError(e);
431
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
432
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
433
- await this.BLEPushConnection();
434
- });
399
+ } else {
400
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
401
+ await this.BLEPushConnection();
402
+ }
435
403
  } else {
436
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
437
- + ` No BLEpushChanges: On: ${this.LightBulb.On}, `
438
- + `OnCached: ${this.accessory.context.On}`);
404
+ await this.debugLog(`No changes (BLEpushChanges): On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
439
405
  }
440
406
  }
441
407
 
442
408
  async openAPIpushChanges(): Promise<void> {
443
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
409
+ await this.debugLog('openAPIpushChanges');
444
410
  if (this.LightBulb.On !== this.accessory.context.On) {
445
- let command = '';
446
- if (this.LightBulb.On) {
447
- command = 'turnOn';
448
- } else {
449
- command = 'turnOff';
450
- }
411
+ const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
451
412
  const bodyChange = JSON.stringify({
452
413
  command: `${command}`,
453
414
  parameter: 'default',
454
415
  commandType: 'command',
455
416
  });
456
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
417
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
457
418
  try {
458
419
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
459
420
  const deviceStatus: any = await body.json();
460
- await this.pushStatusCodes(statusCode, deviceStatus);
461
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
462
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
421
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
422
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
423
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
463
424
  await this.updateHomeKitCharacteristics();
464
425
  } else {
465
426
  await this.statusCode(statusCode);
466
427
  await this.statusCode(deviceStatus.statusCode);
467
428
  }
468
429
  } catch (e: any) {
469
- this.apiError(e);
470
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
471
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
430
+ await this.apiError(e);
431
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
472
432
  }
473
433
  } else {
474
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
475
- + `On: ${this.LightBulb.On}, `
476
- + `OnCached: ${this.accessory.context.On}`);
477
- }
478
- // Push Hue & Saturation Update
479
- if (this.LightBulb.On) {
480
- await this.pushHueSaturationChanges();
481
- }
482
- // Push ColorTemperature Update
483
- if (this.LightBulb.On) {
484
- await this.pushColorTemperatureChanges();
485
- }
486
- // Push Brightness Update
487
- if (this.LightBulb.On) {
488
- await this.pushBrightnessChanges();
434
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
489
435
  }
490
436
  }
491
437
 
492
438
  async pushHueSaturationChanges(): Promise<void> {
493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
494
- if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
495
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
496
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
439
+ await this.debugLog('pushHueSaturationChanges');
440
+ if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
441
+ await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
442
+ await this.debugLog(`Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
497
443
  const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
498
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
444
+ await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
499
445
  const bodyChange = JSON.stringify({
500
446
  command: 'setColor',
501
447
  parameter: `${red}:${green}:${blue}`,
502
448
  commandType: 'command',
503
449
  });
504
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
450
+ await this.debugLog(`(pushHueSaturationChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
505
451
  try {
506
452
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
507
453
  const deviceStatus: any = await body.json();
508
- await this.pushStatusCodes(statusCode, deviceStatus);
509
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
510
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
454
+ await this.debugLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
455
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
456
+ await this.debugSuccessLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
511
457
  await this.updateHomeKitCharacteristics();
512
458
  } else {
513
459
  await this.statusCode(statusCode);
514
460
  await this.statusCode(deviceStatus.statusCode);
515
461
  }
516
462
  } catch (e: any) {
517
- this.apiError(e);
518
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
519
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
463
+ await this.apiError(e);
464
+ await this.errorLog(`failed pushHueSaturationChanges with ${this.device.connectionType} Connection,`
465
+ + ` Error Message: ${JSON.stringify(e.message)}`);
520
466
  }
521
467
  } else {
522
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
523
- + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
468
+ await this.debugLog(`No changes (pushHueSaturationChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
469
+ + ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
524
470
  }
525
471
  }
526
472
 
527
473
  async pushColorTemperatureChanges(): Promise<void> {
528
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
474
+ await this.debugLog('pushColorTemperatureChanges');
529
475
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
530
476
  const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
531
477
  this.accessory.context.kelvin = kelvin;
@@ -534,58 +480,57 @@ export class CeilingLight extends deviceBase {
534
480
  parameter: `${kelvin}`,
535
481
  commandType: 'command',
536
482
  });
537
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
483
+ await this.debugLog(`(pushColorTemperatureChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
538
484
  try {
539
485
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
540
486
  const deviceStatus: any = await body.json();
541
- await this.pushStatusCodes(statusCode, deviceStatus);
542
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
543
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
487
+ await this.debugLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
488
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
489
+ await this.debugSuccessLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
544
490
  await this.updateHomeKitCharacteristics();
545
491
  } else {
546
492
  await this.statusCode(statusCode);
547
493
  await this.statusCode(deviceStatus.statusCode);
548
494
  }
549
495
  } catch (e: any) {
550
- this.apiError(e);
551
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with`
552
- + ` ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
496
+ await this.apiError(e);
497
+ await this.errorLog(`failed pushColorTemperatureChanges with ${this.device.connectionType} Connection,`
498
+ + ` Error Message: ${JSON.stringify(e.message)}`);
553
499
  }
554
500
  } else {
555
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
556
- + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
501
+ await this.debugLog(`No changes (pushColorTemperatureChanges), ColorTemperature: ${this.LightBulb.ColorTemperature},`
502
+ + ` ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
557
503
  }
558
504
  }
559
505
 
560
506
  async pushBrightnessChanges(): Promise<void> {
561
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
507
+ await this.debugLog('pushBrightnessChanges');
562
508
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
563
509
  const bodyChange = JSON.stringify({
564
510
  command: 'setBrightness',
565
511
  parameter: `${this.LightBulb.Brightness}`,
566
512
  commandType: 'command',
567
513
  });
568
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
514
+ await this.debugLog(`(pushBrightnessChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
569
515
  try {
570
516
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
571
517
  const deviceStatus: any = await body.json();
572
- await this.pushStatusCodes(statusCode, deviceStatus);
573
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
574
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
518
+ await this.debugLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
519
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
520
+ await this.debugSuccessLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
575
521
  await this.updateHomeKitCharacteristics();
576
522
  } else {
577
523
  await this.statusCode(statusCode);
578
524
  await this.statusCode(deviceStatus.statusCode);
579
525
  }
580
526
  } catch (e: any) {
581
- this.apiError(e);
582
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
583
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
527
+ await this.apiError(e);
528
+ await this.errorLog(`failed pushBrightnessChanges with ${this.device.connectionType} Connection,`
529
+ + ` Error Message: ${JSON.stringify(e.message)}`);
584
530
  }
585
531
  } else {
586
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.`
587
- + `Brightness: ${this.LightBulb.Brightness}, `
588
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
532
+ await this.debugLog(`No changes (pushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
533
+ + ` BrightnessCached: ${this.accessory.context.Brightness}`);
589
534
  }
590
535
  }
591
536
 
@@ -593,10 +538,10 @@ export class CeilingLight extends deviceBase {
593
538
  * Handle requests to set the value of the "On" characteristic
594
539
  */
595
540
  async OnSet(value: CharacteristicValue): Promise<void> {
596
- if (this.LightBulb.On === this.accessory.context.On) {
597
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
541
+ if (this.LightBulb.On !== this.accessory.context.On) {
542
+ await this.infoLog(`Set On: ${value}`);
598
543
  } else {
599
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
544
+ await this.debugLog(`No Changes, Set On: ${value}`);
600
545
  }
601
546
 
602
547
  this.LightBulb.On = value;
@@ -607,14 +552,15 @@ export class CeilingLight extends deviceBase {
607
552
  * Handle requests to set the value of the "Brightness" characteristic
608
553
  */
609
554
  async BrightnessSet(value: CharacteristicValue): Promise<void> {
610
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
611
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
612
- } else if (this.LightBulb.On) {
613
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
555
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
556
+ await this.infoLog(`Set Brightness: ${value}`);
614
557
  } else {
615
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
558
+ if (this.LightBulb.On) {
559
+ this.debugLog(`No Changes, Brightness: ${value}`);
560
+ } else {
561
+ this.debugLog(`Set Brightness: ${value}, On: ${this.LightBulb.On}`);
562
+ }
616
563
  }
617
-
618
564
  this.LightBulb.Brightness = value;
619
565
  this.doCeilingLightUpdate.next();
620
566
  }
@@ -623,12 +569,14 @@ export class CeilingLight extends deviceBase {
623
569
  * Handle requests to set the value of the "ColorTemperature" characteristic
624
570
  */
625
571
  async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
626
- if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
627
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
628
- } else if (this.LightBulb.On) {
629
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
572
+ if (this.LightBulb.On && (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature)) {
573
+ this.infoLog(`Set ColorTemperature: ${value}`);
630
574
  } else {
631
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
575
+ if (this.LightBulb.On) {
576
+ this.debugLog(`No Changes, ColorTemperature: ${value}`);
577
+ } else {
578
+ this.debugLog(`Set ColorTemperature: ${value}, On: ${this.LightBulb.On}`);
579
+ }
632
580
  }
633
581
 
634
582
  const minKelvin = 2000;
@@ -656,12 +604,14 @@ export class CeilingLight extends deviceBase {
656
604
  * Handle requests to set the value of the "Hue" characteristic
657
605
  */
658
606
  async HueSet(value: CharacteristicValue): Promise<void> {
659
- if (this.LightBulb.Hue === this.accessory.context.Hue) {
660
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
661
- } else if (this.LightBulb.On) {
662
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
607
+ if (this.LightBulb.On && (this.LightBulb.Hue !== this.accessory.context.Hue)) {
608
+ this.infoLog(`Set Hue: ${value}`);
663
609
  } else {
664
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
610
+ if (this.LightBulb.On) {
611
+ this.debugLog(`No Changes, Hue: ${value}`);
612
+ } else {
613
+ this.debugLog(`Set Hue: ${value}, On: ${this.LightBulb.On}`);
614
+ }
665
615
  }
666
616
 
667
617
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
@@ -674,12 +624,14 @@ export class CeilingLight extends deviceBase {
674
624
  * Handle requests to set the value of the "Saturation" characteristic
675
625
  */
676
626
  async SaturationSet(value: CharacteristicValue): Promise<void> {
677
- if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
678
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
679
- } else if (this.LightBulb.On) {
680
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
627
+ if (this.LightBulb.On && (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
628
+ this.infoLog(`Set Saturation: ${value}`);
681
629
  } else {
682
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
630
+ if (this.LightBulb.On) {
631
+ this.debugLog(`No Changes, Saturation: ${value}`);
632
+ } else {
633
+ this.debugLog(`Set Saturation: ${value}, On: ${this.LightBulb.On}`);
634
+ }
683
635
  }
684
636
 
685
637
  this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
@@ -709,25 +661,23 @@ export class CeilingLight extends deviceBase {
709
661
  async adaptiveLighting(device: device & devicesConfig): Promise<void> {
710
662
  if (device.ceilinglight?.adaptiveLightingShift) {
711
663
  this.adaptiveLightingShift = device.ceilinglight.adaptiveLightingShift;
712
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
713
664
  } else {
714
665
  this.adaptiveLightingShift = 0;
715
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
716
666
  }
667
+ await this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
717
668
  }
718
669
 
719
670
  async BLEPushConnection() {
720
671
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
721
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
672
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
722
673
  await this.openAPIpushChanges();
723
674
  }
724
675
  }
725
676
 
726
677
  async BLERefreshConnection(switchbot: any): Promise<void> {
727
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
728
- + ` ${JSON.stringify(switchbot)}`);
678
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
729
679
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
730
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
680
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
731
681
  await this.openAPIRefreshStatus();
732
682
  }
733
683
  }
@@ -739,10 +689,10 @@ export class CeilingLight extends deviceBase {
739
689
  }
740
690
 
741
691
  async apiError(e: any): Promise<void> {
742
- this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.On, e);
743
- this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
744
- this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
745
- this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
746
- this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
692
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
693
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
694
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
695
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
696
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
747
697
  }
748
698
  }