@switchbot/homebridge-switchbot 3.5.1-beta.3 → 3.5.1-beta.5

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.
@@ -344,7 +344,7 @@ export class ColorBulb extends deviceBase {
344
344
  async registerWebhook() {
345
345
  if (this.device.webhook) {
346
346
  await this.debugLog('is listening webhook.');
347
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
347
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
348
348
  try {
349
349
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
350
350
  await this.parseStatusWebhook(context);
@@ -462,7 +462,7 @@ export class Curtain extends deviceBase {
462
462
  async registerWebhook() {
463
463
  if (this.device.webhook) {
464
464
  await this.debugLog('is listening webhook.');
465
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
465
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: curtainWebhookContext | curtain3WebhookContext) => {
466
466
  try {
467
467
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
468
468
  await this.parseStatusWebhook(context);
package/src/device/fan.ts CHANGED
@@ -339,7 +339,7 @@ export class Fan extends deviceBase {
339
339
  async registerWebhook() {
340
340
  if (this.device.webhook) {
341
341
  await this.debugLog('is listening webhook.');
342
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
342
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: batteryCirculatorFanWebhookContext) => {
343
343
  try {
344
344
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
345
345
  await this.parseStatusWebhook(context);
package/src/platform.ts CHANGED
@@ -34,23 +34,23 @@ import { AirConditioner } from './irdevice/airconditioner.js';
34
34
  import { Buffer } from 'buffer';
35
35
  import { request } from 'undici';
36
36
  import asyncmqtt from 'async-mqtt';
37
- import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js';
38
37
  import { createServer } from 'http';
39
38
  import { queueScheduler } from 'rxjs';
40
39
  import fakegato from 'fakegato-history';
41
40
  import crypto, { randomUUID } from 'crypto';
42
41
  import { readFileSync, writeFileSync } from 'fs';
43
42
  import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes';
43
+ import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js';
44
44
  import { PLATFORM_NAME, PLUGIN_NAME, Devices, setupWebhook, updateWebhook, deleteWebhook, queryWebhook } from './settings.js';
45
45
 
46
46
  import type { UrlObject } from 'url';
47
47
  import type { MqttClient } from 'mqtt';
48
48
  import type { Dispatcher } from 'undici';
49
+ import type { irdevice } from './types/irdevicelist.js';
49
50
  import type { Server, IncomingMessage, ServerResponse } from 'http';
51
+ import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js';
50
52
  import type { API, DynamicPlatformPlugin, Logging, PlatformAccessory } from 'homebridge';
51
53
  import type { SwitchBotPlatformConfig, devicesConfig, irDevicesConfig } from './settings.js';
52
- import type { irdevice } from './types/irdevicelist.js';
53
- import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js';
54
54
 
55
55
  /**
56
56
  * HomebridgePlatform
@@ -76,7 +76,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
76
76
 
77
77
  public readonly fakegatoAPI: any;
78
78
  public readonly eve: any;
79
- public readonly webhookEventHandler;
79
+ public readonly webhookEventHandler: { [x: string]: (context: any) => void } = {};
80
80
 
81
81
  constructor(
82
82
  log: Logging,
@@ -154,27 +154,27 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
154
154
  try {
155
155
  const { connectAsync } = asyncmqtt;
156
156
  this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {});
157
- this.debugLog('MQTT connection has been established successfully.');
158
- this.mqttClient.on('error', (e: Error) => {
159
- this.errorLog(`Failed to publish MQTT messages. ${e}`);
157
+ await this.debugLog('MQTT connection has been established successfully.');
158
+ this.mqttClient.on('error', async (e: Error) => {
159
+ await this.errorLog(`Failed to publish MQTT messages. ${e}`);
160
160
  });
161
161
  if (!this.config.options?.webhookURL) {
162
162
  // receive webhook events via MQTT
163
- this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`);
163
+ await this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`);
164
164
  this.mqttClient.subscribe('homebridge-switchbot/webhook/+');
165
165
  this.mqttClient.on('message', async (topic: string, message) => {
166
166
  try {
167
- this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`);
167
+ await this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`);
168
168
  const context = JSON.parse(message.toString());
169
169
  this.webhookEventHandler[context.deviceMac]?.(context);
170
170
  } catch (e: any) {
171
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
171
+ await this.errorLog(`Failed to handle webhook event. Error:${e}`);
172
172
  }
173
173
  });
174
174
  }
175
175
  } catch (e) {
176
176
  this.mqttClient = null;
177
- this.errorLog(`Failed to establish MQTT connection. ${e}`);
177
+ await this.errorLog(`Failed to establish MQTT connection. ${e}`);
178
178
  }
179
179
  }
180
180
  }
@@ -298,9 +298,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
298
298
  }),
299
299
  });
300
300
  const response: any = await body.json();
301
- this.debugLog(`deleteWebhook: url:${url}`);
302
- this.debugLog(`deleteWebhook: body:${JSON.stringify(response)}`);
303
- this.debugLog(`deleteWebhook: statusCode:${statusCode}`);
301
+ this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
304
302
  if (statusCode !== 200 || response?.statusCode !== 100) {
305
303
  this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
306
304
  } else {
@@ -2660,67 +2658,67 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2660
2658
  async statusCode(statusCode: number): Promise<void> {
2661
2659
  switch (statusCode) {
2662
2660
  case 151:
2663
- this.errorLog(`Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here: ` +
2661
+ await this.errorLog(`Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here: ` +
2664
2662
  'https://tinyurl.com/SwitchBotFeatureRequest');
2665
2663
  break;
2666
2664
  case 152:
2667
- this.errorLog(`Device not found, statusCode: ${statusCode}`);
2665
+ await this.errorLog(`Device not found, statusCode: ${statusCode}`);
2668
2666
  break;
2669
2667
  case 160:
2670
- this.errorLog(`Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
2668
+ await this.errorLog(`Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
2671
2669
  break;
2672
2670
  case 161:
2673
- this.errorLog(`Device is offline, statusCode: ${statusCode}`);
2671
+ await this.errorLog(`Device is offline, statusCode: ${statusCode}`);
2674
2672
  break;
2675
2673
  case 171:
2676
- this.errorLog(`is offline, statusCode: ${statusCode}`);
2674
+ await this.errorLog(`is offline, statusCode: ${statusCode}`);
2677
2675
  break;
2678
2676
  case 190:
2679
- this.errorLog(`Requests reached the daily limit, statusCode: ${statusCode}`);
2677
+ await this.errorLog(`Requests reached the daily limit, statusCode: ${statusCode}`);
2680
2678
  break;
2681
2679
  case 100:
2682
- this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2680
+ await this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2683
2681
  break;
2684
2682
  case 200:
2685
- this.debugLog(`Request successful, statusCode: ${statusCode}`);
2683
+ await this.debugLog(`Request successful, statusCode: ${statusCode}`);
2686
2684
  break;
2687
2685
  case 400:
2688
- this.errorLog('Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request '
2686
+ await this.errorLog('Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request '
2689
2687
  + `payload, statusCode: ${statusCode}`);
2690
2688
  break;
2691
2689
  case 401:
2692
- this.errorLog('Unauthorized, Authorization for the API is required, but the request has not been authenticated, '
2690
+ await this.errorLog('Unauthorized, Authorization for the API is required, but the request has not been authenticated, '
2693
2691
  + `statusCode: ${statusCode}`);
2694
2692
  break;
2695
2693
  case 403:
2696
- this.errorLog('Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not '
2694
+ await this.errorLog('Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not '
2697
2695
  + `found, statusCode: ${statusCode}`);
2698
2696
  break;
2699
2697
  case 404:
2700
- this.errorLog(`Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`);
2698
+ await this.errorLog(`Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`);
2701
2699
  break;
2702
2700
  case 406:
2703
- this.errorLog('Not Acceptable, The client has requested a MIME type via the Accept header for a value not supported by the server, '
2701
+ await this.errorLog('Not Acceptable, The client has requested a MIME type via the Accept header for a value not supported by the server, '
2704
2702
  + `statusCode: ${statusCode}`);
2705
2703
  break;
2706
2704
  case 415:
2707
- this.errorLog('Unsupported Media Type, The client has defined a contentType header that is not supported by the server, '
2705
+ await this.errorLog('Unsupported Media Type, The client has defined a contentType header that is not supported by the server, '
2708
2706
  + `statusCode: ${statusCode}`);
2709
2707
  break;
2710
2708
  case 422:
2711
- this.errorLog('Unprocessable Entity, The client has made a valid request, but the server cannot process it. This is often used for '
2709
+ await this.errorLog('Unprocessable Entity, The client has made a valid request, but the server cannot process it. This is often used for '
2712
2710
  + `APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
2713
2711
  break;
2714
2712
  case 429:
2715
- this.errorLog('Too Many Requests, The client has exceeded the number of requests allowed for a given time window, '
2713
+ await this.errorLog('Too Many Requests, The client has exceeded the number of requests allowed for a given time window, '
2716
2714
  + `statusCode: ${statusCode}`);
2717
2715
  break;
2718
2716
  case 500:
2719
- this.errorLog('Internal Server Error, An unexpected error on the SmartThings servers has occurred. These errors should be rare, '
2717
+ await this.errorLog('Internal Server Error, An unexpected error on the SmartThings servers has occurred. These errors should be rare, '
2720
2718
  + `statusCode: ${statusCode}`);
2721
2719
  break;
2722
2720
  default:
2723
- this.errorLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2721
+ await this.errorLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2724
2722
  }
2725
2723
  }
2726
2724
 
@@ -2736,13 +2734,13 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2736
2734
  if (statusCode === 200 || statusCode === 100) {
2737
2735
  return { body, statusCode };
2738
2736
  } else {
2739
- this.debugLog(`Received status code: ${statusCode}`);
2737
+ await this.debugLog(`Received status code: ${statusCode}`);
2740
2738
  }
2741
2739
  } catch (error: any) {
2742
- this.errorLog(`Error making request: ${error.message}`);
2740
+ await this.errorLog(`Error making request: ${error.message}`);
2743
2741
  }
2744
2742
  retryCount++;
2745
- this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
2743
+ await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
2746
2744
  await sleep(delayBetweenRetries);
2747
2745
  }
2748
2746
  return { body: null, statusCode: -1 };
@@ -2754,9 +2752,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2754
2752
  try {
2755
2753
  const SwitchBot = (await import('node-switchbot')).SwitchBot;
2756
2754
  queueScheduler.schedule(() => (switchbot = new SwitchBot()));
2755
+ await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`);
2757
2756
  } catch (e: any) {
2758
2757
  switchbot = false;
2759
- this.errorLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}, Error: ${e}`);
2758
+ await this.errorLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}, Error: ${e}`);
2760
2759
  }
2761
2760
  return switchbot;
2762
2761
  }
@@ -2768,7 +2767,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2768
2767
  'utf-8',
2769
2768
  ),
2770
2769
  );
2771
- this.debugLog(`Plugin Version: ${json.version}`);
2770
+ await this.debugLog(`Plugin Version: ${json.version}`);
2772
2771
  this.version = json.version;
2773
2772
  }
2774
2773
 
@@ -2804,7 +2803,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2804
2803
  platformConfig.delayBetweenRetries = this.delayBetweenRetries / 1000;
2805
2804
  }
2806
2805
  if (Object.entries(platformConfig).length !== 0) {
2807
- this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
2806
+ await this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
2808
2807
  }
2809
2808
  this.platformConfig = platformConfig;
2810
2809
  }
@@ -2814,13 +2813,13 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2814
2813
  this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
2815
2814
  if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
2816
2815
  this.platformLogging = this.config.options.logging;
2817
- this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
2816
+ await this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
2818
2817
  } else if (this.debugMode) {
2819
2818
  this.platformLogging = 'debugMode';
2820
- this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2819
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2821
2820
  } else {
2822
2821
  this.platformLogging = 'standard';
2823
- this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2822
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2824
2823
  }
2825
2824
  }
2826
2825
 
@@ -2828,56 +2827,56 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2828
2827
  * If device level logging is turned on, log to log.warn
2829
2828
  * Otherwise send debug logs to log.debug
2830
2829
  */
2831
- infoLog(...log: any[]): void {
2832
- if (this.enablingPlatformLogging()) {
2830
+ async infoLog(...log: any[]): Promise<void> {
2831
+ if (await this.enablingPlatformLogging()) {
2833
2832
  this.log.info(String(...log));
2834
2833
  }
2835
2834
  }
2836
2835
 
2837
- successLog(...log: any[]): void {
2838
- if (this.enablingPlatformLogging()) {
2836
+ async successLog(...log: any[]): Promise<void> {
2837
+ if (await this.enablingPlatformLogging()) {
2839
2838
  this.log.success(String(...log));
2840
2839
  }
2841
2840
  }
2842
2841
 
2843
- debugSuccessLog(...log: any[]): void {
2844
- if (this.enablingPlatformLogging()) {
2842
+ async debugSuccessLog(...log: any[]): Promise<void> {
2843
+ if (await this.enablingPlatformLogging()) {
2845
2844
  if (this.platformLogging?.includes('debug')) {
2846
2845
  this.log.success('[DEBUG]', String(...log));
2847
2846
  }
2848
2847
  }
2849
2848
  }
2850
2849
 
2851
- warnLog(...log: any[]): void {
2852
- if (this.enablingPlatformLogging()) {
2850
+ async warnLog(...log: any[]): Promise<void> {
2851
+ if (await this.enablingPlatformLogging()) {
2853
2852
  this.log.warn(String(...log));
2854
2853
  }
2855
2854
  }
2856
2855
 
2857
- debugWarnLog(...log: any[]): void {
2858
- if (this.enablingPlatformLogging()) {
2856
+ async debugWarnLog(...log: any[]): Promise<void> {
2857
+ if (await this.enablingPlatformLogging()) {
2859
2858
  if (this.platformLogging?.includes('debug')) {
2860
2859
  this.log.warn('[DEBUG]', String(...log));
2861
2860
  }
2862
2861
  }
2863
2862
  }
2864
2863
 
2865
- errorLog(...log: any[]): void {
2866
- if (this.enablingPlatformLogging()) {
2864
+ async errorLog(...log: any[]): Promise<void> {
2865
+ if (await this.enablingPlatformLogging()) {
2867
2866
  this.log.error(String(...log));
2868
2867
  }
2869
2868
  }
2870
2869
 
2871
- debugErrorLog(...log: any[]): void {
2872
- if (this.enablingPlatformLogging()) {
2870
+ async debugErrorLog(...log: any[]): Promise<void> {
2871
+ if (await this.enablingPlatformLogging()) {
2873
2872
  if (this.platformLogging?.includes('debug')) {
2874
2873
  this.log.error('[DEBUG]', String(...log));
2875
2874
  }
2876
2875
  }
2877
2876
  }
2878
2877
 
2879
- debugLog(...log: any[]): void {
2880
- if (this.enablingPlatformLogging()) {
2878
+ async debugLog(...log: any[]): Promise<void> {
2879
+ if (await this.enablingPlatformLogging()) {
2881
2880
  if (this.platformLogging === 'debugMode') {
2882
2881
  this.log.debug(String(...log));
2883
2882
  } else if (this.platformLogging === 'debug') {
@@ -2886,7 +2885,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2886
2885
  }
2887
2886
  }
2888
2887
 
2889
- enablingPlatformLogging(): boolean {
2888
+ async enablingPlatformLogging(): Promise<boolean> {
2890
2889
  return this.platformLogging?.includes('debug') || this.platformLogging === 'standard';
2891
2890
  }
2892
2891
  }
@@ -10,7 +10,7 @@ type deviceWebhook = {
10
10
  context: deviceWebhookContext
11
11
  };
12
12
 
13
- type deviceWebhookContext = {
13
+ export type deviceWebhookContext = {
14
14
  //properties on all devices
15
15
  deviceMac: string;
16
16
  deviceType: string;