homebridge-openwrt-control 0.3.4 → 0.3.6-beta.0

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.
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { join } from 'path';
2
- import { mkdirSync, existsSync, writeFileSync } from 'fs';
2
+ import { mkdirSync } from 'fs';
3
3
  import OpenWrt from './src/openwrt.js';
4
4
  import Router from './src/router.js';
5
5
  import ImpulseGenerator from './src/impulsegenerator.js';
@@ -7,14 +7,13 @@ import { PluginName, PlatformName } from './src/constants.js';
7
7
 
8
8
  class OpenWrtPlatform {
9
9
  constructor(log, config, api) {
10
- // only load if configured
11
10
  if (!config || !Array.isArray(config.devices)) {
12
11
  log.warn(`No configuration found for ${PluginName}`);
13
12
  return;
14
13
  }
14
+
15
15
  this.accessories = [];
16
16
 
17
- //check if prefs directory exist
18
17
  const prefDir = join(api.user.storagePath(), 'openWrt');
19
18
  try {
20
19
  mkdirSync(prefDir, { recursive: true });
@@ -23,100 +22,133 @@ class OpenWrtPlatform {
23
22
  return;
24
23
  }
25
24
 
26
- api.on('didFinishLaunching', async () => {
27
- for (const device of config.devices) {
28
- const { name, host, displayType } = device;
29
- if (!name || !host || !displayType) {
30
- log.warn(`Device: ${host || 'host missing'}, ${name || 'name missing'}, ${!displayType ? ', display type disabled' : ''} in config, will not be published in the Home app`);
31
- continue;
32
- }
33
- const refreshInterval = (device.refreshInterval ?? 5) * 1000;
34
-
35
- //log config
36
- const logLevel = {
37
- devInfo: device.log?.deviceInfo,
38
- success: device.log?.success,
39
- info: device.log?.info,
40
- warn: device.log?.warn,
41
- error: device.log?.error,
42
- debug: device.log?.debug
43
- };
44
-
45
- if (logLevel.debug) {
46
- log.info(`Device: ${host} ${name}, did finish launching.`);
47
- const safeConfig = {
48
- ...device,
49
- auth: {
50
- ...device.auth,
51
- passwd: 'removed',
52
- },
53
- mqtt: {
54
- auth: {
55
- ...device.mqtt?.auth,
56
- passwd: 'removed',
57
- }
58
- },
59
- };
60
- log.info(`Device: ${host} ${name}, Config: ${JSON.stringify(safeConfig, null, 2)}`);
61
- }
25
+ api.on('didFinishLaunching', () => {
26
+ // Each device is set up independently — a failure in one does not
27
+ // block the others. Promise.allSettled runs all in parallel.
28
+ Promise.allSettled(
29
+ config.devices.map(device =>
30
+ this.setupDevice(device, prefDir, log, api)
31
+ )
32
+ ).then(results => {
33
+ results.forEach((result, i) => {
34
+ if (result.status === 'rejected') {
35
+ log.error(`Device[${i}] setup error: ${result.reason?.message ?? result.reason}`);
36
+ }
37
+ });
38
+ });
39
+ });
40
+ }
41
+
42
+ // ── Per-device setup ──────────────────────────────────────────────────────
62
43
 
44
+ async setupDevice(device, prefDir, log, api) {
45
+ const { name, host, displayType } = device;
46
+
47
+ if (!name || !host || !displayType) {
48
+ const reason = !name ? 'name missing'
49
+ : !host ? 'host missing'
50
+ : 'display type disabled';
51
+ log.warn(`Device ${host ?? '(no host)'} ${name ?? '(unnamed)'}: ${reason} — will not be published in the Home app`);
52
+ return;
53
+ }
54
+
55
+ const refreshInterval = (device.refreshInterval ?? 5) * 1000;
56
+
57
+ const logLevel = {
58
+ devInfo: device.log?.deviceInfo,
59
+ success: device.log?.success,
60
+ info: device.log?.info,
61
+ warn: device.log?.warn,
62
+ error: device.log?.error,
63
+ debug: device.log?.debug,
64
+ };
65
+
66
+ if (logLevel.debug) {
67
+ log.info(`Device: ${host} ${name}, did finish launching`);
68
+ const safeConfig = {
69
+ ...device,
70
+ auth: {
71
+ ...device.auth,
72
+ passwd: 'removed',
73
+ },
74
+ mqtt: {
75
+ auth: {
76
+ ...device.mqtt?.auth,
77
+ passwd: 'removed',
78
+ },
79
+ },
80
+ };
81
+ log.info(`Device: ${host} ${name}, config: ${JSON.stringify(safeConfig, null, 2)}`);
82
+ }
83
+
84
+ // The startup impulse generator retries the full connect cycle
85
+ // every 120 s until it succeeds, then hands off to the openWrt class
86
+ // impulse generator and stops itself.
87
+ const impulseGenerator = new ImpulseGenerator()
88
+ .on('start', async () => {
63
89
  try {
64
- // create impulse generator for every device
65
- const impulseGenerator = new ImpulseGenerator()
66
- .on('start', async () => {
67
- try {
68
- const openWrt = new OpenWrt(device)
69
- .on('success', msg => logLevel.success && log.success(`Device: ${host}, ${msg}`))
70
- .on('info', msg => log.info(`Device: ${host} ${name}, ${msg}`))
71
- .on('debug', msg => log.info(`Device: ${host} ${name}, debug: ${msg}`))
72
- .on('warn', msg => log.warn(`Device: ${host} ${name}, ${msg}`))
73
- .on('error', msg => log.error(`Device: ${host} ${name}, ${msg}`));
74
-
75
- const openWrtInfo = await openWrt.connect();
76
- if (!openWrtInfo.state) {
77
- if (logLevel.warn) log.warn(`Device: ${host} ${name}, ${openWrtInfo.info}`);
78
- return;
79
- }
80
- if (logLevel.success) log.success(`Device: ${host} ${name}, ${openWrtInfo.info}`);
81
-
82
- // start openwrt impulse generator
83
- await openWrt.impulseGenerator.state(true, [{ name: 'connect', sampling: refreshInterval }], false);
84
-
85
- const router = new Router(api, device, openWrt, openWrtInfo)
86
- .on('devInfo', msg => logLevel.devInfo && log.info(msg))
87
- .on('success', msg => logLevel.success && log.success(`Device: ${host} ${name}, ${msg}`))
88
- .on('info', msg => log.info(`Device: ${host} ${name}, ${msg}`))
89
- .on('debug', msg => log.info(`Device: ${host} ${name}, debug: ${msg}`))
90
- .on('warn', msg => log.warn(`Device: ${host} ${name}, ${msg}`))
91
- .on('error', msg => log.error(`Device: ${host} ${name}, ${msg}`));
92
-
93
- const accessory = await router.start();
94
- if (accessory) {
95
- api.publishExternalAccessories(PluginName, [accessory]);
96
- if (logLevel.success) log.success(`Device: ${host} ${name}, Published as external accessory.`);
97
- }
98
-
99
- // stop accessory impulse generator
100
- await impulseGenerator.state(false);
101
- } catch (error) {
102
- if (logLevel.error) log.error(`Device: ${host} ${name}, Start impulse generator error: ${error.message ?? error}, trying again.`);
103
- }
104
- })
105
- .on('state', (state) => {
106
- if (logLevel.debug) log.info(`Device: ${host} ${name}, Start impulse generator ${state ? 'started' : 'stopped'}.`);
107
- });
108
-
109
- // start accessory impulse generator
110
- await impulseGenerator.state(true, [{ name: 'start', sampling: 120000 }]);
90
+ await this.startDevice(
91
+ device, name, host,
92
+ refreshInterval, logLevel,
93
+ log, api, impulseGenerator
94
+ );
111
95
  } catch (error) {
112
- if (logLevel.error) log.error(`Device: ${host} ${name}, Did finish launching error: ${error.message ?? error}`);
96
+ if (logLevel.error) log.error(`Device: ${host} ${name}, Start impulse generator error: ${error.message ?? error}, trying again.`);
113
97
  }
98
+ })
99
+ .on('state', (state) => {
100
+ if (logLevel.debug) log.info(`Device: ${host} ${name}, Start impulse generator ${state ? 'started' : 'stopped'}.`);
101
+ });
114
102
 
115
- await new Promise(r => setTimeout(r, 500));
116
- }
117
- });
103
+ await impulseGenerator.state(true, [{ name: 'start', sampling: 120_000 }]);
118
104
  }
119
105
 
106
+ // ── Connect and register accessory for one device ─────────────────────────
107
+
108
+ async startDevice(device, name, host, refreshInterval, logLevel, log, api, impulseGenerator) {
109
+ const openWrt = new OpenWrt(device)
110
+ .on('success', (msg) => logLevel.success && log.success(`Device: ${host}, ${msg}`))
111
+ .on('info', (msg) => log.info(`Device: ${host} ${name}, ${msg}`))
112
+ .on('debug', (msg) => log.info(`Device: ${host} ${name}, debug: ${msg}`))
113
+ .on('warn', (msg) => log.warn(`Device: ${host} ${name}, ${msg}`))
114
+ .on('error', (msg) => log.error(`Device: ${host} ${name}, ${msg}`));
115
+
116
+ // Connect
117
+ const openWrtInfo = await openWrt.connect();
118
+ if (!openWrtInfo.state) {
119
+ if (logLevel.warn) log.warn(`Device: ${host} ${name}, ${openWrtInfo.info}`);
120
+ return;
121
+ }
122
+ if (logLevel.success) log.success(`Device: ${host} ${name}, ${openWrtInfo.info}`);
123
+
124
+ // Register accessory
125
+ await this.registerDevice({ device, name, host, openWrt, openWrtInfo, refreshInterval, logLevel, log, api, impulseGenerator });
126
+ }
127
+
128
+ // ── Register a single device as a Homebridge accessory ───────────────────
129
+
130
+ async registerDevice({ device, name, host, openWrt, openWrtInfo, refreshInterval, logLevel, log, api, impulseGenerator }) {
131
+ const router = new Router(api, device, openWrt, openWrtInfo)
132
+ .on('devInfo', (msg) => logLevel.devInfo && log.info(msg))
133
+ .on('success', (msg) => logLevel.success && log.success(`Device: ${host} ${name}, ${msg}`))
134
+ .on('info', (msg) => log.info(`Device: ${host} ${name}, ${msg}`))
135
+ .on('debug', (msg) => log.info(`Device: ${host} ${name}, debug: ${msg}`))
136
+ .on('warn', (msg) => log.warn(`Device: ${host} ${name}, ${msg}`))
137
+ .on('error', (msg) => log.error(`Device: ${host} ${name}, ${msg}`));
138
+
139
+ const accessory = await router.start();
140
+ if (accessory) {
141
+ api.publishExternalAccessories(PluginName, [accessory]);
142
+ if (logLevel.success) log.success(`Device: ${host} ${name}, Published as external accessory.`);
143
+ }
144
+
145
+ // Stop startup generator and hand off to the openWrt class generator
146
+ await impulseGenerator.state(false);
147
+ await openWrt.impulseGenerator.state(true, [{ name: 'connect', sampling: refreshInterval }], false);
148
+ }
149
+
150
+ // ── Homebridge accessory cache ────────────────────────────────────────────
151
+
120
152
  configureAccessory(accessory) {
121
153
  this.accessories.push(accessory);
122
154
  }
@@ -124,4 +156,4 @@ class OpenWrtPlatform {
124
156
 
125
157
  export default (api) => {
126
158
  api.registerPlatform(PluginName, PlatformName, OpenWrtPlatform);
127
- }
159
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "OpenWrt Control",
3
3
  "name": "homebridge-openwrt-control",
4
- "version": "0.3.4",
4
+ "version": "0.3.6-beta.0",
5
5
  "description": "Homebridge plugin to control OpenWrt flashed devices.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -30,12 +30,12 @@
30
30
  "LICENSE"
31
31
  ],
32
32
  "engines": {
33
- "homebridge": "^1.8.0 || ^2.0.0 || ^2.0.0-beta.80 || ^2.0.0-alpha.63",
34
- "node": "^20 || ^22 || ^24 || ^25"
33
+ "homebridge": "^1.8.0 || ^2.0.0",
34
+ "node": "^20 || ^22 || ^24 || ^25 || ^26"
35
35
  },
36
36
  "dependencies": {
37
37
  "mqtt": "^5.15.1",
38
- "axios": "^1.15.0",
38
+ "axios": "^1.16.0",
39
39
  "express": "^5.2.1"
40
40
  },
41
41
  "keywords": [
package/src/openwrt.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import EventEmitter from 'events';
2
2
  import axios from 'axios';
3
- import Functions from './functions.js';
4
3
  import ImpulseGenerator from './impulsegenerator.js';
5
- import { AclPath, AclData } from './constants.js';
6
4
 
7
5
  class OpenWrt extends EventEmitter {
8
6
  constructor(config) {
9
7
  super();
10
8
  this.user = config.auth?.user || 'root';
11
9
  this.passwd = config.auth?.passwd;
10
+ this.logWarn = config.log?.warn;
12
11
  this.logError = config.log?.error;
13
12
  this.logDebug = config.log?.debug;
14
13
 
@@ -16,8 +15,6 @@ class OpenWrt extends EventEmitter {
16
15
  this.sessionId = null;
17
16
  this.sessionExpiresAt = 0;
18
17
 
19
- this.functions = new Functions();
20
-
21
18
  const baseUrl = `http://${config.host}/ubus`;
22
19
  this.axiosInstance = axios.create({
23
20
  baseURL: baseUrl,
package/src/restful.js CHANGED
@@ -72,7 +72,7 @@ class RestFul extends EventEmitter {
72
72
  }
73
73
 
74
74
  update(path, data) {
75
- if (this.restFulData.hasOwnProperty(path)) {
75
+ if (Object.hasOwn(this.restFulData, path)) {
76
76
  this.restFulData[path] = data;
77
77
  } else {
78
78
  if (this.logWarn) this.emit('warn', `Unknown RESTFul update path: ${path}, data: ${JSON.stringify(data)}`);
package/src/router.js CHANGED
@@ -27,6 +27,7 @@ class Router extends EventEmitter {
27
27
  this.buttons = (config.buttons ?? []).filter(b => (b.displayType ?? 0) > 0);
28
28
  this.logDeviceInfo = config.log?.deviceInfo || false;
29
29
  this.logInfo = config.log?.info || false;
30
+ this.logWarn = config.log?.warn || false;
30
31
  this.logDebug = config.log?.debug || false;
31
32
 
32
33
  // external integrations
@@ -184,6 +185,7 @@ class Router extends EventEmitter {
184
185
  if (this.restFul.enable) {
185
186
  this.restFul1 = new RestFul({
186
187
  port: this.restFul.port || 3000,
188
+ logWarn: this.logWarn,
187
189
  logDebug: this.logDebug
188
190
  })
189
191
  .on('connected', msg => {
@@ -206,6 +208,7 @@ class Router extends EventEmitter {
206
208
  prefix: this.mqtt.prefix ? `${this.openWrtInfo.systemInfo.model}/${this.mqtt.prefix}/${this.name}` : `${this.openWrtInfo.systemInfo.model}/${this.name}`,
207
209
  user: this.mqtt.auth?.user,
208
210
  passwd: this.mqtt.auth?.passwd,
211
+ logWarn: this.logWarn,
209
212
  logDebug: this.logDebug
210
213
  })
211
214
  .on('connected', msg => {
@@ -228,11 +231,11 @@ class Router extends EventEmitter {
228
231
 
229
232
  switch (key) {
230
233
  case 'SystemReboot':
231
- return this.openWrt.send('externalIntegration', null, null, null, 0);
234
+ return this.openWrt.send('externalIntegration', null, null, null, null, 0);
232
235
  case 'NetworkReload':
233
- return this.openWrt.send('externalIntegration', null, null, null, 1);
236
+ return this.openWrt.send('externalIntegration', null, null, null, null, 1);
234
237
  case 'WirelessReload':
235
- return this.openWrt.send('externalIntegration', null, null, null, 2);
238
+ return this.openWrt.send('externalIntegration', null, null, null, null, 2);
236
239
  default:
237
240
  this.emit('warn', `${integration} unknown key ${key}`);
238
241
  return false;
@@ -245,7 +248,7 @@ class Router extends EventEmitter {
245
248
  const radioId = `radio:${name}:${band}`;
246
249
  const convertedBand = band === '2g' ? '2.4GHz' : band === '5g' ? '5GHz' : '';
247
250
 
248
- const getCurrent = () => this.openWrtInfo.wirelessSsids.find(r => r.name === name && r.band === band);
251
+ const getCurrent = () => this.openWrtInfo.wirelessRadios.find(r => r.name === name && r.band === band);
249
252
 
250
253
  // control
251
254
  if (this.wirelessRadioControl.displayType > 0) {
@@ -301,11 +304,11 @@ class Router extends EventEmitter {
301
304
  service.getCharacteristic(Characteristic.ConfiguredName)
302
305
  .onGet(async () => {
303
306
  const current = getCurrent();
304
- return current.name;
307
+ return current?.name ?? name;
305
308
  })
306
309
  .onSet(async (value) => {
307
310
  const current = getCurrent();
308
- if (current.name === value) return;
311
+ if (!current || current.name === value) return;
309
312
 
310
313
  await this.openWrt.send('ssid', radio, name, value, !current.disabled, null, false); //{type, radioName, ssidName, newSsidName, state, command, restart}
311
314
  });
@@ -394,7 +397,7 @@ class Router extends EventEmitter {
394
397
  if (!state) return;
395
398
 
396
399
  button.state = true;
397
- await this.openWrt.send('button', null, null, null, button.command, false); //{type, radioName, ssidName, newSsidName, state, command, restart}
400
+ await this.openWrt.send('button', null, null, null, null, button.command, false); //{type, radioName, ssidName, newSsidName, state, command, restart}
398
401
  setTimeout(() => {
399
402
  button.state = false;
400
403
  buttonService.updateCharacteristic(Characteristic.On, false);