homebridge-brother-printer 0.2.2 → 0.3.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1 - 2026-11-07
4
+
5
+ ### Fixed
6
+ - Inkjets: the battery is now named Ink instead of Toner, unless you renamed it in Home.
7
+ - Printers that report no levels no longer get a battery added and removed again on every
8
+ restart.
9
+ - Settings that aren't numbers (for example in a hand-edited `config.json`) fall back to
10
+ their defaults. Before, a non-numeric low toner threshold meant the low alert never fired.
11
+ - Numeric settings are kept within the ranges the settings screen allows.
12
+
13
+ ### Changed
14
+ - README: notes that renaming a printer creates a new accessory, and how long going
15
+ offline takes before the printer has answered.
16
+ - Tests for the battery, settings, No Response and the Problem sensor; tests and lint run
17
+ on GitHub Actions for every push and pull request.
18
+
19
+ ## 0.3.0 - 2026-11-07
20
+
21
+ ### Added
22
+ - Optional Toner Low contact sensor (`lowTonerSensor`, Ink Low on inkjets). It opens when
23
+ the toner or ink is low, by the same rule as the battery, so Home sends a notification
24
+ with its own name instead of "battery is low". Off by default.
25
+
3
26
  ## 0.2.2 - 2026-11-03
4
27
 
5
28
  ### Changed
package/README.md CHANGED
@@ -20,6 +20,8 @@ Each printer gets these sensors:
20
20
  - **Toner / Ink** (battery level on the Printer accessory): the toner or ink %, with Home's
21
21
  native low-battery alert at or below your threshold. On color printers it shows the
22
22
  lowest color, so the alert fires when any color runs low.
23
+ - **Toner Low** (optional contact sensor): Opens when the toner or ink is low, so Home
24
+ notifies it by name instead of "battery is low".
23
25
  - **Toner / ink tiles** (optional humidity sensors): the level as a number on its own tile,
24
26
  one per color on color printers.
25
27
 
@@ -74,13 +76,14 @@ Set it up in the plugin's settings screen, or in `config.json`:
74
76
 
75
77
  | Option | Default | Description |
76
78
  |---|---|---|
77
- | `name` | `Printer` | Accessory name. Must be unique per printer. |
79
+ | `name` | `Printer` | Accessory name. Must be unique per printer. Changing it later creates a new accessory: its room, automations and notification settings in Home start over. |
78
80
  | `host` | | Printer IP address or hostname. Printers without one are ignored. |
79
81
  | `community` | `public` | SNMP community string. |
80
82
  | `port` | `161` | SNMP port. |
81
83
  | `snmpVersion` | `auto` | `auto` tries v2c, then v1. Set `1` for older printers that only answer v1. |
82
84
  | `pollInterval` | `60` | Seconds between polls (10–3600). |
83
85
  | `lowTonerThreshold` | `10` | Toner or ink % at or below which Home shows it as low (any color, on color printers). |
86
+ | `lowTonerSensor` | `false` | Add a Toner Low contact sensor (Ink Low on inkjets) that opens when any supply is low, by the same rule as the battery. |
84
87
  | `faultSensor` | `true` | Add the Printer Problem contact sensor. |
85
88
  | `tonerTile` | `false` | Add humidity sensors showing the toner or ink %, one per color on color printers. They also count in Home's climate summary. |
86
89
  | `showDrum` | `false` | Add drum life as a second battery. See below. |
@@ -94,8 +97,10 @@ HomeKit has no printer accessory type, so the plugin uses the closest sensors an
94
97
  words them its own way:
95
98
 
96
99
  - The printing sensor reads **Detected** / **Not Detected**.
97
- - Low toner or ink is announced as **"battery is low"**. For a clearer message, add a Home
98
- automation on that trigger that sends your own notification.
100
+ - Low toner or ink is announced as **"battery is low"**. For a clearer message, turn on
101
+ `lowTonerSensor`: its contact sensor opens at the same point and Home notifies it by
102
+ name, as it does for any contact sensor that opens. Turn on its notifications in the
103
+ Home app.
99
104
  - Home shows only one battery per accessory. With `showDrum` on, it may show the drum
100
105
  instead of the toner, which is why the drum is off by default. It's still read, and the
101
106
  plugin logs a warning when it nears end of life. Apps like Eve show both.
@@ -121,6 +126,10 @@ defaults that's about 3.5 minutes; `pollInterval: 30` with `offlineAfter: 1` bri
121
126
  about 40 s. With `offlineAfter: 1`, a single lost packet can make it flash No Response for
122
127
  one poll.
123
128
 
129
+ If the printer hasn't answered since Homebridge started and `snmpVersion` is `auto`, each
130
+ poll tries v2c and then v1, so the timeout counts twice (about 20 s per poll). Once it has
131
+ answered, the version that worked is used.
132
+
124
133
  Brother lasers normally still answer SNMP in Sleep and Deep Sleep.
125
134
 
126
135
  ## Checking what your printer reports
@@ -80,6 +80,12 @@
80
80
  "maximum": 100,
81
81
  "description": "On color printers, the battery shows the lowest color and turns low when any color reaches this."
82
82
  },
83
+ "lowTonerSensor": {
84
+ "title": "Low toner sensor",
85
+ "type": "boolean",
86
+ "default": false,
87
+ "description": "Adds a contact sensor that opens when toner or ink is low, so Home notifies \"Printer Toner Low opened\" instead of \"battery is low\". Turn on notifications for it in the Home app."
88
+ },
83
89
  "showDrum": {
84
90
  "title": "Show drum life as a second battery",
85
91
  "type": "boolean",
@@ -142,6 +148,7 @@
142
148
  "expandable": true,
143
149
  "items": [
144
150
  "printers[].lowTonerThreshold",
151
+ "printers[].lowTonerSensor",
145
152
  "printers[].showDrum",
146
153
  "printers[].lowDrumThreshold",
147
154
  "printers[].tonerTile",
package/lib/accessory.js CHANGED
@@ -26,6 +26,11 @@ function pagesCharacteristic(hap) {
26
26
  }
27
27
 
28
28
  const clamp = (v) => Math.max(0, Math.min(100, Math.round(v)));
29
+ /** A whole number setting; anything that isn't a number gets the default */
30
+ const setting = (v, def, min, max) => {
31
+ const n = typeof v === 'string' && v.trim() === '' ? NaN : Number(v ?? def);
32
+ return Number.isFinite(n) ? Math.max(min, Math.min(max, Math.round(n))) : def;
33
+ };
29
34
  const cap = (s) => s.charAt(0).toUpperCase() + s.slice(1);
30
35
  const TILE_SUBTYPE = { black: 'tonerTile', cyan: 'tile-cyan', magenta: 'tile-magenta', yellow: 'tile-yellow' };
31
36
  const FAULT_NAMES = {
@@ -49,20 +54,21 @@ class PrinterAccessory {
49
54
 
50
55
  this.options = {
51
56
  host: config.host,
52
- port: Number(config.port) || 161,
57
+ port: setting(config.port, 161, 1, 65535),
53
58
  community: config.community || 'public',
54
59
  timeout: 5000,
55
60
  retries: 1,
56
61
  version: ['1', '2c'].includes(String(config.snmpVersion)) ? String(config.snmpVersion) : 'auto',
57
62
  state: {},
58
63
  };
59
- this.interval = Math.max(10, Number(config.pollInterval) || 60) * 1000;
60
- this.lowToner = clamp(config.lowTonerThreshold ?? 10);
61
- this.lowDrum = clamp(config.lowDrumThreshold ?? 10);
64
+ this.interval = setting(config.pollInterval, 60, 10, 3600) * 1000;
65
+ this.lowToner = setting(config.lowTonerThreshold, 10, 0, 100);
66
+ this.lowDrum = setting(config.lowDrumThreshold, 10, 0, 100);
62
67
  this.showDrum = config.showDrum === true;
63
68
  this.tonerTile = config.tonerTile === true;
64
69
  this.faultSensor = config.faultSensor !== false;
65
- this.offlineAfter = Math.max(1, Number(config.offlineAfter) || 3);
70
+ this.lowTonerSensor = config.lowTonerSensor === true;
71
+ this.offlineAfter = setting(config.offlineAfter, 3, 1, 20);
66
72
  this.debug = config.debug === true;
67
73
 
68
74
  this.failures = 0;
@@ -104,9 +110,15 @@ class PrinterAccessory {
104
110
  if (!this.occupancy.testCharacteristic(Pages)) { this.occupancy.addCharacteristic(Pages); }
105
111
  this.pages = this.occupancy.getCharacteristic(Pages);
106
112
 
107
- // Toner and drum as batteries so Home raises native "low" alerts
108
- this.toner = this.battery('toner', `${this.name} Toner`);
109
- this.occupancy.addLinkedService(this.toner);
113
+ // Toner and drum as batteries so Home raises native "low" alerts. A printer
114
+ // that reported no levels last time gets none until it reports some.
115
+ if (acc.context.noLevels) {
116
+ this.removeSubtype(Service.Battery, 'toner');
117
+ this.toner = null;
118
+ } else {
119
+ this.toner = this.battery('toner', `${this.name} ${acc.context.kind || 'Toner'}`);
120
+ this.occupancy.addLinkedService(this.toner);
121
+ }
110
122
 
111
123
  if (this.showDrum) {
112
124
  this.drum = this.battery('drum', `${this.name} Drum`);
@@ -128,6 +140,11 @@ class PrinterAccessory {
128
140
  this.removeSubtype(Service.ContactSensor, 'problem');
129
141
  }
130
142
 
143
+ // The low toner sensor is created once the printer says toner or ink;
144
+ // here only one restored from the cache is picked up.
145
+ const lowSvc = acc.getServiceById(Service.ContactSensor, 'lowToner');
146
+ if (lowSvc && this.lowTonerSensor) { this.lowSensor = lowSvc; } else if (lowSvc) { acc.removeService(lowSvc); }
147
+
131
148
  // Supply tiles are created once the printer says which colors it has;
132
149
  // here only the ones restored from the cache are picked up.
133
150
  for (const [color, subtype] of Object.entries(TILE_SUBTYPE)) {
@@ -164,6 +181,31 @@ class PrinterAccessory {
164
181
  }
165
182
  }
166
183
 
184
+ /**
185
+ * Low toner as a contact sensor that opens when any supply is low. Home words
186
+ * a low battery as "battery is low"; this gives a notification with its own
187
+ * name, e.g. "Printer Toner Low opened".
188
+ */
189
+ ensureLowSensor(kind) {
190
+ if (!this.lowTonerSensor || this.lowSensor) { return; }
191
+ const { Service, Characteristic: C } = this.hap;
192
+ const name = `${this.name} ${kind} Low`;
193
+ const svc = this.accessory.addService(Service.ContactSensor, name, 'lowToner');
194
+ this.label(svc, name);
195
+ if (!svc.testCharacteristic(C.StatusActive)) { svc.addOptionalCharacteristic(C.StatusActive); }
196
+ svc.getCharacteristic(C.StatusActive).updateValue(true);
197
+ this.occupancy.addLinkedService(svc);
198
+ this.guard(svc.getCharacteristic(C.ContactSensorState));
199
+ this.lowSensor = svc;
200
+ }
201
+
202
+ removeLowSensor() {
203
+ if (!this.lowSensor) { return; }
204
+ this.guarded = this.guarded.filter((ch) => !this.lowSensor.characteristics.includes(ch));
205
+ this.accessory.removeService(this.lowSensor);
206
+ this.lowSensor = null;
207
+ }
208
+
167
209
  /**
168
210
  * Home ignores StatusActive on sensors. It shows "No Response" only when a
169
211
  * read fails, so while the printer is offline the state reads fail too.
@@ -178,6 +220,7 @@ class PrinterAccessory {
178
220
  [this.drum, C.BatteryLevel],
179
221
  [this.drum, C.StatusLowBattery],
180
222
  [this.problem, C.ContactSensorState],
223
+ [this.lowSensor, C.ContactSensorState],
181
224
  ...Object.values(this.tiles).map((svc) => [svc, C.CurrentRelativeHumidity]),
182
225
  ];
183
226
  for (const [svc, type] of pairs) {
@@ -266,6 +309,7 @@ class PrinterAccessory {
266
309
  this.occupancy.updateCharacteristic(C.StatusActive, online);
267
310
  for (const svc of Object.values(this.tiles)) { svc.updateCharacteristic(C.StatusActive, online); }
268
311
  if (this.problem) { this.problem.updateCharacteristic(C.StatusActive, online); }
312
+ if (this.lowSensor) { this.lowSensor.updateCharacteristic(C.StatusActive, online); }
269
313
  if (!online) {
270
314
  this.occupancy.updateCharacteristic(C.OccupancyDetected, 0);
271
315
  // Push the failure so Home shows "No Response" without waiting for a read
@@ -336,6 +380,7 @@ class PrinterAccessory {
336
380
 
337
381
  if (!d.supplies.length) {
338
382
  // Some models (e.g. ink tank printers) report no levels at all
383
+ this.remember({ noLevels: true });
339
384
  if (this.toner) {
340
385
  this.log.info(`[${this.name}] This printer doesn't report ${kind.toLowerCase()} levels; `
341
386
  + 'leaving out the battery.');
@@ -343,6 +388,7 @@ class PrinterAccessory {
343
388
  this.guarded = this.guarded.filter((ch) => !this.toner.characteristics.includes(ch));
344
389
  this.toner = null;
345
390
  }
391
+ this.removeLowSensor();
346
392
  return;
347
393
  }
348
394
  if (!this.toner) {
@@ -351,6 +397,8 @@ class PrinterAccessory {
351
397
  this.guard(this.toner.getCharacteristic(C.BatteryLevel));
352
398
  this.guard(this.toner.getCharacteristic(C.StatusLowBattery));
353
399
  }
400
+ this.renameBattery(kind);
401
+ this.remember({ noLevels: false, kind });
354
402
 
355
403
  const flagged = d.errors.includes('lowToner') || d.errors.includes('noToner');
356
404
  const isLow = (s) => s.level <= this.lowToner || (s.fine !== null && s.fine <= this.lowToner);
@@ -363,6 +411,13 @@ class PrinterAccessory {
363
411
  ? C.StatusLowBattery.BATTERY_LEVEL_LOW
364
412
  : C.StatusLowBattery.BATTERY_LEVEL_NORMAL);
365
413
 
414
+ this.ensureLowSensor(kind);
415
+ if (this.lowSensor) {
416
+ this.lowSensor.updateCharacteristic(C.ContactSensorState, low.length || flagged
417
+ ? C.ContactSensorState.CONTACT_NOT_DETECTED
418
+ : C.ContactSensorState.CONTACT_DETECTED);
419
+ }
420
+
366
421
  this.ensureTiles(d);
367
422
  for (const s of d.supplies) {
368
423
  if (this.tiles[s.color]) { this.tiles[s.color].updateCharacteristic(C.CurrentRelativeHumidity, clamp(s.level)); }
@@ -407,6 +462,29 @@ class PrinterAccessory {
407
462
  this.last.warned[key] = condition;
408
463
  }
409
464
 
465
+ /**
466
+ * The battery is made before the first poll says toner or ink, so on an
467
+ * inkjet it starts as "Toner". Rename it, unless the user renamed it in Home.
468
+ */
469
+ renameBattery(kind) {
470
+ const C = this.hap.Characteristic;
471
+ const was = this.accessory.context.kind || 'Toner';
472
+ if (was === kind) { return; }
473
+ const name = `${this.name} ${kind}`;
474
+ this.toner.setCharacteristic(C.Name, name);
475
+ const current = this.toner.getCharacteristic(C.ConfiguredName).value;
476
+ if (!current || current === `${this.name} ${was}`) { this.toner.setCharacteristic(C.ConfiguredName, name); }
477
+ }
478
+
479
+ /** Store values in the accessory cache, saving only when something changed */
480
+ remember(values) {
481
+ const ctx = this.accessory.context;
482
+ const changed = Object.entries(values).filter(([k, v]) => ctx[k] !== v);
483
+ if (!changed.length) { return; }
484
+ for (const [k, v] of changed) { ctx[k] = v; }
485
+ this.api.updatePlatformAccessories([this.accessory]);
486
+ }
487
+
410
488
  setBattery(svc, level, threshold) {
411
489
  const C = this.hap.Characteristic;
412
490
  svc.updateCharacteristic(C.BatteryLevel, level);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-brother-printer",
3
3
  "displayName": "Brother Printer",
4
- "version": "0.2.2",
4
+ "version": "0.3.1",
5
5
  "description": "Brother laser and inkjet printers in HomeKit: printing state, problem alerts and toner or ink levels, read locally over SNMP.",
6
6
  "author": "Manuel Zamfir",
7
7
  "license": "MIT",