@switchbot/homebridge-switchbot 3.5.1-beta.11 → 3.5.1-beta.13

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 (153) hide show
  1. package/config.schema.json +19 -3
  2. package/dist/device/blindtilt.d.ts +6 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +57 -45
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +6 -3
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +38 -24
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +6 -3
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +44 -27
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +6 -3
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +47 -32
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +6 -3
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +40 -28
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +6 -4
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +35 -22
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +13 -2
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +223 -108
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +6 -3
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +48 -29
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +6 -3
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +43 -26
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +6 -3
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +43 -26
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +6 -3
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +39 -24
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +6 -3
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +43 -27
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +6 -3
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +33 -22
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +6 -3
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +38 -24
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +6 -3
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +37 -24
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +6 -3
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +33 -22
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +6 -3
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +27 -16
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +34 -21
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +6 -3
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +38 -23
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +1 -3
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +32 -138
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +0 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +13 -44
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts.map +1 -1
  87. package/dist/irdevice/camera.js +3 -10
  88. package/dist/irdevice/camera.js.map +1 -1
  89. package/dist/irdevice/fan.d.ts.map +1 -1
  90. package/dist/irdevice/fan.js +7 -25
  91. package/dist/irdevice/fan.js.map +1 -1
  92. package/dist/irdevice/irdevice.d.ts +1 -1
  93. package/dist/irdevice/irdevice.js +5 -5
  94. package/dist/irdevice/irdevice.js.map +1 -1
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +9 -27
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.js +1 -1
  99. package/dist/irdevice/other.js.map +1 -1
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js +3 -18
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js +2 -9
  105. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  106. package/dist/irdevice/waterheater.d.ts.map +1 -1
  107. package/dist/irdevice/waterheater.js +2 -9
  108. package/dist/irdevice/waterheater.js.map +1 -1
  109. package/dist/platform.d.ts +0 -11
  110. package/dist/platform.d.ts.map +1 -1
  111. package/dist/platform.js +11 -184
  112. package/dist/platform.js.map +1 -1
  113. package/dist/settings.d.ts +9 -8
  114. package/dist/settings.d.ts.map +1 -1
  115. package/dist/settings.js.map +1 -1
  116. package/dist/types/bledevicestatus.d.ts +2 -1
  117. package/dist/types/bledevicestatus.d.ts.map +1 -1
  118. package/dist/types/devicestatus.d.ts +1 -1
  119. package/dist/types/devicestatus.d.ts.map +1 -1
  120. package/package.json +9 -9
  121. package/src/device/blindtilt.ts +60 -47
  122. package/src/device/bot.ts +47 -25
  123. package/src/device/ceilinglight.ts +53 -27
  124. package/src/device/colorbulb.ts +50 -39
  125. package/src/device/contact.ts +43 -28
  126. package/src/device/curtain.ts +45 -23
  127. package/src/device/device.ts +226 -96
  128. package/src/device/fan.ts +61 -30
  129. package/src/device/hub.ts +55 -29
  130. package/src/device/humidifier.ts +55 -31
  131. package/src/device/iosensor.ts +45 -27
  132. package/src/device/lightstrip.ts +48 -28
  133. package/src/device/lock.ts +49 -23
  134. package/src/device/meter.ts +54 -26
  135. package/src/device/meterplus.ts +50 -26
  136. package/src/device/motion.ts +45 -24
  137. package/src/device/plug.ts +34 -17
  138. package/src/device/robotvacuumcleaner.ts +49 -23
  139. package/src/device/waterdetector.ts +56 -25
  140. package/src/irdevice/airconditioner.ts +41 -125
  141. package/src/irdevice/airpurifier.ts +18 -40
  142. package/src/irdevice/camera.ts +4 -9
  143. package/src/irdevice/fan.ts +10 -22
  144. package/src/irdevice/irdevice.ts +5 -5
  145. package/src/irdevice/light.ts +10 -24
  146. package/src/irdevice/other.ts +1 -1
  147. package/src/irdevice/tv.ts +5 -16
  148. package/src/irdevice/vacuumcleaner.ts +3 -8
  149. package/src/irdevice/waterheater.ts +3 -8
  150. package/src/platform.ts +11 -184
  151. package/src/settings.ts +10 -8
  152. package/src/types/bledevicestatus.ts +4 -2
  153. package/src/types/devicestatus.ts +1 -1
package/dist/platform.js CHANGED
@@ -31,15 +31,13 @@ import { VacuumCleaner } from './irdevice/vacuumcleaner.js';
31
31
  import { AirConditioner } from './irdevice/airconditioner.js';
32
32
  import { Buffer } from 'buffer';
33
33
  import { request } from 'undici';
34
- import asyncmqtt from 'async-mqtt';
35
- import { createServer } from 'http';
36
34
  import { queueScheduler } from 'rxjs';
37
35
  import fakegato from 'fakegato-history';
38
36
  import crypto, { randomUUID } from 'crypto';
39
37
  import { readFileSync, writeFileSync } from 'fs';
40
38
  import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes';
39
+ import { PLATFORM_NAME, PLUGIN_NAME, Devices } from './settings.js';
41
40
  import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js';
42
- import { PLATFORM_NAME, PLUGIN_NAME, Devices, setupWebhook, updateWebhook, deleteWebhook, queryWebhook } from './settings.js';
43
41
  /**
44
42
  * HomebridgePlatform
45
43
  * This class is the main constructor for your plugin, this is where you should
@@ -57,11 +55,8 @@ export class SwitchBotPlatform {
57
55
  platformConfig;
58
56
  platformLogging;
59
57
  config;
60
- webhookEventListener = null;
61
- mqttClient = null;
62
58
  fakegatoAPI;
63
59
  eve;
64
- webhookEventHandler = {};
65
60
  constructor(log, config, api) {
66
61
  // initialize
67
62
  this.accessories = [];
@@ -120,174 +115,6 @@ export class SwitchBotPlatform {
120
115
  await this.debugErrorLog(`Failed to Discover, Error: ${e}`);
121
116
  }
122
117
  });
123
- this.setupMqtt();
124
- this.setupwebhook();
125
- }
126
- async setupMqtt() {
127
- if (this.config.options?.mqttURL) {
128
- try {
129
- const { connectAsync } = asyncmqtt;
130
- this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {});
131
- await this.debugLog('MQTT connection has been established successfully.');
132
- this.mqttClient.on('error', async (e) => {
133
- await this.errorLog(`Failed to publish MQTT messages. ${e}`);
134
- });
135
- if (!this.config.options?.webhookURL) {
136
- // receive webhook events via MQTT
137
- await this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`);
138
- this.mqttClient.subscribe('homebridge-switchbot/webhook/+');
139
- this.mqttClient.on('message', async (topic, message) => {
140
- try {
141
- await await this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`);
142
- const context = JSON.parse(message.toString());
143
- this.webhookEventHandler[context.deviceMac]?.(context);
144
- }
145
- catch (e) {
146
- await this.errorLog(`Failed to handle webhook event. Error:${e}`);
147
- }
148
- });
149
- }
150
- }
151
- catch (e) {
152
- this.mqttClient = null;
153
- await this.errorLog(`Failed to establish MQTT connection. ${e}`);
154
- }
155
- }
156
- }
157
- async setupwebhook() {
158
- //webhook configuration
159
- if (this.config.options?.webhookURL) {
160
- const url = this.config.options?.webhookURL;
161
- try {
162
- const xurl = new URL(url);
163
- const port = Number(xurl.port);
164
- const path = xurl.pathname;
165
- this.webhookEventListener = createServer((request, response) => {
166
- try {
167
- if (request.url === path && request.method === 'POST') {
168
- request.on('data', async (data) => {
169
- try {
170
- const body = JSON.parse(data);
171
- await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`);
172
- if (this.config.options?.mqttURL) {
173
- const mac = body.context.deviceMac
174
- ?.toLowerCase()
175
- .match(/[\s\S]{1,2}/g)
176
- ?.join(':');
177
- const options = this.config.options?.mqttPubOptions || {};
178
- this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options);
179
- }
180
- this.webhookEventHandler[body.context.deviceMac]?.(body.context);
181
- }
182
- catch (e) {
183
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
184
- }
185
- });
186
- response.writeHead(200, { 'Content-Type': 'text/plain' });
187
- response.end('OK');
188
- }
189
- // else {
190
- // response.writeHead(403, {'Content-Type': 'text/plain'});
191
- // response.end(`NG`);
192
- // }
193
- }
194
- catch (e) {
195
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
196
- }
197
- }).listen(port ? port : 80);
198
- }
199
- catch (e) {
200
- this.errorLog(`Failed to create webhook listener. Error:${e.message}`);
201
- return;
202
- }
203
- try {
204
- const { body, statusCode } = await request(setupWebhook, {
205
- method: 'POST',
206
- headers: this.generateHeaders(),
207
- body: JSON.stringify({
208
- 'action': 'setupWebhook',
209
- 'url': url,
210
- 'deviceList': 'ALL',
211
- }),
212
- });
213
- const response = await body.json();
214
- await this.debugLog(`setupWebhook: url:${url}`);
215
- await this.debugLog(`setupWebhook: body:${JSON.stringify(response)}`);
216
- await this.debugLog(`setupWebhook: statusCode:${statusCode}`);
217
- if (statusCode !== 200 || response?.statusCode !== 100) {
218
- this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} `
219
- + `message:${response?.message}`);
220
- }
221
- }
222
- catch (e) {
223
- this.errorLog(`Failed to configure webhook. Error: ${e.message}`);
224
- }
225
- try {
226
- const { body, statusCode } = await request(updateWebhook, {
227
- method: 'POST', headers: this.generateHeaders(), body: JSON.stringify({
228
- 'action': 'updateWebhook',
229
- 'config': {
230
- 'url': url,
231
- 'enable': true,
232
- },
233
- }),
234
- });
235
- const response = await body.json();
236
- await this.debugLog(`updateWebhook: url:${url}`);
237
- await this.debugLog(`updateWebhook: body:${JSON.stringify(response)}`);
238
- await this.debugLog(`updateWebhook: statusCode:${statusCode}`);
239
- if (statusCode !== 200 || response?.statusCode !== 100) {
240
- this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
241
- }
242
- }
243
- catch (e) {
244
- this.errorLog(`Failed to update webhook. Error:${e.message}`);
245
- }
246
- try {
247
- const { body, statusCode } = await request(queryWebhook, {
248
- method: 'POST',
249
- headers: this.generateHeaders(),
250
- body: JSON.stringify({
251
- 'action': 'queryUrl',
252
- }),
253
- });
254
- const response = await body.json();
255
- await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`);
256
- await this.debugLog(`queryWebhook: statusCode:${statusCode}`);
257
- if (statusCode !== 200 || response?.statusCode !== 100) {
258
- this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
259
- }
260
- else {
261
- this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`);
262
- }
263
- }
264
- catch (e) {
265
- this.errorLog(`Failed to query webhook. Error:${e}`);
266
- }
267
- this.api.on('shutdown', async () => {
268
- try {
269
- const { body, statusCode } = await request(deleteWebhook, {
270
- method: 'POST',
271
- headers: this.generateHeaders(),
272
- body: JSON.stringify({
273
- 'action': 'deleteWebhook',
274
- 'url': url,
275
- }),
276
- });
277
- const response = await body.json();
278
- await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
279
- if (statusCode !== 200 || response?.statusCode !== 100) {
280
- this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
281
- }
282
- else {
283
- this.infoLog('Unregistered webhook to close listening.');
284
- }
285
- }
286
- catch (e) {
287
- this.errorLog(`Failed to delete webhook. Error:${e.message}`);
288
- }
289
- });
290
- }
291
118
  }
292
119
  /**
293
120
  * This function is invoked when homebridge restores cached accessories from disk at startup.
@@ -2559,10 +2386,10 @@ export class SwitchBotPlatform {
2559
2386
  await this.errorLog(`Requests reached the daily limit, statusCode: ${statusCode}`);
2560
2387
  break;
2561
2388
  case 100:
2562
- await await this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2389
+ await this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2563
2390
  break;
2564
2391
  case 200:
2565
- await await this.debugLog(`Request successful, statusCode: ${statusCode}`);
2392
+ await this.debugLog(`Request successful, statusCode: ${statusCode}`);
2566
2393
  break;
2567
2394
  case 400:
2568
2395
  await this.errorLog('Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request '
@@ -2614,14 +2441,14 @@ export class SwitchBotPlatform {
2614
2441
  return { body, statusCode };
2615
2442
  }
2616
2443
  else {
2617
- await await this.debugLog(`Received status code: ${statusCode}`);
2444
+ await this.debugLog(`Received status code: ${statusCode}`);
2618
2445
  }
2619
2446
  }
2620
2447
  catch (error) {
2621
2448
  await this.errorLog(`Error making request: ${error.message}`);
2622
2449
  }
2623
2450
  retryCount++;
2624
- await await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
2451
+ await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
2625
2452
  await sleep(delayBetweenRetries);
2626
2453
  }
2627
2454
  return { body: null, statusCode: -1 };
@@ -2632,7 +2459,7 @@ export class SwitchBotPlatform {
2632
2459
  try {
2633
2460
  const SwitchBot = (await import('node-switchbot')).SwitchBot;
2634
2461
  queueScheduler.schedule(() => (switchbot = new SwitchBot()));
2635
- await await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`);
2462
+ await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`);
2636
2463
  }
2637
2464
  catch (e) {
2638
2465
  switchbot = false;
@@ -2642,7 +2469,7 @@ export class SwitchBotPlatform {
2642
2469
  }
2643
2470
  async getVersion() {
2644
2471
  const json = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
2645
- await await this.debugLog(`Plugin Version: ${json.version}`);
2472
+ await this.debugLog(`Plugin Version: ${json.version}`);
2646
2473
  this.version = json.version;
2647
2474
  }
2648
2475
  async getPlatformConfigSettings() {
@@ -2679,7 +2506,7 @@ export class SwitchBotPlatform {
2679
2506
  platformConfig.delayBetweenRetries = this.delayBetweenRetries / 1000;
2680
2507
  }
2681
2508
  if (Object.entries(platformConfig).length !== 0) {
2682
- await await this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
2509
+ await this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
2683
2510
  }
2684
2511
  this.platformConfig = platformConfig;
2685
2512
  }
@@ -2688,15 +2515,15 @@ export class SwitchBotPlatform {
2688
2515
  this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
2689
2516
  if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
2690
2517
  this.platformLogging = this.config.options.logging;
2691
- await await this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
2518
+ await this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
2692
2519
  }
2693
2520
  else if (this.debugMode) {
2694
2521
  this.platformLogging = 'debugMode';
2695
- await await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2522
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2696
2523
  }
2697
2524
  else {
2698
2525
  this.platformLogging = 'standard';
2699
- await await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2526
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2700
2527
  }
2701
2528
  }
2702
2529
  /**