@switchbot/homebridge-switchbot 5.0.0-beta.3 → 5.0.0-beta.31

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/CHANGELOG.md +13 -0
  2. package/README.md +23 -3
  3. package/config.schema.json +70 -4
  4. package/dist/devices-hap/device.d.ts +1 -0
  5. package/dist/devices-hap/device.d.ts.map +1 -1
  6. package/dist/devices-hap/device.js +70 -30
  7. package/dist/devices-hap/device.js.map +1 -1
  8. package/dist/devices-matter/BaseMatterAccessory.d.ts +23 -0
  9. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
  10. package/dist/devices-matter/BaseMatterAccessory.js +167 -5
  11. package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
  12. package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
  13. package/dist/devices-matter/ColorLightAccessory.js +12 -12
  14. package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
  15. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
  16. package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
  17. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
  18. package/dist/devices-matter/DimmableLightAccessory.js +9 -9
  19. package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
  20. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
  21. package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
  22. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
  23. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
  24. package/dist/devices-matter/OnOffLightAccessory.js +8 -16
  25. package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
  26. package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
  27. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
  28. package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
  29. package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
  30. package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
  31. package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
  32. package/dist/homebridge-ui/public/index.html +48 -1
  33. package/dist/homebridge-ui/server.js +35 -0
  34. package/dist/homebridge-ui/server.js.map +1 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +4 -5
  37. package/dist/index.js.map +1 -1
  38. package/dist/index.test.js +7 -2
  39. package/dist/index.test.js.map +1 -1
  40. package/dist/irdevice/irdevice.d.ts +11 -10
  41. package/dist/irdevice/irdevice.d.ts.map +1 -1
  42. package/dist/irdevice/irdevice.js +76 -35
  43. package/dist/irdevice/irdevice.js.map +1 -1
  44. package/dist/platform-hap.d.ts +11 -14
  45. package/dist/platform-hap.d.ts.map +1 -1
  46. package/dist/platform-hap.js +64 -64
  47. package/dist/platform-hap.js.map +1 -1
  48. package/dist/platform-matter.d.ts +87 -6
  49. package/dist/platform-matter.d.ts.map +1 -1
  50. package/dist/platform-matter.js +1845 -84
  51. package/dist/platform-matter.js.map +1 -1
  52. package/dist/settings.d.ts +11 -0
  53. package/dist/settings.d.ts.map +1 -1
  54. package/dist/settings.js.map +1 -1
  55. package/dist/test/hap/platform-hap.logging.test.d.ts +2 -0
  56. package/dist/test/hap/platform-hap.logging.test.d.ts.map +1 -0
  57. package/dist/test/hap/platform-hap.logging.test.js +33 -0
  58. package/dist/test/hap/platform-hap.logging.test.js.map +1 -0
  59. package/dist/test/hap/platform-hap.test.d.ts +2 -0
  60. package/dist/test/hap/platform-hap.test.d.ts.map +1 -0
  61. package/dist/test/hap/platform-hap.test.js +62 -0
  62. package/dist/test/hap/platform-hap.test.js.map +1 -0
  63. package/dist/test/helpers/platform-fixtures.d.ts +9 -0
  64. package/dist/test/helpers/platform-fixtures.d.ts.map +1 -0
  65. package/dist/test/helpers/platform-fixtures.js +30 -0
  66. package/dist/test/helpers/platform-fixtures.js.map +1 -0
  67. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts +2 -0
  68. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
  69. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js +71 -0
  70. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js.map +1 -0
  71. package/dist/test/matter/platform-matter.additional.test.d.ts +2 -0
  72. package/dist/test/matter/platform-matter.additional.test.d.ts.map +1 -0
  73. package/dist/test/matter/platform-matter.additional.test.js +35 -0
  74. package/dist/test/matter/platform-matter.additional.test.js.map +1 -0
  75. package/dist/test/matter/platform-matter.bleparse.test.d.ts +2 -0
  76. package/dist/test/matter/platform-matter.bleparse.test.d.ts.map +1 -0
  77. package/dist/test/matter/platform-matter.bleparse.test.js +43 -0
  78. package/dist/test/matter/platform-matter.bleparse.test.js.map +1 -0
  79. package/dist/test/matter/platform-matter.cleanup.test.d.ts +2 -0
  80. package/dist/test/matter/platform-matter.cleanup.test.d.ts.map +1 -0
  81. package/dist/test/matter/platform-matter.cleanup.test.js +70 -0
  82. package/dist/test/matter/platform-matter.cleanup.test.js.map +1 -0
  83. package/dist/test/matter/platform-matter.keepstale.test.d.ts +2 -0
  84. package/dist/test/matter/platform-matter.keepstale.test.d.ts.map +1 -0
  85. package/dist/test/matter/platform-matter.keepstale.test.js +27 -0
  86. package/dist/test/matter/platform-matter.keepstale.test.js.map +1 -0
  87. package/dist/test/matter/platform-matter.logging.test.d.ts +2 -0
  88. package/dist/test/matter/platform-matter.logging.test.d.ts.map +1 -0
  89. package/dist/test/matter/platform-matter.logging.test.js +29 -0
  90. package/dist/test/matter/platform-matter.logging.test.js.map +1 -0
  91. package/dist/test/matter/platform-matter.mapping.test.d.ts +2 -0
  92. package/dist/test/matter/platform-matter.mapping.test.d.ts.map +1 -0
  93. package/dist/test/matter/platform-matter.mapping.test.js +43 -0
  94. package/dist/test/matter/platform-matter.mapping.test.js.map +1 -0
  95. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts +2 -0
  96. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts.map +1 -0
  97. package/dist/test/matter/platform-matter.openapi-mapping.test.js +84 -0
  98. package/dist/test/matter/platform-matter.openapi-mapping.test.js.map +1 -0
  99. package/dist/test/matter/platform-matter.test.d.ts +2 -0
  100. package/dist/test/matter/platform-matter.test.d.ts.map +1 -0
  101. package/dist/test/matter/platform-matter.test.js +117 -0
  102. package/dist/test/matter/platform-matter.test.js.map +1 -0
  103. package/dist/test/matter/platform-matter.unregister.test.d.ts +2 -0
  104. package/dist/test/matter/platform-matter.unregister.test.d.ts.map +1 -0
  105. package/dist/test/matter/platform-matter.unregister.test.js +30 -0
  106. package/dist/test/matter/platform-matter.unregister.test.js.map +1 -0
  107. package/dist/utils.d.ts +127 -0
  108. package/dist/utils.d.ts.map +1 -1
  109. package/dist/utils.js +405 -0
  110. package/dist/utils.js.map +1 -1
  111. package/dist/utils.test.d.ts +2 -0
  112. package/dist/utils.test.d.ts.map +1 -0
  113. package/dist/utils.test.js +95 -0
  114. package/dist/utils.test.js.map +1 -0
  115. package/dist/verifyconfig.test.js +2 -2
  116. package/dist/verifyconfig.test.js.map +1 -1
  117. package/docs/assets/main.js +2 -2
  118. package/docs/index.html +20 -2
  119. package/docs/variables/default.html +1 -1
  120. package/package.json +14 -14
  121. package/src/devices-hap/device.ts +68 -30
  122. package/src/devices-matter/BaseMatterAccessory.ts +168 -5
  123. package/src/devices-matter/ColorLightAccessory.ts +12 -12
  124. package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
  125. package/src/devices-matter/DimmableLightAccessory.ts +9 -9
  126. package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
  127. package/src/devices-matter/OnOffLightAccessory.ts +8 -16
  128. package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
  129. package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
  130. package/src/homebridge-ui/public/index.html +48 -1
  131. package/src/homebridge-ui/server.ts +37 -0
  132. package/src/index.test.ts +7 -2
  133. package/src/index.ts +4 -5
  134. package/src/irdevice/irdevice.ts +74 -35
  135. package/src/platform-hap.ts +68 -73
  136. package/src/platform-matter.ts +1879 -87
  137. package/src/settings.ts +15 -0
  138. package/src/test/hap/platform-hap.logging.test.ts +36 -0
  139. package/src/test/hap/platform-hap.test.ts +70 -0
  140. package/src/test/helpers/platform-fixtures.ts +33 -0
  141. package/src/test/matter/devices-matter/baseMatterAccessory.test.ts +88 -0
  142. package/src/test/matter/platform-matter.additional.test.ts +44 -0
  143. package/src/test/matter/platform-matter.bleparse.test.ts +47 -0
  144. package/src/test/matter/platform-matter.cleanup.test.ts +86 -0
  145. package/src/test/matter/platform-matter.keepstale.test.ts +37 -0
  146. package/src/test/matter/platform-matter.logging.test.ts +33 -0
  147. package/src/test/matter/platform-matter.mapping.test.ts +57 -0
  148. package/src/test/matter/platform-matter.openapi-mapping.test.ts +109 -0
  149. package/src/test/matter/platform-matter.test.ts +144 -0
  150. package/src/test/matter/platform-matter.unregister.test.ts +39 -0
  151. package/src/utils.test.ts +96 -0
  152. package/src/utils.ts +419 -3
  153. package/src/verifyconfig.test.ts +11 -10
@@ -36,7 +36,7 @@ import { TV } from './irdevice/tv.js';
36
36
  import { VacuumCleaner } from './irdevice/vacuumcleaner.js';
37
37
  import { WaterHeater } from './irdevice/waterheater.js';
38
38
  import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
39
- import { cleanDeviceConfig, formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, safeStringify, sleep } from './utils.js';
39
+ import { ApiRequestTracker, cleanDeviceConfig, createPlatformLogger, formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, safeStringify, sleep } from './utils.js';
40
40
  /**
41
41
  * HomebridgePlatform
42
42
  * This class is the main constructor for your plugin, this is where you should
@@ -47,6 +47,17 @@ export class SwitchBotHAPPlatform {
47
47
  accessories = [];
48
48
  api;
49
49
  log;
50
+ // Logging helper functions (attached from utils.createPlatformLogger in constructor)
51
+ infoLog;
52
+ successLog;
53
+ debugSuccessLog;
54
+ warnLog;
55
+ debugWarnLog;
56
+ errorLog;
57
+ debugErrorLog;
58
+ debugLog;
59
+ loggingIsDebug;
60
+ enablingPlatformLogging;
50
61
  // Configuration properties
51
62
  platformConfig;
52
63
  platformLogging;
@@ -64,6 +75,8 @@ export class SwitchBotHAPPlatform {
64
75
  // SwitchBot APIs
65
76
  switchBotAPI;
66
77
  switchBotBLE;
78
+ // API request tracking
79
+ apiTracker;
67
80
  // External APIs
68
81
  eve;
69
82
  fakegatoAPI;
@@ -73,9 +86,21 @@ export class SwitchBotHAPPlatform {
73
86
  constructor(log, config, api) {
74
87
  this.api = api;
75
88
  this.log = log;
89
+ // Attach shared platform logging helpers (moved to utils for reuse)
90
+ const _pl = createPlatformLogger(async () => this.platformLogging, this.log);
91
+ this.infoLog = _pl.infoLog;
92
+ this.successLog = _pl.successLog;
93
+ this.debugSuccessLog = _pl.debugSuccessLog;
94
+ this.warnLog = _pl.warnLog;
95
+ this.debugWarnLog = _pl.debugWarnLog;
96
+ this.errorLog = _pl.errorLog;
97
+ this.debugErrorLog = _pl.debugErrorLog;
98
+ this.debugLog = _pl.debugLog;
99
+ this.loggingIsDebug = _pl.loggingIsDebug;
100
+ this.enablingPlatformLogging = _pl.enablingPlatformLogging;
76
101
  // only load if configured
77
102
  if (!config) {
78
- this.log.error('No configuration found for the plugin, please check your config.');
103
+ this.errorLog('No configuration found for the plugin, please check your config.');
79
104
  return;
80
105
  }
81
106
  // Plugin options into our config variables.
@@ -86,6 +111,19 @@ export class SwitchBotHAPPlatform {
86
111
  options: config.options,
87
112
  devices: config.devices,
88
113
  };
114
+ // Determine platform logging preference (match HAP behaviour as closely as
115
+ // possible using config values. We default to 'standard' when unspecified.)
116
+ this.platformLogging = (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none')
117
+ ? this.config.options.logging
118
+ : 'standard';
119
+ // Unconditional diagnostic using the raw Homebridge `log` so it always
120
+ // appears regardless of the platform logging helpers' gating logic.
121
+ try {
122
+ this.log.debug?.(`[SwitchBot HAP] effective platformLogging=${String(this.platformLogging)}`);
123
+ }
124
+ catch (e) {
125
+ // swallow any logging errors — diagnostics are best-effort
126
+ }
89
127
  // Normalize deviceConfig to remove UI-inserted defaults (lots of false/empty values)
90
128
  try {
91
129
  if (this.config.options) {
@@ -171,9 +209,29 @@ export class SwitchBotHAPPlatform {
171
209
  // to start discovery of new accessories.
172
210
  this.api.on('didFinishLaunching', async () => {
173
211
  this.debugLog('Executed didFinishLaunching callback');
212
+ // Initialize API request tracking
213
+ try {
214
+ this.apiTracker = new ApiRequestTracker(this.api, this.log, 'SwitchBot HAP');
215
+ this.apiTracker.startHourlyLogging();
216
+ }
217
+ catch (e) {
218
+ this.errorLog(`Failed to initialize API request tracking: ${e.message ?? e}`);
219
+ }
174
220
  // run the method to discover / register your devices as accessories
175
221
  try {
176
- await this.discoverDevices();
222
+ // Does the user have a version of Homebridge that is compatible with matter?
223
+ if (!this.api.isMatterAvailable?.()) {
224
+ this.debugLog(`Matter is not available in this version of Homebridge. Please update Homebridge to use this plugin, ${this.api.isMatterAvailable?.() ? '' : ' (Matter is not available in this version of Homebridge)'}`);
225
+ }
226
+ if (!this.api.isMatterEnabled?.()) {
227
+ this.debugLog(`Matter is not enabled in Homebridge. Please enable Matter in the Homebridge settings to use this plugin, ${this.api.isMatterEnabled?.() ? '' : ' (Matter is not enabled in Homebridge)'}`);
228
+ }
229
+ if (!this.api.isMatterAvailable?.() && !this.api.isMatterEnabled?.()) {
230
+ await this.discoverDevices();
231
+ }
232
+ else {
233
+ this.infoLog('Matter is enabled in Homebridge. SwitchBot Matter devices will be handled by the Matter platform.');
234
+ }
177
235
  }
178
236
  catch (e) {
179
237
  this.errorLog(`Failed to Discover, Error Message: ${e.message ?? e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
@@ -406,6 +464,7 @@ export class SwitchBotHAPPlatform {
406
464
  this.debugWarnLog(`Delay Between Retries: ${this.platformDelayBetweenRetries}`);
407
465
  while (retryCount < maxRetries) {
408
466
  try {
467
+ this.apiTracker?.track();
409
468
  const { response, statusCode } = await this.switchBotAPI.getDevices();
410
469
  this.debugLog(`response: ${JSON.stringify(response)}`);
411
470
  if (this.isSuccessfulResponse(statusCode)) {
@@ -2637,6 +2696,7 @@ export class SwitchBotHAPPlatform {
2637
2696
  const delayBetweenRetries = deviceDelayBetweenRetries;
2638
2697
  while (retryCount < maxRetries) {
2639
2698
  try {
2699
+ this.apiTracker?.track();
2640
2700
  const { response, statusCode } = await this.switchBotAPI.getDeviceStatus(device.deviceId, this.config.credentials?.token, this.config.credentials?.secret);
2641
2701
  this.debugLog(`response: ${JSON.stringify(response)}`);
2642
2702
  return { response, statusCode };
@@ -2662,6 +2722,7 @@ export class SwitchBotHAPPlatform {
2662
2722
  const delayBetweenRetries = deviceDelayBetweenRetries ?? 1000;
2663
2723
  while (retryCount < maxRetries) {
2664
2724
  try {
2725
+ this.apiTracker?.track();
2665
2726
  const { response, statusCode } = await this.switchBotAPI.controlDevice(device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType, this.config.credentials?.token, this.config.credentials?.secret);
2666
2727
  this.debugLog(`response: ${JSON.stringify(response)}`);
2667
2728
  return { response, statusCode };
@@ -2784,66 +2845,5 @@ export class SwitchBotHAPPlatform {
2784
2845
  return value;
2785
2846
  }
2786
2847
  }
2787
- /**
2788
- * If device level logging is turned on, log to log.warn
2789
- * Otherwise send debug logs to log.debug
2790
- */
2791
- async infoLog(...log) {
2792
- if (await this.enablingPlatformLogging()) {
2793
- this.log.info(String(...log));
2794
- }
2795
- }
2796
- async successLog(...log) {
2797
- if (await this.enablingPlatformLogging()) {
2798
- this.log.success(String(...log));
2799
- }
2800
- }
2801
- async debugSuccessLog(...log) {
2802
- if (await this.enablingPlatformLogging()) {
2803
- if (await this.loggingIsDebug()) {
2804
- this.log.success('[DEBUG]', String(...log));
2805
- }
2806
- }
2807
- }
2808
- async warnLog(...log) {
2809
- if (await this.enablingPlatformLogging()) {
2810
- this.log.warn(String(...log));
2811
- }
2812
- }
2813
- async debugWarnLog(...log) {
2814
- if (await this.enablingPlatformLogging()) {
2815
- if (await this.loggingIsDebug()) {
2816
- this.log.warn('[DEBUG]', String(...log));
2817
- }
2818
- }
2819
- }
2820
- async errorLog(...log) {
2821
- if (await this.enablingPlatformLogging()) {
2822
- this.log.error(String(...log));
2823
- }
2824
- }
2825
- async debugErrorLog(...log) {
2826
- if (await this.enablingPlatformLogging()) {
2827
- if (await this.loggingIsDebug()) {
2828
- this.log.error('[DEBUG]', String(...log));
2829
- }
2830
- }
2831
- }
2832
- async debugLog(...log) {
2833
- if (await this.enablingPlatformLogging()) {
2834
- if (this.platformLogging === 'debug') {
2835
- this.log.info('[DEBUG]', String(...log));
2836
- }
2837
- else if (this.platformLogging === 'debugMode') {
2838
- this.log.debug(String(...log));
2839
- }
2840
- }
2841
- }
2842
- async loggingIsDebug() {
2843
- return this.platformLogging === 'debugMode' || this.platformLogging === 'debug';
2844
- }
2845
- async enablingPlatformLogging() {
2846
- return this.platformLogging === 'debugMode' || this.platformLogging === 'debug' || this.platformLogging === 'standard';
2847
- }
2848
2848
  }
2849
2849
  //# sourceMappingURL=platform-hap.js.map