@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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 +164 -22
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +457 -866
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +851 -913
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +316 -742
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +380 -833
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +267 -602
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +469 -897
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +76 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +846 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +645 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +19 -58
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +304 -640
  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 +300 -710
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +225 -643
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +371 -742
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +309 -642
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +223 -638
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +216 -644
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +209 -588
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +141 -532
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +252 -609
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +179 -521
  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 +337 -482
  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 +108 -333
  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 +47 -299
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +95 -364
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +46 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +393 -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 +121 -376
  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 +441 -584
  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 +94 -353
  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 +48 -299
  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 +50 -305
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +66 -18
  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 +95 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +657 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +488 -937
  143. package/src/device/bot.ts +907 -1016
  144. package/src/device/ceilinglight.ts +330 -798
  145. package/src/device/colorbulb.ts +394 -904
  146. package/src/device/contact.ts +298 -637
  147. package/src/device/curtain.ts +496 -954
  148. package/src/device/device.ts +872 -0
  149. package/src/device/fan.ts +692 -0
  150. package/src/device/hub.ts +338 -671
  151. package/src/device/humidifier.ts +330 -777
  152. package/src/device/iosensor.ts +258 -671
  153. package/src/device/lightstrip.ts +394 -809
  154. package/src/device/lock.ts +345 -700
  155. package/src/device/meter.ts +256 -666
  156. package/src/device/meterplus.ts +248 -672
  157. package/src/device/motion.ts +234 -622
  158. package/src/device/plug.ts +152 -572
  159. package/src/device/robotvacuumcleaner.ts +273 -653
  160. package/src/device/waterdetector.ts +198 -549
  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 +368 -540
  165. package/src/irdevice/airpurifier.ts +134 -369
  166. package/src/irdevice/camera.ts +60 -324
  167. package/src/irdevice/fan.ts +111 -392
  168. package/src/irdevice/irdevice.ts +406 -0
  169. package/src/irdevice/light.ts +159 -425
  170. package/src/irdevice/other.ts +509 -661
  171. package/src/irdevice/tv.ts +123 -388
  172. package/src/irdevice/vacuumcleaner.ts +61 -324
  173. package/src/irdevice/waterheater.ts +63 -333
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +120 -610
  176. package/src/utils.ts +661 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,120 +1,85 @@
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';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * lightstrip.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
6
+ import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
7
+ import { interval, skipWhile, Subject } from 'rxjs';
8
+ import { debounceTime, take, tap } from 'rxjs/operators';
6
9
  /**
7
10
  * Platform Accessory
8
11
  * An instance of this class is created for each accessory your platform registers
9
12
  * Each accessory may expose multiple services of different service types.
10
13
  */
11
- export class StripLight {
14
+ export class StripLight extends deviceBase {
12
15
  platform;
13
- accessory;
14
- device;
15
- api;
16
- log;
17
- config;
18
- hap;
19
16
  // 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;
17
+ LightBulb;
44
18
  // Adaptive Lighting
45
19
  AdaptiveLightingController;
46
- minKelvin;
47
- maxKelvin;
48
- // Others
49
- cacheKelvin;
50
- change;
20
+ adaptiveLightingShift;
51
21
  // Updates
52
22
  stripLightUpdateInProgress;
53
23
  doStripLightUpdate;
54
- // Connection
55
- OpenAPI;
56
- BLE;
57
24
  constructor(platform, accessory, device) {
25
+ super(platform, accessory, device);
58
26
  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);
27
+ this.adaptiveLighting(device);
74
28
  // this is subject we use to track when we need to POST changes to the SwitchBot API
75
29
  this.doStripLightUpdate = new Subject();
76
30
  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;
31
+ // Initialize the LightBulb Service
32
+ accessory.context.LightBulb = accessory.context.LightBulb ?? {};
33
+ this.LightBulb = {
34
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
35
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb),
36
+ On: accessory.context.On ?? false,
37
+ Hue: accessory.context.Hue ?? 0,
38
+ Saturation: accessory.context.Saturation ?? 0,
39
+ Brightness: accessory.context.Brightness ?? 0,
40
+ ColorTemperature: accessory.context.ColorTemperature ?? 140,
41
+ };
42
+ accessory.context.LightBulb = this.LightBulb;
43
+ // Adaptive Lighting
91
44
  if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
92
- this.accessory.removeService(this.lightBulbService);
93
- this.lightBulbService = this.accessory.addService(this.hap.Service.Lightbulb);
45
+ this.accessory.removeService(this.LightBulb.Service);
46
+ this.LightBulb.Service = this.accessory.addService(this.hap.Service.Lightbulb);
94
47
  this.accessory.context.adaptiveLighting = false;
95
48
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
96
49
  }
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);
50
+ if (this.adaptiveLightingShift !== -1) {
51
+ this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
52
+ customTemperatureAdjustment: this.adaptiveLightingShift,
53
+ });
54
+ this.accessory.configureController(this.AdaptiveLightingController);
55
+ this.accessory.context.adaptiveLighting = true;
56
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
57
+ + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
100
58
  }
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
59
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
60
+ // Initialize LightBulb Characteristics
61
+ this.LightBulb.Service
62
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
63
+ .getCharacteristic(this.hap.Characteristic.On)
64
+ .onGet(() => {
65
+ return this.LightBulb.On;
66
+ })
67
+ .onSet(this.OnSet.bind(this));
68
+ // Initialize LightBulb Brightness Characteristic
69
+ this.LightBulb.Service
105
70
  .getCharacteristic(this.hap.Characteristic.Brightness)
106
71
  .setProps({
107
- minStep: this.minStep(device),
72
+ minStep: device.striplight?.set_minStep ?? 1,
108
73
  minValue: 0,
109
74
  maxValue: 100,
110
75
  validValueRanges: [0, 100],
111
76
  })
112
77
  .onGet(() => {
113
- return this.Brightness;
78
+ return this.LightBulb.Brightness;
114
79
  })
115
80
  .onSet(this.BrightnessSet.bind(this));
116
- // handle ColorTemperature events using the ColorTemperature characteristic
117
- this.lightBulbService
81
+ // Initialize LightBulb ColorTemperature Characteristic
82
+ this.LightBulb.Service
118
83
  .getCharacteristic(this.hap.Characteristic.ColorTemperature)
119
84
  .setProps({
120
85
  minValue: 140,
@@ -122,11 +87,11 @@ export class StripLight {
122
87
  validValueRanges: [140, 500],
123
88
  })
124
89
  .onGet(() => {
125
- return this.ColorTemperature;
90
+ return this.LightBulb.ColorTemperature;
126
91
  })
127
92
  .onSet(this.ColorTemperatureSet.bind(this));
128
- // handle Hue events using the Hue characteristic
129
- this.lightBulbService
93
+ // Initialize LightBulb Hue Characteristic
94
+ this.LightBulb.Service
130
95
  .getCharacteristic(this.hap.Characteristic.Hue)
131
96
  .setProps({
132
97
  minValue: 0,
@@ -134,11 +99,11 @@ export class StripLight {
134
99
  validValueRanges: [0, 360],
135
100
  })
136
101
  .onGet(() => {
137
- return this.Hue;
102
+ return this.LightBulb.Hue;
138
103
  })
139
104
  .onSet(this.HueSet.bind(this));
140
- // handle Hue events using the Hue characteristic
141
- this.lightBulbService
105
+ // Initialize LightBulb Saturation Characteristic
106
+ this.LightBulb.Service
142
107
  .getCharacteristic(this.hap.Characteristic.Saturation)
143
108
  .setProps({
144
109
  minValue: 0,
@@ -146,19 +111,11 @@ export class StripLight {
146
111
  validValueRanges: [0, 100],
147
112
  })
148
113
  .onGet(() => {
149
- return this.Saturation;
114
+ return this.LightBulb.Saturation;
150
115
  })
151
116
  .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
- }
117
+ // Retrieve initial values and updateHomekit
118
+ this.refreshStatus();
162
119
  // Update Homekit
163
120
  this.updateHomeKitCharacteristics();
164
121
  // Start an update interval
@@ -168,120 +125,104 @@ export class StripLight {
168
125
  await this.refreshStatus();
169
126
  });
170
127
  //regisiter webhook event handler
171
- if (this.device.webhook) {
172
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
173
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
174
- try {
175
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
176
- const { powerState, brightness, color, colorTemperature } = context;
177
- const { On, Brightness, Hue, Saturation, ColorTemperature } = this;
178
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
179
- '(powerState, brightness, color, colorTemperature) = ' +
180
- `Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
181
- `current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
182
- this.On = powerState === 'ON' ? true : false;
183
- this.Brightness = brightness;
184
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(color)}`);
185
- const [red, green, blue] = color.split(':');
186
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
187
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
188
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
189
- const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
190
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
191
- // Hue
192
- this.Hue = hue;
193
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
194
- // Saturation
195
- this.Saturation = saturation;
196
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
197
- this.updateHomeKitCharacteristics();
198
- }
199
- catch (e) {
200
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
201
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
202
- }
203
- };
204
- }
128
+ this.registerWebhook(accessory, device);
205
129
  // Watch for Bulb change events
206
130
  // We put in a debounce of 1000ms so we don't make duplicate calls
207
131
  this.doStripLightUpdate
208
132
  .pipe(tap(() => {
209
133
  this.stripLightUpdateInProgress = true;
210
- }), debounceTime(this.platform.config.options.pushRate * 1000))
134
+ }), debounceTime(this.devicePushRate * 1000))
211
135
  .subscribe(async () => {
212
136
  try {
213
137
  await this.pushChanges();
214
138
  }
215
139
  catch (e) {
216
140
  this.apiError(e);
217
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,` +
218
- ` Error Message: ${JSON.stringify(e.message)}`);
141
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
142
+ + ` Error Message: ${JSON.stringify(e.message)}`);
219
143
  }
220
144
  this.stripLightUpdateInProgress = false;
221
145
  });
222
146
  }
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() {
147
+ async BLEparseStatus(serviceData) {
242
148
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
243
149
  // State
244
- switch (this.BLE_On) {
150
+ switch (serviceData.state) {
245
151
  case 'on':
246
- this.On = true;
152
+ this.LightBulb.On = true;
247
153
  break;
248
154
  default:
249
- this.On = false;
155
+ this.LightBulb.On = false;
156
+ }
157
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
158
+ // Brightness
159
+ this.LightBulb.Brightness = Number(serviceData.brightness);
160
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
161
+ // Color, Hue & Brightness
162
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
163
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
164
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
165
+ const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
166
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
167
+ + ` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`);
168
+ // Hue
169
+ this.LightBulb.Hue = hue;
170
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
171
+ // Saturation
172
+ this.LightBulb.Saturation = saturation;
173
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
174
+ // ColorTemperature
175
+ if (serviceData.color_temperature) {
176
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
177
+ this.LightBulb.ColorTemperature = serviceData.color_temperature;
178
+ this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
179
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
250
180
  }
251
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
252
181
  }
253
- async openAPIparseStatus() {
182
+ async openAPIparseStatus(deviceStatus) {
254
183
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
255
- switch (this.OpenAPI_On) {
184
+ switch (deviceStatus.body.power) {
256
185
  case 'on':
257
- this.On = true;
186
+ this.LightBulb.On = true;
258
187
  break;
259
188
  default:
260
- this.On = false;
189
+ this.LightBulb.On = false;
261
190
  }
262
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
191
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
263
192
  // Brightness
264
- this.Brightness = Number(this.OpenAPI_Brightness);
265
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
193
+ this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
194
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
266
195
  // 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(':');
196
+ if (deviceStatus.body.color) {
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
198
+ const [red, green, blue] = deviceStatus.body.color.split(':');
270
199
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
271
200
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
272
201
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
273
202
  const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
274
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
203
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
204
+ + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
275
205
  // Hue
276
- this.Hue = hue;
277
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
206
+ this.LightBulb.Hue = hue;
207
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
278
208
  // Saturation
279
- this.Saturation = saturation;
280
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
209
+ this.LightBulb.Saturation = saturation;
210
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
211
+ }
212
+ // Firmware Version
213
+ const version = deviceStatus.body.version?.toString();
214
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
215
+ if (deviceStatus.body.version) {
216
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
217
+ this.accessory
218
+ .getService(this.hap.Service.AccessoryInformation)
219
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
220
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
221
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
222
+ .updateValue(deviceVersion);
223
+ this.accessory.context.deviceVersion = deviceVersion;
224
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
281
225
  }
282
- // FirmwareRevision
283
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
284
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
285
226
  }
286
227
  /**
287
228
  * Asks the SwitchBot API for the latest device information
@@ -298,124 +239,122 @@ export class StripLight {
298
239
  }
299
240
  else {
300
241
  await this.offlineOff();
301
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
302
- ` ${this.device.connectionType}, refreshStatus will not happen.`);
242
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
243
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
303
244
  }
304
245
  }
305
246
  async BLERefreshStatus() {
306
247
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
307
- const switchbot = await this.platform.connectBLE();
308
- // Convert to BLE Address
309
- this.device.bleMac = this.device
310
- .deviceId.match(/.{1,2}/g)
311
- .join(':')
312
- .toLowerCase();
313
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
314
- this.getCustomBLEAddress(switchbot);
315
- // Start to monitor advertisement packets
316
- (async () => {
248
+ const switchbot = await this.switchbotBLE();
249
+ if (switchbot === undefined) {
250
+ await this.BLERefreshConnection(switchbot);
251
+ }
252
+ else {
317
253
  // Start to monitor advertisement packets
318
- await switchbot.startScan({
319
- model: 'r',
320
- id: this.device.bleMac,
321
- });
322
- // Set an event handler
323
- 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') {
327
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
254
+ (async () => {
255
+ // Start to monitor advertisement packets
256
+ const serviceData = await this.monitorAdvertisementPackets(switchbot);
257
+ // Update HomeKit
258
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
259
+ await this.BLEparseStatus(serviceData);
260
+ await this.updateHomeKitCharacteristics();
328
261
  }
329
262
  else {
330
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
331
- }
332
- };
333
- // Wait 1 seconds
334
- await switchbot.wait(this.scanDuration * 1000);
335
- // Stop to monitor
336
- await switchbot.stopScan();
337
- // Update HomeKit
338
- await this.BLEparseStatus();
339
- await this.updateHomeKitCharacteristics();
340
- })();
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}`);
263
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
264
+ await this.BLERefreshConnection(switchbot);
369
265
  }
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
- }*/
266
+ })();
267
+ }
389
268
  }
390
269
  async openAPIRefreshStatus() {
391
270
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
392
271
  try {
393
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
394
- headers: this.platform.generateHeaders(),
395
- });
396
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
272
+ const { body, statusCode } = await this.deviceRefreshStatus();
397
273
  const deviceStatus = await body.json();
398
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
399
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
274
+ await this.refreshStatusCodes(statusCode, deviceStatus);
275
+ ;
400
276
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
401
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
402
- + `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();
408
- this.updateHomeKitCharacteristics();
277
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
278
+ await this.openAPIparseStatus(deviceStatus);
279
+ await this.updateHomeKitCharacteristics();
409
280
  }
410
281
  else {
411
- this.statusCode(statusCode);
412
- this.statusCode(deviceStatus.statusCode);
282
+ await this.statusCodes(statusCode, deviceStatus);
413
283
  }
414
284
  }
415
285
  catch (e) {
416
286
  this.apiError(e);
417
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
418
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
287
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
288
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
289
+ }
290
+ }
291
+ async registerWebhook(accessory, device) {
292
+ if (device.webhook) {
293
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
294
+ this.platform.webhookEventHandler[device.deviceId] = async (context) => {
295
+ try {
296
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
297
+ const { powerState, brightness, color, colorTemperature } = context;
298
+ const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
299
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState, brightness, color, colorTemperature) = Webhook: (${powerState},`
300
+ + ` ${brightness}, ${color}, ${colorTemperature}), current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
301
+ // On
302
+ this.LightBulb.On = powerState === 'ON' ? true : false;
303
+ if (accessory.context.Brightness !== this.LightBulb.On) {
304
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
305
+ }
306
+ else {
307
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
308
+ }
309
+ // Brightness
310
+ this.LightBulb.Brightness = brightness;
311
+ if (accessory.context.Brightness !== this.LightBulb.Brightness) {
312
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
313
+ }
314
+ else {
315
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
316
+ }
317
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} color: ${JSON.stringify(color)}`);
318
+ const [red, green, blue] = color.split(':');
319
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} red: ${JSON.stringify(red)}`);
320
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} green: ${JSON.stringify(green)}`);
321
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} blue: ${JSON.stringify(blue)}`);
322
+ const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
323
+ this.debugLog(`${device.deviceType}: ${accessory.displayName}`
324
+ + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
325
+ // Hue
326
+ this.LightBulb.Hue = hue;
327
+ if (accessory.context.Hue !== this.LightBulb.Hue) {
328
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
329
+ }
330
+ else {
331
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
332
+ }
333
+ // Saturation
334
+ this.LightBulb.Saturation = saturation;
335
+ if (accessory.context.Saturation !== this.LightBulb.Saturation) {
336
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
337
+ }
338
+ else {
339
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
340
+ }
341
+ // ColorTemperature
342
+ this.LightBulb.ColorTemperature = colorTemperature;
343
+ if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
344
+ this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
345
+ }
346
+ else {
347
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
348
+ }
349
+ this.updateHomeKitCharacteristics();
350
+ }
351
+ catch (e) {
352
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
353
+ }
354
+ };
355
+ }
356
+ else {
357
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
419
358
  }
420
359
  }
421
360
  /**
@@ -431,15 +370,17 @@ export class StripLight {
431
370
  async pushChanges() {
432
371
  if (!this.device.enableCloudService && this.OpenAPI) {
433
372
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
434
- /* }else if (this.BLE) {
435
- await this.BLEpushChanges();*/
373
+ }
374
+ else if (this.BLE) {
375
+ await this.BLEpushChanges();
436
376
  }
437
377
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
438
378
  await this.openAPIpushChanges();
439
379
  }
440
380
  else {
441
381
  await this.offlineOff();
442
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, pushChanges will not happen.`);
382
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
383
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
443
384
  }
444
385
  // Refresh the status from the API
445
386
  interval(15000)
@@ -451,26 +392,20 @@ export class StripLight {
451
392
  }
452
393
  async BLEpushChanges() {
453
394
  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}`);
395
+ if (this.LightBulb.On !== this.accessory.context.On) {
396
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
397
+ + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
456
398
  const switchbot = await this.platform.connectBLE();
457
399
  // Convert to BLE Address
458
- this.device.bleMac = this.device
459
- .deviceId.match(/.{1,2}/g)
460
- .join(':')
461
- .toLowerCase();
462
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
400
+ await this.convertBLEAddress();
463
401
  switchbot
464
- .discover({
465
- model: 'r',
466
- id: this.device.bleMac,
467
- })
402
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
468
403
  .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(),
404
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
405
+ return await this.retryBLE({
406
+ max: await this.maxRetryBLE(),
472
407
  fn: async () => {
473
- if (this.On) {
408
+ if (this.LightBulb.On) {
474
409
  return await device_list[0].turnOn({ id: this.device.bleMac });
475
410
  }
476
411
  else {
@@ -481,107 +416,104 @@ export class StripLight {
481
416
  })
482
417
  .then(() => {
483
418
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
484
- this.On = false;
419
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
420
+ + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
421
+ this.LightBulb.On = false;
485
422
  })
486
423
  .catch(async (e) => {
487
424
  this.apiError(e);
488
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
489
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
425
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
426
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
490
427
  await this.BLEPushConnection();
491
428
  });
492
429
  // Push Brightness Update
493
- if (this.On) {
430
+ if (this.LightBulb.On) {
494
431
  await this.BLEpushBrightnessChanges();
495
432
  }
496
433
  // Push Hue & Saturation Update
497
- if (this.On) {
434
+ if (this.LightBulb.On) {
498
435
  await this.BLEpushRGBChanges();
499
436
  }
437
+ if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
438
+ const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
439
+ this.accessory.context.kelvin = kelvin;
440
+ }
441
+ else {
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
443
+ + `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
444
+ }
500
445
  }
501
446
  else {
502
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`);
447
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
448
+ + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
503
449
  }
504
450
  }
505
451
  async BLEpushBrightnessChanges() {
506
452
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
507
- if (this.Brightness !== this.accessory.context.Brightness) {
453
+ if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
508
454
  const switchbot = await this.platform.connectBLE();
509
455
  // Convert to BLE Address
510
- this.device.bleMac = this.device
511
- .deviceId.match(/.{1,2}/g)
512
- .join(':')
513
- .toLowerCase();
514
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
456
+ await this.convertBLEAddress();
515
457
  switchbot
516
- .discover({
517
- model: 'u',
518
- id: this.device.bleMac,
519
- })
458
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
520
459
  .then(async (device_list) => {
521
- this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
522
- return await device_list[0].setBrightness(this.Brightness);
460
+ this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
461
+ return await device_list[0].setBrightness(this.LightBulb.Brightness);
523
462
  })
524
463
  .then(() => {
525
464
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
526
- this.On = false;
465
+ this.LightBulb.On = false;
527
466
  })
528
467
  .catch(async (e) => {
529
468
  this.apiError(e);
530
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}` +
531
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
469
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
470
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
532
471
  await this.BLEPushConnection();
533
472
  });
534
473
  }
535
474
  else {
536
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.` +
537
- `Brightness: ${this.Brightness}, ` +
538
- `BrightnessCached: ${this.accessory.context.Brightness}`);
475
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.`
476
+ + `Brightness: ${this.LightBulb.Brightness}, `
477
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
539
478
  }
540
479
  }
541
480
  async BLEpushRGBChanges() {
542
481
  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));
482
+ if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
483
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
484
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
485
+ const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
547
486
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
548
487
  const switchbot = await this.platform.connectBLE();
549
488
  // Convert to BLE Address
550
- this.device.bleMac = this.device
551
- .deviceId.match(/.{1,2}/g)
552
- .join(':')
553
- .toLowerCase();
554
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
489
+ await this.convertBLEAddress();
555
490
  switchbot
556
- .discover({
557
- model: 'u',
558
- id: this.device.bleMac,
559
- })
491
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
560
492
  .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);
493
+ this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
494
+ return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
563
495
  })
564
496
  .then(() => {
565
497
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
566
- this.On = false;
498
+ this.LightBulb.On = false;
567
499
  })
568
500
  .catch(async (e) => {
569
501
  this.apiError(e);
570
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}` +
571
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
502
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
503
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
572
504
  await this.BLEPushConnection();
573
505
  });
574
506
  }
575
507
  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}`);
508
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
509
+ + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
578
510
  }
579
511
  }
580
512
  async openAPIpushChanges() {
581
513
  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) {
514
+ if (this.LightBulb.On !== this.accessory.context.On) {
515
+ const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
516
+ /*if (this.LightBulb.On) {
585
517
  command = 'turnOn';
586
518
  } else {
587
519
  command = 'turnOff';
@@ -593,51 +525,44 @@ export class StripLight {
593
525
  });
594
526
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
595
527
  try {
596
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
597
- body: bodyChange,
598
- method: 'POST',
599
- headers: this.platform.generateHeaders(),
600
- });
601
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
528
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
602
529
  const deviceStatus = await body.json();
603
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
604
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
605
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
530
+ await this.pushStatusCodes(statusCode, deviceStatus);
606
531
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
607
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
608
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
532
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
533
+ await this.updateHomeKitCharacteristics();
609
534
  }
610
535
  else {
611
- this.statusCode(statusCode);
612
- this.statusCode(deviceStatus.statusCode);
536
+ await this.statusCode(statusCode);
537
+ await this.statusCode(deviceStatus.statusCode);
613
538
  }
614
539
  }
615
540
  catch (e) {
616
541
  this.apiError(e);
617
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
618
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
542
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
543
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
619
544
  }
620
545
  }
621
546
  else {
622
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
623
- `On: ${this.On}, ` +
624
- `OnCached: ${this.accessory.context.On}`);
547
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
548
+ + `On: ${this.LightBulb.On}, `
549
+ + `OnCached: ${this.accessory.context.On}`);
625
550
  }
626
551
  // Push Hue & Saturation Update
627
- if (this.On) {
552
+ if (this.LightBulb.On) {
628
553
  await this.pushHueSaturationChanges();
629
554
  }
630
555
  // Push Brightness Update
631
- if (this.On) {
556
+ if (this.LightBulb.On) {
632
557
  await this.pushBrightnessChanges();
633
558
  }
634
559
  }
635
560
  async pushHueSaturationChanges() {
636
561
  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));
562
+ if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
563
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
564
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
565
+ const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
641
566
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
642
567
  // Make Push On request to the API
643
568
  const bodyChange = JSON.stringify({
@@ -647,240 +572,201 @@ export class StripLight {
647
572
  });
648
573
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
649
574
  try {
650
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
651
- body: bodyChange,
652
- method: 'POST',
653
- headers: this.platform.generateHeaders(),
654
- });
655
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
575
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
656
576
  const deviceStatus = await body.json();
657
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
658
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
659
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
577
+ await this.pushStatusCodes(statusCode, deviceStatus);
660
578
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
661
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
662
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
579
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
580
+ await this.updateHomeKitCharacteristics();
663
581
  }
664
582
  else {
665
- this.statusCode(statusCode);
666
- this.statusCode(deviceStatus.statusCode);
583
+ await this.statusCode(statusCode);
584
+ await this.statusCode(deviceStatus.statusCode);
667
585
  }
668
586
  }
669
587
  catch (e) {
670
588
  this.apiError(e);
671
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}` +
672
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
589
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
590
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
673
591
  }
674
592
  }
675
593
  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}`);
594
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
595
+ + `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
678
596
  }
679
597
  }
680
598
  async pushBrightnessChanges() {
681
599
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
682
- if (this.Brightness !== this.accessory.context.Brightness) {
600
+ if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
683
601
  const bodyChange = JSON.stringify({
684
602
  command: 'setBrightness',
685
- parameter: `${this.Brightness}`,
603
+ parameter: `${this.LightBulb.Brightness}`,
686
604
  commandType: 'command',
687
605
  });
688
606
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
689
607
  try {
690
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
691
- body: bodyChange,
692
- method: 'POST',
693
- headers: this.platform.generateHeaders(),
694
- });
695
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
608
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
696
609
  const deviceStatus = await body.json();
697
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
698
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
699
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
610
+ await this.pushStatusCodes(statusCode, deviceStatus);
700
611
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
701
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
702
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
612
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
613
+ await this.updateHomeKitCharacteristics();
703
614
  }
704
615
  else {
705
- this.statusCode(statusCode);
706
- this.statusCode(deviceStatus.statusCode);
616
+ await this.statusCode(statusCode);
617
+ await this.statusCode(deviceStatus.statusCode);
707
618
  }
708
619
  }
709
620
  catch (e) {
710
621
  this.apiError(e);
711
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}` +
712
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
622
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
623
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
713
624
  }
714
625
  }
715
626
  else {
716
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,` +
717
- `Brightness: ${this.Brightness}, ` +
718
- `BrightnessCached: ${this.accessory.context.Brightness}`);
627
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,`
628
+ + `Brightness: ${this.LightBulb.Brightness}, `
629
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
719
630
  }
720
631
  }
721
632
  /**
722
633
  * Handle requests to set the value of the "On" characteristic
723
634
  */
724
635
  async OnSet(value) {
725
- if (this.On === this.accessory.context.On) {
636
+ if (this.LightBulb.On === this.accessory.context.On) {
726
637
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
727
638
  }
728
639
  else {
729
640
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
730
641
  }
731
- this.On = value;
642
+ this.LightBulb.On = value;
732
643
  this.doStripLightUpdate.next();
733
644
  }
734
645
  /**
735
646
  * Handle requests to set the value of the "Brightness" characteristic
736
647
  */
737
648
  async BrightnessSet(value) {
738
- if (this.Brightness === this.accessory.context.Brightness) {
649
+ if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
739
650
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
740
651
  }
741
- else if (this.On) {
652
+ else if (this.LightBulb.On) {
742
653
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
743
654
  }
744
655
  else {
745
656
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
746
657
  }
747
- this.Brightness = value;
658
+ this.LightBulb.Brightness = value;
748
659
  this.doStripLightUpdate.next();
749
660
  }
750
661
  /**
751
662
  * Handle requests to set the value of the "ColorTemperature" characteristic
752
663
  */
753
664
  async ColorTemperatureSet(value) {
754
- if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
665
+ if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
755
666
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
756
667
  }
757
- else if (this.On) {
668
+ else if (this.LightBulb.On) {
758
669
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
759
670
  }
760
671
  else {
761
672
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
762
673
  }
674
+ const minKelvin = 2000;
675
+ const maxKelvin = 9000;
763
676
  // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
764
677
  const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
765
678
  // 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) {
679
+ const k = Math.min(Math.max(kelvin, minKelvin), maxKelvin);
680
+ if (!this.accessory.context.On || this.accessory.context.maxKelvin === k) {
768
681
  return;
769
682
  }
770
683
  // Updating the hue/sat to the corresponding values mimics native adaptive lighting
771
684
  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;
685
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
686
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
687
+ this.LightBulb.ColorTemperature = value;
775
688
  this.doStripLightUpdate.next();
776
689
  }
777
690
  /**
778
691
  * Handle requests to set the value of the "Hue" characteristic
779
692
  */
780
693
  async HueSet(value) {
781
- if (this.Hue === this.accessory.context.Hue) {
694
+ if (this.LightBulb.Hue === this.accessory.context.Hue) {
782
695
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
783
696
  }
784
- else if (this.On) {
697
+ else if (this.LightBulb.On) {
785
698
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
786
699
  }
787
700
  else {
788
701
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
789
702
  }
790
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
791
- this.Hue = value;
703
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
704
+ this.LightBulb.Hue = value;
792
705
  this.doStripLightUpdate.next();
793
706
  }
794
707
  /**
795
708
  * Handle requests to set the value of the "Saturation" characteristic
796
709
  */
797
710
  async SaturationSet(value) {
798
- if (this.Saturation === this.accessory.context.Saturation) {
711
+ if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
799
712
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
800
713
  }
801
- else if (this.On) {
714
+ else if (this.LightBulb.On) {
802
715
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
803
716
  }
804
717
  else {
805
718
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
806
719
  }
807
- this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
808
- this.Saturation = value;
720
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
721
+ this.LightBulb.Saturation = value;
809
722
  this.doStripLightUpdate.next();
810
723
  }
811
724
  async updateHomeKitCharacteristics() {
812
725
  // On
813
- if (this.On === undefined) {
814
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
726
+ if (this.LightBulb.On === undefined) {
727
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
815
728
  }
816
729
  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}`);
730
+ this.accessory.context.On = this.LightBulb.On;
731
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
732
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
820
733
  }
821
734
  // Brightness
822
- if (this.Brightness === undefined) {
823
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
735
+ if (this.LightBulb.Brightness === undefined) {
736
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
824
737
  }
825
738
  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}`);
739
+ this.accessory.context.Brightness = this.LightBulb.Brightness;
740
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
741
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
829
742
  }
830
743
  // ColorTemperature
831
- if (this.ColorTemperature === undefined) {
832
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
744
+ if (this.LightBulb.ColorTemperature === undefined) {
745
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
833
746
  }
834
747
  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}`);
748
+ this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
749
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
750
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
751
+ + ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
838
752
  }
839
753
  // Hue
840
- if (this.Hue === undefined) {
841
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
754
+ if (this.LightBulb.Hue === undefined) {
755
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
842
756
  }
843
757
  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}`);
758
+ this.accessory.context.Hue = this.LightBulb.Hue;
759
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
760
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
847
761
  }
848
762
  // Saturation
849
- if (this.Saturation === undefined) {
850
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
763
+ if (this.LightBulb.Saturation === undefined) {
764
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
851
765
  }
852
766
  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
- })();
767
+ this.accessory.context.Saturation = this.LightBulb.Saturation;
768
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
769
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
884
770
  }
885
771
  }
886
772
  async BLEPushConnection() {
@@ -897,34 +783,6 @@ export class StripLight {
897
783
  await this.openAPIRefreshStatus();
898
784
  }
899
785
  }
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
- minStep(device) {
920
- if (device.striplight?.set_minStep) {
921
- this.set_minStep = device.striplight?.set_minStep;
922
- }
923
- else {
924
- this.set_minStep = 1;
925
- }
926
- return this.set_minStep;
927
- }
928
786
  async adaptiveLighting(device) {
929
787
  if (device.striplight?.adaptiveLightingShift) {
930
788
  this.adaptiveLightingShift = device.striplight.adaptiveLightingShift;
@@ -935,246 +793,17 @@ export class StripLight {
935
793
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
936
794
  }
937
795
  }
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
796
  async offlineOff() {
1024
797
  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
- }
798
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
1174
799
  }
1175
800
  }
1176
- enablingDeviceLogging() {
1177
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
801
+ async apiError(e) {
802
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
803
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
804
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
805
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
806
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
1178
807
  }
1179
808
  }
1180
809
  //# sourceMappingURL=lightstrip.js.map