@switchbot/homebridge-switchbot 3.5.0-beta.3 → 3.5.0-beta.30

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