@switchbot/homebridge-switchbot 3.5.0-beta.7 → 3.5.0-beta.70

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