@orellbuehler/homeassistant-mcp 1.0.1 → 1.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.
Files changed (40) hide show
  1. package/README.md +142 -39
  2. package/bin/homeassistant-mcp.js +51 -0
  3. package/binaries/darwin-arm64/homeassistant-mcp +0 -0
  4. package/binaries/darwin-x64/homeassistant-mcp +0 -0
  5. package/binaries/linux-arm64/homeassistant-mcp +0 -0
  6. package/binaries/linux-x64/homeassistant-mcp +0 -0
  7. package/binaries/win32-x64/homeassistant-mcp.exe +0 -0
  8. package/package.json +5 -38
  9. package/dist/config.js +0 -26
  10. package/dist/hass/format.js +0 -38
  11. package/dist/hass/rest.js +0 -50
  12. package/dist/hass/ws.js +0 -80
  13. package/dist/index.js +0 -17
  14. package/dist/server.js +0 -9
  15. package/dist/tools/auth.js +0 -191
  16. package/dist/tools/automations.js +0 -62
  17. package/dist/tools/backup.js +0 -375
  18. package/dist/tools/control.js +0 -133
  19. package/dist/tools/dashboards.js +0 -206
  20. package/dist/tools/diagnostics.js +0 -537
  21. package/dist/tools/energy.js +0 -235
  22. package/dist/tools/entities.js +0 -57
  23. package/dist/tools/hacs.js +0 -52
  24. package/dist/tools/helpers.js +0 -98
  25. package/dist/tools/history.js +0 -57
  26. package/dist/tools/integrations.js +0 -538
  27. package/dist/tools/lists.js +0 -216
  28. package/dist/tools/organization.js +0 -548
  29. package/dist/tools/raw.js +0 -41
  30. package/dist/tools/registry.js +0 -195
  31. package/dist/tools/reload.js +0 -40
  32. package/dist/tools/resources.js +0 -56
  33. package/dist/tools/scripts.js +0 -206
  34. package/dist/tools/services.js +0 -33
  35. package/dist/tools/statistics.js +0 -363
  36. package/dist/tools/system.js +0 -57
  37. package/dist/tools/templates.js +0 -21
  38. package/dist/tools/trace.js +0 -74
  39. package/dist/tools/zha.js +0 -121
  40. package/dist/toolsets.js +0 -356
@@ -1,538 +0,0 @@
1
- import { z } from "zod";
2
- import { ok, err, buildQS } from "../hass/format.js";
3
- const INTEGRATION_TYPES = [
4
- "device",
5
- "entity",
6
- "hardware",
7
- "helper",
8
- "hub",
9
- "service",
10
- "system",
11
- "virtual",
12
- ];
13
- const FLOW_PATHS = {
14
- config: "/api/config/config_entries/flow",
15
- options: "/api/config/config_entries/options/flow",
16
- subentry: "/api/config/config_entries/subentries/flow",
17
- };
18
- const flowTypeSchema = z
19
- .enum(["config", "options", "subentry"])
20
- .optional()
21
- .describe("Which kind of flow flow_id belongs to: 'config' (default; start_config_flow or a flow from list_config_flows_in_progress), 'options' (start_options_flow) or 'subentry' (start_subentry_flow)");
22
- function entryPath(entryId) {
23
- return `/api/config/config_entries/entry/${encodeURIComponent(entryId)}`;
24
- }
25
- function flowPath(flowType, flowId) {
26
- return `${FLOW_PATHS[flowType ?? "config"]}/${encodeURIComponent(flowId)}`;
27
- }
28
- function summarizeEntry(e) {
29
- const row = {
30
- entry_id: e.entry_id,
31
- domain: e.domain,
32
- title: e.title ?? null,
33
- state: e.state ?? null,
34
- source: e.source ?? null,
35
- disabled_by: e.disabled_by ?? null,
36
- reason: e.reason ?? null,
37
- supports_options: e.supports_options ?? false,
38
- supports_reconfigure: e.supports_reconfigure ?? false,
39
- supports_unload: e.supports_unload ?? false,
40
- supports_remove_device: e.supports_remove_device ?? false,
41
- num_subentries: e.num_subentries ?? 0,
42
- pref_disable_new_entities: e.pref_disable_new_entities ?? false,
43
- pref_disable_polling: e.pref_disable_polling ?? false,
44
- };
45
- if (e.supported_subentry_types && Object.keys(e.supported_subentry_types).length > 0) {
46
- row.supported_subentry_types = e.supported_subentry_types;
47
- }
48
- if (e.error_reason_translation_key) {
49
- row.error_reason_translation_key = e.error_reason_translation_key;
50
- row.error_reason_translation_domain = e.error_reason_translation_domain ?? null;
51
- row.error_reason_translation_placeholders = e.error_reason_translation_placeholders ?? null;
52
- }
53
- return row;
54
- }
55
- function summarizeFlow(f) {
56
- return {
57
- flow_id: f.flow_id,
58
- handler: f.handler,
59
- step_id: f.step_id ?? null,
60
- source: f.context?.source ?? null,
61
- unique_id: f.context?.unique_id ?? null,
62
- title_placeholders: f.context?.title_placeholders ?? null,
63
- entry_id: f.context?.entry_id ?? null,
64
- };
65
- }
66
- function summarizeManifest(m) {
67
- return {
68
- domain: m.domain,
69
- name: m.name ?? null,
70
- integration_type: m.integration_type ?? "hub",
71
- config_flow: m.config_flow ?? false,
72
- iot_class: m.iot_class ?? null,
73
- version: m.version ?? null,
74
- is_built_in: m.is_built_in ?? null,
75
- documentation: m.documentation ?? null,
76
- };
77
- }
78
- function summarizeCredential(c) {
79
- return {
80
- id: c.id,
81
- domain: c.domain,
82
- client_id: c.client_id,
83
- auth_domain: c.auth_domain ?? null,
84
- name: c.name ?? null,
85
- };
86
- }
87
- function summarizeDiagnostics(d) {
88
- return {
89
- domain: d.domain,
90
- config_entry: d.handlers?.config_entry ?? false,
91
- device: d.handlers?.device ?? false,
92
- };
93
- }
94
- export function registerIntegrationTools(server, ws, client) {
95
- server.tool("list_config_entries", "List config entries — the configured integrations — via the WebSocket API (config_entries/get). Each row has entry_id, domain, title, state ('loaded', 'setup_error', 'setup_retry', 'not_loaded', 'migration_error', 'failed_unload', ...), source ('user', 'zeroconf', 'ignore', ...), disabled_by, reason (the error text when setup failed), supports_options / supports_reconfigure / supports_unload / supports_remove_device, num_subentries, supported_subentry_types and the pref_disable_new_entities / pref_disable_polling flags. The entry's data and options (which can hold credentials) are never included. Filter by integration domain, integration type and/or state, e.g. state 'setup_error' to find broken integrations. Use the entry_id with get_config_entry, reload_config_entry, set_config_entry_enabled, start_options_flow and the other entry tools.", {
96
- domain: z.string().optional().describe("Only entries of this integration domain, e.g. 'hue'"),
97
- type_filter: z
98
- .array(z.enum(INTEGRATION_TYPES))
99
- .optional()
100
- .describe("Only entries whose integration has one of these types, e.g. ['helper'] for UI-created helpers"),
101
- state: z
102
- .string()
103
- .optional()
104
- .describe("Only entries in this state, e.g. 'loaded', 'setup_error', 'not_loaded'"),
105
- }, async ({ domain, type_filter, state }) => {
106
- try {
107
- const payload = {};
108
- if (domain)
109
- payload.domain = domain;
110
- if (type_filter && type_filter.length > 0)
111
- payload.type_filter = type_filter;
112
- const list = (await ws.command("config_entries/get", payload));
113
- const rows = state ? list.filter((e) => e.state === state) : list;
114
- const entries = rows.map(summarizeEntry);
115
- return ok({ count: entries.length, entries });
116
- }
117
- catch (e) {
118
- return err(e);
119
- }
120
- });
121
- server.tool("get_config_entry", "Get one config entry by entry_id via the WebSocket API (config_entries/get_single; requires an admin token). Returns { config_entry } with the full record: domain, title, state, source, disabled_by, reason and its translation fields, supports_* flags, supported_subentry_types, num_subentries, the pref_* flags and created_at / modified_at. The entry's data and options are not included. Use it to check the outcome after reload_config_entry or set_config_entry_enabled.", {
122
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
123
- }, async ({ entry_id }) => {
124
- try {
125
- return ok(await ws.command("config_entries/get_single", { entry_id }));
126
- }
127
- catch (e) {
128
- return err(e);
129
- }
130
- });
131
- server.tool("list_config_subentries", "List the subentries of a config entry via the WebSocket API (config_entries/subentries/list; requires an admin token). Subentries are child items some integrations keep under one entry (e.g. conversation agents or AI tasks). Each row has subentry_id, subentry_type, title and unique_id. The subentry types an entry can add are in supported_subentry_types from list_config_entries.", {
132
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
133
- }, async ({ entry_id }) => {
134
- try {
135
- const list = (await ws.command("config_entries/subentries/list", {
136
- entry_id,
137
- }));
138
- const subentries = list.map((s) => ({
139
- subentry_id: s.subentry_id,
140
- subentry_type: s.subentry_type ?? null,
141
- title: s.title ?? null,
142
- unique_id: s.unique_id ?? null,
143
- }));
144
- return ok({ count: subentries.length, subentries });
145
- }
146
- catch (e) {
147
- return err(e);
148
- }
149
- });
150
- server.tool("update_config_entry", "Update a config entry's title and/or preference flags via the WebSocket API (config_entries/update; requires an admin token). 'title' renames the entry. 'pref_disable_new_entities' makes entities the integration adds later start out disabled. 'pref_disable_polling' stops the integration from polling for updates; changing it reloads a loaded entry. At least one field is required. This does not edit the integration's own settings — use start_options_flow or start_config_flow with entry_id for that. Returns { config_entry, require_restart }.", {
151
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
152
- title: z.string().optional().describe("New entry title"),
153
- pref_disable_new_entities: z
154
- .boolean()
155
- .optional()
156
- .describe("true to disable newly added entities by default"),
157
- pref_disable_polling: z
158
- .boolean()
159
- .optional()
160
- .describe("true to stop the integration from polling for updates"),
161
- }, async ({ entry_id, title, pref_disable_new_entities, pref_disable_polling }) => {
162
- try {
163
- if (title === undefined &&
164
- pref_disable_new_entities === undefined &&
165
- pref_disable_polling === undefined) {
166
- throw new Error("Provide at least one of 'title', 'pref_disable_new_entities' or 'pref_disable_polling' to update the config entry");
167
- }
168
- const payload = { entry_id };
169
- if (title !== undefined)
170
- payload.title = title;
171
- if (pref_disable_new_entities !== undefined) {
172
- payload.pref_disable_new_entities = pref_disable_new_entities;
173
- }
174
- if (pref_disable_polling !== undefined)
175
- payload.pref_disable_polling = pref_disable_polling;
176
- return ok(await ws.command("config_entries/update", payload));
177
- }
178
- catch (e) {
179
- return err(e);
180
- }
181
- });
182
- server.tool("set_config_entry_enabled", "Enable or disable a config entry via the WebSocket API (config_entries/disable with disabled_by = null to enable, 'user' to disable; requires an admin token). Disabling unloads the integration instance and disables its devices and entities; enabling sets it up again. 'require_restart' is true when Home Assistant could not unload it or set it up without a restart. Setup can still fail without require_restart (the entry ends in 'setup_error' or 'setup_retry'), so check state and reason with get_config_entry afterwards.", {
183
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
184
- enabled: z
185
- .boolean()
186
- .describe("true to enable (disabled_by null), false to disable (disabled_by 'user')"),
187
- }, async ({ entry_id, enabled }) => {
188
- try {
189
- const res = (await ws.command("config_entries/disable", {
190
- entry_id,
191
- disabled_by: enabled ? null : "user",
192
- }));
193
- return ok({ entry_id, enabled, require_restart: res?.require_restart === true });
194
- }
195
- catch (e) {
196
- return err(e);
197
- }
198
- });
199
- server.tool("reload_config_entry", "Reload one config entry — unload and set it up again — via POST /api/config/config_entries/entry/{entry_id}/reload (requires an admin token). Use it to make an integration pick up changed files, or to retry one in 'setup_error' / 'setup_retry'. A disabled entry is not set up again. 'require_restart' is true when the entry ended in a state that cannot be recovered without restarting Home Assistant. The response does NOT say whether setup succeeded: read state and reason with get_config_entry afterwards. Fails with 403 'Entry cannot be reloaded' when Home Assistant refuses (e.g. state 'migration_error' or 'failed_unload') and 404 for an unknown entry.", {
200
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
201
- }, async ({ entry_id }) => {
202
- try {
203
- const res = (await client.fetch(`${entryPath(entry_id)}/reload`, {
204
- method: "POST",
205
- }));
206
- return ok({ entry_id, require_restart: res?.require_restart === true });
207
- }
208
- catch (e) {
209
- return err(e);
210
- }
211
- });
212
- server.tool("delete_config_entry", "Permanently delete a config entry via DELETE /api/config/config_entries/entry/{entry_id} (requires an admin token). Home Assistant unloads it, runs the integration's removal cleanup and removes the entry's devices, entities and repair issues from the registries. This cannot be undone — to stop an integration temporarily use set_config_entry_enabled instead. It is also how to un-ignore an ignored discovery (an entry with source 'ignore'). 'require_restart' is true when the entry could not be unloaded cleanly. 404 for an unknown entry.", {
213
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
214
- }, async ({ entry_id }) => {
215
- try {
216
- const res = (await client.fetch(entryPath(entry_id), { method: "DELETE" }));
217
- return ok({ entry_id, deleted: true, require_restart: res?.require_restart === true });
218
- }
219
- catch (e) {
220
- return err(e);
221
- }
222
- });
223
- server.tool("ignore_config_flow", "Ignore a discovered device or service via the WebSocket API (config_entries/ignore_flow; requires an admin token). Takes a flow from list_config_flows_in_progress, creates a config entry with source 'ignore' and the given title so the discovery is no longer offered, and removes the flow. Only works for flows that have a unique_id. Undo it with delete_config_entry on the ignored entry. To merely dismiss a flow for now use abort_config_flow.", {
224
- flow_id: z.string().describe("flow_id from list_config_flows_in_progress"),
225
- title: z.string().describe("Title to give the ignored entry, e.g. the device name"),
226
- }, async ({ flow_id, title }) => {
227
- try {
228
- await ws.command("config_entries/ignore_flow", { flow_id, title });
229
- return ok({ flow_id, title, ignored: true });
230
- }
231
- catch (e) {
232
- return err(e);
233
- }
234
- });
235
- server.tool("rename_config_subentry", "Rename a subentry of a config entry via the WebSocket API (config_entries/subentries/update; requires an admin token). Only the title can be changed here; use start_subentry_flow with subentry_id to reconfigure a subentry's settings. Get subentry ids from list_config_subentries.", {
236
- entry_id: z.string().describe("Config entry id that owns the subentry"),
237
- subentry_id: z.string().describe("Subentry id, as returned by list_config_subentries"),
238
- title: z.string().describe("New subentry title"),
239
- }, async ({ entry_id, subentry_id, title }) => {
240
- try {
241
- await ws.command("config_entries/subentries/update", { entry_id, subentry_id, title });
242
- return ok({ entry_id, subentry_id, title });
243
- }
244
- catch (e) {
245
- return err(e);
246
- }
247
- });
248
- server.tool("delete_config_subentry", "Delete a subentry of a config entry via the WebSocket API (config_entries/subentries/delete; requires an admin token). The integration is updated to drop it (its entities and devices go with it) and this cannot be undone. Get subentry ids from list_config_subentries.", {
249
- entry_id: z.string().describe("Config entry id that owns the subentry"),
250
- subentry_id: z.string().describe("Subentry id, as returned by list_config_subentries"),
251
- }, async ({ entry_id, subentry_id }) => {
252
- try {
253
- await ws.command("config_entries/subentries/delete", { entry_id, subentry_id });
254
- return ok({ entry_id, subentry_id, deleted: true });
255
- }
256
- catch (e) {
257
- return err(e);
258
- }
259
- });
260
- server.tool("list_config_flow_handlers", "List the integration domains that can be set up through a config flow, via GET /api/config/config_entries/flow_handlers — the valid 'handler' values for start_config_flow. Includes custom integrations that declare a config_flow. Returns a sorted { count, handlers } list of domains. The full list has close to a thousand entries, so narrow it with a case-insensitive substring search, or with helpers_only for the UI helpers (the route's type=helper filter; Home Assistant's other type values currently fail with a server error, so they are not exposed).", {
261
- search: z
262
- .string()
263
- .optional()
264
- .describe("Only domains containing this text (case-insensitive), e.g. 'hue'"),
265
- helpers_only: z
266
- .boolean()
267
- .optional()
268
- .describe("true to list only the UI helper integrations (template, group, derivative, ...)"),
269
- }, async ({ search, helpers_only }) => {
270
- try {
271
- const list = (await client.fetch(`/api/config/config_entries/flow_handlers${buildQS({ type: helpers_only ? "helper" : undefined })}`));
272
- const needle = search?.toLowerCase();
273
- const handlers = [...list]
274
- .filter((h) => !needle || h.toLowerCase().includes(needle))
275
- .sort();
276
- return ok({ count: handlers.length, handlers });
277
- }
278
- catch (e) {
279
- return err(e);
280
- }
281
- });
282
- server.tool("list_config_flows_in_progress", "List config flows waiting for input that Home Assistant started itself, via the WebSocket API (config_entries/flow/progress; requires an admin token): discovered devices and services ('zeroconf', 'ssdp', 'dhcp', 'bluetooth', ...), reauth requests for broken credentials and similar. Flows you started with start_config_flow or as reconfigure are not listed — keep their flow_id from the start response. Each row has flow_id, handler (the integration domain), step_id, source, unique_id, title_placeholders (usually the device name) and entry_id (for reauth). Continue one with get_config_flow_state and submit_config_flow_step, dismiss it with abort_config_flow, or hide it for good with ignore_config_flow.", {}, async () => {
283
- try {
284
- const list = (await ws.command("config_entries/flow/progress"));
285
- const flows = list.map(summarizeFlow);
286
- return ok({ count: flows.length, flows });
287
- }
288
- catch (e) {
289
- return err(e);
290
- }
291
- });
292
- server.tool("start_config_flow", "Start a config flow via POST /api/config/config_entries/flow (requires an admin token) — the first step of adding an integration, or of reconfiguring an existing entry when 'entry_id' is set. 'handler' is the integration domain (see list_config_flow_handlers). Returns the first step as Home Assistant sends it: { type, flow_id, step_id, data_schema (list of fields), errors, description_placeholders, ... }. Follow it with submit_config_flow_step using the flow_id: type 'form' needs user_input keyed by the data_schema field names, 'menu' needs { next_step_id } chosen from menu_options, 'progress' is polled with get_config_flow_state, 'external' needs the user to open its 'url' in a browser, 'create_entry' means the integration was added (the new entry is in 'result'; a 'next_flow' [flow type, flow_id] means another flow must be continued), and 'abort' ends the flow without changes ('reason' says why). The flow lives in memory until it ends, so abort_config_flow any you abandon. Setting up an integration can contact devices and cloud services and creates a config entry. Reauth flows cannot be started here; Home Assistant creates them (list_config_flows_in_progress). 404 for an unknown handler.", {
293
- handler: z.string().describe("Integration domain to set up, e.g. 'met' or 'template'"),
294
- entry_id: z
295
- .string()
296
- .optional()
297
- .describe("Existing config entry id to reconfigure (handler must be that entry's domain; the integration must support reconfigure); omit to add a new entry"),
298
- }, async ({ handler, entry_id }) => {
299
- try {
300
- const body = { handler };
301
- if (entry_id)
302
- body.entry_id = entry_id;
303
- return ok(await client.fetch(FLOW_PATHS.config, { method: "POST", body: JSON.stringify(body) }));
304
- }
305
- catch (e) {
306
- return err(e);
307
- }
308
- });
309
- server.tool("start_options_flow", "Start the options flow of a config entry via POST /api/config/config_entries/options/flow (requires an admin token) — the integration's 'Configure' dialog, for entries with supports_options true in list_config_entries. Returns the first step in the same shape as start_config_flow. Continue it with submit_config_flow_step, get_config_flow_state and abort_config_flow, passing flow_type 'options' every time. When the flow ends with type 'create_entry' the submitted data becomes the entry's options (and some integrations reload the entry). Abort any flow you abandon.", {
310
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
311
- }, async ({ entry_id }) => {
312
- try {
313
- return ok(await client.fetch(FLOW_PATHS.options, {
314
- method: "POST",
315
- body: JSON.stringify({ handler: entry_id }),
316
- }));
317
- }
318
- catch (e) {
319
- return err(e);
320
- }
321
- });
322
- server.tool("start_subentry_flow", "Start a subentry flow via POST /api/config/config_entries/subentries/flow (requires an admin token) — adds a subentry of the given type to a config entry, or reconfigures one when 'subentry_id' is set. Valid subentry types are the keys of supported_subentry_types in list_config_entries. Returns the first step in the same shape as start_config_flow. Continue it with submit_config_flow_step, get_config_flow_state and abort_config_flow, passing flow_type 'subentry' every time. A 'create_entry' result adds the subentry. Abort any flow you abandon.", {
323
- entry_id: z.string().describe("Config entry id that owns the subentry"),
324
- subentry_type: z
325
- .string()
326
- .describe("Subentry type to add or reconfigure, from supported_subentry_types"),
327
- subentry_id: z
328
- .string()
329
- .optional()
330
- .describe("Existing subentry id to reconfigure; omit to add a new subentry"),
331
- }, async ({ entry_id, subentry_type, subentry_id }) => {
332
- try {
333
- const body = { handler: [entry_id, subentry_type] };
334
- if (subentry_id)
335
- body.subentry_id = subentry_id;
336
- return ok(await client.fetch(FLOW_PATHS.subentry, {
337
- method: "POST",
338
- body: JSON.stringify(body),
339
- }));
340
- }
341
- catch (e) {
342
- return err(e);
343
- }
344
- });
345
- server.tool("get_config_flow_state", "Get the current step of a flow in progress via GET on the flow route (/api/config/config_entries/flow/{flow_id}, .../options/flow/{flow_id} or .../subentries/flow/{flow_id} by flow_type; requires an admin token). Home Assistant re-runs the current step with no input and returns it, so a form comes back with its data_schema again and a 'progress' step is polled; some integrations repeat discovery work on every call. Same shape as start_config_flow. Use it to resume a flow from list_config_flows_in_progress or to wait on a progress step. 404 when the flow does not exist (finished, aborted, or wrong flow_type).", {
346
- flow_id: z
347
- .string()
348
- .describe("flow_id from a start_*_flow result or list_config_flows_in_progress"),
349
- flow_type: flowTypeSchema,
350
- }, async ({ flow_id, flow_type }) => {
351
- try {
352
- return ok(await client.fetch(flowPath(flow_type, flow_id)));
353
- }
354
- catch (e) {
355
- return err(e);
356
- }
357
- });
358
- server.tool("submit_config_flow_step", "Submit input for the current step of a flow in progress via POST on the flow route (/api/config/config_entries/flow/{flow_id}, .../options/flow/{flow_id} or .../subentries/flow/{flow_id} by flow_type; requires an admin token). 'user_input' is an object keyed by the field names in the step's data_schema; omit it (or send {}) for a step without fields, such as a confirmation. For a 'menu' step send { next_step_id: <one of menu_options> }. Returns the next step in the same shape as start_config_flow, or type 'create_entry' / 'abort' when the flow ends — a 'create_entry' applies the change to Home Assistant. Invalid input fails with 400 and { errors: { field: message } }; 404 means the flow no longer exists or flow_type is wrong.", {
359
- flow_id: z
360
- .string()
361
- .describe("flow_id from a start_*_flow result or list_config_flows_in_progress"),
362
- user_input: z
363
- .record(z.unknown())
364
- .optional()
365
- .describe("Field values for the step's data_schema, or { next_step_id } for a menu step; omit for steps without fields"),
366
- flow_type: flowTypeSchema,
367
- }, async ({ flow_id, user_input, flow_type }) => {
368
- try {
369
- return ok(await client.fetch(flowPath(flow_type, flow_id), {
370
- method: "POST",
371
- body: JSON.stringify(user_input ?? {}),
372
- }));
373
- }
374
- catch (e) {
375
- return err(e);
376
- }
377
- });
378
- server.tool("abort_config_flow", "Abort a flow in progress via DELETE on the flow route (/api/config/config_entries/flow/{flow_id}, .../options/flow/{flow_id} or .../subentries/flow/{flow_id} by flow_type). Cancels it without creating or changing anything — use it for flows you started and no longer need, or to dismiss a discovery for now (the device may be offered again; ignore_config_flow hides it permanently). 404 when the flow does not exist.", {
379
- flow_id: z
380
- .string()
381
- .describe("flow_id from a start_*_flow result or list_config_flows_in_progress"),
382
- flow_type: flowTypeSchema,
383
- }, async ({ flow_id, flow_type }) => {
384
- try {
385
- await client.fetch(flowPath(flow_type, flow_id), { method: "DELETE" });
386
- return ok({ flow_id, aborted: true });
387
- }
388
- catch (e) {
389
- return err(e);
390
- }
391
- });
392
- server.tool("list_integration_manifests", "List integration manifests via the WebSocket API (manifest/list), trimmed to domain, name, integration_type (defaults to 'hub'), config_flow, iot_class, version, is_built_in and documentation. By default covers the integrations currently loaded in Home Assistant; pass 'integrations' to look up specific domains whether or not they are loaded (unknown domains are silently left out). 'custom_only' keeps only custom integrations (is_built_in false). Use get_integration_manifest for the full manifest of one integration.", {
393
- integrations: z
394
- .array(z.string())
395
- .optional()
396
- .describe("Only these integration domains, e.g. ['hue', 'zha']"),
397
- custom_only: z
398
- .boolean()
399
- .optional()
400
- .describe("true to list only custom integrations from custom_components"),
401
- }, async ({ integrations, custom_only }) => {
402
- try {
403
- const payload = {};
404
- if (integrations && integrations.length > 0)
405
- payload.integrations = integrations;
406
- const list = (await ws.command("manifest/list", payload));
407
- const rows = custom_only ? list.filter((m) => m.is_built_in === false) : list;
408
- const manifests = rows.map(summarizeManifest);
409
- return ok({ count: manifests.length, manifests });
410
- }
411
- catch (e) {
412
- return err(e);
413
- }
414
- });
415
- server.tool("get_integration_manifest", "Get the full manifest of one integration via the WebSocket API (manifest/get): name, documentation and issue tracker links, integration_type, iot_class, config_flow, requirements, dependencies, after_dependencies, codeowners, quality_scale, discovery matchers (zeroconf, ssdp, dhcp, bluetooth, usb, ...), version for custom integrations and is_built_in. Fails with 'Integration not found' for an unknown domain.", {
416
- integration: z.string().describe("Integration domain, e.g. 'hue'"),
417
- }, async ({ integration }) => {
418
- try {
419
- return ok(await ws.command("manifest/get", { integration }));
420
- }
421
- catch (e) {
422
- return err(e);
423
- }
424
- });
425
- server.tool("list_integration_setup_times", "List how long each integration took to set up since Home Assistant started, via the WebSocket API (integration/setup_info), slowest first, as { domain, seconds }. Useful for finding the integrations that slow down startup. Returns { total, count, integrations }; 'limit' keeps only the slowest N.", {
426
- limit: z.number().int().min(1).optional().describe("Only the N slowest integrations"),
427
- }, async ({ limit }) => {
428
- try {
429
- const list = (await ws.command("integration/setup_info"));
430
- const sorted = [...list].sort((a, b) => b.seconds - a.seconds);
431
- const integrations = limit ? sorted.slice(0, limit) : sorted;
432
- return ok({ total: sorted.length, count: integrations.length, integrations });
433
- }
434
- catch (e) {
435
- return err(e);
436
- }
437
- });
438
- server.tool("list_application_credential_integrations", "List the integrations that accept OAuth application credentials (a client ID and client secret you register with the service) via the WebSocket API (application_credentials/config). Returns { domains, integrations } where integrations maps each domain to its description_placeholders (links and hints for obtaining credentials from the service, when the integration provides them). Add credentials with create_application_credential before starting that integration's config flow.", {}, async () => {
439
- try {
440
- return ok(await ws.command("application_credentials/config"));
441
- }
442
- catch (e) {
443
- return err(e);
444
- }
445
- });
446
- server.tool("list_application_credentials", "List stored OAuth application credentials via the WebSocket API (application_credentials/list; requires an admin token). Each row has id, domain, client_id, auth_domain and name. The client_secret is deliberately never returned. Use the id with delete_application_credential.", {}, async () => {
447
- try {
448
- const list = (await ws.command("application_credentials/list"));
449
- const credentials = list.map(summarizeCredential);
450
- return ok({ count: credentials.length, credentials });
451
- }
452
- catch (e) {
453
- return err(e);
454
- }
455
- });
456
- server.tool("get_config_entry_application_credential", "Find which stored application credential a config entry uses, via the WebSocket API (application_credentials/config_entry; requires an admin token). Returns { application_credentials_id } (the id as listed by list_application_credentials), or an empty object when the entry does not use one. Fails when the config entry does not exist.", {
457
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
458
- }, async ({ entry_id }) => {
459
- try {
460
- return ok(await ws.command("application_credentials/config_entry", { config_entry_id: entry_id }));
461
- }
462
- catch (e) {
463
- return err(e);
464
- }
465
- });
466
- server.tool("create_application_credential", "Store an OAuth application credential for an integration via the WebSocket API (application_credentials/create; requires an admin token). 'domain' must be an integration listed by list_application_credential_integrations. The client_secret is write-only: it is sent to Home Assistant and never returned or logged by this tool. Home Assistant derives the id from domain and client_id. Existing credentials cannot be edited — delete and recreate them. Returns the stored credential without its secret.", {
467
- domain: z.string().describe("Integration domain, e.g. 'google'"),
468
- client_id: z.string().describe("OAuth client ID from the service's developer console"),
469
- client_secret: z
470
- .string()
471
- .describe("OAuth client secret from the service's developer console"),
472
- name: z.string().optional().describe("Display name for the credential"),
473
- auth_domain: z
474
- .string()
475
- .optional()
476
- .describe("Authentication domain override; leave unset unless the integration requires it"),
477
- }, async ({ domain, client_id, client_secret, name, auth_domain }) => {
478
- try {
479
- const payload = { domain, client_id, client_secret };
480
- if (name !== undefined)
481
- payload.name = name;
482
- if (auth_domain !== undefined)
483
- payload.auth_domain = auth_domain;
484
- const created = (await ws.command("application_credentials/create", payload));
485
- return ok(summarizeCredential(created));
486
- }
487
- catch (e) {
488
- return err(e);
489
- }
490
- });
491
- server.tool("delete_application_credential", "Delete a stored OAuth application credential via the WebSocket API (application_credentials/delete; requires an admin token). Home Assistant refuses to delete a credential that a config entry still uses — remove or reconfigure that entry first (get_config_entry_application_credential finds the link). Get ids from list_application_credentials.", {
492
- credential_id: z
493
- .string()
494
- .describe("Application credential id, as returned by list_application_credentials"),
495
- }, async ({ credential_id }) => {
496
- try {
497
- await ws.command("application_credentials/delete", {
498
- application_credentials_id: credential_id,
499
- });
500
- return ok({ credential_id, deleted: true });
501
- }
502
- catch (e) {
503
- return err(e);
504
- }
505
- });
506
- server.tool("list_diagnostics_handlers", "List which integrations provide diagnostics via the WebSocket API (diagnostics/list, or diagnostics/get when 'domain' is set; requires an admin token). Each row has domain, config_entry (can download diagnostics for a config entry) and device (can download diagnostics for a single device). Use get_diagnostics to download them. With 'domain' fails with 'Domain not supported' when the integration provides none.", {
507
- domain: z.string().optional().describe("Check just this integration domain, e.g. 'hue'"),
508
- }, async ({ domain }) => {
509
- try {
510
- if (domain) {
511
- const info = (await ws.command("diagnostics/get", { domain }));
512
- return ok(summarizeDiagnostics(info));
513
- }
514
- const list = (await ws.command("diagnostics/list"));
515
- const integrations = list.map(summarizeDiagnostics);
516
- return ok({ count: integrations.length, integrations });
517
- }
518
- catch (e) {
519
- return err(e);
520
- }
521
- });
522
- server.tool("get_diagnostics", "Download the diagnostics of a config entry — or of one of its devices when 'device_id' is set — via GET /api/diagnostics/config_entry/{entry_id}[/device/{device_id}] (requires an admin token). Returns the diagnostics file Home Assistant offers as a download: home_assistant system info, custom_components, integration_manifest, setup_times, the integration's own 'data', and open 'issues'. The integration decides what is redacted, so treat it as sensitive, and it can be very large. 404 when the entry or device does not exist, the device does not belong to the entry, or the integration has no diagnostics for it (see list_diagnostics_handlers).", {
523
- entry_id: z.string().describe("Config entry id, as returned by list_config_entries"),
524
- device_id: z
525
- .string()
526
- .optional()
527
- .describe("Device registry id (from list_devices) to download that device's diagnostics instead; the device must belong to the entry"),
528
- }, async ({ entry_id, device_id }) => {
529
- try {
530
- const base = `/api/diagnostics/config_entry/${encodeURIComponent(entry_id)}`;
531
- const path = device_id ? `${base}/device/${encodeURIComponent(device_id)}` : base;
532
- return ok(await client.fetch(path));
533
- }
534
- catch (e) {
535
- return err(e);
536
- }
537
- });
538
- }