matterbridge 3.3.8-dev-20251115-ca5ff21 → 3.3.8

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 (310) hide show
  1. package/dist/broadcastServer.d.ts +115 -0
  2. package/dist/broadcastServer.d.ts.map +1 -0
  3. package/dist/broadcastServer.js +93 -1
  4. package/dist/broadcastServer.js.map +1 -0
  5. package/dist/broadcastServerTypes.d.ts +806 -0
  6. package/dist/broadcastServerTypes.d.ts.map +1 -0
  7. package/dist/broadcastServerTypes.js +24 -0
  8. package/dist/broadcastServerTypes.js.map +1 -0
  9. package/dist/cli.d.ts +30 -0
  10. package/dist/cli.d.ts.map +1 -0
  11. package/dist/cli.js +97 -1
  12. package/dist/cli.js.map +1 -0
  13. package/dist/cliEmitter.d.ts +50 -0
  14. package/dist/cliEmitter.d.ts.map +1 -0
  15. package/dist/cliEmitter.js +37 -0
  16. package/dist/cliEmitter.js.map +1 -0
  17. package/dist/cliHistory.d.ts +48 -0
  18. package/dist/cliHistory.d.ts.map +1 -0
  19. package/dist/cliHistory.js +38 -0
  20. package/dist/cliHistory.js.map +1 -0
  21. package/dist/clusters/export.d.ts +2 -0
  22. package/dist/clusters/export.d.ts.map +1 -0
  23. package/dist/clusters/export.js +2 -0
  24. package/dist/clusters/export.js.map +1 -0
  25. package/dist/defaultConfigSchema.d.ts +28 -0
  26. package/dist/defaultConfigSchema.d.ts.map +1 -0
  27. package/dist/defaultConfigSchema.js +24 -0
  28. package/dist/defaultConfigSchema.js.map +1 -0
  29. package/dist/deviceManager.d.ts +128 -0
  30. package/dist/deviceManager.d.ts.map +1 -0
  31. package/dist/deviceManager.js +105 -1
  32. package/dist/deviceManager.js.map +1 -0
  33. package/dist/devices/airConditioner.d.ts +98 -0
  34. package/dist/devices/airConditioner.d.ts.map +1 -0
  35. package/dist/devices/airConditioner.js +57 -0
  36. package/dist/devices/airConditioner.js.map +1 -0
  37. package/dist/devices/batteryStorage.d.ts +48 -0
  38. package/dist/devices/batteryStorage.d.ts.map +1 -0
  39. package/dist/devices/batteryStorage.js +48 -1
  40. package/dist/devices/batteryStorage.js.map +1 -0
  41. package/dist/devices/cooktop.d.ts +60 -0
  42. package/dist/devices/cooktop.d.ts.map +1 -0
  43. package/dist/devices/cooktop.js +55 -0
  44. package/dist/devices/cooktop.js.map +1 -0
  45. package/dist/devices/dishwasher.d.ts +71 -0
  46. package/dist/devices/dishwasher.d.ts.map +1 -0
  47. package/dist/devices/dishwasher.js +57 -0
  48. package/dist/devices/dishwasher.js.map +1 -0
  49. package/dist/devices/evse.d.ts +76 -0
  50. package/dist/devices/evse.d.ts.map +1 -0
  51. package/dist/devices/evse.js +74 -10
  52. package/dist/devices/evse.js.map +1 -0
  53. package/dist/devices/export.d.ts +17 -0
  54. package/dist/devices/export.d.ts.map +1 -0
  55. package/dist/devices/export.js +5 -0
  56. package/dist/devices/export.js.map +1 -0
  57. package/dist/devices/extractorHood.d.ts +46 -0
  58. package/dist/devices/extractorHood.d.ts.map +1 -0
  59. package/dist/devices/extractorHood.js +43 -0
  60. package/dist/devices/extractorHood.js.map +1 -0
  61. package/dist/devices/heatPump.d.ts +47 -0
  62. package/dist/devices/heatPump.d.ts.map +1 -0
  63. package/dist/devices/heatPump.js +50 -2
  64. package/dist/devices/heatPump.js.map +1 -0
  65. package/dist/devices/laundryDryer.d.ts +67 -0
  66. package/dist/devices/laundryDryer.d.ts.map +1 -0
  67. package/dist/devices/laundryDryer.js +62 -3
  68. package/dist/devices/laundryDryer.js.map +1 -0
  69. package/dist/devices/laundryWasher.d.ts +81 -0
  70. package/dist/devices/laundryWasher.d.ts.map +1 -0
  71. package/dist/devices/laundryWasher.js +70 -4
  72. package/dist/devices/laundryWasher.js.map +1 -0
  73. package/dist/devices/microwaveOven.d.ts +168 -0
  74. package/dist/devices/microwaveOven.d.ts.map +1 -0
  75. package/dist/devices/microwaveOven.js +88 -5
  76. package/dist/devices/microwaveOven.js.map +1 -0
  77. package/dist/devices/oven.d.ts +105 -0
  78. package/dist/devices/oven.d.ts.map +1 -0
  79. package/dist/devices/oven.js +92 -11
  80. package/dist/devices/oven.js.map +1 -0
  81. package/dist/devices/refrigerator.d.ts +118 -0
  82. package/dist/devices/refrigerator.d.ts.map +1 -0
  83. package/dist/devices/refrigerator.js +102 -0
  84. package/dist/devices/refrigerator.js.map +1 -0
  85. package/dist/devices/roboticVacuumCleaner.d.ts +112 -0
  86. package/dist/devices/roboticVacuumCleaner.d.ts.map +1 -0
  87. package/dist/devices/roboticVacuumCleaner.js +111 -20
  88. package/dist/devices/roboticVacuumCleaner.js.map +1 -0
  89. package/dist/devices/solarPower.d.ts +40 -0
  90. package/dist/devices/solarPower.d.ts.map +1 -0
  91. package/dist/devices/solarPower.js +38 -0
  92. package/dist/devices/solarPower.js.map +1 -0
  93. package/dist/devices/speaker.d.ts +87 -0
  94. package/dist/devices/speaker.d.ts.map +1 -0
  95. package/dist/devices/speaker.js +84 -0
  96. package/dist/devices/speaker.js.map +1 -0
  97. package/dist/devices/temperatureControl.d.ts +166 -0
  98. package/dist/devices/temperatureControl.d.ts.map +1 -0
  99. package/dist/devices/temperatureControl.js +24 -3
  100. package/dist/devices/temperatureControl.js.map +1 -0
  101. package/dist/devices/waterHeater.d.ts +111 -0
  102. package/dist/devices/waterHeater.d.ts.map +1 -0
  103. package/dist/devices/waterHeater.js +82 -2
  104. package/dist/devices/waterHeater.js.map +1 -0
  105. package/dist/dgram/coap.d.ts +205 -0
  106. package/dist/dgram/coap.d.ts.map +1 -0
  107. package/dist/dgram/coap.js +126 -13
  108. package/dist/dgram/coap.js.map +1 -0
  109. package/dist/dgram/dgram.d.ts +141 -0
  110. package/dist/dgram/dgram.d.ts.map +1 -0
  111. package/dist/dgram/dgram.js +114 -2
  112. package/dist/dgram/dgram.js.map +1 -0
  113. package/dist/dgram/mb_coap.d.ts +24 -0
  114. package/dist/dgram/mb_coap.d.ts.map +1 -0
  115. package/dist/dgram/mb_coap.js +41 -3
  116. package/dist/dgram/mb_coap.js.map +1 -0
  117. package/dist/dgram/mb_mdns.d.ts +24 -0
  118. package/dist/dgram/mb_mdns.d.ts.map +1 -0
  119. package/dist/dgram/mb_mdns.js +80 -15
  120. package/dist/dgram/mb_mdns.js.map +1 -0
  121. package/dist/dgram/mdns.d.ts +290 -0
  122. package/dist/dgram/mdns.d.ts.map +1 -0
  123. package/dist/dgram/mdns.js +299 -137
  124. package/dist/dgram/mdns.js.map +1 -0
  125. package/dist/dgram/multicast.d.ts +67 -0
  126. package/dist/dgram/multicast.d.ts.map +1 -0
  127. package/dist/dgram/multicast.js +62 -1
  128. package/dist/dgram/multicast.js.map +1 -0
  129. package/dist/dgram/unicast.d.ts +56 -0
  130. package/dist/dgram/unicast.d.ts.map +1 -0
  131. package/dist/dgram/unicast.js +54 -0
  132. package/dist/dgram/unicast.js.map +1 -0
  133. package/dist/frontend.d.ts +238 -0
  134. package/dist/frontend.d.ts.map +1 -0
  135. package/dist/frontend.js +454 -35
  136. package/dist/frontend.js.map +1 -0
  137. package/dist/frontendTypes.d.ts +529 -0
  138. package/dist/frontendTypes.d.ts.map +1 -0
  139. package/dist/frontendTypes.js +45 -0
  140. package/dist/frontendTypes.js.map +1 -0
  141. package/dist/helpers.d.ts +48 -0
  142. package/dist/helpers.d.ts.map +1 -0
  143. package/dist/helpers.js +53 -0
  144. package/dist/helpers.js.map +1 -0
  145. package/dist/index.d.ts +34 -0
  146. package/dist/index.d.ts.map +1 -0
  147. package/dist/index.js +25 -0
  148. package/dist/index.js.map +1 -0
  149. package/dist/jestutils/export.d.ts +2 -0
  150. package/dist/jestutils/export.d.ts.map +1 -0
  151. package/dist/jestutils/export.js +1 -0
  152. package/dist/jestutils/export.js.map +1 -0
  153. package/dist/jestutils/jestHelpers.d.ts +250 -0
  154. package/dist/jestutils/jestHelpers.d.ts.map +1 -0
  155. package/dist/jestutils/jestHelpers.js +275 -4
  156. package/dist/jestutils/jestHelpers.js.map +1 -0
  157. package/dist/logger/export.d.ts +2 -0
  158. package/dist/logger/export.d.ts.map +1 -0
  159. package/dist/logger/export.js +1 -0
  160. package/dist/logger/export.js.map +1 -0
  161. package/dist/matter/behaviors.d.ts +2 -0
  162. package/dist/matter/behaviors.d.ts.map +1 -0
  163. package/dist/matter/behaviors.js +2 -0
  164. package/dist/matter/behaviors.js.map +1 -0
  165. package/dist/matter/clusters.d.ts +2 -0
  166. package/dist/matter/clusters.d.ts.map +1 -0
  167. package/dist/matter/clusters.js +2 -0
  168. package/dist/matter/clusters.js.map +1 -0
  169. package/dist/matter/devices.d.ts +2 -0
  170. package/dist/matter/devices.d.ts.map +1 -0
  171. package/dist/matter/devices.js +2 -0
  172. package/dist/matter/devices.js.map +1 -0
  173. package/dist/matter/endpoints.d.ts +2 -0
  174. package/dist/matter/endpoints.d.ts.map +1 -0
  175. package/dist/matter/endpoints.js +2 -0
  176. package/dist/matter/endpoints.js.map +1 -0
  177. package/dist/matter/export.d.ts +5 -0
  178. package/dist/matter/export.d.ts.map +1 -0
  179. package/dist/matter/export.js +3 -0
  180. package/dist/matter/export.js.map +1 -0
  181. package/dist/matter/types.d.ts +3 -0
  182. package/dist/matter/types.d.ts.map +1 -0
  183. package/dist/matter/types.js +3 -0
  184. package/dist/matter/types.js.map +1 -0
  185. package/dist/matterbridge.d.ts +469 -0
  186. package/dist/matterbridge.d.ts.map +1 -0
  187. package/dist/matterbridge.js +788 -46
  188. package/dist/matterbridge.js.map +1 -0
  189. package/dist/matterbridgeAccessoryPlatform.d.ts +41 -0
  190. package/dist/matterbridgeAccessoryPlatform.d.ts.map +1 -0
  191. package/dist/matterbridgeAccessoryPlatform.js +38 -0
  192. package/dist/matterbridgeAccessoryPlatform.js.map +1 -0
  193. package/dist/matterbridgeBehaviors.d.ts +2404 -0
  194. package/dist/matterbridgeBehaviors.d.ts.map +1 -0
  195. package/dist/matterbridgeBehaviors.js +77 -14
  196. package/dist/matterbridgeBehaviors.js.map +1 -0
  197. package/dist/matterbridgeDeviceTypes.d.ts +698 -0
  198. package/dist/matterbridgeDeviceTypes.d.ts.map +1 -0
  199. package/dist/matterbridgeDeviceTypes.js +635 -14
  200. package/dist/matterbridgeDeviceTypes.js.map +1 -0
  201. package/dist/matterbridgeDynamicPlatform.d.ts +41 -0
  202. package/dist/matterbridgeDynamicPlatform.d.ts.map +1 -0
  203. package/dist/matterbridgeDynamicPlatform.js +38 -0
  204. package/dist/matterbridgeDynamicPlatform.js.map +1 -0
  205. package/dist/matterbridgeEndpoint.d.ts +1490 -0
  206. package/dist/matterbridgeEndpoint.d.ts.map +1 -0
  207. package/dist/matterbridgeEndpoint.js +1432 -53
  208. package/dist/matterbridgeEndpoint.js.map +1 -0
  209. package/dist/matterbridgeEndpointHelpers.d.ts +787 -0
  210. package/dist/matterbridgeEndpointHelpers.d.ts.map +1 -0
  211. package/dist/matterbridgeEndpointHelpers.js +488 -25
  212. package/dist/matterbridgeEndpointHelpers.js.map +1 -0
  213. package/dist/matterbridgeEndpointTypes.d.ts +197 -0
  214. package/dist/matterbridgeEndpointTypes.d.ts.map +1 -0
  215. package/dist/matterbridgeEndpointTypes.js +25 -0
  216. package/dist/matterbridgeEndpointTypes.js.map +1 -0
  217. package/dist/matterbridgePlatform.d.ts +415 -0
  218. package/dist/matterbridgePlatform.d.ts.map +1 -0
  219. package/dist/matterbridgePlatform.js +354 -1
  220. package/dist/matterbridgePlatform.js.map +1 -0
  221. package/dist/matterbridgeTypes.d.ts +239 -0
  222. package/dist/matterbridgeTypes.d.ts.map +1 -0
  223. package/dist/matterbridgeTypes.js +26 -0
  224. package/dist/matterbridgeTypes.js.map +1 -0
  225. package/dist/pluginManager.d.ts +371 -0
  226. package/dist/pluginManager.d.ts.map +1 -0
  227. package/dist/pluginManager.js +339 -4
  228. package/dist/pluginManager.js.map +1 -0
  229. package/dist/shelly.d.ts +174 -0
  230. package/dist/shelly.d.ts.map +1 -0
  231. package/dist/shelly.js +168 -7
  232. package/dist/shelly.js.map +1 -0
  233. package/dist/storage/export.d.ts +2 -0
  234. package/dist/storage/export.d.ts.map +1 -0
  235. package/dist/storage/export.js +1 -0
  236. package/dist/storage/export.js.map +1 -0
  237. package/dist/update.d.ts +75 -0
  238. package/dist/update.d.ts.map +1 -0
  239. package/dist/update.js +69 -0
  240. package/dist/update.js.map +1 -0
  241. package/dist/utils/colorUtils.d.ts +101 -0
  242. package/dist/utils/colorUtils.d.ts.map +1 -0
  243. package/dist/utils/colorUtils.js +97 -2
  244. package/dist/utils/colorUtils.js.map +1 -0
  245. package/dist/utils/commandLine.d.ts +66 -0
  246. package/dist/utils/commandLine.d.ts.map +1 -0
  247. package/dist/utils/commandLine.js +60 -0
  248. package/dist/utils/commandLine.js.map +1 -0
  249. package/dist/utils/copyDirectory.d.ts +33 -0
  250. package/dist/utils/copyDirectory.d.ts.map +1 -0
  251. package/dist/utils/copyDirectory.js +38 -1
  252. package/dist/utils/copyDirectory.js.map +1 -0
  253. package/dist/utils/createDirectory.d.ts +34 -0
  254. package/dist/utils/createDirectory.d.ts.map +1 -0
  255. package/dist/utils/createDirectory.js +33 -0
  256. package/dist/utils/createDirectory.js.map +1 -0
  257. package/dist/utils/createZip.d.ts +39 -0
  258. package/dist/utils/createZip.d.ts.map +1 -0
  259. package/dist/utils/createZip.js +47 -2
  260. package/dist/utils/createZip.js.map +1 -0
  261. package/dist/utils/deepCopy.d.ts +32 -0
  262. package/dist/utils/deepCopy.d.ts.map +1 -0
  263. package/dist/utils/deepCopy.js +39 -0
  264. package/dist/utils/deepCopy.js.map +1 -0
  265. package/dist/utils/deepEqual.d.ts +54 -0
  266. package/dist/utils/deepEqual.d.ts.map +1 -0
  267. package/dist/utils/deepEqual.js +72 -1
  268. package/dist/utils/deepEqual.js.map +1 -0
  269. package/dist/utils/error.d.ts +44 -0
  270. package/dist/utils/error.d.ts.map +1 -0
  271. package/dist/utils/error.js +41 -0
  272. package/dist/utils/error.js.map +1 -0
  273. package/dist/utils/export.d.ts +13 -0
  274. package/dist/utils/export.d.ts.map +1 -0
  275. package/dist/utils/export.js +1 -0
  276. package/dist/utils/export.js.map +1 -0
  277. package/dist/utils/format.d.ts +53 -0
  278. package/dist/utils/format.d.ts.map +1 -0
  279. package/dist/utils/format.js +49 -0
  280. package/dist/utils/format.js.map +1 -0
  281. package/dist/utils/hex.d.ts +89 -0
  282. package/dist/utils/hex.d.ts.map +1 -0
  283. package/dist/utils/hex.js +124 -0
  284. package/dist/utils/hex.js.map +1 -0
  285. package/dist/utils/inspector.d.ts +87 -0
  286. package/dist/utils/inspector.d.ts.map +1 -0
  287. package/dist/utils/inspector.js +69 -1
  288. package/dist/utils/inspector.js.map +1 -0
  289. package/dist/utils/isvalid.d.ts +103 -0
  290. package/dist/utils/isvalid.d.ts.map +1 -0
  291. package/dist/utils/isvalid.js +101 -0
  292. package/dist/utils/isvalid.js.map +1 -0
  293. package/dist/utils/network.d.ts +101 -0
  294. package/dist/utils/network.d.ts.map +1 -0
  295. package/dist/utils/network.js +96 -5
  296. package/dist/utils/network.js.map +1 -0
  297. package/dist/utils/spawn.d.ts +35 -0
  298. package/dist/utils/spawn.d.ts.map +1 -0
  299. package/dist/utils/spawn.js +71 -0
  300. package/dist/utils/spawn.js.map +1 -0
  301. package/dist/utils/tracker.d.ts +108 -0
  302. package/dist/utils/tracker.d.ts.map +1 -0
  303. package/dist/utils/tracker.js +64 -1
  304. package/dist/utils/tracker.js.map +1 -0
  305. package/dist/utils/wait.d.ts +54 -0
  306. package/dist/utils/wait.d.ts.map +1 -0
  307. package/dist/utils/wait.js +60 -8
  308. package/dist/utils/wait.js.map +1 -0
  309. package/npm-shrinkwrap.json +2 -2
  310. package/package.json +2 -1
@@ -1,9 +1,37 @@
1
+ /**
2
+ * This file contains the Plugins class.
3
+ *
4
+ * @file plugins.ts
5
+ * @author Luca Liguori
6
+ * @created 2024-07-14
7
+ * @version 1.3.0
8
+ * @license Apache-2.0
9
+ *
10
+ * Copyright 2024, 2025, 2026 Luca Liguori.
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+ // Node.js import
1
25
  import EventEmitter from 'node:events';
26
+ // AnsiLogger module
2
27
  import { AnsiLogger, UNDERLINE, UNDERLINEOFF, BLUE, db, er, nf, nt, rs, wr, debugStringify, CYAN } from 'node-ansi-logger';
3
28
  import { plg, typ } from './matterbridgeTypes.js';
4
29
  import { inspectError, logError } from './utils/error.js';
5
30
  import { hasParameter } from './utils/commandLine.js';
6
31
  import { BroadcastServer } from './broadcastServer.js';
32
+ /**
33
+ * Manages Matterbridge plugins.
34
+ */
7
35
  export class PluginManager extends EventEmitter {
8
36
  matterbridge;
9
37
  _plugins = new Map();
@@ -11,10 +39,15 @@ export class PluginManager extends EventEmitter {
11
39
  server;
12
40
  debug = hasParameter('debug') || hasParameter('verbose');
13
41
  verbose = hasParameter('verbose');
42
+ /**
43
+ * Creates an instance of PluginManager.
44
+ *
45
+ * @param {Matterbridge} matterbridge - The Matterbridge instance.
46
+ */
14
47
  constructor(matterbridge) {
15
48
  super();
16
49
  this.matterbridge = matterbridge;
17
- this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4, logLevel: hasParameter('debug') ? "debug" : "info" });
50
+ this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: hasParameter('debug') ? "debug" /* LogLevel.DEBUG */ : "info" /* LogLevel.INFO */ });
18
51
  this.log.debug('Matterbridge plugin manager starting...');
19
52
  this.server = new BroadcastServer('plugins', this.log);
20
53
  this.server.on('broadcast_message', this.msgHandler.bind(this));
@@ -164,25 +197,62 @@ export class PluginManager extends EventEmitter {
164
197
  }
165
198
  }
166
199
  }
200
+ /**
201
+ * Gets the number of plugins.
202
+ *
203
+ * @returns {number} The number of plugins.
204
+ */
167
205
  get length() {
168
206
  return this._plugins.size;
169
207
  }
208
+ /**
209
+ * Gets the number of plugins.
210
+ *
211
+ * @returns {number} The number of plugins.
212
+ */
170
213
  get size() {
171
214
  return this._plugins.size;
172
215
  }
216
+ /**
217
+ * Checks if a plugin with the specified name exists.
218
+ *
219
+ * @param {string} name - The name of the plugin.
220
+ * @returns {boolean} True if the plugin exists, false otherwise.
221
+ */
173
222
  has(name) {
174
223
  return this._plugins.has(name);
175
224
  }
225
+ /**
226
+ * Gets a plugin by its name.
227
+ *
228
+ * @param {string} name - The name of the plugin.
229
+ * @returns {Plugin | undefined} The plugin, or undefined if not found.
230
+ */
176
231
  get(name) {
177
232
  return this._plugins.get(name);
178
233
  }
234
+ /**
235
+ * Adds a plugin to the manager.
236
+ *
237
+ * @param {Plugin} plugin - The plugin to add.
238
+ * @returns {Plugin} The added plugin.
239
+ */
179
240
  set(plugin) {
180
241
  this._plugins.set(plugin.name, plugin);
181
242
  return plugin;
182
243
  }
244
+ /**
245
+ * Clears all plugins from the manager.
246
+ */
183
247
  clear() {
184
248
  this._plugins.clear();
185
249
  }
250
+ /**
251
+ * Converts a plugin or API plugin to a storage plugin.
252
+ *
253
+ * @param {Plugin | ApiPlugin} plugin - The plugin or API plugin to convert.
254
+ * @returns {StoragePlugin} The converted storage plugin.
255
+ */
186
256
  toStoragePlugin(plugin) {
187
257
  return {
188
258
  name: plugin.name,
@@ -194,6 +264,12 @@ export class PluginManager extends EventEmitter {
194
264
  enabled: plugin.enabled,
195
265
  };
196
266
  }
267
+ /**
268
+ * Converts a plugin to an API plugin.
269
+ *
270
+ * @param {Plugin} plugin - The plugin to convert.
271
+ * @returns {ApiPlugin} The converted API plugin.
272
+ */
197
273
  toApiPlugin(plugin) {
198
274
  return {
199
275
  name: plugin.name,
@@ -223,9 +299,19 @@ export class PluginManager extends EventEmitter {
223
299
  matter: plugin.serverNode ? this.matterbridge.getServerNodeData(plugin.serverNode) : undefined,
224
300
  };
225
301
  }
302
+ /**
303
+ * Gets an array of all plugins.
304
+ *
305
+ * @returns {Plugin[]} An array of all plugins.
306
+ */
226
307
  array() {
227
308
  return Array.from(this._plugins.values());
228
309
  }
310
+ /**
311
+ * Gets a StoragePlugin array of all plugins suitable for serialization.
312
+ *
313
+ * @returns {StoragePlugin[]} An array of all plugins.
314
+ */
229
315
  storagePluginArray() {
230
316
  const storagePlugins = [];
231
317
  for (const plugin of this._plugins.values()) {
@@ -233,6 +319,11 @@ export class PluginManager extends EventEmitter {
233
319
  }
234
320
  return storagePlugins;
235
321
  }
322
+ /**
323
+ * Gets an ApiPlugin array of all plugins suitable for serialization.
324
+ *
325
+ * @returns {ApiPlugin[]} An array of all plugins.
326
+ */
236
327
  apiPluginArray() {
237
328
  const apiPlugins = [];
238
329
  for (const plugin of this._plugins.values()) {
@@ -240,9 +331,20 @@ export class PluginManager extends EventEmitter {
240
331
  }
241
332
  return apiPlugins;
242
333
  }
334
+ /**
335
+ * Gets an iterator for the plugins.
336
+ *
337
+ * @returns {IterableIterator<Plugin>} An iterator for the plugins.
338
+ */
243
339
  [Symbol.iterator]() {
244
340
  return this._plugins.values();
245
341
  }
342
+ /**
343
+ * Executes a provided function once for each plugin.
344
+ *
345
+ * @param {Function} callback - The function to execute for each plugin.
346
+ * @returns {Promise<void>}
347
+ */
246
348
  async forEach(callback) {
247
349
  if (this.size === 0)
248
350
  return;
@@ -256,22 +358,39 @@ export class PluginManager extends EventEmitter {
256
358
  });
257
359
  await Promise.all(tasks);
258
360
  }
361
+ /**
362
+ * Sets the log level for the plugin manager.
363
+ *
364
+ * @param {LogLevel} logLevel - The log level to set.
365
+ */
259
366
  set logLevel(logLevel) {
260
367
  this.log.logLevel = logLevel;
261
368
  }
369
+ /**
370
+ * Loads registered plugins from storage.
371
+ *
372
+ * @returns {Promise<StoragePlugin[]>} A promise that resolves to an array of registered plugins.
373
+ */
262
374
  async loadFromStorage() {
263
375
  if (!this.matterbridge.nodeContext) {
264
376
  throw new Error('loadFromStorage: node context is not available.');
265
377
  }
378
+ // Load the array from storage and convert it to a map
266
379
  const pluginsArray = await this.matterbridge.nodeContext.get('plugins', []);
267
380
  for (const plugin of pluginsArray)
268
381
  this._plugins.set(plugin.name, plugin);
269
382
  return pluginsArray;
270
383
  }
384
+ /**
385
+ * Saves registered plugins to storage.
386
+ *
387
+ * @returns {Promise<number>} A promise that resolves to the number of registered plugins.
388
+ */
271
389
  async saveToStorage() {
272
390
  if (!this.matterbridge.nodeContext) {
273
391
  throw new Error('loadFromStorage: node context is not available.');
274
392
  }
393
+ // Convert the map to an array
275
394
  const plugins = [];
276
395
  for (const plugin of this.array()) {
277
396
  plugins.push({
@@ -288,13 +407,21 @@ export class PluginManager extends EventEmitter {
288
407
  this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
289
408
  return plugins.length;
290
409
  }
410
+ /**
411
+ * Resolves the name of a plugin by loading and parsing its package.json file.
412
+ *
413
+ * @param {string} pluginPath - The path to the plugin or the path to the plugin's package.json file.
414
+ * @returns {Promise<string | null>} A promise that resolves to the path of the plugin's package.json file or null if it could not be resolved.
415
+ */
291
416
  async resolve(pluginPath) {
292
417
  const { default: path } = await import('node:path');
293
418
  const { promises } = await import('node:fs');
294
419
  if (!pluginPath.endsWith('package.json'))
295
420
  pluginPath = path.join(pluginPath, 'package.json');
421
+ // Resolve the package.json of the plugin
296
422
  let packageJsonPath = path.resolve(pluginPath);
297
423
  this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
424
+ // Check if the package.json file exists
298
425
  try {
299
426
  await promises.access(packageJsonPath);
300
427
  }
@@ -304,7 +431,9 @@ export class PluginManager extends EventEmitter {
304
431
  this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
305
432
  }
306
433
  try {
434
+ // Load the package.json of the plugin
307
435
  const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
436
+ // Check for main issues
308
437
  if (!packageJson.name) {
309
438
  this.log.error(`Package.json name not found at ${packageJsonPath}`);
310
439
  return null;
@@ -317,6 +446,7 @@ export class PluginManager extends EventEmitter {
317
446
  this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
318
447
  return null;
319
448
  }
449
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
320
450
  const checkForProjectChipPackages = (dependencies) => {
321
451
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
322
452
  };
@@ -338,6 +468,7 @@ export class PluginManager extends EventEmitter {
338
468
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
339
469
  return null;
340
470
  }
471
+ // Check for matterbridge package in dependencies and devDependencies
341
472
  const checkForMatterbridgePackage = (dependencies) => {
342
473
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
343
474
  };
@@ -367,6 +498,12 @@ export class PluginManager extends EventEmitter {
367
498
  return null;
368
499
  }
369
500
  }
501
+ /**
502
+ * Installs a package globally using npm.
503
+ *
504
+ * @param {string} packageName - The name of the package to install.
505
+ * @returns {Promise<boolean>} A promise that resolves to true if the installation was successful, false otherwise.
506
+ */
370
507
  async install(packageName) {
371
508
  this.log.debug(`Installing plugin ${plg}${packageName}${db}...`);
372
509
  const { spawnCommand } = await import('./utils/spawn.js');
@@ -374,7 +511,7 @@ export class PluginManager extends EventEmitter {
374
511
  await spawnCommand(this.matterbridge, 'npm', ['install', '-g', packageName, '--omit=dev', '--verbose'], 'install', packageName);
375
512
  this.matterbridge.restartRequired = true;
376
513
  this.matterbridge.fixedRestartRequired = true;
377
- packageName = packageName.replace(/@.*$/, '');
514
+ packageName = packageName.replace(/@.*$/, ''); // Remove @version if present
378
515
  if (packageName !== 'matterbridge') {
379
516
  if (!this.has(packageName))
380
517
  await this.add(packageName);
@@ -396,6 +533,12 @@ export class PluginManager extends EventEmitter {
396
533
  return false;
397
534
  }
398
535
  }
536
+ /**
537
+ * Uninstalls a package globally using npm.
538
+ *
539
+ * @param {string} packageName - The name of the package to uninstall.
540
+ * @returns {Promise<boolean>} A promise that resolves to true if the uninstallation was successful, false otherwise.
541
+ */
399
542
  async uninstall(packageName) {
400
543
  this.log.debug(`Uninstalling plugin ${plg}${packageName}${db}...`);
401
544
  const { spawnCommand } = await import('./utils/spawn.js');
@@ -419,6 +562,12 @@ export class PluginManager extends EventEmitter {
419
562
  return false;
420
563
  }
421
564
  }
565
+ /**
566
+ * Get the author of a plugin from its package.json.
567
+ *
568
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
569
+ * @returns {string} The author of the plugin, or 'Unknown author' if not found.
570
+ */
422
571
  getAuthor(packageJson) {
423
572
  if (packageJson.author && typeof packageJson.author === 'string')
424
573
  return packageJson.author;
@@ -426,6 +575,12 @@ export class PluginManager extends EventEmitter {
426
575
  return packageJson.author.name;
427
576
  return 'Unknown author';
428
577
  }
578
+ /**
579
+ * Get the homepage of a plugin from its package.json.
580
+ *
581
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
582
+ * @returns {string | undefined} The homepage of the plugin, or undefined if not found.
583
+ */
429
584
  getHomepage(packageJson) {
430
585
  if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
431
586
  return packageJson.homepage.replace('git+', '').replace('.git', '');
@@ -434,7 +589,14 @@ export class PluginManager extends EventEmitter {
434
589
  return packageJson.repository.url.replace('git+', '').replace('.git', '');
435
590
  }
436
591
  }
592
+ /**
593
+ * Get the help URL of a plugin from its package.json.
594
+ *
595
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
596
+ * @returns {string | undefined} The URL to the help page or to the README file, or undefined if not found.
597
+ */
437
598
  getHelp(packageJson) {
599
+ // If there's a help field that looks like a URL, return it.
438
600
  if (packageJson.help && typeof packageJson.help === 'string' && packageJson.help.startsWith('http')) {
439
601
  return packageJson.help;
440
602
  }
@@ -445,7 +607,14 @@ export class PluginManager extends EventEmitter {
445
607
  return packageJson.homepage.replace('git+', '').replace('.git', '');
446
608
  }
447
609
  }
610
+ /**
611
+ * Get the changelog URL of a plugin from its package.json.
612
+ *
613
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
614
+ * @returns {string | undefined} The URL to the CHANGELOG file, or undefined if not found.
615
+ */
448
616
  getChangelog(packageJson) {
617
+ // If there's a changelog field that looks like a URL, return it.
449
618
  if (packageJson.changelog && typeof packageJson.changelog === 'string' && packageJson.changelog.startsWith('http')) {
450
619
  return packageJson.changelog;
451
620
  }
@@ -456,6 +625,13 @@ export class PluginManager extends EventEmitter {
456
625
  return packageJson.homepage.replace('git+', '').replace('.git', '');
457
626
  }
458
627
  }
628
+ /**
629
+ * Get the first funding URL(s) of a plugin from its package.json.
630
+ *
631
+ * @param {Record<string, any>} packageJson - The package.json object of the plugin.
632
+ * @returns {string | undefined} The first funding URLs, or undefined if not found.
633
+ */
634
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
459
635
  getFunding(packageJson) {
460
636
  const funding = packageJson.funding;
461
637
  if (!funding)
@@ -464,16 +640,27 @@ export class PluginManager extends EventEmitter {
464
640
  return;
465
641
  if (typeof funding === 'string' && funding.startsWith('http'))
466
642
  return funding;
643
+ // Normalize funding into an array.
467
644
  const fundingEntries = Array.isArray(funding) ? funding : [funding];
468
645
  for (const entry of fundingEntries) {
469
646
  if (entry && typeof entry === 'string' && entry.startsWith('http')) {
647
+ // If the funding entry is a string, assume it is a URL.
470
648
  return entry;
471
649
  }
472
650
  else if (entry && typeof entry === 'object' && typeof entry.url === 'string' && entry.url.startsWith('http')) {
651
+ // If it's an object with a 'url' property, use that.
473
652
  return entry.url;
474
653
  }
475
654
  }
476
655
  }
656
+ /**
657
+ * Parses the plugin package.json and returns it.
658
+ * It will also log warnings and errors for missing or invalid fields.
659
+ * It will return null if critical errors are found.
660
+ *
661
+ * @param {Plugin | PluginName} plugin - The plugin to load the package from.
662
+ * @returns {Promise<Record<string, string | number | object> | null>} A promise that resolves to the parsed package.json object or null if it could not be parsed.
663
+ */
477
664
  async parse(plugin) {
478
665
  const { promises } = await import('node:fs');
479
666
  if (typeof plugin === 'string') {
@@ -511,6 +698,7 @@ export class PluginManager extends EventEmitter {
511
698
  plugin.funding = this.getFunding(packageJson);
512
699
  if (!plugin.type)
513
700
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
701
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
514
702
  const checkForProjectChipPackages = (dependencies) => {
515
703
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
516
704
  };
@@ -532,6 +720,7 @@ export class PluginManager extends EventEmitter {
532
720
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
533
721
  return null;
534
722
  }
723
+ // Check for matterbridge package in dependencies and devDependencies
535
724
  const checkForMatterbridgePackage = (dependencies) => {
536
725
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
537
726
  };
@@ -561,6 +750,16 @@ export class PluginManager extends EventEmitter {
561
750
  return null;
562
751
  }
563
752
  }
753
+ /**
754
+ * Enables a plugin by its name or path.
755
+ *
756
+ * This method enables a plugin by setting its `enabled` property to `true` and saving the updated
757
+ * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
758
+ * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
759
+ *
760
+ * @param {string} nameOrPath - The name or path of the plugin to enable.
761
+ * @returns {Promise<Plugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
762
+ */
564
763
  async enable(nameOrPath) {
565
764
  const { promises } = await import('node:fs');
566
765
  if (!nameOrPath)
@@ -596,6 +795,16 @@ export class PluginManager extends EventEmitter {
596
795
  return null;
597
796
  }
598
797
  }
798
+ /**
799
+ * Disables a plugin by its name or path.
800
+ *
801
+ * This method disables a plugin by setting its `enabled` property to `false` and saving the updated
802
+ * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
803
+ * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and disable it.
804
+ *
805
+ * @param {string} nameOrPath - The name or path of the plugin to enable.
806
+ * @returns {Promise<Plugin | null>} A promise that resolves to the disabled plugin object, or null if the plugin could not be disabled.
807
+ */
599
808
  async disable(nameOrPath) {
600
809
  const { promises } = await import('node:fs');
601
810
  if (!nameOrPath)
@@ -631,6 +840,16 @@ export class PluginManager extends EventEmitter {
631
840
  return null;
632
841
  }
633
842
  }
843
+ /**
844
+ * Removes a plugin by its name or path.
845
+ *
846
+ * This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
847
+ * It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
848
+ * the plugin's `package.json` file to retrieve its name and remove it.
849
+ *
850
+ * @param {string} nameOrPath - The name or path of the plugin to remove.
851
+ * @returns {Promise<Plugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
852
+ */
634
853
  async remove(nameOrPath) {
635
854
  const { promises } = await import('node:fs');
636
855
  if (!nameOrPath)
@@ -666,6 +885,17 @@ export class PluginManager extends EventEmitter {
666
885
  return null;
667
886
  }
668
887
  }
888
+ /**
889
+ * Adds a plugin by its name or path.
890
+ *
891
+ * This method adds a plugin to the plugins map and saves the updated plugin information to storage.
892
+ * It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
893
+ * registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
894
+ * and saves the updated plugin information to storage.
895
+ *
896
+ * @param {string} nameOrPath - The name or path of the plugin to add.
897
+ * @returns {Promise<Plugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
898
+ */
669
899
  async add(nameOrPath) {
670
900
  const { promises } = await import('node:fs');
671
901
  if (!nameOrPath)
@@ -705,6 +935,15 @@ export class PluginManager extends EventEmitter {
705
935
  return null;
706
936
  }
707
937
  }
938
+ /**
939
+ * Loads a plugin and returns the corresponding MatterbridgePlatform instance.
940
+ *
941
+ * @param {Plugin | PluginName} plugin - The plugin to load.
942
+ * @param {boolean} start - Optional flag indicating whether to start the plugin after loading. Default is false.
943
+ * @param {string} message - Optional message to pass to the plugin when starting.
944
+ * @param {boolean} configure - Optional flag indicating whether to configure the plugin after loading. Default is false.
945
+ * @returns {Promise<MatterbridgePlatform | undefined>} A Promise that resolves to the loaded MatterbridgePlatform instance or undefined.
946
+ */
708
947
  async load(plugin, start = false, message = '', configure = false) {
709
948
  const { promises } = await import('node:fs');
710
949
  const { default: path } = await import('node:path');
@@ -726,15 +965,20 @@ export class PluginManager extends EventEmitter {
726
965
  }
727
966
  this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
728
967
  try {
968
+ // Load the package.json of the plugin
729
969
  const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
970
+ // Resolve the main module path relative to package.json
730
971
  const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
972
+ // Dynamically import the plugin
731
973
  const { pathToFileURL } = await import('node:url');
732
974
  const pluginUrl = pathToFileURL(pluginEntry);
733
975
  this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
734
976
  const pluginInstance = await import(pluginUrl.href);
735
977
  this.log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
978
+ // Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
736
979
  if (pluginInstance.default) {
737
980
  const config = await this.loadConfig(plugin);
981
+ // Preset the plugin properties here in case the plugin throws an error during loading. In this case the user can change the config and restart the plugin.
738
982
  plugin.name = packageJson.name;
739
983
  plugin.description = packageJson.description ?? 'No description';
740
984
  plugin.version = packageJson.version;
@@ -743,7 +987,7 @@ export class PluginManager extends EventEmitter {
743
987
  plugin.schemaJson = await this.loadSchema(plugin);
744
988
  config.name = plugin.name;
745
989
  config.version = packageJson.version;
746
- const log = new AnsiLogger({ logName: plugin.description, logTimestampFormat: 4, logLevel: config.debug ? "debug" : this.matterbridge.log.logLevel });
990
+ const log = new AnsiLogger({ logName: plugin.description, logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: config.debug ? "debug" /* LogLevel.DEBUG */ : this.matterbridge.log.logLevel });
747
991
  const platform = pluginInstance.default(this.matterbridge, log, config);
748
992
  config.type = platform.type;
749
993
  platform.name = packageJson.name;
@@ -761,7 +1005,7 @@ export class PluginManager extends EventEmitter {
761
1005
  plugin.platform = platform;
762
1006
  plugin.loaded = true;
763
1007
  plugin.registeredDevices = 0;
764
- await this.saveToStorage();
1008
+ await this.saveToStorage(); // Save the plugin to storage
765
1009
  this.log.notice(`Loaded plugin ${plg}${plugin.name}${nt} type ${typ}${platform.type}${nt} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
766
1010
  this.emit('loaded', plugin.name);
767
1011
  if (start)
@@ -781,6 +1025,14 @@ export class PluginManager extends EventEmitter {
781
1025
  }
782
1026
  return undefined;
783
1027
  }
1028
+ /**
1029
+ * Starts a plugin.
1030
+ *
1031
+ * @param {Plugin | PluginName} plugin - The plugin to start.
1032
+ * @param {string} [message] - Optional message to pass to the plugin's onStart method.
1033
+ * @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
1034
+ * @returns {Promise<Plugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
1035
+ */
784
1036
  async start(plugin, message, configure = false) {
785
1037
  if (typeof plugin === 'string') {
786
1038
  const p = this._plugins.get(plugin);
@@ -819,6 +1071,12 @@ export class PluginManager extends EventEmitter {
819
1071
  }
820
1072
  return undefined;
821
1073
  }
1074
+ /**
1075
+ * Configures a plugin.
1076
+ *
1077
+ * @param {Plugin | PluginName} plugin - The plugin to configure.
1078
+ * @returns {Promise<Plugin | undefined>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
1079
+ */
822
1080
  async configure(plugin) {
823
1081
  if (typeof plugin === 'string') {
824
1082
  const p = this._plugins.get(plugin);
@@ -858,6 +1116,18 @@ export class PluginManager extends EventEmitter {
858
1116
  }
859
1117
  return undefined;
860
1118
  }
1119
+ /**
1120
+ * Shuts down a plugin.
1121
+ *
1122
+ * This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
1123
+ * It logs the shutdown process and optionally removes all devices associated with the plugin.
1124
+ *
1125
+ * @param {Plugin | PluginName} plugin - The plugin to shut down.
1126
+ * @param {string} [reason] - The reason for shutting down the plugin.
1127
+ * @param {boolean} [removeAllDevices] - Whether to remove all devices associated with the plugin.
1128
+ * @param {boolean} [force] - Whether to force the shutdown even if the plugin is not loaded or started.
1129
+ * @returns {Promise<Plugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
1130
+ */
861
1131
  async shutdown(plugin, reason, removeAllDevices = false, force = false) {
862
1132
  if (typeof plugin === 'string') {
863
1133
  const p = this._plugins.get(plugin);
@@ -908,6 +1178,15 @@ export class PluginManager extends EventEmitter {
908
1178
  }
909
1179
  return undefined;
910
1180
  }
1181
+ /**
1182
+ * Loads the configuration for a plugin.
1183
+ * If the configuration file exists, it reads the file and returns the parsed JSON data.
1184
+ * If the configuration file does not exist, it creates a new file with default configuration and returns it.
1185
+ * If any error occurs during file access or creation, it logs an error and return un empty config.
1186
+ *
1187
+ * @param {Plugin} plugin - The plugin for which to load the configuration.
1188
+ * @returns {Promise<PlatformConfig>} A promise that resolves to the loaded or created configuration.
1189
+ */
911
1190
  async loadConfig(plugin) {
912
1191
  const { default: path } = await import('node:path');
913
1192
  const { promises } = await import('node:fs');
@@ -919,6 +1198,8 @@ export class PluginManager extends EventEmitter {
919
1198
  const data = await promises.readFile(configFile, 'utf8');
920
1199
  const config = JSON.parse(data);
921
1200
  this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
1201
+ // this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
1202
+ // The first time a plugin is added to the system, the config file is created with the plugin name and type "AnyPlatform".
922
1203
  config.name = plugin.name;
923
1204
  config.type = plugin.type;
924
1205
  if (config.debug === undefined)
@@ -940,6 +1221,8 @@ export class PluginManager extends EventEmitter {
940
1221
  }
941
1222
  catch (_err) {
942
1223
  this.log.debug(`Default config file ${defaultConfigFile} for plugin ${plg}${plugin.name}${db} does not exist, creating new config file...`);
1224
+ // TODO: Remove this when all these plugins have their own default config file
1225
+ // istanbul ignore next if
943
1226
  if (plugin.name === 'matterbridge-zigbee2mqtt')
944
1227
  config = zigbee2mqtt_config;
945
1228
  else if (plugin.name === 'matterbridge-somfy-tahoma')
@@ -952,6 +1235,7 @@ export class PluginManager extends EventEmitter {
952
1235
  try {
953
1236
  await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
954
1237
  this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
1238
+ // this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
955
1239
  return config;
956
1240
  }
957
1241
  catch (err) {
@@ -965,6 +1249,19 @@ export class PluginManager extends EventEmitter {
965
1249
  }
966
1250
  }
967
1251
  }
1252
+ /**
1253
+ * Saves the configuration of a plugin to a file.
1254
+ *
1255
+ * This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
1256
+ * If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
1257
+ * is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
1258
+ * the error and rejects the promise.
1259
+ *
1260
+ * @param {Plugin} plugin - The plugin whose configuration is to be saved.
1261
+ * @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
1262
+ * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
1263
+ * @throws {Error} If the plugin's configuration is not found.
1264
+ */
968
1265
  async saveConfigFromPlugin(plugin, restartRequired = false) {
969
1266
  const { default: path } = await import('node:path');
970
1267
  const { promises } = await import('node:fs');
@@ -979,6 +1276,7 @@ export class PluginManager extends EventEmitter {
979
1276
  if (restartRequired)
980
1277
  plugin.restartRequired = true;
981
1278
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
1279
+ // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, plugin.platform.config);
982
1280
  return Promise.resolve();
983
1281
  }
984
1282
  catch (err) {
@@ -986,6 +1284,20 @@ export class PluginManager extends EventEmitter {
986
1284
  return Promise.reject(err);
987
1285
  }
988
1286
  }
1287
+ /**
1288
+ * Saves the configuration of a plugin from a JSON object to a file.
1289
+ *
1290
+ * This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
1291
+ * It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
1292
+ * the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
1293
+ * successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
1294
+ * during the file write operation, it logs the error and returns.
1295
+ *
1296
+ * @param {Plugin} plugin - The plugin whose configuration is to be saved.
1297
+ * @param {PlatformConfig} config - The configuration data to be saved.
1298
+ * @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
1299
+ * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
1300
+ */
989
1301
  async saveConfigFromJson(plugin, config, restartRequired = false) {
990
1302
  const { default: path } = await import('node:path');
991
1303
  const { promises } = await import('node:fs');
@@ -1004,12 +1316,23 @@ export class PluginManager extends EventEmitter {
1004
1316
  plugin.platform.onConfigChanged(config).catch((err) => this.log.error(`Error calling onConfigChanged for plugin ${plg}${plugin.name}${er}: ${err}`));
1005
1317
  }
1006
1318
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
1319
+ // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
1007
1320
  }
1008
1321
  catch (err) {
1009
1322
  logError(this.log, `Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
1010
1323
  return;
1011
1324
  }
1012
1325
  }
1326
+ /**
1327
+ * Loads the schema for a plugin.
1328
+ *
1329
+ * This method attempts to load the schema file for the specified plugin. If the schema file is found,
1330
+ * it reads and parses the file, updates the schema's title and description, and logs the process.
1331
+ * If the schema file is not found, it logs the event and loads a default schema for the plugin.
1332
+ *
1333
+ * @param {Plugin} plugin - The plugin whose schema is to be loaded.
1334
+ * @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
1335
+ */
1013
1336
  async loadSchema(plugin) {
1014
1337
  const { promises } = await import('node:fs');
1015
1338
  const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
@@ -1020,6 +1343,7 @@ export class PluginManager extends EventEmitter {
1020
1343
  schema.title = plugin.description;
1021
1344
  schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
1022
1345
  this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.`);
1346
+ // this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.\nSchema:${rs}\n`, schema);
1023
1347
  return schema;
1024
1348
  }
1025
1349
  catch (_err) {
@@ -1027,6 +1351,16 @@ export class PluginManager extends EventEmitter {
1027
1351
  return this.getDefaultSchema(plugin);
1028
1352
  }
1029
1353
  }
1354
+ /**
1355
+ * Returns the default schema for a plugin.
1356
+ *
1357
+ * This method generates a default schema object for the specified plugin. The schema includes
1358
+ * metadata such as the plugin's title, description, version, and author. It also defines the
1359
+ * properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
1360
+ *
1361
+ * @param {Plugin} plugin - The plugin for which the default schema is to be generated.
1362
+ * @returns {PlatformSchema} The default schema object for the plugin.
1363
+ */
1030
1364
  getDefaultSchema(plugin) {
1031
1365
  return {
1032
1366
  title: plugin.description,
@@ -1057,3 +1391,4 @@ export class PluginManager extends EventEmitter {
1057
1391
  };
1058
1392
  }
1059
1393
  }
1394
+ //# sourceMappingURL=pluginManager.js.map