@switchbot/homebridge-switchbot 3.5.0-beta.6 → 3.5.0-beta.60

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