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