@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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 (177) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +164 -22
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +457 -866
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +851 -913
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +316 -742
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +380 -833
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +267 -602
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +469 -897
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +76 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +846 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +645 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +19 -58
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +304 -640
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +300 -710
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +225 -643
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +371 -742
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +309 -642
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +223 -638
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +216 -644
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +209 -588
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +141 -532
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +252 -609
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +179 -521
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +337 -482
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +108 -333
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +47 -299
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +95 -364
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +46 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +393 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +121 -376
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +441 -584
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +94 -353
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +48 -299
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +50 -305
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +66 -18
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +95 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +657 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +488 -937
  143. package/src/device/bot.ts +907 -1016
  144. package/src/device/ceilinglight.ts +330 -798
  145. package/src/device/colorbulb.ts +394 -904
  146. package/src/device/contact.ts +298 -637
  147. package/src/device/curtain.ts +496 -954
  148. package/src/device/device.ts +872 -0
  149. package/src/device/fan.ts +692 -0
  150. package/src/device/hub.ts +338 -671
  151. package/src/device/humidifier.ts +330 -777
  152. package/src/device/iosensor.ts +258 -671
  153. package/src/device/lightstrip.ts +394 -809
  154. package/src/device/lock.ts +345 -700
  155. package/src/device/meter.ts +256 -666
  156. package/src/device/meterplus.ts +248 -672
  157. package/src/device/motion.ts +234 -622
  158. package/src/device/plug.ts +152 -572
  159. package/src/device/robotvacuumcleaner.ts +273 -653
  160. package/src/device/waterdetector.ts +198 -549
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +368 -540
  165. package/src/irdevice/airpurifier.ts +134 -369
  166. package/src/irdevice/camera.ts +60 -324
  167. package/src/irdevice/fan.ts +111 -392
  168. package/src/irdevice/irdevice.ts +406 -0
  169. package/src/irdevice/light.ts +159 -425
  170. package/src/irdevice/other.ts +509 -661
  171. package/src/irdevice/tv.ts +123 -388
  172. package/src/irdevice/vacuumcleaner.ts +61 -324
  173. package/src/irdevice/waterheater.ts +63 -333
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +120 -610
  176. package/src/utils.ts +661 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,122 +1,86 @@
1
- import { request } from 'undici';
2
- import { sleep } from '../utils.js';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * ceilinglight.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
3
6
  import { interval, Subject } from 'rxjs';
7
+ import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
4
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
- import { hs2rgb, rgb2hs, m2hs, Devices } from '../settings.js';
6
9
  /**
7
10
  * Platform Accessory
8
11
  * An instance of this class is created for each accessory your platform registers
9
12
  * Each accessory may expose multiple services of different service types.
10
13
  */
11
- export class CeilingLight {
14
+ export class CeilingLight extends deviceBase {
12
15
  platform;
13
- accessory;
14
- device;
15
- api;
16
- log;
17
- config;
18
- hap;
19
16
  // Services
20
- lightBulbService;
21
- // Characteristic Values
22
- On;
23
- Hue;
24
- Saturation;
25
- Brightness;
26
- ColorTemperature;
27
- FirmwareRevision;
28
- // OpenAPI Status
29
- OpenAPI_On;
30
- OpenAPI_RGB;
31
- OpenAPI_Brightness;
32
- OpenAPI_FirmwareRevision;
33
- OpenAPI_ColorTemperature;
34
- // BLE Status
35
- BLE_Delay;
36
- BLE_On;
37
- BLE_WifiRssi;
38
- // BLE Others
39
- BLE_IsConnected;
40
- // Config
41
- set_minStep;
42
- scanDuration;
43
- deviceLogging;
44
- deviceRefreshRate;
45
- adaptiveLightingShift;
17
+ LightBulb;
46
18
  // Adaptive Lighting
47
19
  AdaptiveLightingController;
48
- minKelvin;
49
- maxKelvin;
50
- // Others
51
- cacheKelvin;
20
+ adaptiveLightingShift;
52
21
  // Updates
53
22
  ceilingLightUpdateInProgress;
54
23
  doCeilingLightUpdate;
55
- // Connection
56
- OpenAPI;
57
- BLE;
58
24
  constructor(platform, accessory, device) {
25
+ super(platform, accessory, device);
59
26
  this.platform = platform;
60
- this.accessory = accessory;
61
- this.device = device;
62
- this.api = this.platform.api;
63
- this.log = this.platform.log;
64
- this.config = this.platform.config;
65
- this.hap = this.api.hap;
66
- // Connection
67
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
68
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
69
27
  // default placeholders
70
- this.deviceLogs(device);
71
- this.scan(device);
72
- this.refreshRate(device);
73
28
  this.adaptiveLighting(device);
74
- this.deviceContext();
75
- this.deviceConfig(device);
76
29
  // this is subject we use to track when we need to POST changes to the SwitchBot API
77
30
  this.doCeilingLightUpdate = new Subject();
78
31
  this.ceilingLightUpdateInProgress = false;
79
- // Retrieve initial values and updateHomekit
80
- this.refreshStatus();
81
- // set accessory information
82
- accessory
83
- .getService(this.hap.Service.AccessoryInformation)
84
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
85
- .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
86
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
87
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
88
- // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
89
- // you can create multiple services for each accessory
90
- const lightBulbService = `${accessory.displayName} ${device.deviceType}`;
91
- (this.lightBulbService = accessory.getService(this.hap.Service.Lightbulb)
92
- || accessory.addService(this.hap.Service.Lightbulb)), lightBulbService;
93
- if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
94
- this.accessory.removeService(this.lightBulbService);
95
- this.lightBulbService = this.accessory.addService(this.hap.Service.Lightbulb);
96
- this.accessory.context.adaptiveLighting = false;
97
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
32
+ // Initialize LightBulb Service
33
+ accessory.context.LightBulb = accessory.context.LightBulb ?? {};
34
+ this.LightBulb = {
35
+ Name: accessory.context.LightBul.bName ?? accessory.displayName,
36
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb),
37
+ On: accessory.context.On ?? false,
38
+ Hue: accessory.context.Hue ?? 0,
39
+ Saturation: accessory.context.Saturation ?? 0,
40
+ Brightness: accessory.context.Brightness ?? 0,
41
+ ColorTemperature: accessory.context.ColorTemperature ?? 140,
42
+ };
43
+ accessory.context.LightBulb = this.LightBulb;
44
+ // Adaptive Lighting
45
+ if (this.adaptiveLightingShift === -1 && accessory.context.adaptiveLighting) {
46
+ accessory.removeService(this.LightBulb.Service);
47
+ this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
48
+ accessory.context.adaptiveLighting = false;
49
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting}`);
98
50
  }
99
- this.lightBulbService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
100
- if (!this.lightBulbService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
101
- this.lightBulbService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
51
+ if (this.adaptiveLightingShift !== -1) {
52
+ this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
53
+ customTemperatureAdjustment: this.adaptiveLightingShift,
54
+ });
55
+ this.accessory.configureController(this.AdaptiveLightingController);
56
+ this.accessory.context.adaptiveLighting = true;
57
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
58
+ + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
102
59
  }
103
- // handle on / off events using the On characteristic
104
- this.lightBulbService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
105
- // handle Brightness events using the Brightness characteristic
106
- this.lightBulbService
60
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
61
+ // Initialize LightBulb Characteristics
62
+ this.LightBulb.Service
63
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
64
+ .getCharacteristic(this.hap.Characteristic.On)
65
+ .onGet(() => {
66
+ return this.LightBulb.On;
67
+ })
68
+ .onSet(this.OnSet.bind(this));
69
+ // Initialize LightBulb Brightness
70
+ this.LightBulb.Service
107
71
  .getCharacteristic(this.hap.Characteristic.Brightness)
108
72
  .setProps({
109
- minStep: this.minStep(device),
73
+ minStep: device.ceilinglight?.set_minStep ?? 1,
110
74
  minValue: 0,
111
75
  maxValue: 100,
112
76
  validValueRanges: [0, 100],
113
77
  })
114
78
  .onGet(() => {
115
- return this.Brightness;
79
+ return this.LightBulb.Brightness;
116
80
  })
117
81
  .onSet(this.BrightnessSet.bind(this));
118
- // handle ColorTemperature events using the ColorTemperature characteristic
119
- this.lightBulbService
82
+ // Initialize LightBulb ColorTemperature
83
+ this.LightBulb.Service
120
84
  .getCharacteristic(this.hap.Characteristic.ColorTemperature)
121
85
  .setProps({
122
86
  minValue: 140,
@@ -124,11 +88,11 @@ export class CeilingLight {
124
88
  validValueRanges: [140, 500],
125
89
  })
126
90
  .onGet(() => {
127
- return this.ColorTemperature;
91
+ return this.LightBulb.ColorTemperature;
128
92
  })
129
93
  .onSet(this.ColorTemperatureSet.bind(this));
130
- // handle Hue events using the Hue characteristic
131
- this.lightBulbService
94
+ // Initialize LightBulb Hue
95
+ this.LightBulb.Service
132
96
  .getCharacteristic(this.hap.Characteristic.Hue)
133
97
  .setProps({
134
98
  minValue: 0,
@@ -136,11 +100,11 @@ export class CeilingLight {
136
100
  validValueRanges: [0, 360],
137
101
  })
138
102
  .onGet(() => {
139
- return this.Hue;
103
+ return this.LightBulb.Hue;
140
104
  })
141
105
  .onSet(this.HueSet.bind(this));
142
- // handle Hue events using the Hue characteristic
143
- this.lightBulbService
106
+ // Initialize LightBulb Saturation
107
+ this.LightBulb.Service
144
108
  .getCharacteristic(this.hap.Characteristic.Saturation)
145
109
  .setProps({
146
110
  minValue: 0,
@@ -148,19 +112,11 @@ export class CeilingLight {
148
112
  validValueRanges: [0, 100],
149
113
  })
150
114
  .onGet(() => {
151
- return this.Saturation;
115
+ return this.LightBulb.Saturation;
152
116
  })
153
117
  .onSet(this.SaturationSet.bind(this));
154
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
155
- if (this.adaptiveLightingShift !== -1) {
156
- this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.lightBulbService, {
157
- customTemperatureAdjustment: this.adaptiveLightingShift,
158
- });
159
- this.accessory.configureController(this.AdaptiveLightingController);
160
- this.accessory.context.adaptiveLighting = true;
161
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
162
- ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
163
- }
118
+ // Retrieve initial values and updateHomekit
119
+ this.refreshStatus();
164
120
  // Update Homekit
165
121
  this.updateHomeKitCharacteristics();
166
122
  // Start an update interval
@@ -170,128 +126,95 @@ export class CeilingLight {
170
126
  await this.refreshStatus();
171
127
  });
172
128
  //regisiter webhook event handler
173
- if (this.device.webhook) {
174
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
175
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
176
- try {
177
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
178
- const { powerState, brightness, colorTemperature } = context;
179
- const { On, Brightness, ColorTemperature } = this;
180
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
181
- '(powerState, brightness, colorTemperature) = ' +
182
- `Webhook:(${powerState}, ${brightness}, ${colorTemperature}), ` +
183
- `current:(${On}, ${Brightness}, ${ColorTemperature})`);
184
- this.On = powerState === 'ON' ? true : false;
185
- this.Brightness = brightness;
186
- this.ColorTemperature = colorTemperature;
187
- this.updateHomeKitCharacteristics();
188
- }
189
- catch (e) {
190
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
191
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
192
- }
193
- };
194
- }
129
+ this.registerWebhook(accessory, device);
195
130
  // Watch for Bulb change events
196
131
  // We put in a debounce of 100ms so we don't make duplicate calls
197
132
  this.doCeilingLightUpdate
198
133
  .pipe(tap(() => {
199
134
  this.ceilingLightUpdateInProgress = true;
200
- }), debounceTime(this.platform.config.options.pushRate * 1000))
135
+ }), debounceTime(this.devicePushRate * 1000))
201
136
  .subscribe(async () => {
202
137
  try {
203
138
  await this.pushChanges();
204
139
  }
205
140
  catch (e) {
206
141
  this.apiError(e);
207
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,` +
208
- ` Error Message: ${JSON.stringify(e.message)}`);
142
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
143
+ + ` Error Message: ${JSON.stringify(e.message)}`);
209
144
  }
210
145
  this.ceilingLightUpdateInProgress = false;
211
146
  });
212
147
  }
213
- model(device) {
214
- let model;
215
- if (device.deviceType === 'Ceiling Light') {
216
- model = 'W2612230' || 'W2612240';
217
- }
218
- else if (device.deviceType === 'Ceiling Light Pro') {
219
- model = 'W2612210' || 'W2612220';
220
- }
221
- else {
222
- model = 'unknown';
223
- }
224
- return model;
225
- }
226
148
  /**
227
- * Parse the device status from the SwitchBot api
149
+ * Parse the device status from the SwitchBotBLE API
228
150
  */
229
- async parseStatus() {
230
- if (!this.device.enableCloudService && this.OpenAPI) {
231
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
232
- }
233
- /*if (this.BLE) {
234
- await this.BLEparseStatus();
235
- } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
236
- await this.openAPIparseStatus();
237
- }
238
- else {
239
- await this.offlineOff();
240
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
241
- }
242
- }
243
- async BLEparseStatus() {
151
+ async BLEparseStatus(serviceData) {
244
152
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
245
153
  // State
246
- switch (this.BLE_On) {
154
+ switch (serviceData.state) {
247
155
  case 'on':
248
- this.On = true;
156
+ this.LightBulb.On = true;
249
157
  break;
250
158
  default:
251
- this.On = false;
159
+ this.LightBulb.On = false;
252
160
  }
253
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
161
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
254
162
  }
255
- async openAPIparseStatus() {
163
+ /**
164
+ * Parse the device status from the SwitchBot OpenAPI
165
+ */
166
+ async openAPIparseStatus(deviceStatus) {
256
167
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
257
- switch (this.OpenAPI_On) {
168
+ switch (deviceStatus.body.power) {
258
169
  case 'on':
259
- this.On = true;
170
+ this.LightBulb.On = true;
260
171
  break;
261
172
  default:
262
- this.On = false;
173
+ this.LightBulb.On = false;
263
174
  }
264
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
175
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
265
176
  // Brightness
266
- this.Brightness = Number(this.OpenAPI_Brightness);
267
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
177
+ this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
178
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
268
179
  // Color, Hue & Brightness
269
- if (this.OpenAPI_RGB) {
270
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(this.OpenAPI_RGB)}`);
271
- const [red, green, blue] = this.OpenAPI_RGB.split(':');
180
+ if (deviceStatus.body.color) {
181
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
182
+ const [red, green, blue] = deviceStatus.body.color.split(':');
272
183
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
273
184
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
274
185
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
275
186
  const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
276
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
187
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
188
+ + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
277
189
  // Hue
278
- this.Hue = hue;
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
190
+ this.LightBulb.Hue = hue;
191
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
280
192
  // Saturation
281
- this.Saturation = saturation;
282
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
193
+ this.LightBulb.Saturation = saturation;
194
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
283
195
  }
284
196
  // ColorTemperature
285
- if (!Number.isNaN(this.OpenAPI_ColorTemperature)) {
286
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.OpenAPI_ColorTemperature}`);
287
- const mired = Math.round(1000000 / this.OpenAPI_ColorTemperature);
288
- this.ColorTemperature = Number(mired);
289
- this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
290
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
197
+ if (!Number.isNaN(deviceStatus.body.colorTemperature)) {
198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI ColorTemperature: ${deviceStatus.body.colorTemperature}`);
199
+ const mired = Math.round(1000000 / deviceStatus.body.colorTemperature);
200
+ this.LightBulb.ColorTemperature = Number(mired);
201
+ this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
202
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
203
+ }
204
+ // Firmware Version
205
+ const version = deviceStatus.body.version?.toString();
206
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
207
+ if (deviceStatus.body.version) {
208
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
209
+ this.accessory
210
+ .getService(this.hap.Service.AccessoryInformation)
211
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
212
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
213
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
214
+ .updateValue(deviceVersion);
215
+ this.accessory.context.deviceVersion = deviceVersion;
216
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
291
217
  }
292
- // FirmwareRevision
293
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
294
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
295
218
  }
296
219
  /**
297
220
  * Asks the SwitchBot API for the latest device information
@@ -307,138 +230,100 @@ export class CeilingLight {
307
230
  }
308
231
  else {
309
232
  await this.offlineOff();
310
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
311
- ` ${this.device.connectionType}, refreshStatus will not happen.`);
233
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
234
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
312
235
  }
313
236
  }
314
237
  async BLERefreshStatus() {
315
238
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
316
- const switchbot = await this.platform.connectBLE();
317
- // Convert to BLE Address
318
- this.device.bleMac = this.device
319
- .deviceId.match(/.{1,2}/g)
320
- .join(':')
321
- .toLowerCase();
322
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
323
- this.getCustomBLEAddress(switchbot);
324
- // Start to monitor advertisement packets
325
- (async () => {
326
- // Start to monitor advertisement packets
327
- await switchbot.startScan({
328
- id: this.device.bleMac,
329
- });
330
- // Set an event handler
331
- switchbot.onadvertisement = (ad) => {
332
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
333
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
334
- if (this.device.bleMac === ad.address && ad.model === 's') {
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
336
- this.BLE_On = ad.serviceData.state;
337
- //this.delay = ad.serviceData.delay;
338
- //this.timer = ad.serviceData.timer;
339
- //this.syncUtcTime = ad.serviceData.syncUtcTime;
340
- //this.wifiRssi = ad.serviceData.wifiRssi;
341
- //this.overload = ad.serviceData.overload;
342
- //this.currentPower = ad.serviceData.currentPower;
239
+ const switchbot = await this.switchbotBLE();
240
+ if (switchbot === undefined) {
241
+ await this.BLERefreshConnection(switchbot);
242
+ }
243
+ else {
244
+ (async () => {
245
+ // Start to monitor advertisement packets
246
+ const serviceData = await this.monitorAdvertisementPackets(switchbot);
247
+ // Update HomeKit
248
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
249
+ await this.BLEparseStatus(serviceData);
250
+ await this.updateHomeKitCharacteristics();
343
251
  }
344
252
  else {
345
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
346
- }
347
- };
348
- // Wait 1 seconds
349
- await switchbot.wait(this.scanDuration * 1000);
350
- // Stop to monitor
351
- await switchbot.stopScan();
352
- // Update HomeKit
353
- await this.BLEparseStatus();
354
- await this.updateHomeKitCharacteristics();
355
- })();
356
- /*if (switchbot !== false) {
357
- switchbot
358
- .startScan({
359
- model: '',
360
- id: this.device.bleMac,
361
- })
362
- .then(async () => {
363
- // Set an event handler
364
- switchbot.onadvertisement = async (ad: ad) => {
365
- this.debugLog(
366
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
367
- ` BLE Address Found: ${ad.address}`,
368
- );
369
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
370
- this.BLE_On = ad.serviceData.state;
371
- //this.delay = ad.serviceData.delay;
372
- //this.timer = ad.serviceData.timer;
373
- //this.syncUtcTime = ad.serviceData.syncUtcTime;
374
- //this.wifiRssi = ad.serviceData.wifiRssi;
375
- //this.overload = ad.serviceData.overload;
376
- //this.currentPower = ad.serviceData.currentPower;
377
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
378
- this.debugLog(
379
- `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
380
- `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
381
- `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
382
- );
383
-
384
- if (ad.serviceData) {
385
- this.BLE_IsConnected = true;
386
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
387
- await this.stopScanning(switchbot);
388
- } else {
389
- this.BLE_IsConnected = false;
390
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
253
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
254
+ await this.BLERefreshConnection(switchbot);
391
255
  }
392
- };
393
- // Wait
394
- return await sleep(this.scanDuration * 1000);
395
- })
396
- .then(async () => {
397
- // Stop to monitor
398
- await this.stopScanning(switchbot);
399
- })
400
- .catch(async (e: any) => {
401
- this.apiError(e);
402
- this.errorLog(
403
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
404
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
405
- );
406
- await this.BLERefreshConnection(switchbot);
407
- });
408
- } else {
409
- await this.BLERefreshConnection(switchbot);
410
- }*/
256
+ })();
257
+ }
411
258
  }
412
259
  async openAPIRefreshStatus() {
413
260
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
414
261
  try {
415
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
416
- headers: this.platform.generateHeaders(),
417
- });
418
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
262
+ const { body, statusCode } = await this.deviceRefreshStatus();
419
263
  const deviceStatus = await body.json();
420
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
421
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
264
+ await this.refreshStatusCodes(statusCode, deviceStatus);
265
+ ;
422
266
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
423
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
424
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
425
- this.OpenAPI_On = deviceStatus.body.power;
426
- this.OpenAPI_RGB = deviceStatus.body.color;
427
- this.OpenAPI_Brightness = deviceStatus.body.brightness;
428
- this.OpenAPI_ColorTemperature = deviceStatus.body.colorTemperature;
429
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
430
- this.openAPIparseStatus();
431
- this.updateHomeKitCharacteristics();
267
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
268
+ await this.openAPIparseStatus(deviceStatus);
269
+ await this.updateHomeKitCharacteristics();
432
270
  }
433
271
  else {
434
- this.statusCode(statusCode);
435
- this.statusCode(deviceStatus.statusCode);
272
+ await this.statusCodes(statusCode, deviceStatus);
436
273
  }
437
274
  }
438
275
  catch (e) {
439
276
  this.apiError(e);
440
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
441
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
277
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
278
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
279
+ }
280
+ }
281
+ async registerWebhook(accessory, device) {
282
+ if (device.webhook) {
283
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
284
+ this.platform.webhookEventHandler[device.deviceId] = async (context) => {
285
+ try {
286
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
287
+ const { powerState, brightness, colorTemperature } = context;
288
+ const { On, Brightness, ColorTemperature } = this.LightBulb;
289
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
290
+ '(powerState, brightness, colorTemperature) = ' +
291
+ `Webhook:(${powerState}, ${brightness}, ${colorTemperature}), `
292
+ + `current:(${On}, ${Brightness}, ${ColorTemperature})`);
293
+ // On
294
+ this.LightBulb.On = powerState === 'ON' ? true : false;
295
+ if (accessory.context.Brightness !== this.LightBulb.On) {
296
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
297
+ }
298
+ else {
299
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
300
+ }
301
+ // Brightness
302
+ this.LightBulb.Brightness = brightness;
303
+ if (accessory.context.Brightness !== this.LightBulb.Brightness) {
304
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
305
+ }
306
+ else {
307
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
308
+ }
309
+ // ColorTemperature
310
+ this.LightBulb.ColorTemperature = colorTemperature;
311
+ if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
312
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
313
+ }
314
+ else {
315
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
316
+ }
317
+ this.updateHomeKitCharacteristics();
318
+ }
319
+ catch (e) {
320
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} `
321
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
322
+ }
323
+ };
324
+ }
325
+ else {
326
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
442
327
  }
443
328
  }
444
329
  /**
@@ -455,15 +340,17 @@ export class CeilingLight {
455
340
  async pushChanges() {
456
341
  if (!this.device.enableCloudService && this.OpenAPI) {
457
342
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
458
- /*} else if (this.BLE) {
459
- await this.BLEpushChanges();*/
343
+ }
344
+ else if (this.BLE) {
345
+ await this.BLEpushChanges();
460
346
  }
461
347
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
462
348
  await this.openAPIpushChanges();
463
349
  }
464
350
  else {
465
351
  await this.offlineOff();
466
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, pushChanges will not happen.`);
352
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
353
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
467
354
  }
468
355
  // Refresh the status from the API
469
356
  interval(15000)
@@ -475,26 +362,20 @@ export class CeilingLight {
475
362
  }
476
363
  async BLEpushChanges() {
477
364
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
478
- if (this.On !== this.accessory.context.On) {
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
365
+ if (this.LightBulb.On !== this.accessory.context.On) {
366
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
367
+ + ` BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
480
368
  const switchbot = await this.platform.connectBLE();
481
369
  // Convert to BLE Address
482
- this.device.bleMac = this.device
483
- .deviceId.match(/.{1,2}/g)
484
- .join(':')
485
- .toLowerCase();
486
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
370
+ await this.convertBLEAddress();
487
371
  switchbot
488
- .discover({
489
- model: 'u',
490
- id: this.device.bleMac,
491
- })
372
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
492
373
  .then(async (device_list) => {
493
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
494
- return await this.retry({
495
- max: this.maxRetry(),
374
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
375
+ return await this.retryBLE({
376
+ max: await this.maxRetryBLE(),
496
377
  fn: async () => {
497
- if (this.On) {
378
+ if (this.LightBulb.On) {
498
379
  return await device_list[0].turnOn({ id: this.device.bleMac });
499
380
  }
500
381
  else {
@@ -505,24 +386,28 @@ export class CeilingLight {
505
386
  })
506
387
  .then(() => {
507
388
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
508
- this.On = false;
389
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
390
+ + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
391
+ this.LightBulb.On = false;
509
392
  })
510
393
  .catch(async (e) => {
511
394
  this.apiError(e);
512
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
513
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
395
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
396
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
514
397
  await this.BLEPushConnection();
515
398
  });
516
399
  }
517
400
  else {
518
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`);
401
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
402
+ + ` No BLEpushChanges: On: ${this.LightBulb.On}, `
403
+ + `OnCached: ${this.accessory.context.On}`);
519
404
  }
520
405
  }
521
406
  async openAPIpushChanges() {
522
407
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
523
- if (this.On !== this.accessory.context.On) {
408
+ if (this.LightBulb.On !== this.accessory.context.On) {
524
409
  let command = '';
525
- if (this.On) {
410
+ if (this.LightBulb.On) {
526
411
  command = 'turnOn';
527
412
  }
528
413
  else {
@@ -535,55 +420,48 @@ export class CeilingLight {
535
420
  });
536
421
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
537
422
  try {
538
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
539
- body: bodyChange,
540
- method: 'POST',
541
- headers: this.platform.generateHeaders(),
542
- });
543
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
423
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
544
424
  const deviceStatus = await body.json();
545
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
546
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
547
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
425
+ await this.pushStatusCodes(statusCode, deviceStatus);
548
426
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
549
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
550
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
427
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
428
+ await this.updateHomeKitCharacteristics();
551
429
  }
552
430
  else {
553
- this.statusCode(statusCode);
554
- this.statusCode(deviceStatus.statusCode);
431
+ await this.statusCode(statusCode);
432
+ await this.statusCode(deviceStatus.statusCode);
555
433
  }
556
434
  }
557
435
  catch (e) {
558
436
  this.apiError(e);
559
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
560
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
437
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
438
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
561
439
  }
562
440
  }
563
441
  else {
564
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
565
- `On: ${this.On}, ` +
566
- `OnCached: ${this.accessory.context.On}`);
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
443
+ + `On: ${this.LightBulb.On}, `
444
+ + `OnCached: ${this.accessory.context.On}`);
567
445
  }
568
446
  // Push Hue & Saturation Update
569
- if (this.On) {
447
+ if (this.LightBulb.On) {
570
448
  await this.pushHueSaturationChanges();
571
449
  }
572
450
  // Push ColorTemperature Update
573
- if (this.On) {
451
+ if (this.LightBulb.On) {
574
452
  await this.pushColorTemperatureChanges();
575
453
  }
576
454
  // Push Brightness Update
577
- if (this.On) {
455
+ if (this.LightBulb.On) {
578
456
  await this.pushBrightnessChanges();
579
457
  }
580
458
  }
581
459
  async pushHueSaturationChanges() {
582
460
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
583
- if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
584
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
585
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
586
- const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
461
+ if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
462
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
463
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
464
+ const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
587
465
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
588
466
  const bodyChange = JSON.stringify({
589
467
  command: 'setColor',
@@ -592,41 +470,34 @@ export class CeilingLight {
592
470
  });
593
471
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
594
472
  try {
595
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
596
- body: bodyChange,
597
- method: 'POST',
598
- headers: this.platform.generateHeaders(),
599
- });
600
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
473
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
601
474
  const deviceStatus = await body.json();
602
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
603
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
604
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
475
+ await this.pushStatusCodes(statusCode, deviceStatus);
605
476
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
606
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
607
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
477
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
478
+ await this.updateHomeKitCharacteristics();
608
479
  }
609
480
  else {
610
- this.statusCode(statusCode);
611
- this.statusCode(deviceStatus.statusCode);
481
+ await this.statusCode(statusCode);
482
+ await this.statusCode(deviceStatus.statusCode);
612
483
  }
613
484
  }
614
485
  catch (e) {
615
486
  this.apiError(e);
616
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}` +
617
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
487
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
488
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
618
489
  }
619
490
  }
620
491
  else {
621
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, ` +
622
- `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
492
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
493
+ + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
623
494
  }
624
495
  }
625
496
  async pushColorTemperatureChanges() {
626
497
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
627
- if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
628
- const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
629
- this.cacheKelvin = kelvin;
498
+ if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
499
+ const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
500
+ this.accessory.context.kelvin = kelvin;
630
501
  const bodyChange = JSON.stringify({
631
502
  command: 'setColorTemperature',
632
503
  parameter: `${kelvin}`,
@@ -634,207 +505,196 @@ export class CeilingLight {
634
505
  });
635
506
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
636
507
  try {
637
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
638
- body: bodyChange,
639
- method: 'POST',
640
- headers: this.platform.generateHeaders(),
641
- });
642
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
508
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
643
509
  const deviceStatus = await body.json();
644
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
645
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
646
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
510
+ await this.pushStatusCodes(statusCode, deviceStatus);
647
511
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
648
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
649
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
512
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
513
+ await this.updateHomeKitCharacteristics();
650
514
  }
651
515
  else {
652
- this.statusCode(statusCode);
653
- this.statusCode(deviceStatus.statusCode);
516
+ await this.statusCode(statusCode);
517
+ await this.statusCode(deviceStatus.statusCode);
654
518
  }
655
519
  }
656
520
  catch (e) {
657
521
  this.apiError(e);
658
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}` +
659
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
522
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with`
523
+ + ` ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
660
524
  }
661
525
  }
662
526
  else {
663
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
664
- `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
527
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
528
+ + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
665
529
  }
666
530
  }
667
531
  async pushBrightnessChanges() {
668
532
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
669
- if (this.Brightness !== this.accessory.context.Brightness) {
533
+ if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
670
534
  const bodyChange = JSON.stringify({
671
535
  command: 'setBrightness',
672
- parameter: `${this.Brightness}`,
536
+ parameter: `${this.LightBulb.Brightness}`,
673
537
  commandType: 'command',
674
538
  });
675
539
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
676
540
  try {
677
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
678
- body: bodyChange,
679
- method: 'POST',
680
- headers: this.platform.generateHeaders(),
681
- });
682
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
541
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
683
542
  const deviceStatus = await body.json();
684
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
685
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
686
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
543
+ await this.pushStatusCodes(statusCode, deviceStatus);
687
544
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
688
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
689
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
545
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
546
+ await this.updateHomeKitCharacteristics();
690
547
  }
691
548
  else {
692
- this.statusCode(statusCode);
693
- this.statusCode(deviceStatus.statusCode);
549
+ await this.statusCode(statusCode);
550
+ await this.statusCode(deviceStatus.statusCode);
694
551
  }
695
552
  }
696
553
  catch (e) {
697
554
  this.apiError(e);
698
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}` +
699
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
555
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
556
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
700
557
  }
701
558
  }
702
559
  else {
703
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` +
704
- `Brightness: ${this.Brightness}, ` +
705
- `BrightnessCached: ${this.accessory.context.Brightness}`);
560
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.`
561
+ + `Brightness: ${this.LightBulb.Brightness}, `
562
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
706
563
  }
707
564
  }
708
565
  /**
709
566
  * Handle requests to set the value of the "On" characteristic
710
567
  */
711
568
  async OnSet(value) {
712
- if (this.On === this.accessory.context.On) {
569
+ if (this.LightBulb.On === this.accessory.context.On) {
713
570
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
714
571
  }
715
572
  else {
716
573
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
717
574
  }
718
- this.On = value;
575
+ this.LightBulb.On = value;
719
576
  this.doCeilingLightUpdate.next();
720
577
  }
721
578
  /**
722
579
  * Handle requests to set the value of the "Brightness" characteristic
723
580
  */
724
581
  async BrightnessSet(value) {
725
- if (this.Brightness === this.accessory.context.Brightness) {
582
+ if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
726
583
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
727
584
  }
728
- else if (this.On) {
585
+ else if (this.LightBulb.On) {
729
586
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
730
587
  }
731
588
  else {
732
589
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
733
590
  }
734
- this.Brightness = value;
591
+ this.LightBulb.Brightness = value;
735
592
  this.doCeilingLightUpdate.next();
736
593
  }
737
594
  /**
738
595
  * Handle requests to set the value of the "ColorTemperature" characteristic
739
596
  */
740
597
  async ColorTemperatureSet(value) {
741
- if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
598
+ if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
742
599
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
743
600
  }
744
- else if (this.On) {
601
+ else if (this.LightBulb.On) {
745
602
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
746
603
  }
747
604
  else {
748
605
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
749
606
  }
607
+ const minKelvin = 2000;
608
+ const maxKelvin = 9000;
750
609
  // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
751
610
  const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
752
611
  // Check and increase/decrease kelvin to range of device
753
- const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
754
- if (!this.accessory.context.On || this.cacheKelvin === k) {
612
+ const k = Math.min(Math.max(kelvin, minKelvin), maxKelvin);
613
+ if (!this.accessory.context.On || this.accessory.context.kelvin === k) {
755
614
  return;
756
615
  }
757
616
  // Updating the hue/sat to the corresponding values mimics native adaptive lighting
758
617
  const hs = m2hs(value);
759
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
760
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
761
- this.ColorTemperature = value;
618
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
619
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
620
+ this.LightBulb.ColorTemperature = value;
762
621
  this.doCeilingLightUpdate.next();
763
622
  }
764
623
  /**
765
624
  * Handle requests to set the value of the "Hue" characteristic
766
625
  */
767
626
  async HueSet(value) {
768
- if (this.Hue === this.accessory.context.Hue) {
627
+ if (this.LightBulb.Hue === this.accessory.context.Hue) {
769
628
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
770
629
  }
771
- else if (this.On) {
630
+ else if (this.LightBulb.On) {
772
631
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
773
632
  }
774
633
  else {
775
634
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
776
635
  }
777
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
778
- this.Hue = value;
636
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
637
+ this.LightBulb.Hue = value;
779
638
  this.doCeilingLightUpdate.next();
780
639
  }
781
640
  /**
782
641
  * Handle requests to set the value of the "Saturation" characteristic
783
642
  */
784
643
  async SaturationSet(value) {
785
- if (this.Saturation === this.accessory.context.Saturation) {
644
+ if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
786
645
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
787
646
  }
788
- else if (this.On) {
647
+ else if (this.LightBulb.On) {
789
648
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
790
649
  }
791
650
  else {
792
651
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
793
652
  }
794
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
795
- this.Saturation = value;
653
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
654
+ this.LightBulb.Saturation = value;
796
655
  this.doCeilingLightUpdate.next();
797
656
  }
798
657
  async updateHomeKitCharacteristics() {
799
- if (this.On === undefined) {
800
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
658
+ if (this.LightBulb.On === undefined) {
659
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
801
660
  }
802
661
  else {
803
- this.accessory.context.On = this.On;
804
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.On, this.On);
805
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
662
+ this.accessory.context.On = this.LightBulb.On;
663
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
664
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
806
665
  }
807
- if (this.Brightness === undefined) {
808
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
666
+ if (this.LightBulb.Brightness === undefined) {
667
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
809
668
  }
810
669
  else {
811
- this.accessory.context.Brightness = this.Brightness;
812
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
813
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
670
+ this.accessory.context.Brightness = this.LightBulb.Brightness;
671
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
672
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
814
673
  }
815
- if (this.ColorTemperature === undefined) {
816
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
674
+ if (this.LightBulb.ColorTemperature === undefined) {
675
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
817
676
  }
818
677
  else {
819
- this.accessory.context.ColorTemperature = this.ColorTemperature;
820
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.ColorTemperature);
821
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
678
+ this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
679
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
680
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
681
+ + ` updateCharacteristic ColorTemperature: ${this.LightBulb.ColorTemperature}`);
822
682
  }
823
- if (this.Hue === undefined) {
824
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
683
+ if (this.LightBulb.Hue === undefined) {
684
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
825
685
  }
826
686
  else {
827
- this.accessory.context.Hue = this.Hue;
828
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, this.Hue);
829
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
687
+ this.accessory.context.Hue = this.LightBulb.Hue;
688
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
689
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
830
690
  }
831
- if (this.Saturation === undefined) {
832
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
691
+ if (this.LightBulb.Saturation === undefined) {
692
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
833
693
  }
834
694
  else {
835
- this.accessory.context.Saturation = this.Saturation;
836
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, this.Saturation);
837
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
695
+ this.accessory.context.Saturation = this.LightBulb.Saturation;
696
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
697
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
838
698
  }
839
699
  }
840
700
  async adaptiveLighting(device) {
@@ -847,34 +707,6 @@ export class CeilingLight {
847
707
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
848
708
  }
849
709
  }
850
- async stopScanning(switchbot) {
851
- switchbot.stopScan();
852
- if (this.BLE_IsConnected) {
853
- await this.BLEparseStatus();
854
- await this.updateHomeKitCharacteristics();
855
- }
856
- else {
857
- await this.BLERefreshConnection(switchbot);
858
- }
859
- }
860
- async getCustomBLEAddress(switchbot) {
861
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
862
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
863
- (async () => {
864
- // Start to monitor advertisement packets
865
- await switchbot.startScan({
866
- model: 'u',
867
- });
868
- // Set an event handler
869
- switchbot.onadvertisement = (ad) => {
870
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
871
- };
872
- await sleep(10000);
873
- // Stop to monitor
874
- switchbot.stopScan();
875
- })();
876
- }
877
- }
878
710
  async BLEPushConnection() {
879
711
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
880
712
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
@@ -889,275 +721,17 @@ export class CeilingLight {
889
721
  await this.openAPIRefreshStatus();
890
722
  }
891
723
  }
892
- async retry({ max, fn }) {
893
- return fn().catch(async (e) => {
894
- if (max === 0) {
895
- throw e;
896
- }
897
- this.infoLog(e);
898
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
899
- await sleep(1000);
900
- return this.retry({ max: max - 1, fn });
901
- });
902
- }
903
- maxRetry() {
904
- if (this.device.maxRetry) {
905
- return this.device.maxRetry;
906
- }
907
- else {
908
- return 5;
909
- }
910
- }
911
- minStep(device) {
912
- if (device.ceilinglight?.set_minStep) {
913
- this.set_minStep = device.ceilinglight?.set_minStep;
914
- }
915
- else {
916
- this.set_minStep = 1;
917
- }
918
- return this.set_minStep;
919
- }
920
- async scan(device) {
921
- if (device.scanDuration) {
922
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
923
- if (this.BLE) {
924
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
925
- }
926
- }
927
- else {
928
- this.scanDuration = this.accessory.context.scanDuration = 1;
929
- if (this.BLE) {
930
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
931
- }
932
- }
933
- }
934
- async statusCode(statusCode) {
935
- switch (statusCode) {
936
- case 151:
937
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
938
- break;
939
- case 152:
940
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
941
- break;
942
- case 160:
943
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
944
- break;
945
- case 161:
946
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
947
- this.offlineOff();
948
- break;
949
- case 171:
950
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
951
- `Hub: ${this.device.hubDeviceId}`);
952
- this.offlineOff();
953
- break;
954
- case 190:
955
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
956
- ` Or command format is invalid, statusCode: ${statusCode}`);
957
- break;
958
- case 100:
959
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
960
- break;
961
- case 200:
962
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
963
- break;
964
- case 400:
965
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
966
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
967
- break;
968
- case 401:
969
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
970
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
971
- break;
972
- case 403:
973
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
974
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
975
- break;
976
- case 404:
977
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
978
- + `statusCode: ${statusCode}`);
979
- break;
980
- case 406:
981
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
982
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
983
- break;
984
- case 415:
985
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
986
- + `header that is not supported by the server, statusCode: ${statusCode}`);
987
- break;
988
- case 422:
989
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
990
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
991
- break;
992
- case 429:
993
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
994
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
995
- break;
996
- case 500:
997
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
998
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
999
- break;
1000
- default:
1001
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
1002
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
1003
- }
1004
- }
1005
724
  async offlineOff() {
1006
725
  if (this.device.offline) {
1007
- await this.deviceContext();
1008
- await this.updateHomeKitCharacteristics();
726
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
1009
727
  }
1010
728
  }
1011
729
  apiError(e) {
1012
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.On, e);
1013
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, e);
1014
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Brightness, e);
1015
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, e);
1016
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
1017
- }
1018
- async deviceContext() {
1019
- if (this.On === undefined) {
1020
- this.On = false;
1021
- }
1022
- else {
1023
- this.On = this.accessory.context.On;
1024
- }
1025
- if (this.Hue === undefined) {
1026
- this.Hue = 0;
1027
- }
1028
- else {
1029
- this.Hue = this.accessory.context.Hue;
1030
- }
1031
- if (this.Brightness === undefined) {
1032
- this.Brightness = 0;
1033
- }
1034
- else {
1035
- this.Brightness = this.accessory.context.Brightness;
1036
- }
1037
- if (this.Brightness === undefined) {
1038
- this.Saturation = 0;
1039
- }
1040
- else {
1041
- this.Saturation = this.accessory.context.Saturation;
1042
- }
1043
- if (this.ColorTemperature === undefined) {
1044
- this.ColorTemperature = 140;
1045
- }
1046
- else {
1047
- this.ColorTemperature = this.accessory.context.ColorTemperature;
1048
- }
1049
- this.minKelvin = 2000;
1050
- this.maxKelvin = 9000;
1051
- if (this.FirmwareRevision === undefined) {
1052
- this.FirmwareRevision = this.platform.version;
1053
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
1054
- }
1055
- }
1056
- async refreshRate(device) {
1057
- if (device.refreshRate) {
1058
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1059
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1060
- }
1061
- else if (this.platform.config.options.refreshRate) {
1062
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1063
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1064
- }
1065
- }
1066
- async deviceConfig(device) {
1067
- let config = {};
1068
- if (device.ceilinglight) {
1069
- config = device.ceilinglight;
1070
- }
1071
- if (device.connectionType !== undefined) {
1072
- config['connectionType'] = device.connectionType;
1073
- }
1074
- if (device.external !== undefined) {
1075
- config['external'] = device.external;
1076
- }
1077
- if (device.logging !== undefined) {
1078
- config['logging'] = device.logging;
1079
- }
1080
- if (device.refreshRate !== undefined) {
1081
- config['refreshRate'] = device.refreshRate;
1082
- }
1083
- if (device.scanDuration !== undefined) {
1084
- config['scanDuration'] = device.scanDuration;
1085
- }
1086
- if (device.offline !== undefined) {
1087
- config['offline'] = device.offline;
1088
- }
1089
- if (device.maxRetry !== undefined) {
1090
- config['maxRetry'] = device.maxRetry;
1091
- }
1092
- if (device.webhook !== undefined) {
1093
- config['webhook'] = device.webhook;
1094
- }
1095
- if (Object.entries(config).length !== 0) {
1096
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1097
- }
1098
- }
1099
- async deviceLogs(device) {
1100
- if (this.platform.debugMode) {
1101
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
1102
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1103
- }
1104
- else if (device.logging) {
1105
- this.deviceLogging = this.accessory.context.logging = device.logging;
1106
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1107
- }
1108
- else if (this.platform.config.options?.logging) {
1109
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
1110
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1111
- }
1112
- else {
1113
- this.deviceLogging = this.accessory.context.logging = 'standard';
1114
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1115
- }
1116
- }
1117
- /**
1118
- * Logging for Device
1119
- */
1120
- infoLog(...log) {
1121
- if (this.enablingDeviceLogging()) {
1122
- this.platform.log.info(String(...log));
1123
- }
1124
- }
1125
- warnLog(...log) {
1126
- if (this.enablingDeviceLogging()) {
1127
- this.platform.log.warn(String(...log));
1128
- }
1129
- }
1130
- debugWarnLog(...log) {
1131
- if (this.enablingDeviceLogging()) {
1132
- if (this.deviceLogging?.includes('debug')) {
1133
- this.platform.log.warn('[DEBUG]', String(...log));
1134
- }
1135
- }
1136
- }
1137
- errorLog(...log) {
1138
- if (this.enablingDeviceLogging()) {
1139
- this.platform.log.error(String(...log));
1140
- }
1141
- }
1142
- debugErrorLog(...log) {
1143
- if (this.enablingDeviceLogging()) {
1144
- if (this.deviceLogging?.includes('debug')) {
1145
- this.platform.log.error('[DEBUG]', String(...log));
1146
- }
1147
- }
1148
- }
1149
- debugLog(...log) {
1150
- if (this.enablingDeviceLogging()) {
1151
- if (this.deviceLogging === 'debug') {
1152
- this.platform.log.info('[DEBUG]', String(...log));
1153
- }
1154
- else {
1155
- this.platform.log.debug(String(...log));
1156
- }
1157
- }
1158
- }
1159
- enablingDeviceLogging() {
1160
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
730
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
731
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
732
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
733
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
734
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
1161
735
  }
1162
736
  }
1163
737
  //# sourceMappingURL=ceilinglight.js.map