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

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 +9 -9
  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,82 +1,41 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * plug.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';
6
+ import { deviceBase } from './device.js';
7
+ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
5
8
  import { Devices } from '../settings.js';
6
- export class Plug {
9
+ export class Plug extends deviceBase {
7
10
  platform;
8
- accessory;
9
- device;
10
- api;
11
- log;
12
- config;
13
- hap;
14
11
  // Services
15
- outletService;
16
- // Characteristic Values
17
- On;
18
- FirmwareRevision;
19
- // OpenAPI Others
20
- OpenAPI_On;
21
- OpenAPI_FirmwareRevision;
22
- // BLE Others
23
- BLE_IsConnected;
24
- BLE_On;
25
- // Config
26
- scanDuration;
27
- deviceLogging;
28
- deviceRefreshRate;
29
- maxRetries;
30
- delayBetweenRetries;
12
+ Outlet;
31
13
  // Updates
32
14
  plugUpdateInProgress;
33
15
  doPlugUpdate;
34
- // Connection
35
- OpenAPI;
36
- BLE;
37
16
  constructor(platform, accessory, device) {
17
+ super(platform, accessory, device);
38
18
  this.platform = platform;
39
- this.accessory = accessory;
40
- this.device = device;
41
- this.api = this.platform.api;
42
- this.log = this.platform.log;
43
- this.config = this.platform.config;
44
- this.hap = this.api.hap;
45
- // Connection
46
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
47
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
48
- // default placeholders
49
- this.deviceLogs(device);
50
- this.scan(device);
51
- this.refreshRate(device);
52
- this.deviceContext();
53
- this.deviceRetry(device);
54
- this.deviceConfig(device);
55
19
  // this is subject we use to track when we need to POST changes to the SwitchBot API
56
20
  this.doPlugUpdate = new Subject();
57
21
  this.plugUpdateInProgress = false;
58
22
  // Retrieve initial values and updateHomekit
59
23
  this.refreshStatus();
60
- // set accessory information
61
- accessory
62
- .getService(this.hap.Service.AccessoryInformation)
63
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
64
- .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
65
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
66
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
24
+ // Initialize Outlet property
25
+ this.Outlet = {
26
+ Service: this.accessory.addService(this.hap.Service.Battery),
27
+ On: accessory.context.On || false,
28
+ };
67
29
  // get the Outlet service if it exists, otherwise create a new Outlet service
68
30
  // you can create multiple services for each accessory
69
- const outletService = `${accessory.displayName} ${device.deviceType}`;
70
- (this.outletService = accessory.getService(this.hap.Service.Outlet)
71
- || accessory.addService(this.hap.Service.Outlet)), outletService;
72
- this.outletService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
73
- if (!this.outletService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
74
- this.outletService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
75
- }
31
+ const OutletService = `${accessory.displayName} ${device.deviceType}`;
32
+ (this.Outlet.Service = accessory.getService(this.hap.Service.Outlet)
33
+ || accessory.addService(this.hap.Service.Outlet)), OutletService;
34
+ this.Outlet.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
76
35
  // each service must implement at-minimum the "required characteristics" for the given service type
77
36
  // see https://developers.homebridge.io/#/service/Outlet
78
37
  // create handlers for required characteristics
79
- this.outletService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
38
+ this.Outlet.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
80
39
  // Update Homekit
81
40
  this.updateHomeKitCharacteristics();
82
41
  // Start an update interval
@@ -92,12 +51,12 @@ export class Plug {
92
51
  try {
93
52
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
94
53
  const { powerState } = context;
95
- const { On } = this;
54
+ const { On } = this.Outlet;
96
55
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
97
56
  '(powerState) = ' +
98
57
  `Webhook:(${powerState}), ` +
99
58
  `current:(${On})`);
100
- this.On = powerState === 'ON' ? true : false;
59
+ this.Outlet.On = powerState === 'ON' ? true : false;
101
60
  this.updateHomeKitCharacteristics();
102
61
  }
103
62
  catch (e) {
@@ -124,49 +83,30 @@ export class Plug {
124
83
  this.plugUpdateInProgress = false;
125
84
  });
126
85
  }
127
- /**
128
- * Parse the device status from the SwitchBot api
129
- */
130
- async parseStatus() {
131
- if (!this.device.enableCloudService && this.OpenAPI) {
132
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
133
- }
134
- else if (this.BLE) {
135
- await this.BLEparseStatus();
136
- }
137
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
138
- await this.openAPIparseStatus();
139
- }
140
- else {
141
- await this.offlineOff();
142
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
143
- }
144
- }
145
- async BLEparseStatus() {
86
+ async BLEparseStatus(serviceData) {
146
87
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
147
88
  // State
148
- switch (this.BLE_On) {
89
+ switch (serviceData.state) {
149
90
  case 'on':
150
- this.On = true;
91
+ this.Outlet.On = true;
151
92
  break;
152
93
  default:
153
- this.On = false;
94
+ this.Outlet.On = false;
154
95
  }
155
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
96
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
156
97
  }
157
- async openAPIparseStatus() {
98
+ async openAPIparseStatus(deviceStatus) {
158
99
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
159
- switch (this.OpenAPI_On) {
100
+ switch (deviceStatus.body.power) {
160
101
  case 'on':
161
- this.On = true;
102
+ this.Outlet.On = true;
162
103
  break;
163
104
  default:
164
- this.On = false;
105
+ this.Outlet.On = false;
165
106
  }
166
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
107
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
167
108
  // FirmwareRevision
168
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
169
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
109
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
170
110
  }
171
111
  /**
172
112
  * Asks the SwitchBot API for the latest device information
@@ -199,85 +139,35 @@ export class Plug {
199
139
  this.getCustomBLEAddress(switchbot);
200
140
  // Start to monitor advertisement packets
201
141
  (async () => {
202
- await switchbot.startScan({
203
- model: this.BLEmodel(),
204
- id: this.device.bleMac,
205
- });
142
+ // Start to monitor advertisement packets
143
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
206
144
  // Set an event handler
207
145
  switchbot.onadvertisement = (ad) => {
208
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
209
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
210
- if (this.device.bleMac === ad.address && ad.serviceData.model === this.BLEmodel()) {
146
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
147
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
148
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
211
149
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
212
- this.BLE_On = ad.serviceData.state;
213
150
  }
214
151
  else {
215
152
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
216
153
  }
217
154
  };
218
- // Wait 1 seconds
155
+ // Wait 10 seconds
219
156
  await switchbot.wait(this.scanDuration * 1000);
220
157
  // Stop to monitor
221
158
  await switchbot.stopScan();
222
159
  // Update HomeKit
223
- await this.BLEparseStatus();
160
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
224
161
  await this.updateHomeKitCharacteristics();
225
162
  })();
226
- /*if (switchbot !== false) {
227
- switchbot
228
- .startScan({
229
- model: this.BLEmodel(),
230
- id: this.device.bleMac,
231
- })
232
- .then(async () => {
233
- // Set an event handler
234
- switchbot.onadvertisement = async (ad: ad) => {
235
- this.debugLog(
236
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
237
- ` BLE Address Found: ${ad.address}`,
238
- );
239
- this.BLE_On = ad.serviceData.state;
240
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
241
- this.debugLog(
242
- `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
243
- `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
244
- `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
245
- );
246
-
247
- if (ad.serviceData) {
248
- this.BLE_IsConnected = true;
249
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
250
- await this.stopScanning(switchbot);
251
- } else {
252
- this.BLE_IsConnected = false;
253
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
254
- }
255
- };
256
- // Wait
257
- return await sleep(this.scanDuration * 1000);
258
- })
259
- .then(async () => {
260
- // Stop to monitor
261
- await this.stopScanning(switchbot);
262
- })
263
- .catch(async (e: any) => {
264
- this.apiError(e);
265
- this.errorLog(
266
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
267
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
268
- );
269
- await this.BLERefreshConnection(switchbot);
270
- });
271
- } else {
272
- await this.BLERefreshConnection(switchbot);
273
- }*/
163
+ if (switchbot === undefined) {
164
+ await this.BLERefreshConnection(switchbot);
165
+ }
274
166
  }
275
167
  async openAPIRefreshStatus() {
276
168
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
277
169
  try {
278
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, {
279
- headers: this.platform.generateHeaders(),
280
- });
170
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
281
171
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
282
172
  const deviceStatus = await body.json();
283
173
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -285,8 +175,7 @@ export class Plug {
285
175
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
286
176
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
287
177
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
288
- this.OpenAPI_On = deviceStatus.body.power;
289
- this.openAPIparseStatus();
178
+ this.openAPIparseStatus(deviceStatus);
290
179
  this.updateHomeKitCharacteristics();
291
180
  }
292
181
  else {
@@ -333,8 +222,9 @@ export class Plug {
333
222
  }
334
223
  async BLEpushChanges() {
335
224
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
336
- if (this.On !== this.accessory.context.On) {
337
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
225
+ if (this.Outlet.On !== this.accessory.context.On) {
226
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
227
+ + ` On: ${this.Outlet.On} OnCached: ${this.accessory.context.On}`);
338
228
  const switchbot = await this.platform.connectBLE();
339
229
  // Convert to BLE Address
340
230
  this.device.bleMac = this.device
@@ -344,15 +234,15 @@ export class Plug {
344
234
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
345
235
  switchbot
346
236
  .discover({
347
- model: this.BLEmodel(),
237
+ model: this.device.bleModel,
348
238
  id: this.device.bleMac,
349
239
  })
350
240
  .then(async (device_list) => {
351
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
352
- return await this.retry({
353
- max: this.maxRetry(),
241
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
242
+ return await this.retryBLE({
243
+ max: await this.maxRetryBLE(),
354
244
  fn: async () => {
355
- if (this.On) {
245
+ if (this.Outlet.On) {
356
246
  return await device_list[0].turnOn({ id: this.device.bleMac });
357
247
  }
358
248
  else {
@@ -364,8 +254,8 @@ export class Plug {
364
254
  .then(() => {
365
255
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
366
256
  this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
367
- + `On: ${this.On} sent over BLE, sent successfully`);
368
- this.On = false;
257
+ + `On: ${this.Outlet.On} sent over BLE, sent successfully`);
258
+ this.Outlet.On = false;
369
259
  })
370
260
  .catch(async (e) => {
371
261
  this.apiError(e);
@@ -375,14 +265,15 @@ export class Plug {
375
265
  });
376
266
  }
377
267
  else {
378
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`);
268
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
269
+ + ` On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
379
270
  }
380
271
  }
381
272
  async openAPIpushChanges() {
382
273
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
383
- if (this.On !== this.accessory.context.On) {
274
+ if (this.Outlet.On !== this.accessory.context.On) {
384
275
  let command = '';
385
- if (this.On) {
276
+ if (this.Outlet.On) {
386
277
  command = 'turnOn';
387
278
  }
388
279
  else {
@@ -418,73 +309,37 @@ export class Plug {
418
309
  }
419
310
  catch (e) {
420
311
  this.apiError(e);
421
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
422
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
312
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
313
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
423
314
  }
424
315
  }
425
316
  else {
426
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
427
- `On: ${this.On}, ` +
428
- `OnCached: ${this.accessory.context.On}`);
317
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
318
+ + `On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
429
319
  }
430
320
  }
431
321
  /**
432
322
  * Handle requests to set the value of the "On" characteristic
433
323
  */
434
324
  async OnSet(value) {
435
- if (this.On === this.accessory.context.On) {
325
+ if (this.Outlet.On === this.accessory.context.On) {
436
326
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
437
327
  }
438
328
  else {
439
329
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
440
330
  }
441
- this.On = value;
331
+ this.Outlet.On = value;
442
332
  this.doPlugUpdate.next();
443
333
  }
444
334
  async updateHomeKitCharacteristics() {
445
335
  // On
446
- if (this.On === undefined) {
447
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
448
- }
449
- else {
450
- this.accessory.context.On = this.On;
451
- this.outletService.updateCharacteristic(this.hap.Characteristic.On, this.On);
452
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
453
- }
454
- }
455
- async stopScanning(switchbot) {
456
- switchbot.stopScan();
457
- if (this.BLE_IsConnected) {
458
- await this.BLEparseStatus();
459
- await this.updateHomeKitCharacteristics();
336
+ if (this.Outlet.On === undefined) {
337
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
460
338
  }
461
339
  else {
462
- await this.BLERefreshConnection(switchbot);
463
- }
464
- }
465
- BLEmodel() {
466
- if (this.device.deviceType === 'Plug Mini (US)' || this.device.configDeviceType === 'Plug Mini (US)') {
467
- return 'g';
468
- }
469
- else {
470
- return 'j';
471
- }
472
- }
473
- async getCustomBLEAddress(switchbot) {
474
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
475
- (async () => {
476
- // Start to monitor advertisement packets
477
- await switchbot.startScan({
478
- model: this.BLEmodel(),
479
- });
480
- // Set an event handler
481
- switchbot.onadvertisement = (ad) => {
482
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
483
- };
484
- await sleep(10000);
485
- // Stop to monitor
486
- switchbot.stopScan();
487
- })();
340
+ this.accessory.context.On = this.Outlet.On;
341
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, this.Outlet.On);
342
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Outlet.On}`);
488
343
  }
489
344
  }
490
345
  async BLEPushConnection() {
@@ -501,273 +356,13 @@ export class Plug {
501
356
  await this.openAPIRefreshStatus();
502
357
  }
503
358
  }
504
- async retry({ max, fn }) {
505
- return fn().catch(async (e) => {
506
- if (max === 0) {
507
- throw e;
508
- }
509
- this.infoLog(e);
510
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
511
- await sleep(1000);
512
- return this.retry({ max: max - 1, fn });
513
- });
514
- }
515
- maxRetry() {
516
- if (this.device.maxRetry) {
517
- return this.device.maxRetry;
518
- }
519
- else {
520
- return 5;
521
- }
522
- }
523
- model(device) {
524
- let model;
525
- if (device.deviceType === 'Plug Mini (US)') {
526
- model = 'W1901400';
527
- model = 'W1901401';
528
- }
529
- else if (device.deviceType === 'Plug Mini (JP)') {
530
- model = 'W2001400';
531
- }
532
- else {
533
- model = 'SP11';
534
- }
535
- return model;
536
- }
537
- async scan(device) {
538
- if (device.scanDuration) {
539
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
540
- if (this.BLE) {
541
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
542
- }
543
- }
544
- else {
545
- this.scanDuration = this.accessory.context.scanDuration = 1;
546
- if (this.BLE) {
547
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
548
- }
549
- }
550
- }
551
- async statusCode(statusCode) {
552
- switch (statusCode) {
553
- case 151:
554
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
555
- break;
556
- case 152:
557
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
558
- break;
559
- case 160:
560
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
561
- break;
562
- case 161:
563
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
564
- this.offlineOff();
565
- break;
566
- case 171:
567
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
568
- `Hub: ${this.device.hubDeviceId}`);
569
- this.offlineOff();
570
- break;
571
- case 190:
572
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
573
- ` Or command format is invalid, statusCode: ${statusCode}`);
574
- break;
575
- case 100:
576
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
577
- break;
578
- case 200:
579
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
580
- break;
581
- case 400:
582
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
583
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
584
- break;
585
- case 401:
586
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
587
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
588
- break;
589
- case 403:
590
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
591
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
592
- break;
593
- case 404:
594
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
595
- + `statusCode: ${statusCode}`);
596
- break;
597
- case 406:
598
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
599
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
600
- break;
601
- case 415:
602
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
603
- + `header that is not supported by the server, statusCode: ${statusCode}`);
604
- break;
605
- case 422:
606
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
607
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
608
- break;
609
- case 429:
610
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
611
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
612
- break;
613
- case 500:
614
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
615
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
616
- break;
617
- default:
618
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
619
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
620
- }
621
- }
622
359
  async offlineOff() {
623
360
  if (this.device.offline) {
624
- await this.deviceContext();
625
- await this.updateHomeKitCharacteristics();
361
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, false);
626
362
  }
627
363
  }
628
364
  async apiError(e) {
629
- this.outletService.updateCharacteristic(this.hap.Characteristic.On, e);
630
- }
631
- async deviceContext() {
632
- if (this.On === undefined) {
633
- this.On = false;
634
- }
635
- else {
636
- this.On = this.accessory.context.On;
637
- }
638
- if (this.FirmwareRevision === undefined) {
639
- this.FirmwareRevision = this.platform.version;
640
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
641
- }
642
- }
643
- async refreshRate(device) {
644
- if (device.refreshRate) {
645
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
646
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
647
- }
648
- else if (this.platform.config.options.refreshRate) {
649
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
650
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
651
- }
652
- }
653
- async deviceRetry(device) {
654
- if (!device.maxRetries) {
655
- this.maxRetries = 5; // Maximum number of retries
656
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
657
- }
658
- else {
659
- this.maxRetries = device.maxRetries;
660
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
661
- }
662
- if (!device.delayBetweenRetries) {
663
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
664
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
665
- }
666
- else {
667
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
668
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
669
- }
670
- }
671
- async deviceConfig(device) {
672
- let config = {};
673
- if (device.plug) {
674
- config = device.plug;
675
- }
676
- if (device.connectionType !== undefined) {
677
- config['connectionType'] = device.connectionType;
678
- }
679
- if (device.external !== undefined) {
680
- config['external'] = device.external;
681
- }
682
- if (device.logging !== undefined) {
683
- config['logging'] = device.logging;
684
- }
685
- if (device.refreshRate !== undefined) {
686
- config['refreshRate'] = device.refreshRate;
687
- }
688
- if (device.scanDuration !== undefined) {
689
- config['scanDuration'] = device.scanDuration;
690
- }
691
- if (device.offline !== undefined) {
692
- config['offline'] = device.offline;
693
- }
694
- if (device.maxRetry !== undefined) {
695
- config['maxRetry'] = device.maxRetry;
696
- }
697
- if (device.webhook !== undefined) {
698
- config['webhook'] = device.webhook;
699
- }
700
- if (Object.entries(config).length !== 0) {
701
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
702
- }
703
- }
704
- async deviceLogs(device) {
705
- if (this.platform.debugMode) {
706
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
707
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
708
- }
709
- else if (device.logging) {
710
- this.deviceLogging = this.accessory.context.logging = device.logging;
711
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
712
- }
713
- else if (this.platform.config.options?.logging) {
714
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
715
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
716
- }
717
- else {
718
- this.deviceLogging = this.accessory.context.logging = 'standard';
719
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
720
- }
721
- }
722
- /**
723
- * Logging for Device
724
- */
725
- successLog(...log) {
726
- if (this.enablingDeviceLogging()) {
727
- this.platform.log.success(String(...log));
728
- }
729
- }
730
- infoLog(...log) {
731
- if (this.enablingDeviceLogging()) {
732
- this.platform.log.info(String(...log));
733
- }
734
- }
735
- warnLog(...log) {
736
- if (this.enablingDeviceLogging()) {
737
- this.platform.log.warn(String(...log));
738
- }
739
- }
740
- debugWarnLog(...log) {
741
- if (this.enablingDeviceLogging()) {
742
- if (this.deviceLogging?.includes('debug')) {
743
- this.platform.log.warn('[DEBUG]', String(...log));
744
- }
745
- }
746
- }
747
- errorLog(...log) {
748
- if (this.enablingDeviceLogging()) {
749
- this.platform.log.error(String(...log));
750
- }
751
- }
752
- debugErrorLog(...log) {
753
- if (this.enablingDeviceLogging()) {
754
- if (this.deviceLogging?.includes('debug')) {
755
- this.platform.log.error('[DEBUG]', String(...log));
756
- }
757
- }
758
- }
759
- debugLog(...log) {
760
- if (this.enablingDeviceLogging()) {
761
- if (this.deviceLogging === 'debug') {
762
- this.platform.log.info('[DEBUG]', String(...log));
763
- }
764
- else {
765
- this.platform.log.debug(String(...log));
766
- }
767
- }
768
- }
769
- enablingDeviceLogging() {
770
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
365
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, e);
771
366
  }
772
367
  }
773
368
  //# sourceMappingURL=plug.js.map