homebridge-brother-printer 0.3.0 → 0.4.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/CHANGELOG.md +26 -0
- package/README.md +22 -3
- package/dist/accessory.js +595 -0
- package/{bin → dist/bin}/brother-printer-probe.js +12 -14
- package/dist/decode.js +326 -0
- package/dist/index.js +6 -0
- package/dist/platform.js +89 -0
- package/dist/settings.js +5 -0
- package/dist/snmp.js +183 -0
- package/package.json +16 -9
- package/index.js +0 -8
- package/lib/accessory.js +0 -474
- package/lib/decode.js +0 -299
- package/lib/platform.js +0 -87
- package/lib/settings.js +0 -6
- package/lib/snmp.js +0 -152
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0 - 2026-11-08
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
- Rewritten in TypeScript. The published package is compiled JavaScript in `dist/`;
|
|
7
|
+
behavior, settings and supported Node and Homebridge versions are unchanged.
|
|
8
|
+
- Tests are TypeScript too and are type-checked with the plugin.
|
|
9
|
+
- npm builds the plugin on install, `npm pack` and `npm publish`, so a package made any
|
|
10
|
+
of those ways (including installing from GitHub) contains the compiled plugin.
|
|
11
|
+
- `npm publish` lints and tests first.
|
|
12
|
+
|
|
13
|
+
## 0.3.1 - 2026-11-07
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
- Inkjets: the battery is now named Ink instead of Toner, unless you renamed it in Home.
|
|
17
|
+
- Printers that report no levels no longer get a battery added and removed again on every
|
|
18
|
+
restart.
|
|
19
|
+
- Settings that aren't numbers (for example in a hand-edited `config.json`) fall back to
|
|
20
|
+
their defaults. Before, a non-numeric low toner threshold meant the low alert never fired.
|
|
21
|
+
- Numeric settings are kept within the ranges the settings screen allows.
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
- README: notes that renaming a printer creates a new accessory, and how long going
|
|
25
|
+
offline takes before the printer has answered.
|
|
26
|
+
- Tests for the battery, settings, No Response and the Problem sensor; tests and lint run
|
|
27
|
+
on GitHub Actions for every push and pull request.
|
|
28
|
+
|
|
3
29
|
## 0.3.0 - 2026-11-07
|
|
4
30
|
|
|
5
31
|
### Added
|
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ Each printer gets these sensors:
|
|
|
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
23
|
- **Toner Low** (optional contact sensor): Opens when the toner or ink is low, so Home
|
|
24
|
-
notifies
|
|
24
|
+
notifies it by name instead of "battery is low".
|
|
25
25
|
- **Toner / ink tiles** (optional humidity sensors): the level as a number on its own tile,
|
|
26
26
|
one per color on color printers.
|
|
27
27
|
|
|
@@ -76,7 +76,7 @@ Set it up in the plugin's settings screen, or in `config.json`:
|
|
|
76
76
|
|
|
77
77
|
| Option | Default | Description |
|
|
78
78
|
|---|---|---|
|
|
79
|
-
| `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. |
|
|
80
80
|
| `host` | | Printer IP address or hostname. Printers without one are ignored. |
|
|
81
81
|
| `community` | `public` | SNMP community string. |
|
|
82
82
|
| `port` | `161` | SNMP port. |
|
|
@@ -99,7 +99,8 @@ words them its own way:
|
|
|
99
99
|
- The printing sensor reads **Detected** / **Not Detected**.
|
|
100
100
|
- Low toner or ink is announced as **"battery is low"**. For a clearer message, turn on
|
|
101
101
|
`lowTonerSensor`: its contact sensor opens at the same point and Home notifies it by
|
|
102
|
-
name
|
|
102
|
+
name, as it does for any contact sensor that opens. Turn on its notifications in the
|
|
103
|
+
Home app.
|
|
103
104
|
- Home shows only one battery per accessory. With `showDrum` on, it may show the drum
|
|
104
105
|
instead of the toner, which is why the drum is off by default. It's still read, and the
|
|
105
106
|
plugin logs a warning when it nears end of life. Apps like Eve show both.
|
|
@@ -125,6 +126,10 @@ defaults that's about 3.5 minutes; `pollInterval: 30` with `offlineAfter: 1` bri
|
|
|
125
126
|
about 40 s. With `offlineAfter: 1`, a single lost packet can make it flash No Response for
|
|
126
127
|
one poll.
|
|
127
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
|
+
|
|
128
133
|
Brother lasers normally still answer SNMP in Sleep and Deep Sleep.
|
|
129
134
|
|
|
130
135
|
## Checking what your printer reports
|
|
@@ -173,6 +178,20 @@ Printing and problem state come from the standard Host Resources MIB (`hrPrinter
|
|
|
173
178
|
The record codes follow the [brother](https://github.com/bieniu/brother) Python library
|
|
174
179
|
by Maciej Bieniek (Apache-2.0), and the tests use SNMP captures from its test suite.
|
|
175
180
|
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
The plugin and its tests are written in TypeScript (`src/`, `test/`). It's published as
|
|
184
|
+
compiled JavaScript (`dist/`), which npm builds on `npm ci`, `npm pack` and `npm publish`.
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
npm ci # installs and builds
|
|
188
|
+
npm test # runs the tests against the TypeScript sources
|
|
189
|
+
npm run lint # ESLint and type checking
|
|
190
|
+
npm run build # build only
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`npm publish` lints and tests first.
|
|
194
|
+
|
|
176
195
|
## License
|
|
177
196
|
|
|
178
197
|
MIT
|
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrinterAccessory = void 0;
|
|
4
|
+
const snmp_1 = require("./snmp");
|
|
5
|
+
// Resolved from dist/ at runtime, like the published package
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
7
|
+
const { version } = require('../package.json');
|
|
8
|
+
const PAGES_UUID = 'B0E5A1C2-7D3E-4F1A-9C6B-2E8D4F7A1B30';
|
|
9
|
+
let PagesPrinted;
|
|
10
|
+
function pagesCharacteristic(hap) {
|
|
11
|
+
if (PagesPrinted) {
|
|
12
|
+
return PagesPrinted;
|
|
13
|
+
}
|
|
14
|
+
const { Characteristic: Base, Formats, Perms } = hap;
|
|
15
|
+
class Pages extends Base {
|
|
16
|
+
static UUID = PAGES_UUID;
|
|
17
|
+
constructor() {
|
|
18
|
+
super('Pages Printed', PAGES_UUID, {
|
|
19
|
+
format: "uint32" /* Formats.UINT32 */,
|
|
20
|
+
perms: ["pr" /* Perms.PAIRED_READ */, "ev" /* Perms.NOTIFY */],
|
|
21
|
+
minValue: 0,
|
|
22
|
+
maxValue: 4294967295,
|
|
23
|
+
minStep: 1,
|
|
24
|
+
});
|
|
25
|
+
this.value = this.getDefaultValue();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
PagesPrinted = Pages;
|
|
29
|
+
return Pages;
|
|
30
|
+
}
|
|
31
|
+
const clamp = (v) => Math.max(0, Math.min(100, Math.round(v)));
|
|
32
|
+
/** A whole number setting; anything that isn't a number gets the default */
|
|
33
|
+
const setting = (v, def, min, max) => {
|
|
34
|
+
const n = typeof v === 'string' && v.trim() === '' ? NaN : Number(v ?? def);
|
|
35
|
+
return Number.isFinite(n) ? Math.max(min, Math.min(max, Math.round(n))) : def;
|
|
36
|
+
};
|
|
37
|
+
const cap = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
38
|
+
const errorText = (err, stack = false) => {
|
|
39
|
+
const e = err;
|
|
40
|
+
return (stack && e?.stack) || e?.message || String(err);
|
|
41
|
+
};
|
|
42
|
+
const TILE_SUBTYPE = {
|
|
43
|
+
black: 'tonerTile', cyan: 'tile-cyan', magenta: 'tile-magenta', yellow: 'tile-yellow',
|
|
44
|
+
};
|
|
45
|
+
const FAULT_NAMES = {
|
|
46
|
+
noPaper: 'out of paper', noToner: 'toner or ink empty', doorOpen: 'cover open', jammed: 'paper jam',
|
|
47
|
+
serviceRequested: 'service requested', inputTrayMissing: 'tray missing', outputTrayMissing: 'output tray missing',
|
|
48
|
+
markerSupplyMissing: 'cartridge missing', outputFull: 'output tray full',
|
|
49
|
+
};
|
|
50
|
+
const PART_NAMES = {
|
|
51
|
+
belt: 'Belt unit', fuser: 'Fuser unit', laser: 'Laser unit',
|
|
52
|
+
paperFeed1: 'Paper feed kit (tray 1)', paperFeedMP: 'Paper feed kit (MP tray)', inkWasteBox: 'Ink waste box',
|
|
53
|
+
};
|
|
54
|
+
class PrinterAccessory {
|
|
55
|
+
platform;
|
|
56
|
+
log;
|
|
57
|
+
api;
|
|
58
|
+
hap;
|
|
59
|
+
accessory;
|
|
60
|
+
name;
|
|
61
|
+
options;
|
|
62
|
+
interval;
|
|
63
|
+
lowToner;
|
|
64
|
+
lowDrum;
|
|
65
|
+
showDrum;
|
|
66
|
+
tonerTile;
|
|
67
|
+
faultSensor;
|
|
68
|
+
lowTonerSensor;
|
|
69
|
+
offlineAfter;
|
|
70
|
+
debug;
|
|
71
|
+
failures = 0;
|
|
72
|
+
online = true;
|
|
73
|
+
last = {};
|
|
74
|
+
timer = null;
|
|
75
|
+
stopped = false;
|
|
76
|
+
tiles = {};
|
|
77
|
+
guarded = [];
|
|
78
|
+
occupancy;
|
|
79
|
+
pages;
|
|
80
|
+
toner = null;
|
|
81
|
+
drum = null;
|
|
82
|
+
problem = null;
|
|
83
|
+
lowSensor = null;
|
|
84
|
+
query;
|
|
85
|
+
constructor(platform, accessory, config, deps = {}) {
|
|
86
|
+
this.query = deps.query || snmp_1.query;
|
|
87
|
+
this.platform = platform;
|
|
88
|
+
this.log = platform.log;
|
|
89
|
+
this.api = platform.api;
|
|
90
|
+
this.hap = platform.api.hap;
|
|
91
|
+
this.accessory = accessory;
|
|
92
|
+
this.name = config.name;
|
|
93
|
+
const snmpVersion = String(config.snmpVersion);
|
|
94
|
+
this.options = {
|
|
95
|
+
host: config.host,
|
|
96
|
+
port: setting(config.port, 161, 1, 65535),
|
|
97
|
+
community: config.community || 'public',
|
|
98
|
+
timeout: 5000,
|
|
99
|
+
retries: 1,
|
|
100
|
+
version: snmpVersion === '1' || snmpVersion === '2c' ? snmpVersion : 'auto',
|
|
101
|
+
state: {},
|
|
102
|
+
};
|
|
103
|
+
this.interval = setting(config.pollInterval, 60, 10, 3600) * 1000;
|
|
104
|
+
this.lowToner = setting(config.lowTonerThreshold, 10, 0, 100);
|
|
105
|
+
this.lowDrum = setting(config.lowDrumThreshold, 10, 0, 100);
|
|
106
|
+
this.showDrum = config.showDrum === true;
|
|
107
|
+
this.tonerTile = config.tonerTile === true;
|
|
108
|
+
this.faultSensor = config.faultSensor !== false;
|
|
109
|
+
this.lowTonerSensor = config.lowTonerSensor === true;
|
|
110
|
+
this.offlineAfter = setting(config.offlineAfter, 3, 1, 20);
|
|
111
|
+
this.debug = config.debug === true;
|
|
112
|
+
this.buildServices();
|
|
113
|
+
this.guardReads();
|
|
114
|
+
void this.poll();
|
|
115
|
+
}
|
|
116
|
+
/* ------------------------------------------------------------ services */
|
|
117
|
+
buildServices() {
|
|
118
|
+
const { Service: S, Characteristic: C } = this.hap;
|
|
119
|
+
const acc = this.accessory;
|
|
120
|
+
acc.getService(S.AccessoryInformation)
|
|
121
|
+
.setCharacteristic(C.Manufacturer, 'Brother')
|
|
122
|
+
.setCharacteristic(C.Model, acc.context.model || 'Brother printer')
|
|
123
|
+
.setCharacteristic(C.SerialNumber, acc.context.serial || this.options.host)
|
|
124
|
+
.setCharacteristic(C.FirmwareRevision, version);
|
|
125
|
+
// Printing state: the tile people see in the Home app
|
|
126
|
+
this.occupancy = acc.getService(S.OccupancySensor)
|
|
127
|
+
|| acc.addService(S.OccupancySensor, this.name);
|
|
128
|
+
this.label(this.occupancy, this.name);
|
|
129
|
+
this.occupancy.setPrimaryService(true);
|
|
130
|
+
for (const ch of [C.StatusActive, C.StatusFault]) {
|
|
131
|
+
if (!this.occupancy.testCharacteristic(ch)) {
|
|
132
|
+
this.occupancy.addOptionalCharacteristic(ch);
|
|
133
|
+
}
|
|
134
|
+
this.occupancy.getCharacteristic(ch);
|
|
135
|
+
}
|
|
136
|
+
this.occupancy.getCharacteristic(C.StatusActive).updateValue(true);
|
|
137
|
+
const Pages = pagesCharacteristic(this.hap);
|
|
138
|
+
// testCharacteristic/addCharacteristic are typed for HAP's base class; a subclass works the same
|
|
139
|
+
const PagesClass = Pages;
|
|
140
|
+
if (!this.occupancy.testCharacteristic(PagesClass)) {
|
|
141
|
+
this.occupancy.addCharacteristic(PagesClass);
|
|
142
|
+
}
|
|
143
|
+
this.pages = this.occupancy.getCharacteristic(Pages);
|
|
144
|
+
// Toner and drum as batteries so Home raises native "low" alerts. A printer
|
|
145
|
+
// that reported no levels last time gets none until it reports some.
|
|
146
|
+
if (acc.context.noLevels) {
|
|
147
|
+
this.removeSubtype(S.Battery, 'toner');
|
|
148
|
+
this.toner = null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
this.toner = this.battery('toner', `${this.name} ${acc.context.kind || 'Toner'}`);
|
|
152
|
+
this.occupancy.addLinkedService(this.toner);
|
|
153
|
+
}
|
|
154
|
+
if (this.showDrum) {
|
|
155
|
+
this.drum = this.battery('drum', `${this.name} Drum`);
|
|
156
|
+
this.occupancy.addLinkedService(this.drum);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.removeSubtype(S.Battery, 'drum');
|
|
160
|
+
}
|
|
161
|
+
// Home doesn't surface StatusFault on occupancy sensors, so problems
|
|
162
|
+
// (jam, cover open, no paper...) also open a contact sensor.
|
|
163
|
+
if (this.faultSensor) {
|
|
164
|
+
const problem = acc.getServiceById(S.ContactSensor, 'problem')
|
|
165
|
+
|| acc.addService(S.ContactSensor, `${this.name} Problem`, 'problem');
|
|
166
|
+
this.label(problem, `${this.name} Problem`);
|
|
167
|
+
if (!problem.testCharacteristic(C.StatusActive)) {
|
|
168
|
+
problem.addOptionalCharacteristic(C.StatusActive);
|
|
169
|
+
}
|
|
170
|
+
problem.getCharacteristic(C.StatusActive).updateValue(true);
|
|
171
|
+
this.occupancy.addLinkedService(problem);
|
|
172
|
+
this.problem = problem;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.removeSubtype(S.ContactSensor, 'problem');
|
|
176
|
+
}
|
|
177
|
+
// The low toner sensor is created once the printer says toner or ink;
|
|
178
|
+
// here only one restored from the cache is picked up.
|
|
179
|
+
const lowSvc = acc.getServiceById(S.ContactSensor, 'lowToner');
|
|
180
|
+
if (lowSvc && this.lowTonerSensor) {
|
|
181
|
+
this.lowSensor = lowSvc;
|
|
182
|
+
}
|
|
183
|
+
else if (lowSvc) {
|
|
184
|
+
acc.removeService(lowSvc);
|
|
185
|
+
}
|
|
186
|
+
// Supply tiles are created once the printer says which colors it has;
|
|
187
|
+
// here only the ones restored from the cache are picked up.
|
|
188
|
+
for (const [color, subtype] of Object.entries(TILE_SUBTYPE)) {
|
|
189
|
+
const svc = acc.getServiceById(S.HumiditySensor, subtype);
|
|
190
|
+
if (!svc) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (this.tonerTile) {
|
|
194
|
+
this.tiles[color] = svc;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
acc.removeService(svc);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Make one tile per reported supply; remove tiles for colors not reported */
|
|
202
|
+
ensureTiles(d) {
|
|
203
|
+
if (!this.tonerTile) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const { Service: S, Characteristic: C } = this.hap;
|
|
207
|
+
const colors = new Set(d.supplies.map((s) => s.color));
|
|
208
|
+
const mono = d.supplies.length === 1;
|
|
209
|
+
const kind = d.type === 'ink' ? 'Ink' : 'Toner';
|
|
210
|
+
for (const [color, subtype] of Object.entries(TILE_SUBTYPE)) {
|
|
211
|
+
const existing = this.tiles[color];
|
|
212
|
+
if (!colors.has(color)) {
|
|
213
|
+
if (existing) {
|
|
214
|
+
this.accessory.removeService(existing);
|
|
215
|
+
delete this.tiles[color];
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (existing) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const name = mono ? `${this.name} ${kind}` : `${this.name} ${cap(color)}`;
|
|
223
|
+
const svc = this.accessory.addService(S.HumiditySensor, name, subtype);
|
|
224
|
+
this.label(svc, name);
|
|
225
|
+
if (!svc.testCharacteristic(C.StatusActive)) {
|
|
226
|
+
svc.addOptionalCharacteristic(C.StatusActive);
|
|
227
|
+
}
|
|
228
|
+
svc.getCharacteristic(C.StatusActive).updateValue(true);
|
|
229
|
+
this.guard(svc.getCharacteristic(C.CurrentRelativeHumidity));
|
|
230
|
+
this.tiles[color] = svc;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Low toner as a contact sensor that opens when any supply is low. Home words
|
|
235
|
+
* a low battery as "battery is low"; this gives a notification with its own
|
|
236
|
+
* name.
|
|
237
|
+
*/
|
|
238
|
+
ensureLowSensor(kind) {
|
|
239
|
+
if (!this.lowTonerSensor || this.lowSensor) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const { Service: S, Characteristic: C } = this.hap;
|
|
243
|
+
const name = `${this.name} ${kind} Low`;
|
|
244
|
+
const svc = this.accessory.addService(S.ContactSensor, name, 'lowToner');
|
|
245
|
+
this.label(svc, name);
|
|
246
|
+
if (!svc.testCharacteristic(C.StatusActive)) {
|
|
247
|
+
svc.addOptionalCharacteristic(C.StatusActive);
|
|
248
|
+
}
|
|
249
|
+
svc.getCharacteristic(C.StatusActive).updateValue(true);
|
|
250
|
+
this.occupancy.addLinkedService(svc);
|
|
251
|
+
this.guard(svc.getCharacteristic(C.ContactSensorState));
|
|
252
|
+
this.lowSensor = svc;
|
|
253
|
+
}
|
|
254
|
+
removeLowSensor() {
|
|
255
|
+
const svc = this.lowSensor;
|
|
256
|
+
if (!svc) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
this.guarded = this.guarded.filter((ch) => !svc.characteristics.includes(ch));
|
|
260
|
+
this.accessory.removeService(svc);
|
|
261
|
+
this.lowSensor = null;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Home ignores StatusActive on sensors. It shows "No Response" only when a
|
|
265
|
+
* read fails, so while the printer is offline the state reads fail too.
|
|
266
|
+
*/
|
|
267
|
+
guardReads() {
|
|
268
|
+
const C = this.hap.Characteristic;
|
|
269
|
+
const pairs = [
|
|
270
|
+
[this.occupancy, C.OccupancyDetected],
|
|
271
|
+
[this.occupancy, C.StatusFault],
|
|
272
|
+
[this.toner, C.BatteryLevel],
|
|
273
|
+
[this.toner, C.StatusLowBattery],
|
|
274
|
+
[this.drum, C.BatteryLevel],
|
|
275
|
+
[this.drum, C.StatusLowBattery],
|
|
276
|
+
[this.problem, C.ContactSensorState],
|
|
277
|
+
[this.lowSensor, C.ContactSensorState],
|
|
278
|
+
...Object.values(this.tiles).map((svc) => [svc, C.CurrentRelativeHumidity]),
|
|
279
|
+
];
|
|
280
|
+
for (const [svc, type] of pairs) {
|
|
281
|
+
if (svc) {
|
|
282
|
+
this.guard(svc.getCharacteristic(type));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
guard(ch) {
|
|
287
|
+
if (this.guarded.includes(ch)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
this.guarded.push(ch);
|
|
291
|
+
ch.onGet(() => {
|
|
292
|
+
if (!this.online) {
|
|
293
|
+
throw this.noResponse();
|
|
294
|
+
}
|
|
295
|
+
return ch.value;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
noResponse() {
|
|
299
|
+
const { HapStatusError: StatusError, HAPStatus } = this.hap;
|
|
300
|
+
return new StatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
301
|
+
}
|
|
302
|
+
battery(subtype, name) {
|
|
303
|
+
const { Service: S, Characteristic: C } = this.hap;
|
|
304
|
+
const svc = this.accessory.getServiceById(S.Battery, subtype)
|
|
305
|
+
|| this.accessory.addService(S.Battery, name, subtype);
|
|
306
|
+
this.label(svc, name);
|
|
307
|
+
svc.setCharacteristic(C.ChargingState, C.ChargingState.NOT_CHARGEABLE);
|
|
308
|
+
return svc;
|
|
309
|
+
}
|
|
310
|
+
label(svc, name) {
|
|
311
|
+
const C = this.hap.Characteristic;
|
|
312
|
+
svc.setCharacteristic(C.Name, name);
|
|
313
|
+
if (!svc.testCharacteristic(C.ConfiguredName)) {
|
|
314
|
+
svc.addOptionalCharacteristic(C.ConfiguredName);
|
|
315
|
+
}
|
|
316
|
+
// Only seed the name; don't overwrite a name the user set in the Home app
|
|
317
|
+
const current = svc.getCharacteristic(C.ConfiguredName).value;
|
|
318
|
+
if (!current) {
|
|
319
|
+
svc.setCharacteristic(C.ConfiguredName, name);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
removeSubtype(type, subtype) {
|
|
323
|
+
const svc = this.accessory.getServiceById(type, subtype);
|
|
324
|
+
if (svc) {
|
|
325
|
+
this.accessory.removeService(svc);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/* ------------------------------------------------------------- polling */
|
|
329
|
+
async poll() {
|
|
330
|
+
if (this.stopped) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
let data;
|
|
334
|
+
try {
|
|
335
|
+
data = await this.query(this.options);
|
|
336
|
+
}
|
|
337
|
+
catch (err) {
|
|
338
|
+
this.failures += 1;
|
|
339
|
+
const msg = errorText(err);
|
|
340
|
+
if (this.failures === this.offlineAfter) {
|
|
341
|
+
this.log.warn(`[${this.name}] Can't reach ${this.options.host} (${msg}). Showing it as offline.`);
|
|
342
|
+
this.setOnline(false);
|
|
343
|
+
}
|
|
344
|
+
else if (this.debug || this.failures < this.offlineAfter) {
|
|
345
|
+
this.log.debug(`[${this.name}] Poll failed (${this.failures}/${this.offlineAfter}): ${msg}`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (data) {
|
|
349
|
+
this.failures = 0;
|
|
350
|
+
try {
|
|
351
|
+
this.apply(data);
|
|
352
|
+
}
|
|
353
|
+
catch (err) {
|
|
354
|
+
this.log.error(`[${this.name}] Couldn't update HomeKit: ${errorText(err, true)}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (!this.stopped) {
|
|
358
|
+
this.timer = setTimeout(() => void this.poll(), this.interval);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
stop() {
|
|
362
|
+
this.stopped = true;
|
|
363
|
+
if (this.timer) {
|
|
364
|
+
clearTimeout(this.timer);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
setOnline(online) {
|
|
368
|
+
const C = this.hap.Characteristic;
|
|
369
|
+
const wasOnline = this.online;
|
|
370
|
+
if (online && !wasOnline) {
|
|
371
|
+
this.log.info(`[${this.name}] Back online.`);
|
|
372
|
+
}
|
|
373
|
+
this.online = online;
|
|
374
|
+
if (online && !wasOnline) {
|
|
375
|
+
for (const ch of this.guarded) {
|
|
376
|
+
ch.updateValue(ch.value);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
this.occupancy.updateCharacteristic(C.StatusActive, online);
|
|
380
|
+
for (const svc of Object.values(this.tiles)) {
|
|
381
|
+
svc.updateCharacteristic(C.StatusActive, online);
|
|
382
|
+
}
|
|
383
|
+
if (this.problem) {
|
|
384
|
+
this.problem.updateCharacteristic(C.StatusActive, online);
|
|
385
|
+
}
|
|
386
|
+
if (this.lowSensor) {
|
|
387
|
+
this.lowSensor.updateCharacteristic(C.StatusActive, online);
|
|
388
|
+
}
|
|
389
|
+
if (!online) {
|
|
390
|
+
this.occupancy.updateCharacteristic(C.OccupancyDetected, 0);
|
|
391
|
+
// Push the failure so Home shows "No Response" without waiting for a read
|
|
392
|
+
for (const ch of this.guarded) {
|
|
393
|
+
ch.updateValue(this.noResponse());
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
apply(d) {
|
|
398
|
+
const C = this.hap.Characteristic;
|
|
399
|
+
const log = (m) => (this.debug ? this.log.info(m) : this.log.debug(m));
|
|
400
|
+
log(`[${this.name}] ${JSON.stringify(d)}`);
|
|
401
|
+
this.setOnline(true);
|
|
402
|
+
this.updateInfo(d);
|
|
403
|
+
// Printing / fault
|
|
404
|
+
this.occupancy.updateCharacteristic(C.OccupancyDetected, d.printing
|
|
405
|
+
? C.OccupancyDetected.OCCUPANCY_DETECTED
|
|
406
|
+
: C.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
|
|
407
|
+
this.occupancy.updateCharacteristic(C.StatusFault, d.fault
|
|
408
|
+
? C.StatusFault.GENERAL_FAULT
|
|
409
|
+
: C.StatusFault.NO_FAULT);
|
|
410
|
+
if (this.problem) {
|
|
411
|
+
this.problem.updateCharacteristic(C.ContactSensorState, d.fault
|
|
412
|
+
? C.ContactSensorState.CONTACT_NOT_DETECTED
|
|
413
|
+
: C.ContactSensorState.CONTACT_DETECTED);
|
|
414
|
+
}
|
|
415
|
+
if (d.fault && !this.last.fault) {
|
|
416
|
+
const what = d.faults.map((f) => FAULT_NAMES[f] || f).join(', ');
|
|
417
|
+
const shown = d.statusText ? `display: "${d.statusText}"` : '';
|
|
418
|
+
this.log.warn(`[${this.name}] Problem: ${[what, shown].filter(Boolean).join('; ')}`);
|
|
419
|
+
}
|
|
420
|
+
else if (!d.fault && this.last.fault) {
|
|
421
|
+
this.log.info(`[${this.name}] Problem cleared.`);
|
|
422
|
+
}
|
|
423
|
+
this.last.fault = d.fault;
|
|
424
|
+
if (d.statusText && d.statusText !== this.last.statusText) {
|
|
425
|
+
this.log.info(`[${this.name}] Status: ${d.statusText}`);
|
|
426
|
+
}
|
|
427
|
+
this.applySupplies(d);
|
|
428
|
+
this.applyDrumAndParts(d);
|
|
429
|
+
if (d.totalPages !== null) {
|
|
430
|
+
this.pages.updateValue(d.totalPages);
|
|
431
|
+
}
|
|
432
|
+
this.last.statusText = d.statusText;
|
|
433
|
+
if (this.last.summary === undefined) {
|
|
434
|
+
const kind = d.type === 'ink' ? 'ink' : 'toner';
|
|
435
|
+
const levels = d.supplies.length
|
|
436
|
+
? d.supplies.map((s) => (d.supplies.length === 1 ? `${kind} ${s.level}%` : `${s.color} ${s.level}%`)).join(', ')
|
|
437
|
+
: `no ${kind} levels reported`;
|
|
438
|
+
const drum = d.drumPercent !== null ? `, drum ${d.drumPercent}%` : '';
|
|
439
|
+
this.log.info(`[${this.name}] Connected to ${d.model || 'printer'} (SNMP v${d.snmpVersion}): ${levels}${drum}, `
|
|
440
|
+
+ `${d.totalPages ?? '?'} pages, status "${d.statusText || 'unknown'}".`);
|
|
441
|
+
this.last.summary = true;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Toner/ink battery: the lowest supply, so Home alerts when any color runs
|
|
446
|
+
* low. A supply counts as low when its level or its finer reading is at or
|
|
447
|
+
* below the threshold, or the printer itself flags low/no toner.
|
|
448
|
+
*/
|
|
449
|
+
applySupplies(d) {
|
|
450
|
+
const C = this.hap.Characteristic;
|
|
451
|
+
const kind = d.type === 'ink' ? 'Ink' : 'Toner';
|
|
452
|
+
if (!d.supplies.length || !d.lowestSupply) {
|
|
453
|
+
// Some models (e.g. ink tank printers) report no levels at all
|
|
454
|
+
this.remember({ noLevels: true });
|
|
455
|
+
const toner = this.toner;
|
|
456
|
+
if (toner) {
|
|
457
|
+
this.log.info(`[${this.name}] This printer doesn't report ${kind.toLowerCase()} levels; `
|
|
458
|
+
+ 'leaving out the battery.');
|
|
459
|
+
this.accessory.removeService(toner);
|
|
460
|
+
this.guarded = this.guarded.filter((ch) => !toner.characteristics.includes(ch));
|
|
461
|
+
this.toner = null;
|
|
462
|
+
}
|
|
463
|
+
this.removeLowSensor();
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
let toner = this.toner;
|
|
467
|
+
if (!toner) {
|
|
468
|
+
toner = this.battery('toner', `${this.name} ${kind}`);
|
|
469
|
+
this.occupancy.addLinkedService(toner);
|
|
470
|
+
this.guard(toner.getCharacteristic(C.BatteryLevel));
|
|
471
|
+
this.guard(toner.getCharacteristic(C.StatusLowBattery));
|
|
472
|
+
this.toner = toner;
|
|
473
|
+
}
|
|
474
|
+
this.renameBattery(toner, kind);
|
|
475
|
+
this.remember({ noLevels: false, kind });
|
|
476
|
+
const flagged = d.errors.includes('lowToner') || d.errors.includes('noToner');
|
|
477
|
+
const isLow = (s) => s.level <= this.lowToner || (s.fine !== null && s.fine <= this.lowToner);
|
|
478
|
+
const low = d.supplies.filter(isLow);
|
|
479
|
+
const level = clamp(d.lowestSupply.level);
|
|
480
|
+
toner.updateCharacteristic(C.BatteryLevel, level);
|
|
481
|
+
toner.updateCharacteristic(C.StatusLowBattery, low.length || flagged
|
|
482
|
+
? C.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
483
|
+
: C.StatusLowBattery.BATTERY_LEVEL_NORMAL);
|
|
484
|
+
this.ensureLowSensor(kind);
|
|
485
|
+
if (this.lowSensor) {
|
|
486
|
+
this.lowSensor.updateCharacteristic(C.ContactSensorState, low.length || flagged
|
|
487
|
+
? C.ContactSensorState.CONTACT_NOT_DETECTED
|
|
488
|
+
: C.ContactSensorState.CONTACT_DETECTED);
|
|
489
|
+
}
|
|
490
|
+
this.ensureTiles(d);
|
|
491
|
+
for (const s of d.supplies) {
|
|
492
|
+
this.tiles[s.color]?.updateCharacteristic(C.CurrentRelativeHumidity, clamp(s.level));
|
|
493
|
+
}
|
|
494
|
+
const mono = d.supplies.length === 1;
|
|
495
|
+
const label = (s) => (mono ? kind : `${cap(s.color)} ${kind.toLowerCase()}`);
|
|
496
|
+
const prev = this.last.supplies || {};
|
|
497
|
+
for (const s of d.supplies) {
|
|
498
|
+
const was = prev[s.color];
|
|
499
|
+
if (was && was.level !== s.level) {
|
|
500
|
+
this.log.info(`[${this.name}] ${label(s)} ${s.level}%`);
|
|
501
|
+
}
|
|
502
|
+
if (isLow(s) && !(was && was.low)) {
|
|
503
|
+
const fine = s.fine !== null && s.fine !== s.level ? ` (${s.fine}% by the finer reading)` : '';
|
|
504
|
+
this.log.warn(`[${this.name}] ${label(s)} is low: ${s.level}%${fine}.`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
this.last.supplies = Object.fromEntries(d.supplies.map((s) => [s.color, { level: s.level, low: isLow(s) }]));
|
|
508
|
+
}
|
|
509
|
+
/** Drum battery (optional) and log warnings for drums and other parts */
|
|
510
|
+
applyDrumAndParts(d) {
|
|
511
|
+
const drums = Object.entries(d.drums)
|
|
512
|
+
.filter((e) => typeof e[1].percent === 'number');
|
|
513
|
+
if (drums.length) {
|
|
514
|
+
const [color, lowest] = drums.reduce((a, b) => (b[1].percent < a[1].percent ? b : a));
|
|
515
|
+
const level = clamp(lowest.percent);
|
|
516
|
+
if (this.drum) {
|
|
517
|
+
this.setBattery(this.drum, level, this.lowDrum);
|
|
518
|
+
}
|
|
519
|
+
const name = drums.length > 1 ? `${cap(color)} drum` : 'Drum';
|
|
520
|
+
this.warnOnce('drum', level <= this.lowDrum, `${name} is near end of life (${level}%, ${lowest.pagesRemaining ?? '?'} pages left).`);
|
|
521
|
+
}
|
|
522
|
+
for (const [key, part] of Object.entries(d.parts)) {
|
|
523
|
+
if (typeof part.percent !== 'number' || !PART_NAMES[key]) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const left = part.pagesRemaining !== undefined ? `, ${part.pagesRemaining} pages left` : '';
|
|
527
|
+
this.warnOnce(`part-${key}`, part.percent <= this.lowDrum, `${PART_NAMES[key]} is near end of life (${part.percent}%${left}).`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
warnOnce(key, condition, message) {
|
|
531
|
+
this.last.warned = this.last.warned || {};
|
|
532
|
+
if (condition && !this.last.warned[key]) {
|
|
533
|
+
this.log.warn(`[${this.name}] ${message}`);
|
|
534
|
+
}
|
|
535
|
+
this.last.warned[key] = condition;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* The battery is made before the first poll says toner or ink, so on an
|
|
539
|
+
* inkjet it starts as "Toner". Rename it, unless the user renamed it in Home.
|
|
540
|
+
*/
|
|
541
|
+
renameBattery(toner, kind) {
|
|
542
|
+
const C = this.hap.Characteristic;
|
|
543
|
+
const was = this.accessory.context.kind || 'Toner';
|
|
544
|
+
if (was === kind) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const name = `${this.name} ${kind}`;
|
|
548
|
+
toner.setCharacteristic(C.Name, name);
|
|
549
|
+
const current = toner.getCharacteristic(C.ConfiguredName).value;
|
|
550
|
+
if (!current || current === `${this.name} ${was}`) {
|
|
551
|
+
toner.setCharacteristic(C.ConfiguredName, name);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
/** Store values in the accessory cache, saving only when something changed */
|
|
555
|
+
remember(values) {
|
|
556
|
+
const ctx = this.accessory.context;
|
|
557
|
+
const changed = Object.entries(values).filter(([k, v]) => ctx[k] !== v);
|
|
558
|
+
if (!changed.length) {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
for (const [k, v] of changed) {
|
|
562
|
+
ctx[k] = v;
|
|
563
|
+
}
|
|
564
|
+
this.api.updatePlatformAccessories([this.accessory]);
|
|
565
|
+
}
|
|
566
|
+
setBattery(svc, level, threshold) {
|
|
567
|
+
const C = this.hap.Characteristic;
|
|
568
|
+
svc.updateCharacteristic(C.BatteryLevel, level);
|
|
569
|
+
svc.updateCharacteristic(C.StatusLowBattery, level <= threshold
|
|
570
|
+
? C.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
571
|
+
: C.StatusLowBattery.BATTERY_LEVEL_NORMAL);
|
|
572
|
+
}
|
|
573
|
+
updateInfo(d) {
|
|
574
|
+
const { Service: S, Characteristic: C } = this.hap;
|
|
575
|
+
const ctx = this.accessory.context;
|
|
576
|
+
let changed = false;
|
|
577
|
+
const model = d.model ? d.model.replace(/^Brother\s+/i, '') : null;
|
|
578
|
+
if (model && model !== ctx.model) {
|
|
579
|
+
ctx.model = model;
|
|
580
|
+
changed = true;
|
|
581
|
+
}
|
|
582
|
+
if (d.serial && d.serial !== ctx.serial) {
|
|
583
|
+
ctx.serial = d.serial;
|
|
584
|
+
changed = true;
|
|
585
|
+
}
|
|
586
|
+
if (!changed) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
this.accessory.getService(S.AccessoryInformation)
|
|
590
|
+
.updateCharacteristic(C.Model, ctx.model || 'Brother printer')
|
|
591
|
+
.updateCharacteristic(C.SerialNumber, ctx.serial || this.options.host);
|
|
592
|
+
this.api.updatePlatformAccessories([this.accessory]);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
exports.PrinterAccessory = PrinterAccessory;
|