zigbee2mqtt-mcp 0.1.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/LICENSE +21 -0
- package/README.md +156 -0
- package/dist/client.js +293 -0
- package/dist/config.js +81 -0
- package/dist/devices.js +187 -0
- package/dist/index.js +70 -0
- package/dist/tools.js +770 -0
- package/package.json +52 -0
package/dist/tools.js
ADDED
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import { buildHealthReport, detectCapabilities, isCoordinator, normalize, } from "./devices.js";
|
|
2
|
+
const EMPTY = { type: "object", properties: {} };
|
|
3
|
+
const DEVICE_ID = {
|
|
4
|
+
type: "string",
|
|
5
|
+
description: "Device friendly_name or ieee_address.",
|
|
6
|
+
};
|
|
7
|
+
// --------------------------------------------------------------------- helpers
|
|
8
|
+
async function loadDevices(ctx, collectSeconds) {
|
|
9
|
+
if (collectSeconds) {
|
|
10
|
+
await ctx.client.collectState(Math.min(Number(collectSeconds), 120) * 1000);
|
|
11
|
+
}
|
|
12
|
+
const snap = await ctx.client.snapshot();
|
|
13
|
+
const raw = snap.devices.filter((d) => !isCoordinator(d));
|
|
14
|
+
const devices = raw.map((d) => normalize(d, snap.deviceState, snap.availability));
|
|
15
|
+
const capabilities = detectCapabilities(snap.info, snap.availability);
|
|
16
|
+
return { snap, raw, devices, capabilities };
|
|
17
|
+
}
|
|
18
|
+
const COLLECT_SECONDS = {
|
|
19
|
+
type: "number",
|
|
20
|
+
description: "Listen for live device traffic for this many seconds before answering, to populate linkquality, " +
|
|
21
|
+
"battery and OTA fields that Zigbee2MQTT does not publish as retained messages. Max 120.",
|
|
22
|
+
};
|
|
23
|
+
function findRaw(raw, id) {
|
|
24
|
+
const needle = id.toLowerCase();
|
|
25
|
+
const match = raw.find((d) => d.friendly_name.toLowerCase() === needle || d.ieee_address.toLowerCase() === needle);
|
|
26
|
+
if (match)
|
|
27
|
+
return match;
|
|
28
|
+
const partial = raw.filter((d) => d.friendly_name.toLowerCase().includes(needle));
|
|
29
|
+
if (partial.length === 1)
|
|
30
|
+
return partial[0];
|
|
31
|
+
if (partial.length > 1) {
|
|
32
|
+
throw new Error(`'${id}' matches ${partial.length} devices: ${partial
|
|
33
|
+
.slice(0, 10)
|
|
34
|
+
.map((d) => d.friendly_name)
|
|
35
|
+
.join(", ")}. Use an exact friendly_name or ieee_address.`);
|
|
36
|
+
}
|
|
37
|
+
throw new Error(`No device matches '${id}'. Use z2m_list_devices to see available devices.`);
|
|
38
|
+
}
|
|
39
|
+
function requireConfirm(args, action) {
|
|
40
|
+
if (args.confirm !== true) {
|
|
41
|
+
throw new Error(`${action} is irreversible or disruptive. Re-run with confirm=true to proceed.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function summariseExposes(exposes) {
|
|
45
|
+
const out = [];
|
|
46
|
+
const walk = (node) => {
|
|
47
|
+
if (!node || typeof node !== "object")
|
|
48
|
+
return;
|
|
49
|
+
if (Array.isArray(node.features)) {
|
|
50
|
+
for (const feature of node.features)
|
|
51
|
+
walk(feature);
|
|
52
|
+
if (node.type && !node.property)
|
|
53
|
+
out.push(`${node.type}`);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (node.property)
|
|
57
|
+
out.push(`${node.property}:${node.type ?? "unknown"}`);
|
|
58
|
+
};
|
|
59
|
+
for (const expose of exposes ?? [])
|
|
60
|
+
walk(expose);
|
|
61
|
+
return [...new Set(out)];
|
|
62
|
+
}
|
|
63
|
+
// ----------------------------------------------------------------- read tools
|
|
64
|
+
const readTools = [
|
|
65
|
+
{
|
|
66
|
+
name: "z2m_bridge_info",
|
|
67
|
+
tier: "read",
|
|
68
|
+
description: "Get Zigbee2MQTT bridge status: version, coordinator type and firmware, Zigbee channel and PAN ID, " +
|
|
69
|
+
"permit_join state, log level, whether a restart is required, and host OS/memory. " +
|
|
70
|
+
"Start here to understand the estate.",
|
|
71
|
+
inputSchema: {
|
|
72
|
+
type: "object",
|
|
73
|
+
properties: {
|
|
74
|
+
include_config: {
|
|
75
|
+
type: "boolean",
|
|
76
|
+
description: "Include the full Zigbee2MQTT configuration (large). Default false.",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
handler: async (args, ctx) => {
|
|
81
|
+
const snap = await ctx.client.snapshot();
|
|
82
|
+
const info = (snap.info ?? {});
|
|
83
|
+
const health = snap.health;
|
|
84
|
+
const capabilities = detectCapabilities(snap.info, snap.availability);
|
|
85
|
+
const result = {
|
|
86
|
+
bridge_state: snap.state?.state ?? "unknown",
|
|
87
|
+
version: info.version,
|
|
88
|
+
commit: info.commit,
|
|
89
|
+
coordinator: info.coordinator,
|
|
90
|
+
network: info.network,
|
|
91
|
+
permit_join: info.permit_join,
|
|
92
|
+
permit_join_end: info.permit_join_end
|
|
93
|
+
? new Date(Number(info.permit_join_end) * 1000).toISOString()
|
|
94
|
+
: undefined,
|
|
95
|
+
log_level: info.log_level,
|
|
96
|
+
restart_required: info.restart_required,
|
|
97
|
+
os: info.os,
|
|
98
|
+
mqtt: info.mqtt,
|
|
99
|
+
zigbee_herdsman: info.zigbee_herdsman,
|
|
100
|
+
zigbee_herdsman_converters: info.zigbee_herdsman_converters,
|
|
101
|
+
device_count: snap.devices.filter((d) => !isCoordinator(d)).length,
|
|
102
|
+
group_count: snap.groups.length,
|
|
103
|
+
capabilities,
|
|
104
|
+
runtime: health
|
|
105
|
+
? {
|
|
106
|
+
uptime_hours: health.process?.uptime_sec
|
|
107
|
+
? Math.round(Number(health.process.uptime_sec) / 360) / 10
|
|
108
|
+
: undefined,
|
|
109
|
+
process_memory_mb: health.process?.memory_used_mb,
|
|
110
|
+
os_load_average: health.os?.load_average,
|
|
111
|
+
mqtt_connected: health.mqtt?.connected,
|
|
112
|
+
mqtt_queued: health.mqtt?.queued,
|
|
113
|
+
mqtt_published: health.mqtt?.published,
|
|
114
|
+
mqtt_received: health.mqtt?.received,
|
|
115
|
+
}
|
|
116
|
+
: undefined,
|
|
117
|
+
};
|
|
118
|
+
if (args.include_config)
|
|
119
|
+
result.config = info.config;
|
|
120
|
+
return result;
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: "z2m_list_devices",
|
|
125
|
+
tier: "read",
|
|
126
|
+
description: "List Zigbee devices with administrative detail: type, model, vendor, power source, link quality, " +
|
|
127
|
+
"battery, availability, last_seen, interview state and pending OTA updates. Supports filtering and " +
|
|
128
|
+
"sorting, so it is the main tool for questions like 'which devices are offline' or 'which have the weakest signal'.",
|
|
129
|
+
inputSchema: {
|
|
130
|
+
type: "object",
|
|
131
|
+
properties: {
|
|
132
|
+
search: { type: "string", description: "Case-insensitive substring match on name, model, vendor or description." },
|
|
133
|
+
type: { type: "string", enum: ["Router", "EndDevice"], description: "Filter by Zigbee device type." },
|
|
134
|
+
availability: { type: "string", enum: ["online", "offline", "unknown"] },
|
|
135
|
+
only_problems: {
|
|
136
|
+
type: "boolean",
|
|
137
|
+
description: "Only devices that are offline, disabled, unsupported, mid-interview, weak-signal or low-battery.",
|
|
138
|
+
},
|
|
139
|
+
has_update: { type: "boolean", description: "Only devices with a pending OTA firmware update." },
|
|
140
|
+
sort_by: {
|
|
141
|
+
type: "string",
|
|
142
|
+
enum: ["friendly_name", "linkquality", "battery", "last_seen", "model", "vendor"],
|
|
143
|
+
description: "Default friendly_name. Numeric sorts are ascending, so weakest/lowest first.",
|
|
144
|
+
},
|
|
145
|
+
collect_seconds: COLLECT_SECONDS,
|
|
146
|
+
limit: { type: "number", description: "Maximum number of devices to return." },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
handler: async (args, ctx) => {
|
|
150
|
+
const { devices, capabilities } = await loadDevices(ctx, args.collect_seconds);
|
|
151
|
+
let list = devices;
|
|
152
|
+
if (args.search) {
|
|
153
|
+
const needle = String(args.search).toLowerCase();
|
|
154
|
+
list = list.filter((d) => [d.friendly_name, d.model, d.vendor, d.description]
|
|
155
|
+
.filter(Boolean)
|
|
156
|
+
.some((field) => String(field).toLowerCase().includes(needle)));
|
|
157
|
+
}
|
|
158
|
+
if (args.type)
|
|
159
|
+
list = list.filter((d) => d.type === args.type);
|
|
160
|
+
if (args.availability) {
|
|
161
|
+
list = list.filter((d) => (d.availability ?? "unknown") === args.availability);
|
|
162
|
+
}
|
|
163
|
+
if (args.has_update)
|
|
164
|
+
list = list.filter((d) => d.update?.state === "available");
|
|
165
|
+
if (args.only_problems) {
|
|
166
|
+
list = list.filter((d) => d.availability === "offline" ||
|
|
167
|
+
d.disabled ||
|
|
168
|
+
!d.supported ||
|
|
169
|
+
d.interview_state !== "SUCCESSFUL" ||
|
|
170
|
+
(d.linkquality !== undefined && d.linkquality < ctx.config.weakLinkThreshold) ||
|
|
171
|
+
(d.battery !== undefined && d.battery < ctx.config.lowBatteryThreshold) ||
|
|
172
|
+
d.battery_low === true);
|
|
173
|
+
}
|
|
174
|
+
const sortBy = args.sort_by ?? "friendly_name";
|
|
175
|
+
list = [...list].sort((a, b) => {
|
|
176
|
+
const av = a[sortBy];
|
|
177
|
+
const bv = b[sortBy];
|
|
178
|
+
if (av === undefined && bv === undefined)
|
|
179
|
+
return 0;
|
|
180
|
+
if (av === undefined)
|
|
181
|
+
return 1;
|
|
182
|
+
if (bv === undefined)
|
|
183
|
+
return -1;
|
|
184
|
+
if (typeof av === "number" && typeof bv === "number")
|
|
185
|
+
return av - bv;
|
|
186
|
+
return String(av).localeCompare(String(bv));
|
|
187
|
+
});
|
|
188
|
+
const total = list.length;
|
|
189
|
+
if (args.limit)
|
|
190
|
+
list = list.slice(0, Number(args.limit));
|
|
191
|
+
return { total, returned: list.length, capabilities, devices: list };
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: "z2m_get_device",
|
|
196
|
+
tier: "read",
|
|
197
|
+
description: "Get full detail for one device: current state, exposed properties, configurable device options with " +
|
|
198
|
+
"their schema, endpoints, bindings and configured reportings. Use before changing options or bindings.",
|
|
199
|
+
inputSchema: {
|
|
200
|
+
type: "object",
|
|
201
|
+
properties: {
|
|
202
|
+
device: DEVICE_ID,
|
|
203
|
+
include_raw: {
|
|
204
|
+
type: "boolean",
|
|
205
|
+
description: "Include the raw exposes/options definitions (verbose). Default false.",
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
required: ["device"],
|
|
209
|
+
},
|
|
210
|
+
handler: async (args, ctx) => {
|
|
211
|
+
const { snap, raw } = await loadDevices(ctx);
|
|
212
|
+
const target = findRaw(raw, String(args.device));
|
|
213
|
+
const device = normalize(target, snap.deviceState, snap.availability);
|
|
214
|
+
const result = {
|
|
215
|
+
...device,
|
|
216
|
+
definition_source: target.definition?.source,
|
|
217
|
+
model_id: target.model_id,
|
|
218
|
+
exposes: summariseExposes(target.definition?.exposes),
|
|
219
|
+
settable_options: (target.definition?.options ?? []).map((o) => ({
|
|
220
|
+
property: o?.property,
|
|
221
|
+
type: o?.type,
|
|
222
|
+
description: o?.description,
|
|
223
|
+
value_min: o?.value_min,
|
|
224
|
+
value_max: o?.value_max,
|
|
225
|
+
values: o?.values,
|
|
226
|
+
})),
|
|
227
|
+
endpoints: target.endpoints,
|
|
228
|
+
current_state: snap.deviceState.get(target.friendly_name) ?? null,
|
|
229
|
+
};
|
|
230
|
+
if (args.include_raw) {
|
|
231
|
+
result.raw_exposes = target.definition?.exposes;
|
|
232
|
+
result.raw_options = target.definition?.options;
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
name: "z2m_health_report",
|
|
239
|
+
tier: "read",
|
|
240
|
+
description: "Audit the whole Zigbee estate in one call and return everything that needs attention: offline devices, " +
|
|
241
|
+
"weak links, stale devices, low batteries, failed or pending interviews, unsupported devices, disabled " +
|
|
242
|
+
"devices, pending OTA updates, and devices that keep rejoining or changing network address. " +
|
|
243
|
+
"Use this to answer 'is my Zigbee network healthy?'.",
|
|
244
|
+
inputSchema: {
|
|
245
|
+
type: "object",
|
|
246
|
+
properties: { collect_seconds: COLLECT_SECONDS },
|
|
247
|
+
},
|
|
248
|
+
handler: async (args, ctx) => {
|
|
249
|
+
const { snap, devices, capabilities } = await loadDevices(ctx, args.collect_seconds);
|
|
250
|
+
return buildHealthReport(devices, snap.info, snap.state, snap.health, capabilities, ctx.config);
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
name: "z2m_network_map",
|
|
255
|
+
tier: "read",
|
|
256
|
+
description: "Scan the Zigbee mesh topology and return per-device parent, depth, link quality and route count, plus " +
|
|
257
|
+
"orphaned or weakly-attached devices. WARNING: the scan makes the network less responsive and can take " +
|
|
258
|
+
"10 seconds to several minutes depending on estate size. Run it deliberately, not routinely.",
|
|
259
|
+
inputSchema: {
|
|
260
|
+
type: "object",
|
|
261
|
+
properties: {
|
|
262
|
+
include_routes: { type: "boolean", description: "Include active routes. Slower. Default false." },
|
|
263
|
+
raw: { type: "boolean", description: "Return the unsummarised graph. Default false." },
|
|
264
|
+
timeout_ms: { type: "number", description: "Scan timeout. Default 180000." },
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
handler: async (args, ctx) => {
|
|
268
|
+
const data = await ctx.client.request("networkmap", { type: "raw", routes: args.include_routes === true }, Number(args.timeout_ms ?? 180_000));
|
|
269
|
+
const value = data?.value ?? data;
|
|
270
|
+
if (args.raw)
|
|
271
|
+
return value;
|
|
272
|
+
const nodes = value?.nodes ?? [];
|
|
273
|
+
const links = value?.links ?? [];
|
|
274
|
+
const byAddress = new Map(nodes.map((n) => [n.ieeeAddr, n]));
|
|
275
|
+
const summary = nodes.map((node) => {
|
|
276
|
+
const inbound = links.filter((l) => l.targetIeeeAddr === node.ieeeAddr);
|
|
277
|
+
const best = inbound.sort((a, b) => (b.linkquality ?? 0) - (a.linkquality ?? 0))[0];
|
|
278
|
+
return {
|
|
279
|
+
friendly_name: node.friendlyName,
|
|
280
|
+
ieee_address: node.ieeeAddr,
|
|
281
|
+
type: node.type,
|
|
282
|
+
parent: best ? byAddress.get(best.sourceIeeeAddr)?.friendlyName ?? best.sourceIeeeAddr : null,
|
|
283
|
+
linkquality: best?.linkquality,
|
|
284
|
+
depth: best?.depth,
|
|
285
|
+
relationship: best?.relationship,
|
|
286
|
+
neighbour_count: inbound.length,
|
|
287
|
+
route_count: best?.routes?.length ?? 0,
|
|
288
|
+
};
|
|
289
|
+
});
|
|
290
|
+
return {
|
|
291
|
+
node_count: nodes.length,
|
|
292
|
+
link_count: links.length,
|
|
293
|
+
orphans: summary.filter((n) => n.parent === null && n.type !== "Coordinator"),
|
|
294
|
+
weak: summary.filter((n) => n.linkquality !== undefined && n.linkquality < ctx.config.weakLinkThreshold),
|
|
295
|
+
nodes: summary,
|
|
296
|
+
};
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
name: "z2m_list_groups",
|
|
301
|
+
tier: "read",
|
|
302
|
+
description: "List Zigbee groups with their members and scenes.",
|
|
303
|
+
inputSchema: EMPTY,
|
|
304
|
+
handler: async (_args, ctx) => {
|
|
305
|
+
const snap = await ctx.client.snapshot();
|
|
306
|
+
return { total: snap.groups.length, groups: snap.groups };
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
name: "z2m_get_logs",
|
|
311
|
+
tier: "read",
|
|
312
|
+
description: "Read Zigbee2MQTT bridge logs and lifecycle events (device_joined, device_interview, device_leave, " +
|
|
313
|
+
"device_announce). Returns buffered history by default, or watches live for a window. Use to diagnose " +
|
|
314
|
+
"pairing failures and rejoin loops.",
|
|
315
|
+
inputSchema: {
|
|
316
|
+
type: "object",
|
|
317
|
+
properties: {
|
|
318
|
+
level: {
|
|
319
|
+
type: "string",
|
|
320
|
+
enum: ["error", "warning", "info"],
|
|
321
|
+
description: "Minimum severity. Note: Zigbee2MQTT never publishes debug lines to MQTT.",
|
|
322
|
+
},
|
|
323
|
+
contains: { type: "string", description: "Only lines containing this substring (case-insensitive)." },
|
|
324
|
+
watch_seconds: {
|
|
325
|
+
type: "number",
|
|
326
|
+
description: "Collect new lines live for this many seconds instead of returning buffered history. Max 120.",
|
|
327
|
+
},
|
|
328
|
+
limit: { type: "number", description: "Maximum lines to return. Default 100." },
|
|
329
|
+
include_events: { type: "boolean", description: "Include bridge lifecycle events. Default true." },
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
handler: async (args, ctx) => {
|
|
333
|
+
const rank = { error: 3, warning: 2, warn: 2, info: 1 };
|
|
334
|
+
const min = args.level ? rank[String(args.level)] ?? 0 : 0;
|
|
335
|
+
let lines = args.watch_seconds
|
|
336
|
+
? await ctx.client.collectLogs(Math.min(Number(args.watch_seconds), 120) * 1000)
|
|
337
|
+
: [...ctx.client.recentLogs()];
|
|
338
|
+
lines = lines.filter((l) => (rank[l.level] ?? 1) >= min);
|
|
339
|
+
if (args.contains) {
|
|
340
|
+
const needle = String(args.contains).toLowerCase();
|
|
341
|
+
lines = lines.filter((l) => l.message.toLowerCase().includes(needle));
|
|
342
|
+
}
|
|
343
|
+
const limit = Number(args.limit ?? 100);
|
|
344
|
+
return {
|
|
345
|
+
note: ctx.client.recentLogs().length === 0 && !args.watch_seconds
|
|
346
|
+
? "No logs buffered yet. Logs are captured only while this server is connected; use watch_seconds to collect live."
|
|
347
|
+
: undefined,
|
|
348
|
+
logs: lines.slice(-limit),
|
|
349
|
+
events: args.include_events === false ? undefined : ctx.client.recentEvents().slice(-limit),
|
|
350
|
+
};
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
// ----------------------------------------------------------- safe write tools
|
|
355
|
+
const safeTools = [
|
|
356
|
+
{
|
|
357
|
+
name: "z2m_check_updates",
|
|
358
|
+
tier: "safe",
|
|
359
|
+
description: "Actively query devices for available OTA firmware updates. Without arguments this checks every " +
|
|
360
|
+
"OTA-capable device, which generates real Zigbee traffic and can take a while. For a passive answer " +
|
|
361
|
+
"with no network cost, use z2m_health_report instead.",
|
|
362
|
+
inputSchema: {
|
|
363
|
+
type: "object",
|
|
364
|
+
properties: {
|
|
365
|
+
device: { ...DEVICE_ID, description: "Check a single device. Omit to check all mains-powered devices." },
|
|
366
|
+
timeout_ms: { type: "number", description: "Per-device timeout. Default 60000." },
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
handler: async (args, ctx) => {
|
|
370
|
+
const { raw } = await loadDevices(ctx);
|
|
371
|
+
const timeout = Number(args.timeout_ms ?? 60_000);
|
|
372
|
+
const targets = args.device
|
|
373
|
+
? [findRaw(raw, String(args.device))]
|
|
374
|
+
: raw.filter((d) => d.disabled !== true && d.type === "Router");
|
|
375
|
+
const results = [];
|
|
376
|
+
for (const target of targets) {
|
|
377
|
+
try {
|
|
378
|
+
const data = await ctx.client.request("device/ota_update/check", { id: target.friendly_name }, timeout);
|
|
379
|
+
results.push({ device: target.friendly_name, ...data });
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
results.push({ device: target.friendly_name, error: error.message });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return { checked: results.length, results };
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
name: "z2m_permit_join",
|
|
390
|
+
tier: "safe",
|
|
391
|
+
description: "Open or close the network for new devices to join. Optionally scope joining to a single router, which " +
|
|
392
|
+
"is the recommended way to pair a device into a specific part of the mesh.",
|
|
393
|
+
inputSchema: {
|
|
394
|
+
type: "object",
|
|
395
|
+
properties: {
|
|
396
|
+
time: {
|
|
397
|
+
type: "number",
|
|
398
|
+
description: "Seconds to allow joining. 0 closes the network. Maximum 254.",
|
|
399
|
+
},
|
|
400
|
+
device: {
|
|
401
|
+
type: "string",
|
|
402
|
+
description: "Restrict joining to this router's friendly_name, or 'coordinator'. Omit to allow via any router.",
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
required: ["time"],
|
|
406
|
+
},
|
|
407
|
+
handler: async (args, ctx) => {
|
|
408
|
+
const payload = { time: Number(args.time) };
|
|
409
|
+
if (args.device)
|
|
410
|
+
payload.device = String(args.device);
|
|
411
|
+
return ctx.client.request("permit_join", payload);
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: "z2m_set_device_options",
|
|
416
|
+
tier: "safe",
|
|
417
|
+
description: "Change Zigbee2MQTT device options such as transition, retain, debounce, temperature_precision or " +
|
|
418
|
+
"calibration offsets. Call z2m_get_device first to see settable_options for that device. " +
|
|
419
|
+
"The response reports whether a bridge restart is required.",
|
|
420
|
+
inputSchema: {
|
|
421
|
+
type: "object",
|
|
422
|
+
properties: {
|
|
423
|
+
device: DEVICE_ID,
|
|
424
|
+
options: { type: "object", description: 'Options to merge, e.g. {"transition": 1, "retain": true}.' },
|
|
425
|
+
},
|
|
426
|
+
required: ["device", "options"],
|
|
427
|
+
},
|
|
428
|
+
handler: async (args, ctx) => {
|
|
429
|
+
const { raw } = await loadDevices(ctx);
|
|
430
|
+
const target = findRaw(raw, String(args.device));
|
|
431
|
+
return ctx.client.request("device/options", { id: target.friendly_name, options: args.options });
|
|
432
|
+
},
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
name: "z2m_rename_device",
|
|
436
|
+
tier: "safe",
|
|
437
|
+
description: "Rename a device's friendly_name. Renaming changes its MQTT topic, so anything referencing the old " +
|
|
438
|
+
"name (automations, dashboards, scripts) must be updated. Set homeassistant_rename to also rename the " +
|
|
439
|
+
"Home Assistant entity.",
|
|
440
|
+
inputSchema: {
|
|
441
|
+
type: "object",
|
|
442
|
+
properties: {
|
|
443
|
+
device: DEVICE_ID,
|
|
444
|
+
new_name: { type: "string", description: "New friendly_name. '/' creates folder structure in MQTT." },
|
|
445
|
+
homeassistant_rename: {
|
|
446
|
+
type: "boolean",
|
|
447
|
+
description: "Also update the Home Assistant entity ID. Default false.",
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
required: ["device", "new_name"],
|
|
451
|
+
},
|
|
452
|
+
handler: async (args, ctx) => {
|
|
453
|
+
const { raw } = await loadDevices(ctx);
|
|
454
|
+
const target = findRaw(raw, String(args.device));
|
|
455
|
+
return ctx.client.request("device/rename", {
|
|
456
|
+
from: target.friendly_name,
|
|
457
|
+
to: String(args.new_name),
|
|
458
|
+
homeassistant_rename: args.homeassistant_rename === true,
|
|
459
|
+
});
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
name: "z2m_configure_device",
|
|
464
|
+
tier: "safe",
|
|
465
|
+
description: "Re-run a device's configuration routine (bindings and attribute reporting). Use when a device stopped " +
|
|
466
|
+
"reporting values. Battery devices must be woken immediately before calling this.",
|
|
467
|
+
inputSchema: { type: "object", properties: { device: DEVICE_ID }, required: ["device"] },
|
|
468
|
+
handler: async (args, ctx) => {
|
|
469
|
+
const { raw } = await loadDevices(ctx);
|
|
470
|
+
const target = findRaw(raw, String(args.device));
|
|
471
|
+
return ctx.client.request("device/configure", { id: target.friendly_name }, 60_000);
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
name: "z2m_interview_device",
|
|
476
|
+
tier: "safe",
|
|
477
|
+
description: "Re-interview a device so Zigbee2MQTT re-reads its endpoints, clusters and basic attributes. Useful " +
|
|
478
|
+
"after a firmware upgrade adds functionality, or to recover a device stuck in a failed interview.",
|
|
479
|
+
inputSchema: { type: "object", properties: { device: DEVICE_ID }, required: ["device"] },
|
|
480
|
+
handler: async (args, ctx) => {
|
|
481
|
+
const { raw } = await loadDevices(ctx);
|
|
482
|
+
const target = findRaw(raw, String(args.device));
|
|
483
|
+
return ctx.client.request("device/interview", { id: target.friendly_name }, 120_000);
|
|
484
|
+
},
|
|
485
|
+
},
|
|
486
|
+
{
|
|
487
|
+
name: "z2m_set_state",
|
|
488
|
+
tier: "safe",
|
|
489
|
+
description: "Send a state command to a device or group, e.g. {\"state\":\"ON\"} or {\"brightness\":128}. " +
|
|
490
|
+
"Also supports reading values back with mode='get'. Consult z2m_get_device exposes for valid properties.",
|
|
491
|
+
inputSchema: {
|
|
492
|
+
type: "object",
|
|
493
|
+
properties: {
|
|
494
|
+
device: { type: "string", description: "Device or group friendly_name, or ieee_address." },
|
|
495
|
+
payload: { type: "object", description: 'Command payload, e.g. {"state": "ON"}.' },
|
|
496
|
+
mode: { type: "string", enum: ["set", "get"], description: "Default 'set'." },
|
|
497
|
+
},
|
|
498
|
+
required: ["device", "payload"],
|
|
499
|
+
},
|
|
500
|
+
handler: async (args, ctx) => {
|
|
501
|
+
const snap = await ctx.client.snapshot();
|
|
502
|
+
const raw = snap.devices.filter((d) => !isCoordinator(d));
|
|
503
|
+
const groupNames = snap.groups.map((g) => String(g.friendly_name));
|
|
504
|
+
const id = String(args.device);
|
|
505
|
+
const name = groupNames.some((g) => g.toLowerCase() === id.toLowerCase())
|
|
506
|
+
? id
|
|
507
|
+
: findRaw(raw, id).friendly_name;
|
|
508
|
+
const mode = args.mode === "get" ? "get" : "set";
|
|
509
|
+
await ctx.client.publish(`${ctx.config.baseTopic}/${name}/${mode}`, args.payload);
|
|
510
|
+
return { published_to: `${ctx.config.baseTopic}/${name}/${mode}`, payload: args.payload };
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
name: "z2m_manage_group",
|
|
515
|
+
tier: "safe",
|
|
516
|
+
description: "Create or remove groups, rename them, and add or remove device members. Groups let a single Zigbee " +
|
|
517
|
+
"multicast control many devices, which is far more responsive than commanding each device in turn.",
|
|
518
|
+
inputSchema: {
|
|
519
|
+
type: "object",
|
|
520
|
+
properties: {
|
|
521
|
+
action: {
|
|
522
|
+
type: "string",
|
|
523
|
+
enum: ["add", "remove", "rename", "add_member", "remove_member", "remove_all_members"],
|
|
524
|
+
},
|
|
525
|
+
group: { type: "string", description: "Group friendly_name or numeric ID." },
|
|
526
|
+
new_name: { type: "string", description: "Required for action=rename." },
|
|
527
|
+
id: { type: "number", description: "Optional numeric ID for action=add." },
|
|
528
|
+
device: { type: "string", description: "Device for member actions. Append /ENDPOINT to target an endpoint." },
|
|
529
|
+
force: { type: "boolean", description: "Force removal even if a member device is unreachable." },
|
|
530
|
+
},
|
|
531
|
+
required: ["action", "group"],
|
|
532
|
+
},
|
|
533
|
+
handler: async (args, ctx) => {
|
|
534
|
+
const group = String(args.group);
|
|
535
|
+
switch (args.action) {
|
|
536
|
+
case "add":
|
|
537
|
+
return ctx.client.request("group/add", {
|
|
538
|
+
friendly_name: group,
|
|
539
|
+
...(args.id !== undefined ? { id: Number(args.id) } : {}),
|
|
540
|
+
});
|
|
541
|
+
case "remove":
|
|
542
|
+
return ctx.client.request("group/remove", { id: group, force: args.force === true });
|
|
543
|
+
case "rename":
|
|
544
|
+
if (!args.new_name)
|
|
545
|
+
throw new Error("new_name is required for action=rename");
|
|
546
|
+
return ctx.client.request("group/rename", { from: group, to: String(args.new_name) });
|
|
547
|
+
case "add_member":
|
|
548
|
+
if (!args.device)
|
|
549
|
+
throw new Error("device is required for action=add_member");
|
|
550
|
+
return ctx.client.request("group/members/add", { group, device: String(args.device) });
|
|
551
|
+
case "remove_member":
|
|
552
|
+
if (!args.device)
|
|
553
|
+
throw new Error("device is required for action=remove_member");
|
|
554
|
+
return ctx.client.request("group/members/remove", { group, device: String(args.device) });
|
|
555
|
+
case "remove_all_members":
|
|
556
|
+
return ctx.client.request("group/members/remove_all", { group });
|
|
557
|
+
default:
|
|
558
|
+
throw new Error(`Unknown action '${args.action}'`);
|
|
559
|
+
}
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
name: "z2m_bind",
|
|
564
|
+
tier: "safe",
|
|
565
|
+
description: "Bind or unbind clusters between two devices, or between a device and a group. Binding lets a remote " +
|
|
566
|
+
"control a light directly over Zigbee without a round trip through the coordinator, so it keeps working " +
|
|
567
|
+
"even if the bridge is down. Append /ENDPOINT to target a specific endpoint, e.g. 'my_remote/left'.",
|
|
568
|
+
inputSchema: {
|
|
569
|
+
type: "object",
|
|
570
|
+
properties: {
|
|
571
|
+
action: { type: "string", enum: ["bind", "unbind", "clear"] },
|
|
572
|
+
from: { type: "string", description: "Source device, optionally with /ENDPOINT." },
|
|
573
|
+
to: { type: "string", description: "Target device or group. Not needed for action=clear." },
|
|
574
|
+
clusters: {
|
|
575
|
+
type: "array",
|
|
576
|
+
items: { type: "string" },
|
|
577
|
+
description: "Clusters to bind, e.g. ['genOnOff','genLevelCtrl']. Omit to bind all supported clusters.",
|
|
578
|
+
},
|
|
579
|
+
},
|
|
580
|
+
required: ["action", "from"],
|
|
581
|
+
},
|
|
582
|
+
handler: async (args, ctx) => {
|
|
583
|
+
if (args.action === "clear") {
|
|
584
|
+
return ctx.client.request("device/binds/clear", { id: String(args.from) }, 60_000);
|
|
585
|
+
}
|
|
586
|
+
if (!args.to)
|
|
587
|
+
throw new Error("'to' is required for bind and unbind");
|
|
588
|
+
const payload = { from: String(args.from), to: String(args.to) };
|
|
589
|
+
if (Array.isArray(args.clusters) && args.clusters.length > 0)
|
|
590
|
+
payload.clusters = args.clusters;
|
|
591
|
+
return ctx.client.request(args.action === "bind" ? "device/bind" : "device/unbind", payload, 60_000);
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
name: "z2m_configure_reporting",
|
|
596
|
+
tier: "safe",
|
|
597
|
+
description: "Configure how often a device reports an attribute. Tightening intervals improves responsiveness; " +
|
|
598
|
+
"loosening them saves battery. Set maximum_report_interval to 65535 to disable reporting. " +
|
|
599
|
+
"Battery devices must be woken immediately before calling this.",
|
|
600
|
+
inputSchema: {
|
|
601
|
+
type: "object",
|
|
602
|
+
properties: {
|
|
603
|
+
device: DEVICE_ID,
|
|
604
|
+
endpoint: { type: "number", description: "Endpoint ID. Default 1." },
|
|
605
|
+
cluster: { type: "string", description: "Cluster name, e.g. 'genLevelCtrl'." },
|
|
606
|
+
attribute: { type: "string", description: "Attribute name, e.g. 'currentLevel'." },
|
|
607
|
+
minimum_report_interval: { type: "number", description: "Seconds. 0 means report on every change." },
|
|
608
|
+
maximum_report_interval: { type: "number", description: "Seconds. 65535 disables reporting." },
|
|
609
|
+
reportable_change: { type: "number", description: "Minimum change worth reporting, in the attribute's unit." },
|
|
610
|
+
},
|
|
611
|
+
required: ["device", "cluster", "attribute", "minimum_report_interval", "maximum_report_interval"],
|
|
612
|
+
},
|
|
613
|
+
handler: async (args, ctx) => {
|
|
614
|
+
const { raw } = await loadDevices(ctx);
|
|
615
|
+
const target = findRaw(raw, String(args.device));
|
|
616
|
+
return ctx.client.request("device/reporting/configure", {
|
|
617
|
+
id: target.friendly_name,
|
|
618
|
+
endpoint: Number(args.endpoint ?? 1),
|
|
619
|
+
cluster: String(args.cluster),
|
|
620
|
+
attribute: String(args.attribute),
|
|
621
|
+
minimum_report_interval: Number(args.minimum_report_interval),
|
|
622
|
+
maximum_report_interval: Number(args.maximum_report_interval),
|
|
623
|
+
...(args.reportable_change !== undefined ? { reportable_change: Number(args.reportable_change) } : {}),
|
|
624
|
+
}, 60_000);
|
|
625
|
+
},
|
|
626
|
+
},
|
|
627
|
+
];
|
|
628
|
+
// ----------------------------------------------------------- full write tools
|
|
629
|
+
const fullTools = [
|
|
630
|
+
{
|
|
631
|
+
name: "z2m_remove_device",
|
|
632
|
+
tier: "full",
|
|
633
|
+
description: "Remove a device from the Zigbee network. The coordinator can only ask a device to leave, so sleeping " +
|
|
634
|
+
"battery devices often fail. force=true deletes it from the database only, leaving it holding the " +
|
|
635
|
+
"network key until factory reset. Requires confirm=true.",
|
|
636
|
+
inputSchema: {
|
|
637
|
+
type: "object",
|
|
638
|
+
properties: {
|
|
639
|
+
device: DEVICE_ID,
|
|
640
|
+
force: { type: "boolean", description: "Delete from the database even if the device does not respond." },
|
|
641
|
+
block: { type: "boolean", description: "Prevent the device from rejoining." },
|
|
642
|
+
clear_cache: { type: "boolean", description: "Discard cached data so a rejoin re-interviews from scratch." },
|
|
643
|
+
confirm: { type: "boolean", description: "Must be true to proceed." },
|
|
644
|
+
},
|
|
645
|
+
required: ["device", "confirm"],
|
|
646
|
+
},
|
|
647
|
+
handler: async (args, ctx) => {
|
|
648
|
+
requireConfirm(args, "Removing a device");
|
|
649
|
+
const { raw } = await loadDevices(ctx);
|
|
650
|
+
const target = findRaw(raw, String(args.device));
|
|
651
|
+
return ctx.client.request("device/remove", {
|
|
652
|
+
id: target.friendly_name,
|
|
653
|
+
force: args.force === true,
|
|
654
|
+
block: args.block === true,
|
|
655
|
+
clear_cache: args.clear_cache === true,
|
|
656
|
+
}, 60_000);
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
name: "z2m_ota_update",
|
|
661
|
+
tier: "full",
|
|
662
|
+
description: "Flash a device's firmware over the air. This can take many minutes, must not be interrupted, and can " +
|
|
663
|
+
"brick the device if it loses power. Check availability with z2m_check_updates first. Requires confirm=true.",
|
|
664
|
+
inputSchema: {
|
|
665
|
+
type: "object",
|
|
666
|
+
properties: {
|
|
667
|
+
device: DEVICE_ID,
|
|
668
|
+
action: {
|
|
669
|
+
type: "string",
|
|
670
|
+
enum: ["update", "abort", "schedule", "unschedule"],
|
|
671
|
+
description: "Default 'update'. 'schedule' defers the flash until the device next checks in.",
|
|
672
|
+
},
|
|
673
|
+
timeout_ms: { type: "number", description: "Default 1800000 (30 minutes)." },
|
|
674
|
+
confirm: { type: "boolean", description: "Must be true to proceed." },
|
|
675
|
+
},
|
|
676
|
+
required: ["device", "confirm"],
|
|
677
|
+
},
|
|
678
|
+
handler: async (args, ctx) => {
|
|
679
|
+
requireConfirm(args, "Flashing firmware");
|
|
680
|
+
const { raw } = await loadDevices(ctx);
|
|
681
|
+
const target = findRaw(raw, String(args.device));
|
|
682
|
+
const action = String(args.action ?? "update");
|
|
683
|
+
const topic = action === "abort"
|
|
684
|
+
? "device/ota_update/update/abort"
|
|
685
|
+
: action === "schedule"
|
|
686
|
+
? "device/ota_update/schedule"
|
|
687
|
+
: action === "unschedule"
|
|
688
|
+
? "device/ota_update/unschedule"
|
|
689
|
+
: "device/ota_update/update";
|
|
690
|
+
return ctx.client.request(topic, { id: target.friendly_name }, Number(args.timeout_ms ?? 1_800_000));
|
|
691
|
+
},
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
name: "z2m_restart_bridge",
|
|
695
|
+
tier: "full",
|
|
696
|
+
description: "Restart Zigbee2MQTT. Briefly interrupts all Zigbee control. Needed after changing options that report " +
|
|
697
|
+
"restart_required=true.",
|
|
698
|
+
inputSchema: {
|
|
699
|
+
type: "object",
|
|
700
|
+
properties: { confirm: { type: "boolean", description: "Must be true to proceed." } },
|
|
701
|
+
required: ["confirm"],
|
|
702
|
+
},
|
|
703
|
+
handler: async (args, ctx) => {
|
|
704
|
+
requireConfirm(args, "Restarting Zigbee2MQTT");
|
|
705
|
+
return ctx.client.request("restart", {}, 30_000);
|
|
706
|
+
},
|
|
707
|
+
},
|
|
708
|
+
{
|
|
709
|
+
name: "z2m_set_bridge_options",
|
|
710
|
+
tier: "full",
|
|
711
|
+
description: "Change Zigbee2MQTT configuration at runtime, e.g. log level, availability or last_seen. " +
|
|
712
|
+
"The config schema is available via z2m_bridge_info with include_config=true. Some changes require a restart.",
|
|
713
|
+
inputSchema: {
|
|
714
|
+
type: "object",
|
|
715
|
+
properties: {
|
|
716
|
+
options: {
|
|
717
|
+
type: "object",
|
|
718
|
+
description: 'Nested config fragment, e.g. {"advanced": {"last_seen": "ISO_8601"}}.',
|
|
719
|
+
},
|
|
720
|
+
confirm: { type: "boolean", description: "Must be true to proceed." },
|
|
721
|
+
},
|
|
722
|
+
required: ["options", "confirm"],
|
|
723
|
+
},
|
|
724
|
+
handler: async (args, ctx) => {
|
|
725
|
+
requireConfirm(args, "Changing bridge configuration");
|
|
726
|
+
return ctx.client.request("options", { options: args.options }, 30_000);
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
{
|
|
730
|
+
name: "z2m_coordinator_check",
|
|
731
|
+
tier: "full",
|
|
732
|
+
description: "Check whether any routers are missing from the coordinator's memory, which causes pairing failures and " +
|
|
733
|
+
"devices dropping off. Only supported on Texas Instruments adapters (CC2652/CC1352); other adapters " +
|
|
734
|
+
"will return an error.",
|
|
735
|
+
inputSchema: EMPTY,
|
|
736
|
+
handler: async (_args, ctx) => ctx.client.request("coordinator_check", {}, 60_000),
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
name: "z2m_touchlink",
|
|
740
|
+
tier: "full",
|
|
741
|
+
description: "Touchlink scan, identify or factory reset. Factory reset affects the physically nearest Touchlink " +
|
|
742
|
+
"device, which may not be the one you intend. Requires confirm=true.",
|
|
743
|
+
inputSchema: {
|
|
744
|
+
type: "object",
|
|
745
|
+
properties: {
|
|
746
|
+
action: { type: "string", enum: ["scan", "identify", "factory_reset"] },
|
|
747
|
+
ieee_address: { type: "string", description: "Required for identify and targeted factory_reset." },
|
|
748
|
+
channel: { type: "number", description: "Required for identify and targeted factory_reset." },
|
|
749
|
+
confirm: { type: "boolean", description: "Must be true to proceed." },
|
|
750
|
+
},
|
|
751
|
+
required: ["action", "confirm"],
|
|
752
|
+
},
|
|
753
|
+
handler: async (args, ctx) => {
|
|
754
|
+
requireConfirm(args, "Touchlink");
|
|
755
|
+
const payload = args.ieee_address && args.channel !== undefined
|
|
756
|
+
? { ieee_address: String(args.ieee_address), channel: Number(args.channel) }
|
|
757
|
+
: {};
|
|
758
|
+
return ctx.client.request(`touchlink/${String(args.action)}`, payload, 120_000);
|
|
759
|
+
},
|
|
760
|
+
},
|
|
761
|
+
];
|
|
762
|
+
const TIER_RANK = { read: 0, safe: 1, full: 2 };
|
|
763
|
+
export function selectTools(mode) {
|
|
764
|
+
const all = [...readTools, ...safeTools, ...fullTools];
|
|
765
|
+
if (mode === "off")
|
|
766
|
+
return all.filter((t) => t.tier === "read");
|
|
767
|
+
const max = TIER_RANK[mode];
|
|
768
|
+
return all.filter((t) => TIER_RANK[t.tier] <= max);
|
|
769
|
+
}
|
|
770
|
+
//# sourceMappingURL=tools.js.map
|