@switchbot/homebridge-switchbot 3.5.0-beta.7 → 3.5.0-beta.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +120 -17
  4. package/dist/device/blindtilt.d.ts +23 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +425 -778
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +26 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +803 -837
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +14 -68
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +241 -606
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +14 -75
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +283 -658
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +15 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +254 -545
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +19 -88
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +412 -806
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +59 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +711 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +51 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +540 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +16 -60
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +261 -616
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +240 -594
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +11 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +196 -600
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +11 -69
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +281 -586
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +16 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +252 -560
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +14 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +191 -593
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +14 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +180 -593
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +13 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +188 -532
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +9 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +96 -458
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +12 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +202 -521
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +10 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +146 -475
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +324 -452
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +93 -307
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +39 -281
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +10 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +113 -341
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +40 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +330 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +118 -362
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +438 -570
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +87 -335
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +45 -285
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +47 -291
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +19 -6
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +310 -172
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +62 -19
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +77 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +638 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +19 -15
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +462 -817
  143. package/src/device/bot.ts +862 -931
  144. package/src/device/ceilinglight.ts +259 -633
  145. package/src/device/colorbulb.ts +300 -688
  146. package/src/device/contact.ts +288 -571
  147. package/src/device/curtain.ts +441 -838
  148. package/src/device/device.ts +734 -0
  149. package/src/device/fan.ts +596 -0
  150. package/src/device/hub.ts +299 -645
  151. package/src/device/humidifier.ts +274 -647
  152. package/src/device/iosensor.ts +233 -626
  153. package/src/device/lightstrip.ts +309 -620
  154. package/src/device/lock.ts +292 -601
  155. package/src/device/meter.ts +228 -619
  156. package/src/device/meterplus.ts +216 -619
  157. package/src/device/motion.ts +218 -559
  158. package/src/device/plug.ts +111 -490
  159. package/src/device/robotvacuumcleaner.ts +228 -550
  160. package/src/device/waterdetector.ts +169 -501
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +350 -477
  165. package/src/irdevice/airpurifier.ts +120 -330
  166. package/src/irdevice/camera.ts +53 -301
  167. package/src/irdevice/fan.ts +130 -360
  168. package/src/irdevice/irdevice.ts +346 -0
  169. package/src/irdevice/light.ts +157 -412
  170. package/src/irdevice/other.ts +507 -648
  171. package/src/irdevice/tv.ts +117 -367
  172. package/src/irdevice/vacuumcleaner.ts +59 -311
  173. package/src/irdevice/waterheater.ts +61 -320
  174. package/src/platform.ts +355 -219
  175. package/src/settings.ts +116 -611
  176. package/src/utils.ts +642 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,87 +1,45 @@
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';
4
- import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
6
  import { Devices } from '../settings.js';
6
- export class RobotVacuumCleaner {
7
+ import { deviceBase } from './device.js';
8
+ import { Subject, interval, skipWhile } from 'rxjs';
9
+ import { debounceTime, take, tap } from 'rxjs/operators';
10
+ export class RobotVacuumCleaner extends deviceBase {
7
11
  platform;
8
- accessory;
9
- device;
10
- api;
11
- log;
12
- config;
13
- hap;
14
12
  // Services
15
- batteryService;
16
- robotVacuumCleanerService;
17
- // Characteristic Values
18
- On;
19
- Brightness;
20
- BatteryLevel;
21
- StatusLowBattery;
22
- FirmwareRevision;
23
- // OpenAPI Status
24
- OpenAPI_On;
25
- OpenAPI_BatteryLevel;
26
- OpenAPI_FirmwareRevision;
27
- // BLE Status
28
- BLE_On;
29
- // BLE Others
30
- BLE_IsConnected;
31
- // Config
32
- scanDuration;
33
- deviceLogging;
34
- deviceRefreshRate;
13
+ LightBulb;
14
+ Battery;
35
15
  // Updates
36
16
  robotVacuumCleanerUpdateInProgress;
37
17
  doRobotVacuumCleanerUpdate;
38
- // Connection
39
- OpenAPI;
40
- BLE;
41
18
  constructor(platform, accessory, device) {
19
+ super(platform, accessory, device);
42
20
  this.platform = platform;
43
- this.accessory = accessory;
44
- this.device = device;
45
- this.api = this.platform.api;
46
- this.log = this.platform.log;
47
- this.config = this.platform.config;
48
- this.hap = this.api.hap;
49
- // Connection
50
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
51
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
52
- // default placeholders
53
- this.deviceLogs(device);
54
- this.scan(device);
55
- this.refreshRate(device);
56
- this.deviceContext();
57
- this.deviceConfig(device);
58
21
  // this is subject we use to track when we need to POST changes to the SwitchBot API
59
22
  this.doRobotVacuumCleanerUpdate = new Subject();
60
23
  this.robotVacuumCleanerUpdateInProgress = false;
61
- // Retrieve initial values and updateHomekit
62
- this.refreshStatus();
63
- // set accessory information
64
- accessory
65
- .getService(this.hap.Service.AccessoryInformation)
66
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
67
- .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
68
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
69
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
70
- // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
71
- // you can create multiple services for each accessory
72
- const robotVacuumCleanerService = `${accessory.displayName} ${device.deviceType}`;
73
- (this.robotVacuumCleanerService = accessory.getService(this.hap.Service.Lightbulb)
74
- || accessory.addService(this.hap.Service.Lightbulb)), robotVacuumCleanerService;
75
- this.robotVacuumCleanerService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
76
- if (!this.robotVacuumCleanerService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
77
- this.robotVacuumCleanerService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
78
- }
79
- // each service must implement at-minimum the "required characteristics" for the given service type
80
- // see https://developers.homebridge.io/#/service/Lightbulb
81
- // create handlers for required characteristics
82
- this.robotVacuumCleanerService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
83
- // handle Brightness events using the Brightness characteristic
84
- this.robotVacuumCleanerService
24
+ // Initialize Lightbulb Service
25
+ accessory.context.LightBulb = accessory.context.LightBulb ?? {};
26
+ this.LightBulb = {
27
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
28
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb),
29
+ On: accessory.context.On ?? false,
30
+ Brightness: accessory.context.Brightness ?? 0,
31
+ };
32
+ accessory.context.LightBulb = this.LightBulb;
33
+ // Initialize LightBulb Characteristics
34
+ this.LightBulb.Service
35
+ .setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
36
+ .getCharacteristic(this.hap.Characteristic.On)
37
+ .onGet(() => {
38
+ return this.LightBulb.On;
39
+ })
40
+ .onSet(this.OnSet.bind(this));
41
+ // Initialize LightBulb Brightness Characteristic
42
+ this.LightBulb.Service
85
43
  .getCharacteristic(this.hap.Characteristic.Brightness)
86
44
  .setProps({
87
45
  minStep: 25,
@@ -91,17 +49,38 @@ export class RobotVacuumCleaner {
91
49
  validValueRanges: [0, 100],
92
50
  })
93
51
  .onGet(() => {
94
- return this.Brightness;
52
+ return this.LightBulb.Brightness;
95
53
  })
96
54
  .onSet(this.BrightnessSet.bind(this));
97
- // Battery Service
98
- const batteryService = `${accessory.displayName} Battery`;
99
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
100
- || accessory.addService(this.hap.Service.Battery)), batteryService;
101
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
102
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
103
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
104
- }
55
+ // Initialize Battery Service
56
+ accessory.context.Battery = accessory.context.Battery ?? {};
57
+ this.Battery = {
58
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
59
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery),
60
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
61
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
62
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
63
+ };
64
+ accessory.context.Battery = this.Battery;
65
+ // Initialize Battery Characteristics
66
+ this.Battery.Service
67
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
68
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
69
+ .onGet(() => {
70
+ return this.Battery.BatteryLevel;
71
+ });
72
+ this.Battery.Service
73
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
74
+ .onGet(() => {
75
+ return this.Battery.StatusLowBattery;
76
+ });
77
+ this.Battery.Service
78
+ .getCharacteristic(this.hap.Characteristic.ChargingState)
79
+ .onGet(() => {
80
+ return this.Battery.ChargingState;
81
+ });
82
+ // Retrieve initial values and updateHomekit
83
+ this.refreshStatus();
105
84
  // Update Homekit
106
85
  this.updateHomeKitCharacteristics();
107
86
  // Start an update interval
@@ -111,19 +90,22 @@ export class RobotVacuumCleaner {
111
90
  await this.refreshStatus();
112
91
  });
113
92
  //regisiter webhook event handler
114
- if (this.device.webhook) {
115
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
93
+ if (device.webhook) {
94
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
116
95
  this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
117
96
  try {
118
97
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
119
- const { onlineStatus, battery } = context;
120
- const { On, BatteryLevel } = this;
98
+ const { onlineStatus, battery, workingStatus } = context;
99
+ const { On } = this.LightBulb;
100
+ const { BatteryLevel, ChargingState } = this.Battery;
121
101
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
122
- '(onlineStatus, battery) = ' +
123
- `Webhook:(${onlineStatus}, ${battery}), ` +
124
- `current:(${On}, ${BatteryLevel})`);
125
- this.On = onlineStatus === 'online' ? true : false;
126
- this.BatteryLevel = battery;
102
+ '(onlineStatus, battery, workingStatus) = ' +
103
+ `Webhook:(${onlineStatus}, ${battery}, ${workingStatus}), ` +
104
+ `current:(${On}, ${BatteryLevel}, ${ChargingState})`);
105
+ this.LightBulb.On = onlineStatus === 'online' ? true : false;
106
+ this.Battery.ChargingState = workingStatus === 'Charging'
107
+ ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
108
+ this.Battery.BatteryLevel = battery;
127
109
  this.updateHomeKitCharacteristics();
128
110
  }
129
111
  catch (e) {
@@ -140,10 +122,10 @@ export class RobotVacuumCleaner {
140
122
  }), debounceTime(this.platform.config.options.pushRate * 1000))
141
123
  .subscribe(async () => {
142
124
  try {
143
- if (this.On !== this.accessory.context.On) {
125
+ if (this.LightBulb.On !== this.accessory.context.On) {
144
126
  await this.pushChanges();
145
127
  }
146
- if (this.On && this.Brightness !== this.accessory.context.Brightness) {
128
+ if (this.LightBulb.On && this.LightBulb.Brightness !== this.accessory.context.Brightness) {
147
129
  await this.openAPIpushBrightnessChanges();
148
130
  }
149
131
  }
@@ -155,57 +137,68 @@ export class RobotVacuumCleaner {
155
137
  this.robotVacuumCleanerUpdateInProgress = false;
156
138
  });
157
139
  }
158
- /**
159
- * Parse the device status from the SwitchBot api
160
- */
161
- async parseStatus() {
162
- if (!this.device.enableCloudService && this.OpenAPI) {
163
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
164
- /*} else if (this.BLE) {
165
- await this.BLEparseStatus();*/
166
- }
167
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
168
- await this.openAPIparseStatus();
140
+ async BLEparseStatus(serviceData) {
141
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
142
+ // Battery
143
+ this.Battery.BatteryLevel = Number(serviceData.battery);
144
+ if (this.Battery.BatteryLevel < 10) {
145
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
169
146
  }
170
147
  else {
171
- await this.offlineOff();
172
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
148
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
173
149
  }
174
- }
175
- async BLEparseStatus() {
176
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
150
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
177
151
  // State
178
- switch (this.BLE_On) {
152
+ switch (serviceData.state) {
179
153
  case 'on':
180
- this.On = true;
154
+ this.LightBulb.On = true;
181
155
  break;
182
156
  default:
183
- this.On = false;
157
+ this.LightBulb.On = false;
184
158
  }
185
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
159
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
186
160
  }
187
- async openAPIparseStatus() {
161
+ async openAPIparseStatus(deviceStatus) {
188
162
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
189
- switch (this.OpenAPI_On) {
163
+ switch (deviceStatus.body.power) {
190
164
  case 'on':
191
- this.On = true;
165
+ this.LightBulb.On = true;
192
166
  break;
193
167
  default:
194
- this.On = false;
168
+ this.LightBulb.On = false;
195
169
  }
196
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
197
- // Battery
198
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
199
- if (this.BatteryLevel < 15) {
200
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
170
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
171
+ // BatteryLevel
172
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
173
+ // StatusLowBattery
174
+ if (this.Battery.BatteryLevel < 10) {
175
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
201
176
  }
202
177
  else {
203
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
178
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
179
+ }
180
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
181
+ this.Battery.BatteryLevel = 100;
182
+ }
183
+ // ChargingState
184
+ this.Battery.ChargingState = deviceStatus.body.workingStatus === 'Charging'
185
+ ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
186
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
187
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}, ChargingState: ${this.Battery.ChargingState}`);
188
+ // Firmware Version
189
+ const version = deviceStatus.body.version?.toString();
190
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
191
+ if (deviceStatus.body.version) {
192
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
193
+ this.accessory
194
+ .getService(this.hap.Service.AccessoryInformation)
195
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
196
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
197
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
198
+ .updateValue(deviceVersion);
199
+ this.accessory.context.deviceVersion = deviceVersion;
200
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
204
201
  }
205
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
206
- // FirmwareRevision
207
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
208
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
209
202
  }
210
203
  /**
211
204
  * Asks the SwitchBot API for the latest device information
@@ -213,8 +206,9 @@ export class RobotVacuumCleaner {
213
206
  async refreshStatus() {
214
207
  if (!this.device.enableCloudService && this.OpenAPI) {
215
208
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
216
- /*} else if (this.BLE) {
217
- await this.BLERefreshStatus();*/
209
+ }
210
+ else if (this.BLE) {
211
+ await this.BLERefreshStatus();
218
212
  }
219
213
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
220
214
  await this.openAPIRefreshStatus();
@@ -237,96 +231,43 @@ export class RobotVacuumCleaner {
237
231
  this.getCustomBLEAddress(switchbot);
238
232
  // Start to monitor advertisement packets
239
233
  (async () => {
240
- await switchbot.startScan({
241
- model: '?',
242
- id: this.device.bleMac,
243
- });
234
+ // Start to monitor advertisement packets
235
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
244
236
  // Set an event handler
245
237
  switchbot.onadvertisement = (ad) => {
246
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
247
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
248
- if (this.device.bleMac === ad.address && ad.serviceData.model === '?') {
238
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
239
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
240
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
249
241
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
250
- this.BLE_On = ad.serviceData.state;
251
242
  }
252
243
  else {
253
244
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
254
245
  }
255
246
  };
256
- // Wait 1 seconds
247
+ // Wait 10 seconds
257
248
  await switchbot.wait(this.scanDuration * 1000);
258
249
  // Stop to monitor
259
250
  await switchbot.stopScan();
260
251
  // Update HomeKit
261
- await this.BLEparseStatus();
252
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
262
253
  await this.updateHomeKitCharacteristics();
263
254
  })();
264
- /*if (switchbot !== false) {
265
- switchbot
266
- .startScan({
267
- model: this.BLEmodel(),
268
- id: this.device.bleMac,
269
- })
270
- .then(async () => {
271
- // Set an event handler
272
- switchbot.onadvertisement = async (ad: ad) => {
273
- this.debugLog(
274
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
275
- ` BLE Address Found: ${ad.address}`,
276
- );
277
- this.BLE_On = ad.serviceData.state;
278
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
279
- this.debugLog(
280
- `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
281
- `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
282
- `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
283
- );
284
-
285
- if (ad.serviceData) {
286
- this.BLE_IsConnected = true;
287
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
288
- await this.stopScanning(switchbot);
289
- } else {
290
- this.BLE_IsConnected = false;
291
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
292
- }
293
- };
294
- // Wait
295
- return await sleep(this.scanDuration * 1000);
296
- })
297
- .then(async () => {
298
- // Stop to monitor
299
- await this.stopScanning(switchbot);
300
- })
301
- .catch(async (e: any) => {
302
- this.apiError(e);
303
- this.errorLog(
304
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
305
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
306
- );
307
- await this.BLERefreshConnection(switchbot);
308
- });
309
- } else {
310
- await this.BLERefreshConnection(switchbot);
311
- }*/
255
+ if (switchbot === undefined) {
256
+ await this.BLERefreshConnection(switchbot);
257
+ }
312
258
  }
313
259
  async openAPIRefreshStatus() {
314
260
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
315
261
  try {
316
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
317
- headers: this.platform.generateHeaders(),
318
- });
262
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
319
263
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
320
264
  const deviceStatus = await body.json();
321
265
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
322
266
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
323
267
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
324
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
268
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
325
269
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
326
- this.OpenAPI_On = deviceStatus.body.power;
327
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
328
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
329
- this.openAPIparseStatus();
270
+ this.openAPIparseStatus(deviceStatus);
330
271
  this.updateHomeKitCharacteristics();
331
272
  }
332
273
  else {
@@ -371,8 +312,9 @@ export class RobotVacuumCleaner {
371
312
  }
372
313
  async BLEpushChanges() {
373
314
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
374
- if (this.On !== this.accessory.context.On) {
375
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
315
+ if (this.LightBulb.On !== this.accessory.context.On) {
316
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
317
+ + ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
376
318
  const switchbot = await this.platform.connectBLE();
377
319
  // Convert to BLE Address
378
320
  this.device.bleMac = this.device
@@ -386,11 +328,11 @@ export class RobotVacuumCleaner {
386
328
  id: this.device.bleMac,
387
329
  })
388
330
  .then(async (device_list) => {
389
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
390
- return await this.retry({
391
- max: this.maxRetry(),
331
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
332
+ return await this.retryBLE({
333
+ max: await this.maxRetryBLE(),
392
334
  fn: async () => {
393
- if (this.On) {
335
+ if (this.LightBulb.On) {
394
336
  return await device_list[0].turnOn({ id: this.device.bleMac });
395
337
  }
396
338
  else {
@@ -401,7 +343,9 @@ export class RobotVacuumCleaner {
401
343
  })
402
344
  .then(() => {
403
345
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
404
- this.On = false;
346
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
347
+ + `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
348
+ this.LightBulb.On = false;
405
349
  })
406
350
  .catch(async (e) => {
407
351
  this.apiError(e);
@@ -411,24 +355,14 @@ export class RobotVacuumCleaner {
411
355
  });
412
356
  }
413
357
  else {
414
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`);
358
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
359
+ + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
415
360
  }
416
361
  }
417
362
  async openAPIpushChanges() {
418
363
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
419
- if (this.On !== this.accessory.context.On) {
420
- let command = '';
421
- if (this.On) {
422
- command = 'turnOn';
423
- }
424
- else {
425
- command = 'turnOff';
426
- }
427
- const bodyChange = JSON.stringify({
428
- command: `${command}`,
429
- parameter: 'default',
430
- commandType: 'command',
431
- });
364
+ if (this.LightBulb.On !== this.accessory.context.On) {
365
+ const bodyChange = await this.commands();
432
366
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
433
367
  try {
434
368
  const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
@@ -444,6 +378,8 @@ export class RobotVacuumCleaner {
444
378
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
445
379
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
446
380
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
381
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
382
+ + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
447
383
  }
448
384
  else {
449
385
  this.statusCode(statusCode);
@@ -458,16 +394,17 @@ export class RobotVacuumCleaner {
458
394
  }
459
395
  else {
460
396
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
461
- `On: ${this.On}, ` +
397
+ `On: ${this.LightBulb.On}, ` +
462
398
  `OnCached: ${this.accessory.context.On}`);
463
399
  }
464
400
  }
465
401
  async openAPIpushBrightnessChanges() {
466
402
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushBrightnessChanges`);
467
- const body = await this.brightnessCommands();
468
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
403
+ const bodyChange = await this.brightnessCommands();
404
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
469
405
  try {
470
406
  const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
407
+ body: bodyChange,
471
408
  method: 'POST',
472
409
  headers: this.platform.generateHeaders(),
473
410
  });
@@ -476,8 +413,10 @@ export class RobotVacuumCleaner {
476
413
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
477
414
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
478
415
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
479
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
416
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
480
417
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
418
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
419
+ + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
481
420
  }
482
421
  else {
483
422
  this.statusCode(statusCode);
@@ -490,10 +429,10 @@ export class RobotVacuumCleaner {
490
429
  ` Connection, Error Message: ${JSON.stringify(e.message)}`);
491
430
  }
492
431
  }
493
- commands() {
432
+ async commands() {
494
433
  let command;
495
434
  let parameter;
496
- if (this.On) {
435
+ if (this.LightBulb.On) {
497
436
  command = 'start';
498
437
  parameter = 'default';
499
438
  }
@@ -508,22 +447,22 @@ export class RobotVacuumCleaner {
508
447
  });
509
448
  return body;
510
449
  }
511
- brightnessCommands() {
450
+ async brightnessCommands() {
512
451
  let command;
513
452
  let parameter;
514
- if (this.Brightness === 25) {
453
+ if (this.LightBulb.Brightness === 25) {
515
454
  command = 'PowLevel';
516
455
  parameter = '0';
517
456
  }
518
- else if (this.Brightness === 50) {
457
+ else if (this.LightBulb.Brightness === 50) {
519
458
  command = 'PowLevel';
520
459
  parameter = '1';
521
460
  }
522
- else if (this.Brightness === 75) {
461
+ else if (this.LightBulb.Brightness === 75) {
523
462
  command = 'PowLevel';
524
463
  parameter = '2';
525
464
  }
526
- else if (this.Brightness === 100) {
465
+ else if (this.LightBulb.Brightness === 100) {
527
466
  command = 'PowLevel';
528
467
  parameter = '3';
529
468
  }
@@ -531,105 +470,89 @@ export class RobotVacuumCleaner {
531
470
  command = 'dock';
532
471
  parameter = 'default';
533
472
  }
534
- const body = JSON.stringify({
473
+ const bodyChange = JSON.stringify({
535
474
  command: `${command}`,
536
475
  parameter: `${parameter}`,
537
476
  commandType: 'command',
538
477
  });
539
- return body;
478
+ return bodyChange;
540
479
  }
541
480
  /**
542
481
  * Handle requests to set the value of the "On" characteristic
543
482
  */
544
483
  async OnSet(value) {
545
- if (this.On === this.accessory.context.On) {
484
+ if (this.LightBulb.On === this.accessory.context.On) {
546
485
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
547
486
  }
548
487
  else {
549
488
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
550
489
  }
551
- this.On = value;
490
+ this.LightBulb.On = value;
552
491
  this.doRobotVacuumCleanerUpdate.next();
553
492
  }
554
493
  /**
555
494
  * Handle requests to set the value of the "Brightness" characteristic
556
495
  */
557
496
  async BrightnessSet(value) {
558
- if (this.Brightness === this.accessory.context.Brightness) {
497
+ if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
559
498
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
560
499
  }
561
- else if (this.On) {
500
+ else if (this.LightBulb.On) {
562
501
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
563
502
  }
564
503
  else {
565
504
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
566
505
  }
567
- this.Brightness = value;
506
+ this.LightBulb.Brightness = value;
568
507
  this.doRobotVacuumCleanerUpdate.next();
569
508
  }
570
509
  async updateHomeKitCharacteristics() {
571
510
  // On
572
- if (this.On === undefined) {
573
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
511
+ if (this.LightBulb.On === undefined) {
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
574
513
  }
575
514
  else {
576
- this.accessory.context.On = this.On;
577
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, this.On);
578
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
515
+ this.accessory.context.On = this.LightBulb.On;
516
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
517
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
579
518
  }
580
519
  // Brightness
581
- if (this.Brightness === undefined) {
582
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
520
+ if (this.LightBulb.Brightness === undefined) {
521
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
583
522
  }
584
523
  else {
585
- this.accessory.context.Brightness = this.Brightness;
586
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
587
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
524
+ this.accessory.context.Brightness = this.LightBulb.Brightness;
525
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
526
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
588
527
  }
589
528
  // BatteryLevel
590
- if (this.BatteryLevel === undefined) {
591
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
529
+ if (this.Battery.BatteryLevel === undefined) {
530
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
592
531
  }
593
532
  else {
594
- this.accessory.context.BatteryLevel = this.BatteryLevel;
595
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
596
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
533
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
534
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
535
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
597
536
  }
598
537
  // StatusLowBattery
599
- if (this.StatusLowBattery === undefined) {
600
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
538
+ if (this.Battery.StatusLowBattery === undefined) {
539
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
601
540
  }
602
541
  else {
603
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
604
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
605
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
542
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
543
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
544
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
545
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
606
546
  }
607
- }
608
- async stopScanning(switchbot) {
609
- switchbot.stopScan();
610
- if (this.BLE_IsConnected) {
611
- await this.BLEparseStatus();
612
- await this.updateHomeKitCharacteristics();
547
+ // ChargingState
548
+ if (this.Battery.ChargingState === undefined) {
549
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
613
550
  }
614
551
  else {
615
- await this.BLERefreshConnection(switchbot);
616
- }
617
- }
618
- async getCustomBLEAddress(switchbot) {
619
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
620
- (async () => {
621
- // Start to monitor advertisement packets
622
- await switchbot.startScan({
623
- model: '?',
624
- });
625
- // Set an event handler
626
- switchbot.onadvertisement = (ad) => {
627
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
628
- };
629
- await sleep(10000);
630
- // Stop to monitor
631
- switchbot.stopScan();
632
- })();
552
+ this.accessory.context.ChargingState = this.Battery.ChargingState;
553
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
554
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
555
+ + ` ChargingState: ${this.Battery.ChargingState}`);
633
556
  }
634
557
  }
635
558
  async BLEPushConnection() {
@@ -646,255 +569,13 @@ export class RobotVacuumCleaner {
646
569
  await this.openAPIRefreshStatus();
647
570
  }
648
571
  }
649
- async retry({ max, fn }) {
650
- return fn().catch(async (e) => {
651
- if (max === 0) {
652
- throw e;
653
- }
654
- this.infoLog(e);
655
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
656
- await sleep(1000);
657
- return this.retry({ max: max - 1, fn });
658
- });
659
- }
660
- maxRetry() {
661
- if (this.device.maxRetry) {
662
- return this.device.maxRetry;
663
- }
664
- else {
665
- return 5;
666
- }
667
- }
668
- model(device) {
669
- let model;
670
- if (device.deviceType === 'Robot Vacuum Cleaner S1 Plus') {
671
- model = 'W3011010';
672
- }
673
- else if (device.deviceType === 'Robot Vacuum Cleaner S1') {
674
- model = 'W3011000';
675
- }
676
- else {
677
- model = 'N/A';
678
- }
679
- return model;
680
- }
681
- async scan(device) {
682
- if (device.scanDuration) {
683
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
684
- if (this.BLE) {
685
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
686
- }
687
- }
688
- else {
689
- this.scanDuration = this.accessory.context.scanDuration = 1;
690
- if (this.BLE) {
691
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
692
- }
693
- }
694
- }
695
- async statusCode(statusCode) {
696
- switch (statusCode) {
697
- case 151:
698
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
699
- break;
700
- case 152:
701
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
702
- break;
703
- case 160:
704
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
705
- break;
706
- case 161:
707
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
708
- this.offlineOff();
709
- break;
710
- case 171:
711
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
712
- `Hub: ${this.device.hubDeviceId}`);
713
- this.offlineOff();
714
- break;
715
- case 190:
716
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
717
- ` Or command format is invalid, statusCode: ${statusCode}`);
718
- break;
719
- case 100:
720
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
721
- break;
722
- case 200:
723
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
724
- break;
725
- case 400:
726
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
727
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
728
- break;
729
- case 401:
730
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
731
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
732
- break;
733
- case 403:
734
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
735
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
736
- break;
737
- case 404:
738
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
739
- + `statusCode: ${statusCode}`);
740
- break;
741
- case 406:
742
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
743
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
744
- break;
745
- case 415:
746
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
747
- + `header that is not supported by the server, statusCode: ${statusCode}`);
748
- break;
749
- case 422:
750
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
751
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
752
- break;
753
- case 429:
754
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
755
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
756
- break;
757
- case 500:
758
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
759
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
760
- break;
761
- default:
762
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
763
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
764
- }
765
- }
766
572
  async offlineOff() {
767
573
  if (this.device.offline) {
768
- await this.deviceContext();
769
- await this.updateHomeKitCharacteristics();
574
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
770
575
  }
771
576
  }
772
577
  async apiError(e) {
773
- this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, e);
774
- }
775
- async deviceContext() {
776
- if (this.On === undefined) {
777
- this.On = false;
778
- }
779
- else {
780
- this.On = this.accessory.context.On;
781
- }
782
- if (this.Brightness === undefined) {
783
- this.Brightness = 0;
784
- }
785
- else {
786
- this.Brightness = this.accessory.context.Brightness;
787
- }
788
- if (this.FirmwareRevision === undefined) {
789
- this.FirmwareRevision = this.platform.version;
790
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
791
- }
792
- }
793
- async refreshRate(device) {
794
- if (device.refreshRate) {
795
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
796
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
797
- }
798
- else if (this.platform.config.options.refreshRate) {
799
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
800
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
801
- }
802
- }
803
- async deviceConfig(device) {
804
- let config = {};
805
- if (device.plug) {
806
- config = device.plug;
807
- }
808
- if (device.connectionType !== undefined) {
809
- config['connectionType'] = device.connectionType;
810
- }
811
- if (device.external !== undefined) {
812
- config['external'] = device.external;
813
- }
814
- if (device.logging !== undefined) {
815
- config['logging'] = device.logging;
816
- }
817
- if (device.refreshRate !== undefined) {
818
- config['refreshRate'] = device.refreshRate;
819
- }
820
- if (device.scanDuration !== undefined) {
821
- config['scanDuration'] = device.scanDuration;
822
- }
823
- if (device.offline !== undefined) {
824
- config['offline'] = device.offline;
825
- }
826
- if (device.maxRetry !== undefined) {
827
- config['maxRetry'] = device.maxRetry;
828
- }
829
- if (device.webhook !== undefined) {
830
- config['webhook'] = device.webhook;
831
- }
832
- if (Object.entries(config).length !== 0) {
833
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
834
- }
835
- }
836
- async deviceLogs(device) {
837
- if (this.platform.debugMode) {
838
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
839
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
840
- }
841
- else if (device.logging) {
842
- this.deviceLogging = this.accessory.context.logging = device.logging;
843
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
844
- }
845
- else if (this.platform.config.options?.logging) {
846
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
847
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
848
- }
849
- else {
850
- this.deviceLogging = this.accessory.context.logging = 'standard';
851
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
852
- }
853
- }
854
- /**
855
- * Logging for Device
856
- */
857
- infoLog(...log) {
858
- if (this.enablingDeviceLogging()) {
859
- this.platform.log.info(String(...log));
860
- }
861
- }
862
- warnLog(...log) {
863
- if (this.enablingDeviceLogging()) {
864
- this.platform.log.warn(String(...log));
865
- }
866
- }
867
- debugWarnLog(...log) {
868
- if (this.enablingDeviceLogging()) {
869
- if (this.deviceLogging?.includes('debug')) {
870
- this.platform.log.warn('[DEBUG]', String(...log));
871
- }
872
- }
873
- }
874
- errorLog(...log) {
875
- if (this.enablingDeviceLogging()) {
876
- this.platform.log.error(String(...log));
877
- }
878
- }
879
- debugErrorLog(...log) {
880
- if (this.enablingDeviceLogging()) {
881
- if (this.deviceLogging?.includes('debug')) {
882
- this.platform.log.error('[DEBUG]', String(...log));
883
- }
884
- }
885
- }
886
- debugLog(...log) {
887
- if (this.enablingDeviceLogging()) {
888
- if (this.deviceLogging === 'debug') {
889
- this.platform.log.info('[DEBUG]', String(...log));
890
- }
891
- else {
892
- this.platform.log.debug(String(...log));
893
- }
894
- }
895
- }
896
- enablingDeviceLogging() {
897
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
578
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
898
579
  }
899
580
  }
900
581
  //# sourceMappingURL=robotvacuumcleaner.js.map