@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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