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