@switchbot/homebridge-switchbot 3.5.0-beta.23 → 3.5.0-beta.24

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 (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,73 +1,39 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * robotvacuumcleaner.ts: @switchbot/homebridge-switchbot.
4
+ */
1
5
  import { request } from 'undici';
2
- import { sleep } from '../utils.js';
3
- import { interval, Subject } from 'rxjs';
6
+ import { Subject, interval, skipWhile } from 'rxjs';
7
+ import { deviceBase } from './device.js';
4
8
  import { SwitchBotPlatform } from '../platform.js';
5
- import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
- import { device, devicesConfig, deviceStatus, serviceData, Devices, SwitchBotPlatformConfig } from '../settings.js';
8
-
9
- export class RobotVacuumCleaner {
10
- public readonly api: API;
11
- public readonly log: Logging;
12
- public readonly config!: SwitchBotPlatformConfig;
13
- protected readonly hap: HAP;
9
+ import { debounceTime, take, tap } from 'rxjs/operators';
10
+ import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
11
+ import { device, devicesConfig, deviceStatus, serviceData, Devices } from '../settings.js';
12
+
13
+ export class RobotVacuumCleaner extends deviceBase {
14
14
  // Services
15
- batteryService: Service;
16
- robotVacuumCleanerService: Service;
17
-
18
- // Characteristic Values
19
- On!: CharacteristicValue;
20
- Brightness!: CharacteristicValue;
21
- BatteryLevel!: CharacteristicValue;
22
- StatusLowBattery!: CharacteristicValue;
23
- FirmwareRevision!: CharacteristicValue;
24
-
25
- // OpenAPI Status
26
- OpenAPI_On: deviceStatus['power'];
27
- OpenAPI_BatteryLevel: deviceStatus['battery'];
28
- OpenAPI_FirmwareRevision: deviceStatus['version'];
29
-
30
- // BLE Status
31
- BLE_On: serviceData['state'];
32
-
33
- // BLE Others
34
- BLE_IsConnected?: boolean;
35
-
36
- // Config
37
- scanDuration!: number;
38
- deviceLogging!: string;
39
- deviceRefreshRate!: number;
40
- maxRetries!: number;
41
- delayBetweenRetries!: number;
15
+ private LightBulb: {
16
+ Service: Service;
17
+ On: CharacteristicValue;
18
+ Brightness: CharacteristicValue;
19
+ };
20
+
21
+ private Battery: {
22
+ Service: Service;
23
+ BatteryLevel: CharacteristicValue;
24
+ StatusLowBattery: CharacteristicValue;
25
+ };
42
26
 
43
27
  // Updates
44
28
  robotVacuumCleanerUpdateInProgress!: boolean;
45
29
  doRobotVacuumCleanerUpdate!: Subject<void>;
46
30
 
47
- // Connection
48
- private readonly OpenAPI: boolean;
49
- private readonly BLE: boolean;
50
-
51
31
  constructor(
52
- private readonly platform: SwitchBotPlatform,
53
- private accessory: PlatformAccessory,
54
- public device: device & devicesConfig,
32
+ readonly platform: SwitchBotPlatform,
33
+ accessory: PlatformAccessory,
34
+ device: device & devicesConfig,
55
35
  ) {
56
- this.api = this.platform.api;
57
- this.log = this.platform.log;
58
- this.config = this.platform.config;
59
- this.hap = this.api.hap;
60
- // Connection
61
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
62
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
63
- // default placeholders
64
- this.deviceLogs(device);
65
- this.scan(device);
66
- this.refreshRate(device);
67
- this.deviceContext();
68
- this.deviceRetry(device);
69
- this.deviceConfig(device);
70
-
36
+ super(platform, accessory, device);
71
37
  // this is subject we use to track when we need to POST changes to the SwitchBot API
72
38
  this.doRobotVacuumCleanerUpdate = new Subject();
73
39
  this.robotVacuumCleanerUpdateInProgress = false;
@@ -75,32 +41,35 @@ export class RobotVacuumCleaner {
75
41
  // Retrieve initial values and updateHomekit
76
42
  this.refreshStatus();
77
43
 
78
- // set accessory information
79
- accessory
80
- .getService(this.hap.Service.AccessoryInformation)!
81
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
82
- .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
83
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
84
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
44
+ // Initialize Lightbulb property
45
+ this.LightBulb = {
46
+ Service: this.accessory.addService(this.hap.Service.Lightbulb),
47
+ On: accessory.context.On || false,
48
+ Brightness: accessory.context.Brightness || 0,
49
+ };
50
+
51
+ // Initialize Battery property
52
+ this.Battery = {
53
+ Service: this.accessory.addService(this.hap.Service.Battery),
54
+ BatteryLevel: accessory.context.BatteryLevel || 100,
55
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
56
+ };
85
57
 
86
58
  // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
87
59
  // you can create multiple services for each accessory
88
- const robotVacuumCleanerService = `${accessory.displayName} ${device.deviceType}`;
89
- (this.robotVacuumCleanerService = accessory.getService(this.hap.Service.Lightbulb)
90
- || accessory.addService(this.hap.Service.Lightbulb)), robotVacuumCleanerService;
60
+ const LightBulbService = `${accessory.displayName} ${device.deviceType}`;
61
+ (this.LightBulb.Service = accessory.getService(this.hap.Service.Lightbulb)
62
+ || accessory.addService(this.hap.Service.Lightbulb)), LightBulbService;
91
63
 
92
- this.robotVacuumCleanerService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
93
- if (!this.robotVacuumCleanerService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
94
- this.robotVacuumCleanerService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
95
- }
64
+ this.LightBulb.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
96
65
  // each service must implement at-minimum the "required characteristics" for the given service type
97
66
  // see https://developers.homebridge.io/#/service/Lightbulb
98
67
 
99
68
  // create handlers for required characteristics
100
- this.robotVacuumCleanerService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
69
+ this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
101
70
 
102
71
  // handle Brightness events using the Brightness characteristic
103
- this.robotVacuumCleanerService
72
+ this.LightBulb.Service
104
73
  .getCharacteristic(this.hap.Characteristic.Brightness)
105
74
  .setProps({
106
75
  minStep: 25,
@@ -110,19 +79,16 @@ export class RobotVacuumCleaner {
110
79
  validValueRanges: [0, 100],
111
80
  })
112
81
  .onGet(() => {
113
- return this.Brightness;
82
+ return this.LightBulb.Brightness;
114
83
  })
115
84
  .onSet(this.BrightnessSet.bind(this));
116
85
 
117
86
  // Battery Service
118
- const batteryService = `${accessory.displayName} Battery`;
119
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
120
- || accessory.addService(this.hap.Service.Battery)), batteryService;
87
+ const BatteryService = `${accessory.displayName} Battery`;
88
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
89
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
121
90
 
122
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
123
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
124
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
125
- }
91
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
126
92
 
127
93
  // Update Homekit
128
94
  this.updateHomeKitCharacteristics();
@@ -141,13 +107,14 @@ export class RobotVacuumCleaner {
141
107
  try {
142
108
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
143
109
  const { onlineStatus, battery } = context;
144
- const { On, BatteryLevel } = this;
110
+ const { On } = this.LightBulb;
111
+ const { BatteryLevel } = this.Battery;
145
112
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
146
113
  '(onlineStatus, battery) = ' +
147
114
  `Webhook:(${onlineStatus}, ${battery}), ` +
148
115
  `current:(${On}, ${BatteryLevel})`);
149
- this.On = onlineStatus === 'online' ? true : false;
150
- this.BatteryLevel = battery;
116
+ this.LightBulb.On = onlineStatus === 'online' ? true : false;
117
+ this.Battery.BatteryLevel = battery;
151
118
  this.updateHomeKitCharacteristics();
152
119
  } catch (e: any) {
153
120
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
@@ -167,10 +134,10 @@ export class RobotVacuumCleaner {
167
134
  )
168
135
  .subscribe(async () => {
169
136
  try {
170
- if (this.On !== this.accessory.context.On) {
137
+ if (this.LightBulb.On !== this.accessory.context.On) {
171
138
  await this.pushChanges();
172
139
  }
173
- if (this.On && this.Brightness !== this.accessory.context.Brightness) {
140
+ if (this.LightBulb.On && this.LightBulb.Brightness !== this.accessory.context.Brightness) {
174
141
  await this.openAPIpushBrightnessChanges();
175
142
  }
176
143
  } catch (e: any) {
@@ -184,60 +151,55 @@ export class RobotVacuumCleaner {
184
151
  });
185
152
  }
186
153
 
187
- /**
188
- * Parse the device status from the SwitchBot api
189
- */
190
- async parseStatus(): Promise<void> {
191
- if (!this.device.enableCloudService && this.OpenAPI) {
192
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
193
- /*} else if (this.BLE) {
194
- await this.BLEparseStatus();*/
195
- } else if (this.OpenAPI && this.platform.config.credentials?.token) {
196
- await this.openAPIparseStatus();
154
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
155
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
156
+
157
+ // Battery
158
+ this.Battery.BatteryLevel = Number(serviceData.battery);
159
+ if (this.Battery.BatteryLevel < 10) {
160
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
197
161
  } else {
198
- await this.offlineOff();
199
- this.debugWarnLog(
200
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
201
- );
162
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
202
163
  }
203
- }
164
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
204
165
 
205
- async BLEparseStatus(): Promise<void> {
206
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
207
166
  // State
208
- switch (this.BLE_On) {
167
+ switch (serviceData.state) {
209
168
  case 'on':
210
- this.On = true;
169
+ this.LightBulb.On = true;
211
170
  break;
212
171
  default:
213
- this.On = false;
172
+ this.LightBulb.On = false;
214
173
  }
215
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
174
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
216
175
  }
217
176
 
218
- async openAPIparseStatus() {
177
+ async openAPIparseStatus(deviceStatus: deviceStatus) {
219
178
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
220
- switch (this.OpenAPI_On) {
179
+ switch (deviceStatus.body.power) {
221
180
  case 'on':
222
- this.On = true;
181
+ this.LightBulb.On = true;
223
182
  break;
224
183
  default:
225
- this.On = false;
184
+ this.LightBulb.On = false;
226
185
  }
227
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
186
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
228
187
 
229
- // Battery
230
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
231
- if (this.BatteryLevel < 15) {
232
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
188
+ // BatteryLevel
189
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
190
+ if (this.Battery.BatteryLevel < 10) {
191
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
233
192
  } else {
234
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
193
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
235
194
  }
236
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
195
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
196
+ this.Battery.BatteryLevel = 100;
197
+ }
198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
199
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
237
200
 
238
201
  // FirmwareRevision
239
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
240
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
202
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
241
203
  }
242
204
 
243
205
  /**
@@ -281,7 +243,6 @@ export class RobotVacuumCleaner {
281
243
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
282
244
  if (this.device.bleMac === ad.address && ad.serviceData.model === '?') {
283
245
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
284
- this.BLE_On = ad.serviceData.state;
285
246
  } else {
286
247
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
287
248
  }
@@ -291,7 +252,7 @@ export class RobotVacuumCleaner {
291
252
  // Stop to monitor
292
253
  await switchbot.stopScan();
293
254
  // Update HomeKit
294
- await this.BLEparseStatus();
255
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
295
256
  await this.updateHomeKitCharacteristics();
296
257
  })();
297
258
  /*if (switchbot !== false) {
@@ -307,7 +268,7 @@ export class RobotVacuumCleaner {
307
268
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
308
269
  ` BLE Address Found: ${ad.address}`,
309
270
  );
310
- this.BLE_On = ad.serviceData.state;
271
+ serviceData.state = ad.serviceData.state;
311
272
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
312
273
  this.debugLog(
313
274
  `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
@@ -347,7 +308,7 @@ export class RobotVacuumCleaner {
347
308
  async openAPIRefreshStatus() {
348
309
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
349
310
  try {
350
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries,
311
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
351
312
  `${Devices}/${this.device.deviceId}/status`, {
352
313
  headers: this.platform.generateHeaders(),
353
314
  });
@@ -358,10 +319,7 @@ export class RobotVacuumCleaner {
358
319
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
359
320
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
360
321
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
361
- this.OpenAPI_On = deviceStatus.body.power;
362
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
363
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
364
- this.openAPIparseStatus();
322
+ this.openAPIparseStatus(deviceStatus);
365
323
  this.updateHomeKitCharacteristics();
366
324
  } else {
367
325
  this.statusCode(statusCode);
@@ -409,8 +367,9 @@ export class RobotVacuumCleaner {
409
367
 
410
368
  async BLEpushChanges(): Promise<void> {
411
369
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
412
- if (this.On !== this.accessory.context.On) {
413
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
370
+ if (this.LightBulb.On !== this.accessory.context.On) {
371
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
372
+ + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
414
373
  const switchbot = await this.platform.connectBLE();
415
374
  // Convert to BLE Address
416
375
  this.device.bleMac = this.device
@@ -424,11 +383,11 @@ export class RobotVacuumCleaner {
424
383
  id: this.device.bleMac,
425
384
  })
426
385
  .then(async (device_list: any) => {
427
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
428
- return await this.retry({
429
- max: this.maxRetry(),
386
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
387
+ return await this.retryBLE({
388
+ max: await this.maxRetryBLE(),
430
389
  fn: async () => {
431
- if (this.On) {
390
+ if (this.LightBulb.On) {
432
391
  return await device_list[0].turnOn({ id: this.device.bleMac });
433
392
  } else {
434
393
  return await device_list[0].turnOff({ id: this.device.bleMac });
@@ -439,8 +398,8 @@ export class RobotVacuumCleaner {
439
398
  .then(() => {
440
399
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
441
400
  this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
442
- + `On: ${this.On} sent over BLE, sent successfully`);
443
- this.On = false;
401
+ + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
402
+ this.LightBulb.On = false;
444
403
  })
445
404
  .catch(async (e: any) => {
446
405
  this.apiError(e);
@@ -451,26 +410,15 @@ export class RobotVacuumCleaner {
451
410
  await this.BLEPushConnection();
452
411
  });
453
412
  } else {
454
- this.debugLog(
455
- `${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`,
456
- );
413
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
414
+ + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
457
415
  }
458
416
  }
459
417
 
460
418
  async openAPIpushChanges() {
461
419
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
462
- if (this.On !== this.accessory.context.On) {
463
- let command = '';
464
- if (this.On) {
465
- command = 'turnOn';
466
- } else {
467
- command = 'turnOff';
468
- }
469
- const bodyChange = JSON.stringify({
470
- command: `${command}`,
471
- parameter: 'default',
472
- commandType: 'command',
473
- });
420
+ if (this.LightBulb.On !== this.accessory.context.On) {
421
+ const bodyChange = await this.commands();
474
422
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
475
423
  try {
476
424
  const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
@@ -502,7 +450,7 @@ export class RobotVacuumCleaner {
502
450
  } else {
503
451
  this.debugLog(
504
452
  `${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
505
- `On: ${this.On}, ` +
453
+ `On: ${this.LightBulb.On}, ` +
506
454
  `OnCached: ${this.accessory.context.On}`,
507
455
  );
508
456
  }
@@ -510,7 +458,7 @@ export class RobotVacuumCleaner {
510
458
 
511
459
  async openAPIpushBrightnessChanges() {
512
460
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushBrightnessChanges`);
513
- const bodyChange = this.brightnessCommands();
461
+ const bodyChange = await this.brightnessCommands();
514
462
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
515
463
  try {
516
464
  const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
@@ -540,10 +488,10 @@ export class RobotVacuumCleaner {
540
488
  }
541
489
  }
542
490
 
543
- private commands() {
491
+ async commands() {
544
492
  let command: string;
545
493
  let parameter: string;
546
- if (this.On) {
494
+ if (this.LightBulb.On) {
547
495
  command = 'start';
548
496
  parameter = 'default';
549
497
  } else {
@@ -558,19 +506,19 @@ export class RobotVacuumCleaner {
558
506
  return body;
559
507
  }
560
508
 
561
- private brightnessCommands() {
509
+ async brightnessCommands(): Promise<string> {
562
510
  let command: string;
563
511
  let parameter: string;
564
- if (this.Brightness === 25) {
512
+ if (this.LightBulb.Brightness === 25) {
565
513
  command = 'PowLevel';
566
514
  parameter = '0';
567
- } else if (this.Brightness === 50) {
515
+ } else if (this.LightBulb.Brightness === 50) {
568
516
  command = 'PowLevel';
569
517
  parameter = '1';
570
- } else if (this.Brightness === 75) {
518
+ } else if (this.LightBulb.Brightness === 75) {
571
519
  command = 'PowLevel';
572
520
  parameter = '2';
573
- } else if (this.Brightness === 100) {
521
+ } else if (this.LightBulb.Brightness === 100) {
574
522
  command = 'PowLevel';
575
523
  parameter = '3';
576
524
  } else {
@@ -589,13 +537,13 @@ export class RobotVacuumCleaner {
589
537
  * Handle requests to set the value of the "On" characteristic
590
538
  */
591
539
  async OnSet(value: CharacteristicValue): Promise<void> {
592
- if (this.On === this.accessory.context.On) {
540
+ if (this.LightBulb.On === this.accessory.context.On) {
593
541
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
594
542
  } else {
595
543
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
596
544
  }
597
545
 
598
- this.On = value;
546
+ this.LightBulb.On = value;
599
547
  this.doRobotVacuumCleanerUpdate.next();
600
548
  }
601
549
 
@@ -603,78 +551,51 @@ export class RobotVacuumCleaner {
603
551
  * Handle requests to set the value of the "Brightness" characteristic
604
552
  */
605
553
  async BrightnessSet(value: CharacteristicValue): Promise<void> {
606
- if (this.Brightness === this.accessory.context.Brightness) {
554
+ if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
607
555
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
608
- } else if (this.On) {
556
+ } else if (this.LightBulb.On) {
609
557
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
610
558
  } else {
611
559
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
612
560
  }
613
561
 
614
- this.Brightness = value;
562
+ this.LightBulb.Brightness = value;
615
563
  this.doRobotVacuumCleanerUpdate.next();
616
564
  }
617
565
 
618
566
  async updateHomeKitCharacteristics(): Promise<void> {
619
567
  // On
620
- if (this.On === undefined) {
621
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
568
+ if (this.LightBulb.On === undefined) {
569
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
622
570
  } else {
623
- this.accessory.context.On = this.On;
624
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, this.On);
625
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
571
+ this.accessory.context.On = this.LightBulb.On;
572
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
573
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
626
574
  }
627
575
  // Brightness
628
- if (this.Brightness === undefined) {
629
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
576
+ if (this.LightBulb.Brightness === undefined) {
577
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
630
578
  } else {
631
- this.accessory.context.Brightness = this.Brightness;
632
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
633
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
579
+ this.accessory.context.Brightness = this.LightBulb.Brightness;
580
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
581
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
634
582
  }
635
583
  // BatteryLevel
636
- if (this.BatteryLevel === undefined) {
637
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
584
+ if (this.Battery.BatteryLevel === undefined) {
585
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
638
586
  } else {
639
- this.accessory.context.BatteryLevel = this.BatteryLevel;
640
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
641
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
587
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
588
+ this.Battery.Service?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
589
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
642
590
  }
643
591
  // StatusLowBattery
644
- if (this.StatusLowBattery === undefined) {
645
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
592
+ if (this.Battery.StatusLowBattery === undefined) {
593
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
646
594
  } else {
647
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
648
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
649
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
650
- }
651
- }
652
-
653
- async stopScanning(switchbot: any) {
654
- switchbot.stopScan();
655
- if (this.BLE_IsConnected) {
656
- await this.BLEparseStatus();
657
- await this.updateHomeKitCharacteristics();
658
- } else {
659
- await this.BLERefreshConnection(switchbot);
660
- }
661
- }
662
-
663
- async getCustomBLEAddress(switchbot: any) {
664
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
665
- (async () => {
666
- // Start to monitor advertisement packets
667
- await switchbot.startScan({
668
- model: '?',
669
- });
670
- // Set an event handler
671
- switchbot.onadvertisement = (ad: any) => {
672
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
673
- };
674
- await sleep(10000);
675
- // Stop to monitor
676
- switchbot.stopScan();
677
- })();
595
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
596
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
597
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
598
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
678
599
  }
679
600
  }
680
601
 
@@ -694,289 +615,13 @@ export class RobotVacuumCleaner {
694
615
  }
695
616
  }
696
617
 
697
- async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
698
- return fn().catch(async (e: any) => {
699
- if (max === 0) {
700
- throw e;
701
- }
702
- this.infoLog(e);
703
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
704
- await sleep(1000);
705
- return this.retry({ max: max - 1, fn });
706
- });
707
- }
708
-
709
- maxRetry(): number {
710
- if (this.device.maxRetry) {
711
- return this.device.maxRetry;
712
- } else {
713
- return 5;
714
- }
715
- }
716
-
717
- model(device: device & devicesConfig): string {
718
- let model: string;
719
- if (device.deviceType === 'Robot Vacuum Cleaner S1 Plus') {
720
- model = 'W3011010';
721
- } else if (device.deviceType === 'Robot Vacuum Cleaner S1') {
722
- model = 'W3011000';
723
- } else {
724
- model = 'N/A';
725
- }
726
- return model;
727
- }
728
-
729
- async scan(device: device & devicesConfig): Promise<void> {
730
- if (device.scanDuration) {
731
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
732
- if (this.BLE) {
733
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
734
- }
735
- } else {
736
- this.scanDuration = this.accessory.context.scanDuration = 1;
737
- if (this.BLE) {
738
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
739
- }
740
- }
741
- }
742
-
743
- async statusCode(statusCode: number): Promise<void> {
744
- switch (statusCode) {
745
- case 151:
746
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
747
- break;
748
- case 152:
749
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
750
- break;
751
- case 160:
752
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
753
- break;
754
- case 161:
755
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
756
- this.offlineOff();
757
- break;
758
- case 171:
759
- this.errorLog(
760
- `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
761
- `Hub: ${this.device.hubDeviceId}`,
762
- );
763
- this.offlineOff();
764
- break;
765
- case 190:
766
- this.errorLog(
767
- `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
768
- ` Or command format is invalid, statusCode: ${statusCode}`,
769
- );
770
- break;
771
- case 100:
772
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
773
- break;
774
- case 200:
775
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
776
- break;
777
- case 400:
778
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
779
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
780
- break;
781
- case 401:
782
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
783
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
784
- break;
785
- case 403:
786
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
787
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
788
- break;
789
- case 404:
790
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
791
- + `statusCode: ${statusCode}`);
792
- break;
793
- case 406:
794
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
795
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
796
- break;
797
- case 415:
798
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
799
- + `header that is not supported by the server, statusCode: ${statusCode}`);
800
- break;
801
- case 422:
802
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
803
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
804
- break;
805
- case 429:
806
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
807
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
808
- break;
809
- case 500:
810
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
811
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
812
- break;
813
- default:
814
- this.infoLog(
815
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
816
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
817
- );
818
- }
819
- }
820
-
821
618
  async offlineOff(): Promise<void> {
822
619
  if (this.device.offline) {
823
- await this.deviceContext();
824
- await this.updateHomeKitCharacteristics();
620
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
825
621
  }
826
622
  }
827
623
 
828
624
  async apiError(e: any): Promise<void> {
829
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, e);
830
- }
831
-
832
- async deviceContext() {
833
- if (this.On === undefined) {
834
- this.On = false;
835
- } else {
836
- this.On = this.accessory.context.On;
837
- }
838
- if (this.Brightness === undefined) {
839
- this.Brightness = 0;
840
- } else {
841
- this.Brightness = this.accessory.context.Brightness;
842
- }
843
- if (this.FirmwareRevision === undefined) {
844
- this.FirmwareRevision = this.platform.version;
845
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
846
- }
847
- }
848
-
849
- async refreshRate(device: device & devicesConfig): Promise<void> {
850
- if (device.refreshRate) {
851
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
852
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
853
- } else if (this.platform.config.options!.refreshRate) {
854
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
855
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
856
- }
857
- }
858
-
859
- async deviceRetry(device: device & devicesConfig): Promise<void> {
860
- if (!device.maxRetries) {
861
- this.maxRetries = 5; // Maximum number of retries
862
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
863
- } else {
864
- this.maxRetries = device.maxRetries;
865
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
866
- }
867
- if (!device.delayBetweenRetries) {
868
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
869
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
870
- } else {
871
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
872
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
873
- }
874
- }
875
-
876
- async deviceConfig(device: device & devicesConfig): Promise<void> {
877
- let config = {};
878
- if (device.plug) {
879
- config = device.plug;
880
- }
881
- if (device.connectionType !== undefined) {
882
- config['connectionType'] = device.connectionType;
883
- }
884
- if (device.external !== undefined) {
885
- config['external'] = device.external;
886
- }
887
- if (device.logging !== undefined) {
888
- config['logging'] = device.logging;
889
- }
890
- if (device.refreshRate !== undefined) {
891
- config['refreshRate'] = device.refreshRate;
892
- }
893
- if (device.scanDuration !== undefined) {
894
- config['scanDuration'] = device.scanDuration;
895
- }
896
- if (device.offline !== undefined) {
897
- config['offline'] = device.offline;
898
- }
899
- if (device.maxRetry !== undefined) {
900
- config['maxRetry'] = device.maxRetry;
901
- }
902
- if (device.webhook !== undefined) {
903
- config['webhook'] = device.webhook;
904
- }
905
- if (Object.entries(config).length !== 0) {
906
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
907
- }
908
- }
909
-
910
- async deviceLogs(device: device & devicesConfig): Promise<void> {
911
- if (this.platform.debugMode) {
912
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
913
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
914
- } else if (device.logging) {
915
- this.deviceLogging = this.accessory.context.logging = device.logging;
916
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
917
- } else if (this.platform.config.options?.logging) {
918
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
919
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
920
- } else {
921
- this.deviceLogging = this.accessory.context.logging = 'standard';
922
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
923
- }
924
- }
925
-
926
- /**
927
- * Logging for Device
928
- */
929
- successLog(...log: any[]): void {
930
- if (this.enablingDeviceLogging()) {
931
- this.platform.log.success(String(...log));
932
- }
933
- }
934
-
935
- infoLog(...log: any[]): void {
936
- if (this.enablingDeviceLogging()) {
937
- this.platform.log.info(String(...log));
938
- }
939
- }
940
-
941
- warnLog(...log: any[]): void {
942
- if (this.enablingDeviceLogging()) {
943
- this.platform.log.warn(String(...log));
944
- }
945
- }
946
-
947
- debugWarnLog(...log: any[]): void {
948
- if (this.enablingDeviceLogging()) {
949
- if (this.deviceLogging?.includes('debug')) {
950
- this.platform.log.warn('[DEBUG]', String(...log));
951
- }
952
- }
953
- }
954
-
955
- errorLog(...log: any[]): void {
956
- if (this.enablingDeviceLogging()) {
957
- this.platform.log.error(String(...log));
958
- }
959
- }
960
-
961
- debugErrorLog(...log: any[]): void {
962
- if (this.enablingDeviceLogging()) {
963
- if (this.deviceLogging?.includes('debug')) {
964
- this.platform.log.error('[DEBUG]', String(...log));
965
- }
966
- }
967
- }
968
-
969
- debugLog(...log: any[]): void {
970
- if (this.enablingDeviceLogging()) {
971
- if (this.deviceLogging === 'debug') {
972
- this.platform.log.info('[DEBUG]', String(...log));
973
- } else {
974
- this.platform.log.debug(String(...log));
975
- }
976
- }
977
- }
978
-
979
- enablingDeviceLogging(): boolean {
980
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
625
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
981
626
  }
982
627
  }