@switchbot/homebridge-switchbot 3.5.0-beta.23 → 3.5.0-beta.25

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