@orellbuehler/homeassistant-mcp 1.0.0 → 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.
- package/README.md +142 -39
- package/bin/homeassistant-mcp.js +51 -0
- package/binaries/darwin-arm64/homeassistant-mcp +0 -0
- package/binaries/darwin-x64/homeassistant-mcp +0 -0
- package/binaries/linux-arm64/homeassistant-mcp +0 -0
- package/binaries/linux-x64/homeassistant-mcp +0 -0
- package/binaries/win32-x64/homeassistant-mcp.exe +0 -0
- package/package.json +5 -38
- package/dist/config.js +0 -26
- package/dist/hass/format.js +0 -38
- package/dist/hass/rest.js +0 -50
- package/dist/hass/ws.js +0 -80
- package/dist/index.js +0 -17
- package/dist/server.js +0 -9
- package/dist/tools/auth.js +0 -191
- package/dist/tools/automations.js +0 -62
- package/dist/tools/backup.js +0 -375
- package/dist/tools/control.js +0 -133
- package/dist/tools/dashboards.js +0 -206
- package/dist/tools/diagnostics.js +0 -537
- package/dist/tools/energy.js +0 -235
- package/dist/tools/entities.js +0 -57
- package/dist/tools/hacs.js +0 -52
- package/dist/tools/helpers.js +0 -98
- package/dist/tools/history.js +0 -57
- package/dist/tools/integrations.js +0 -538
- package/dist/tools/lists.js +0 -216
- package/dist/tools/organization.js +0 -548
- package/dist/tools/raw.js +0 -41
- package/dist/tools/registry.js +0 -195
- package/dist/tools/reload.js +0 -40
- package/dist/tools/resources.js +0 -56
- package/dist/tools/scripts.js +0 -206
- package/dist/tools/services.js +0 -33
- package/dist/tools/statistics.js +0 -363
- package/dist/tools/system.js +0 -57
- package/dist/tools/templates.js +0 -21
- package/dist/tools/trace.js +0 -74
- package/dist/tools/zha.js +0 -121
- package/dist/toolsets.js +0 -356
package/dist/tools/auth.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
const SECRET_KEYS = new Set([
|
|
4
|
-
"token",
|
|
5
|
-
"access_token",
|
|
6
|
-
"refresh_token",
|
|
7
|
-
"jwt_key",
|
|
8
|
-
"password",
|
|
9
|
-
"client_secret",
|
|
10
|
-
]);
|
|
11
|
-
const groupIds = z
|
|
12
|
-
.array(z.enum(["system-admin", "system-users", "system-read-only"]))
|
|
13
|
-
.describe("Permission groups: 'system-admin' (administrator), 'system-users' (regular user), 'system-read-only' (read-only)");
|
|
14
|
-
function stripSecrets(value) {
|
|
15
|
-
if (Array.isArray(value))
|
|
16
|
-
return value.map(stripSecrets);
|
|
17
|
-
if (value !== null && typeof value === "object") {
|
|
18
|
-
return Object.fromEntries(Object.entries(value)
|
|
19
|
-
.filter(([key]) => !SECRET_KEYS.has(key))
|
|
20
|
-
.map(([key, inner]) => [key, stripSecrets(inner)]));
|
|
21
|
-
}
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
export function registerAuthTools(server, ws) {
|
|
25
|
-
server.tool("get_current_user", "Get the Home Assistant user this server authenticates as via the WebSocket API (auth/current_user): id, name, is_owner, is_admin, the login credentials it has (auth_provider_type/id) and its multi-factor modules. Use it to confirm the token's identity and whether it is an administrator before attempting config writes.", {}, async () => {
|
|
26
|
-
try {
|
|
27
|
-
return ok(stripSecrets(await ws.command("auth/current_user")));
|
|
28
|
-
}
|
|
29
|
-
catch (e) {
|
|
30
|
-
return err(e);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
server.tool("list_users", "List all Home Assistant users via the WebSocket API (config/auth/list): id, username (null if the user has no username/password login), name, is_owner, is_active, local_only (may only log in from the local network), system_generated (Supervisor, add-on and other internal users), group_ids and credentials types. Passwords and tokens are never part of it. Requires an admin token.", {}, async () => {
|
|
34
|
-
try {
|
|
35
|
-
const list = (stripSecrets(await ws.command("config/auth/list")) ?? []);
|
|
36
|
-
return ok({ count: list.length, users: list });
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
return err(e);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
server.tool("create_user", "Create a Home Assistant user via the WebSocket API (config/auth/create) with a name, optional permission groups and an optional local-only restriction. The new user has NO login credentials yet, so it cannot sign in until a username/password (or another login method) is added in the Home Assistant UI — this server cannot set passwords. Granting 'system-admin' makes the user an administrator. Returns { user }. Requires an admin token.", {
|
|
43
|
-
name: z.string().describe("Display name of the new user"),
|
|
44
|
-
group_ids: groupIds
|
|
45
|
-
.optional()
|
|
46
|
-
.describe("Permission groups; normally set one (default: none)"),
|
|
47
|
-
local_only: z
|
|
48
|
-
.boolean()
|
|
49
|
-
.optional()
|
|
50
|
-
.describe("Only allow this user to log in from the local network"),
|
|
51
|
-
}, async ({ name, group_ids, local_only }) => {
|
|
52
|
-
try {
|
|
53
|
-
const payload = { name };
|
|
54
|
-
if (group_ids !== undefined)
|
|
55
|
-
payload.group_ids = group_ids;
|
|
56
|
-
if (local_only !== undefined)
|
|
57
|
-
payload.local_only = local_only;
|
|
58
|
-
return ok(stripSecrets(await ws.command("config/auth/create", payload)));
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
return err(e);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
server.tool("update_user", "Update a Home Assistant user via the WebSocket API (config/auth/update): rename, activate/deactivate (is_active), change permission groups or the local-only restriction. Only the given fields change. Refuses system-generated users, deactivating the owner, and — to avoid locking this server out — changing is_active, group_ids or local_only of the account this server authenticates as (see get_current_user); renaming that account is fine. Returns { user }. Requires an admin token.", {
|
|
65
|
-
user_id: z.string().describe("User id from list_users"),
|
|
66
|
-
name: z.string().optional().describe("New display name"),
|
|
67
|
-
is_active: z
|
|
68
|
-
.boolean()
|
|
69
|
-
.optional()
|
|
70
|
-
.describe("false deactivates (blocks login), true reactivates"),
|
|
71
|
-
group_ids: groupIds.optional().describe("Replaces the user's permission groups"),
|
|
72
|
-
local_only: z.boolean().optional().describe("Only allow login from the local network"),
|
|
73
|
-
}, async ({ user_id, name, is_active, group_ids, local_only }) => {
|
|
74
|
-
try {
|
|
75
|
-
if (name === undefined &&
|
|
76
|
-
is_active === undefined &&
|
|
77
|
-
group_ids === undefined &&
|
|
78
|
-
local_only === undefined) {
|
|
79
|
-
throw new Error("Provide at least one of 'name', 'is_active', 'group_ids' or 'local_only' to update");
|
|
80
|
-
}
|
|
81
|
-
if (is_active !== undefined || group_ids !== undefined || local_only !== undefined) {
|
|
82
|
-
const me = (await ws.command("auth/current_user"));
|
|
83
|
-
if (me?.id === user_id) {
|
|
84
|
-
throw new Error("Refusing to change is_active, group_ids or local_only of the account this server authenticates as — that could lock it out. Change it in the Home Assistant UI instead.");
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const payload = { user_id };
|
|
88
|
-
if (name !== undefined)
|
|
89
|
-
payload.name = name;
|
|
90
|
-
if (is_active !== undefined)
|
|
91
|
-
payload.is_active = is_active;
|
|
92
|
-
if (group_ids !== undefined)
|
|
93
|
-
payload.group_ids = group_ids;
|
|
94
|
-
if (local_only !== undefined)
|
|
95
|
-
payload.local_only = local_only;
|
|
96
|
-
return ok(stripSecrets(await ws.command("config/auth/update", payload)));
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
return err(e);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
server.tool("delete_user", "DELETE a Home Assistant user via the WebSocket API (config/auth/delete) together with its credentials and all of its refresh/long-lived tokens, so everything signed in as that user is logged out. Cannot be undone. Home Assistant refuses to delete your own account, the owner and system-generated users. A linked person entry is kept but unlinked from the user. Requires an admin token.", { user_id: z.string().describe("User id from list_users") }, async ({ user_id }) => {
|
|
103
|
-
try {
|
|
104
|
-
await ws.command("config/auth/delete", { user_id });
|
|
105
|
-
return ok({ deleted: user_id });
|
|
106
|
-
}
|
|
107
|
-
catch (e) {
|
|
108
|
-
return err(e);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
server.tool("list_refresh_tokens", "List the refresh and long-lived access tokens of the user this server authenticates as via the WebSocket API (auth/refresh_tokens) — metadata only: id, type (normal/long_lived_access_token/system), client_name, client_id, client_icon, created_at, last_used_at, last_used_ip, expire_at, auth_provider_type and is_current (true for the token this server itself uses). The token secrets are never returned. Only the calling user's own tokens are listed, not other users'.", {}, async () => {
|
|
112
|
-
try {
|
|
113
|
-
const list = (stripSecrets(await ws.command("auth/refresh_tokens")) ?? []);
|
|
114
|
-
return ok({ count: list.length, tokens: list });
|
|
115
|
-
}
|
|
116
|
-
catch (e) {
|
|
117
|
-
return err(e);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
server.tool("delete_refresh_token", "Revoke one refresh or long-lived access token of the user this server authenticates as via the WebSocket API (auth/delete_refresh_token): whatever uses it (a browser session, the companion app, an integration) is logged out and must sign in again; it cannot be undone. Refuses the token this server itself uses (is_current in list_refresh_tokens) because that would lock it out. Get the id from list_refresh_tokens.", {
|
|
121
|
-
refresh_token_id: z.string().describe("The token's 'id' from list_refresh_tokens"),
|
|
122
|
-
}, async ({ refresh_token_id }) => {
|
|
123
|
-
try {
|
|
124
|
-
const tokens = ((await ws.command("auth/refresh_tokens")) ?? []);
|
|
125
|
-
if (tokens.find((t) => t.id === refresh_token_id)?.is_current) {
|
|
126
|
-
throw new Error("Refusing to revoke the token this server authenticates with — that would lock it out. Revoke it from the Home Assistant profile page instead.");
|
|
127
|
-
}
|
|
128
|
-
await ws.command("auth/delete_refresh_token", { refresh_token_id });
|
|
129
|
-
return ok({ deleted: refresh_token_id });
|
|
130
|
-
}
|
|
131
|
-
catch (e) {
|
|
132
|
-
return err(e);
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
server.tool("delete_all_refresh_tokens", "Revoke ALL refresh and long-lived access tokens of the user this server authenticates as, EXCEPT the one this server itself uses, via the WebSocket API (auth/delete_all_refresh_tokens with delete_current_token=false). Every other session, the companion apps and integrations using that user's tokens are logged out and must sign in again. Optionally limit it to one token_type. Cannot be undone. Returns { deleted_count, token_type }.", {
|
|
136
|
-
token_type: z
|
|
137
|
-
.enum(["normal", "long_lived_access_token"])
|
|
138
|
-
.optional()
|
|
139
|
-
.describe("Only this type: 'normal' (login sessions and app tokens) or 'long_lived_access_token'"),
|
|
140
|
-
}, async ({ token_type }) => {
|
|
141
|
-
try {
|
|
142
|
-
const tokens = ((await ws.command("auth/refresh_tokens")) ?? []);
|
|
143
|
-
const deleted_count = tokens.filter((t) => !t.is_current && (token_type === undefined || t.type === token_type)).length;
|
|
144
|
-
const payload = { delete_current_token: false };
|
|
145
|
-
if (token_type !== undefined)
|
|
146
|
-
payload.token_type = token_type;
|
|
147
|
-
await ws.command("auth/delete_all_refresh_tokens", payload);
|
|
148
|
-
return ok({ deleted_count, token_type: token_type ?? "all" });
|
|
149
|
-
}
|
|
150
|
-
catch (e) {
|
|
151
|
-
return err(e);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
server.tool("set_refresh_token_expiry", "Enable or disable automatic expiry of a refresh/long-lived token of the user this server authenticates as via the WebSocket API (auth/refresh_token_set_expiry). Enabling makes the token expire 90 days after it was last used (an existing expiry is kept); disabling removes the expiry so it never expires. Get the id from list_refresh_tokens (expire_at shows the current state).", {
|
|
155
|
-
refresh_token_id: z.string().describe("The token's 'id' from list_refresh_tokens"),
|
|
156
|
-
enable_expiry: z
|
|
157
|
-
.boolean()
|
|
158
|
-
.describe("true to let it expire after 90 days unused, false to never expire"),
|
|
159
|
-
}, async ({ refresh_token_id, enable_expiry }) => {
|
|
160
|
-
try {
|
|
161
|
-
await ws.command("auth/refresh_token_set_expiry", { refresh_token_id, enable_expiry });
|
|
162
|
-
return ok({ refresh_token_id, expiry_enabled: enable_expiry });
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
return err(e);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
server.tool("update_own_profile", "Update the display name and/or picture of the user this server authenticates as, and of the person entry linked to it, via the WebSocket API (person/update_own_profile). name is trimmed and must not be empty; picture must be an image already uploaded to Home Assistant ('/api/image/serve/<32-hex id>/512x512' style path) or null to remove it, and needs a storage-managed person linked to the user. Returns { user_name, person }. Not allowed for system-generated users.", {
|
|
169
|
-
name: z.string().optional().describe("New display name"),
|
|
170
|
-
picture: z
|
|
171
|
-
.string()
|
|
172
|
-
.nullable()
|
|
173
|
-
.optional()
|
|
174
|
-
.describe("Uploaded image path such as '/api/image/serve/<id>/512x512', or null to remove"),
|
|
175
|
-
}, async ({ name, picture }) => {
|
|
176
|
-
try {
|
|
177
|
-
if (name === undefined && picture === undefined) {
|
|
178
|
-
throw new Error("Provide at least one of 'name' or 'picture' to update");
|
|
179
|
-
}
|
|
180
|
-
const payload = {};
|
|
181
|
-
if (name !== undefined)
|
|
182
|
-
payload.name = name;
|
|
183
|
-
if (picture !== undefined)
|
|
184
|
-
payload.picture = picture;
|
|
185
|
-
return ok(await ws.command("person/update_own_profile", payload));
|
|
186
|
-
}
|
|
187
|
-
catch (e) {
|
|
188
|
-
return err(e);
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
async function resolveId(automation, client) {
|
|
4
|
-
if (!automation.startsWith("automation."))
|
|
5
|
-
return automation;
|
|
6
|
-
const state = (await client.fetch(`/api/states/${encodeURIComponent(automation)}`));
|
|
7
|
-
const id = state.attributes?.id;
|
|
8
|
-
if (id === undefined || id === null || id === "") {
|
|
9
|
-
throw new Error(`Automation ${automation} has no 'id' attribute, so it is not stored in automations.yaml and cannot be read or edited via the config API (it is defined elsewhere in YAML without an id)`);
|
|
10
|
-
}
|
|
11
|
-
return String(id);
|
|
12
|
-
}
|
|
13
|
-
const configPath = (id) => `/api/config/automation/config/${encodeURIComponent(id)}`;
|
|
14
|
-
export function registerAutomationTools(server, client) {
|
|
15
|
-
server.tool("get_automation_config", "Get the stored configuration of an automation (alias, description, triggers, conditions, actions, mode) from automations.yaml via GET /api/config/automation/config/{id}. Accepts the automation.* entity id (the internal id is resolved automatically) or the internal id itself. Only automations with an 'id' (UI-created, stored in automations.yaml) are accessible.", {
|
|
16
|
-
automation: z
|
|
17
|
-
.string()
|
|
18
|
-
.describe("automation.* entity id, or the automation's internal id (the 'id' attribute)"),
|
|
19
|
-
}, async ({ automation }) => {
|
|
20
|
-
try {
|
|
21
|
-
const id = await resolveId(automation, client);
|
|
22
|
-
return ok(await client.fetch(configPath(id)));
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
return err(e);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
server.tool("upsert_automation", "Create a new automation or replace an existing one via POST /api/config/automation/config/{id} (requires an admin token). Home Assistant validates the config, writes it to automations.yaml and reloads automations automatically — no separate reload needed. The config REPLACES the stored one entirely, so to edit call get_automation_config first and send back the modified object. Omit 'automation' to create a new one (an id is generated and returned); its entity id becomes automation.<slugified alias>. Note that once its triggers fire, an automation runs its actions — review the config before writing.", {
|
|
29
|
-
config: z
|
|
30
|
-
.record(z.unknown())
|
|
31
|
-
.describe("Full automation config as stored in automations.yaml: alias, description?, triggers, conditions?, actions, mode? (the legacy singular trigger/condition/action keys are also accepted)"),
|
|
32
|
-
automation: z
|
|
33
|
-
.string()
|
|
34
|
-
.optional()
|
|
35
|
-
.describe("Existing automation.* entity id or internal id to replace; omit to create a new automation"),
|
|
36
|
-
}, async ({ config, automation }) => {
|
|
37
|
-
try {
|
|
38
|
-
const id = automation ? await resolveId(automation, client) : String(Date.now());
|
|
39
|
-
const result = await client.fetch(configPath(id), {
|
|
40
|
-
method: "POST",
|
|
41
|
-
body: JSON.stringify(config),
|
|
42
|
-
});
|
|
43
|
-
return ok({ id, created: !automation, result });
|
|
44
|
-
}
|
|
45
|
-
catch (e) {
|
|
46
|
-
return err(e);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
server.tool("delete_automation", "Delete an automation from automations.yaml via DELETE /api/config/automation/config/{id} (requires an admin token). Accepts the automation.* entity id or the internal id. Only UI-managed automations (those stored in automations.yaml) can be deleted; Home Assistant reloads automations automatically afterwards.", {
|
|
50
|
-
automation: z
|
|
51
|
-
.string()
|
|
52
|
-
.describe("automation.* entity id, or the automation's internal id (the 'id' attribute)"),
|
|
53
|
-
}, async ({ automation }) => {
|
|
54
|
-
try {
|
|
55
|
-
const id = await resolveId(automation, client);
|
|
56
|
-
return ok(await client.fetch(configPath(id), { method: "DELETE" }));
|
|
57
|
-
}
|
|
58
|
-
catch (e) {
|
|
59
|
-
return err(e);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
package/dist/tools/backup.js
DELETED
|
@@ -1,375 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
const folderSchema = z
|
|
4
|
-
.enum(["share", "addons/local", "ssl", "media"])
|
|
5
|
-
.describe("Supervisor folder: share, addons/local, ssl or media");
|
|
6
|
-
const retentionSchema = () => z
|
|
7
|
-
.object({
|
|
8
|
-
copies: z.number().int().min(1).nullable().optional().describe("Keep this many backups"),
|
|
9
|
-
days: z.number().int().min(1).nullable().optional().describe("Keep backups this many days"),
|
|
10
|
-
})
|
|
11
|
-
.describe("null (or omitted) means no limit for that field");
|
|
12
|
-
const notes = {
|
|
13
|
-
job: "The backup runs in the background; poll list_backups until state is idle and check last_action_event for the outcome.",
|
|
14
|
-
restore: "Home Assistant is restarting to restore this backup. The connection normally drops now; wait for it to come back and verify with get_config.",
|
|
15
|
-
};
|
|
16
|
-
function summarize(b) {
|
|
17
|
-
const row = {
|
|
18
|
-
backup_id: b.backup_id,
|
|
19
|
-
name: b.name,
|
|
20
|
-
date: b.date,
|
|
21
|
-
homeassistant_version: b.homeassistant_version ?? null,
|
|
22
|
-
homeassistant_included: b.homeassistant_included,
|
|
23
|
-
database_included: b.database_included,
|
|
24
|
-
folders: b.folders ?? [],
|
|
25
|
-
addons: (b.addons ?? []).map((a) => a.slug),
|
|
26
|
-
agents: b.agents ?? {},
|
|
27
|
-
with_automatic_settings: b.with_automatic_settings ?? null,
|
|
28
|
-
};
|
|
29
|
-
if (b.failed_addons?.length)
|
|
30
|
-
row.failed_addons = b.failed_addons;
|
|
31
|
-
if (b.failed_agent_ids?.length)
|
|
32
|
-
row.failed_agent_ids = b.failed_agent_ids;
|
|
33
|
-
if (b.failed_folders?.length)
|
|
34
|
-
row.failed_folders = b.failed_folders;
|
|
35
|
-
return row;
|
|
36
|
-
}
|
|
37
|
-
export function registerBackupTools(server, ws) {
|
|
38
|
-
server.tool("list_backups", "List stored backups and the backup manager's status via the WebSocket API (backup/info). Returns { count, state, last_action_event, last/next automatic backup times, agent_errors?, backups } with backups newest first: backup_id, name, date, homeassistant_version, what it includes (homeassistant_included, database_included, folders, addons as slugs), agents (per backup location: protected = encrypted, size in bytes) and with_automatic_settings. state is 'idle' when nothing is running ('create_backup', 'restore_backup', ... otherwise) — poll it after create_backup. agent_errors names locations that could not be read. Requires an admin token.", {}, async () => {
|
|
39
|
-
try {
|
|
40
|
-
const info = ((await ws.command("backup/info")) ?? {});
|
|
41
|
-
const backups = [...(info.backups ?? [])]
|
|
42
|
-
.sort((a, b) => (b.date ?? "").localeCompare(a.date ?? ""))
|
|
43
|
-
.map(summarize);
|
|
44
|
-
const out = {
|
|
45
|
-
count: backups.length,
|
|
46
|
-
state: info.state,
|
|
47
|
-
last_action_event: info.last_action_event ?? null,
|
|
48
|
-
last_attempted_automatic_backup: info.last_attempted_automatic_backup ?? null,
|
|
49
|
-
last_completed_automatic_backup: info.last_completed_automatic_backup ?? null,
|
|
50
|
-
next_automatic_backup: info.next_automatic_backup ?? null,
|
|
51
|
-
next_automatic_backup_additional: info.next_automatic_backup_additional ?? false,
|
|
52
|
-
};
|
|
53
|
-
if (info.agent_errors && Object.keys(info.agent_errors).length > 0) {
|
|
54
|
-
out.agent_errors = info.agent_errors;
|
|
55
|
-
}
|
|
56
|
-
out.backups = backups;
|
|
57
|
-
return ok(out);
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
return err(e);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
server.tool("get_backup", "Get the full details of one backup via the WebSocket API (backup/details): everything list_backups shows plus the add-ons with name and version, extra_metadata, failed_addons/failed_folders and per-location agents. Returns { backup, agent_errors? }; errors if no backup location has that backup_id. Requires an admin token.", { backup_id: z.string().describe("Backup id from list_backups") }, async ({ backup_id }) => {
|
|
64
|
-
try {
|
|
65
|
-
const res = ((await ws.command("backup/details", { backup_id })) ?? {});
|
|
66
|
-
if (!res.backup) {
|
|
67
|
-
const reasons = Object.entries(res.agent_errors ?? {}).map(([a, m]) => `${a}: ${m}`);
|
|
68
|
-
throw new Error(`Backup ${backup_id} was not found in any backup location${reasons.length ? ` (${reasons.join("; ")})` : ""}`);
|
|
69
|
-
}
|
|
70
|
-
const out = { backup: res.backup };
|
|
71
|
-
if (res.agent_errors && Object.keys(res.agent_errors).length > 0) {
|
|
72
|
-
out.agent_errors = res.agent_errors;
|
|
73
|
-
}
|
|
74
|
-
return ok(out);
|
|
75
|
-
}
|
|
76
|
-
catch (e) {
|
|
77
|
-
return err(e);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
server.tool("list_backup_agents", "List the backup locations (agents) Home Assistant can store backups in via the WebSocket API (backup/agents/info), e.g. backup.local, hassio.local or a cloud/NAS integration. Returns { count, agents: [{ agent_id, name }] }. The agent_id is what create_backup and restore_backup take. Requires an admin token.", {}, async () => {
|
|
81
|
-
try {
|
|
82
|
-
const res = (await ws.command("backup/agents/info"));
|
|
83
|
-
const agents = res?.agents ?? [];
|
|
84
|
-
return ok({ count: agents.length, agents });
|
|
85
|
-
}
|
|
86
|
-
catch (e) {
|
|
87
|
-
return err(e);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
server.tool("get_backup_config", "Get the automatic backup settings via the WebSocket API (backup/config/info): create_backup (agent_ids, include_addons, include_all_addons, include_database, include_folders, name), schedule (recurrence never/daily/custom_days, days, time), global retention (copies, days), per-agent settings (protected, retention), automatic_backups_configured, last_attempted/last_completed automatic backup and next_automatic_backup. The stored encryption password is NEVER returned — create_backup.password_set only says whether one is configured. Requires an admin token.", {}, async () => {
|
|
91
|
-
try {
|
|
92
|
-
const res = ((await ws.command("backup/config/info")) ?? {});
|
|
93
|
-
const config = { ...(res.config ?? {}) };
|
|
94
|
-
if (config.create_backup) {
|
|
95
|
-
const { password, ...rest } = config.create_backup;
|
|
96
|
-
config.create_backup = { ...rest, password_set: Boolean(password) };
|
|
97
|
-
}
|
|
98
|
-
return ok({ config });
|
|
99
|
-
}
|
|
100
|
-
catch (e) {
|
|
101
|
-
return err(e);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
server.tool("update_backup_config", "Change the automatic backup settings via the WebSocket API (backup/config/update); changes take effect immediately and reschedule the next backup. Provide any of: create_backup (what automatic backups contain and where they go; merged field by field), schedule, retention, agents (per-location settings, merged per agent) and automatic_backups_configured. CAUTION: schedule and retention REPLACE the whole block — omitted fields reset (schedule recurrence to 'never', retention copies/days to no limit), so always send the complete schedule (recurrence, plus days for custom_days, plus time). Current values come from get_backup_config. Requires an admin token.", {
|
|
105
|
-
create_backup: z
|
|
106
|
-
.object({
|
|
107
|
-
agent_ids: z
|
|
108
|
-
.array(z.string())
|
|
109
|
-
.optional()
|
|
110
|
-
.describe("Backup locations, from list_backup_agents"),
|
|
111
|
-
include_addons: z
|
|
112
|
-
.array(z.string())
|
|
113
|
-
.nullable()
|
|
114
|
-
.optional()
|
|
115
|
-
.describe("Add-on slugs to include"),
|
|
116
|
-
include_all_addons: z.boolean().optional(),
|
|
117
|
-
include_database: z.boolean().optional(),
|
|
118
|
-
include_folders: z.array(folderSchema).nullable().optional(),
|
|
119
|
-
name: z.string().nullable().optional().describe("Backup name; null for the default"),
|
|
120
|
-
password: z
|
|
121
|
-
.string()
|
|
122
|
-
.nullable()
|
|
123
|
-
.optional()
|
|
124
|
-
.describe("Encryption password for automatic backups; null disables encryption"),
|
|
125
|
-
})
|
|
126
|
-
.optional()
|
|
127
|
-
.describe("What automatic backups contain; only the given fields change"),
|
|
128
|
-
schedule: z
|
|
129
|
-
.object({
|
|
130
|
-
recurrence: z.enum(["never", "daily", "custom_days"]),
|
|
131
|
-
days: z
|
|
132
|
-
.array(z.enum(["mon", "tue", "wed", "thu", "fri", "sat", "sun"]))
|
|
133
|
-
.optional()
|
|
134
|
-
.describe("Weekdays for custom_days"),
|
|
135
|
-
time: z
|
|
136
|
-
.string()
|
|
137
|
-
.nullable()
|
|
138
|
-
.optional()
|
|
139
|
-
.describe("Local time 'HH:MM' or 'HH:MM:SS'; null for the default (about 04:45)"),
|
|
140
|
-
})
|
|
141
|
-
.optional()
|
|
142
|
-
.describe("When automatic backups run; replaces the whole schedule"),
|
|
143
|
-
retention: retentionSchema().optional(),
|
|
144
|
-
agents: z
|
|
145
|
-
.record(z.object({
|
|
146
|
-
protected: z.boolean().optional().describe("Encrypt backups stored at this location"),
|
|
147
|
-
retention: retentionSchema()
|
|
148
|
-
.nullable()
|
|
149
|
-
.optional()
|
|
150
|
-
.describe("Per-location retention; null removes it"),
|
|
151
|
-
}))
|
|
152
|
-
.optional()
|
|
153
|
-
.describe("Per-location settings keyed by agent_id"),
|
|
154
|
-
automatic_backups_configured: z
|
|
155
|
-
.boolean()
|
|
156
|
-
.optional()
|
|
157
|
-
.describe("Whether automatic backups were set up (used by the UI)"),
|
|
158
|
-
}, async ({ create_backup, schedule, retention, agents, automatic_backups_configured }) => {
|
|
159
|
-
try {
|
|
160
|
-
const payload = {};
|
|
161
|
-
if (create_backup !== undefined)
|
|
162
|
-
payload.create_backup = create_backup;
|
|
163
|
-
if (schedule !== undefined)
|
|
164
|
-
payload.schedule = schedule;
|
|
165
|
-
if (retention !== undefined)
|
|
166
|
-
payload.retention = retention;
|
|
167
|
-
if (agents !== undefined)
|
|
168
|
-
payload.agents = agents;
|
|
169
|
-
if (automatic_backups_configured !== undefined) {
|
|
170
|
-
payload.automatic_backups_configured = automatic_backups_configured;
|
|
171
|
-
}
|
|
172
|
-
if (Object.keys(payload).length === 0) {
|
|
173
|
-
throw new Error("Provide at least one of 'create_backup', 'schedule', 'retention', 'agents' or 'automatic_backups_configured'");
|
|
174
|
-
}
|
|
175
|
-
await ws.command("backup/config/update", payload);
|
|
176
|
-
return ok({ updated: Object.keys(payload) });
|
|
177
|
-
}
|
|
178
|
-
catch (e) {
|
|
179
|
-
return err(e);
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
server.tool("create_backup", "Create a backup NOW via the WebSocket API (backup/generate). It copies the Home Assistant configuration (and optionally the database, folders and add-ons) to the chosen backup locations, then returns immediately with { backup_job_id } while the backup runs in the background — poll list_backups (state back to 'idle', last_action_event) for the outcome. Only one backup or restore can run at a time. Add-ons and folders are only available on Supervisor installs. Take one before risky changes such as bulk registry edits, restores or updates. Requires an admin token.", {
|
|
183
|
-
agent_ids: z
|
|
184
|
-
.array(z.string())
|
|
185
|
-
.min(1)
|
|
186
|
-
.describe("Backup locations to store it in, from list_backup_agents, e.g. ['backup.local']"),
|
|
187
|
-
name: z
|
|
188
|
-
.string()
|
|
189
|
-
.nullable()
|
|
190
|
-
.optional()
|
|
191
|
-
.describe("Backup name (default 'Custom backup <version>')"),
|
|
192
|
-
password: z
|
|
193
|
-
.string()
|
|
194
|
-
.nullable()
|
|
195
|
-
.optional()
|
|
196
|
-
.describe("Encrypt the backup with this password; keep it, restoring needs it"),
|
|
197
|
-
include_homeassistant: z
|
|
198
|
-
.boolean()
|
|
199
|
-
.optional()
|
|
200
|
-
.describe("Include the Home Assistant configuration (default true)"),
|
|
201
|
-
include_database: z
|
|
202
|
-
.boolean()
|
|
203
|
-
.optional()
|
|
204
|
-
.describe("Include the recorder database (default true)"),
|
|
205
|
-
include_folders: z.array(folderSchema).optional().describe("Extra folders (Supervisor only)"),
|
|
206
|
-
include_addons: z
|
|
207
|
-
.array(z.string())
|
|
208
|
-
.optional()
|
|
209
|
-
.describe("Add-on slugs to include (Supervisor only); exclusive with include_all_addons"),
|
|
210
|
-
include_all_addons: z
|
|
211
|
-
.boolean()
|
|
212
|
-
.optional()
|
|
213
|
-
.describe("Include every add-on (Supervisor only; default false)"),
|
|
214
|
-
}, async ({ agent_ids, name, password, include_homeassistant, include_database, include_folders, include_addons, include_all_addons, }) => {
|
|
215
|
-
try {
|
|
216
|
-
if (include_all_addons && include_addons?.length) {
|
|
217
|
-
throw new Error("Use either include_all_addons or include_addons, not both");
|
|
218
|
-
}
|
|
219
|
-
const payload = { agent_ids };
|
|
220
|
-
if (name !== undefined)
|
|
221
|
-
payload.name = name;
|
|
222
|
-
if (password !== undefined)
|
|
223
|
-
payload.password = password;
|
|
224
|
-
if (include_homeassistant !== undefined)
|
|
225
|
-
payload.include_homeassistant = include_homeassistant;
|
|
226
|
-
if (include_database !== undefined)
|
|
227
|
-
payload.include_database = include_database;
|
|
228
|
-
if (include_folders !== undefined)
|
|
229
|
-
payload.include_folders = include_folders;
|
|
230
|
-
if (include_addons !== undefined)
|
|
231
|
-
payload.include_addons = include_addons;
|
|
232
|
-
if (include_all_addons !== undefined)
|
|
233
|
-
payload.include_all_addons = include_all_addons;
|
|
234
|
-
const res = (await ws.command("backup/generate", payload));
|
|
235
|
-
return ok({ backup_job_id: res?.backup_job_id ?? null, note: notes.job });
|
|
236
|
-
}
|
|
237
|
-
catch (e) {
|
|
238
|
-
return err(e);
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
server.tool("create_backup_with_automatic_settings", "Create a backup NOW using the stored automatic backup settings via the WebSocket API (backup/generate_with_automatic_settings) — same contents, locations, name and encryption as the scheduled backup (see get_backup_config; Home Assistant itself is always included), and it counts as an automatic backup for retention. Returns { backup_job_id } immediately while the backup runs in the background — poll list_backups. Fails if no backup location is configured. Requires an admin token.", {}, async () => {
|
|
242
|
-
try {
|
|
243
|
-
const res = (await ws.command("backup/generate_with_automatic_settings"));
|
|
244
|
-
return ok({ backup_job_id: res?.backup_job_id ?? null, note: notes.job });
|
|
245
|
-
}
|
|
246
|
-
catch (e) {
|
|
247
|
-
return err(e);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
server.tool("delete_backup", "DELETE a backup via the WebSocket API (backup/delete). It is removed from EVERY backup location that holds it (local, cloud, NAS...) and cannot be recovered. Returns { backup_id, deleted, agent_errors } — deleted is false and agent_errors names the locations where removal failed (the backup may then still exist there, or be gone from the others). Requires an admin token.", { backup_id: z.string().describe("Backup id from list_backups") }, async ({ backup_id }) => {
|
|
251
|
-
try {
|
|
252
|
-
const res = ((await ws.command("backup/delete", { backup_id })) ?? {});
|
|
253
|
-
const agent_errors = res.agent_errors ?? {};
|
|
254
|
-
return ok({
|
|
255
|
-
backup_id,
|
|
256
|
-
deleted: Object.keys(agent_errors).length === 0,
|
|
257
|
-
agent_errors,
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
catch (e) {
|
|
261
|
-
return err(e);
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
server.tool("restore_backup", "RESTORE a backup via the WebSocket API (backup/restore). DESTRUCTIVE: this RESTARTS Home Assistant and OVERWRITES its current configuration (and, unless restore_database is false, its database) with the contents of the backup; anything changed since the backup was made is lost, and Home Assistant is unavailable for the restart. The WebSocket connection normally drops while it restarts, so an error such as 'closed before a result was received' can mean the restore started — wait, then check get_config. Take a fresh create_backup first unless the current state is already lost. On Core installs only the configuration and database can be restored; add-ons and folders need Supervisor. Errors if the backup is not found, the password is wrong or another backup/restore is running. Requires an admin token.", {
|
|
265
|
-
backup_id: z.string().describe("Backup id from list_backups"),
|
|
266
|
-
agent_id: z
|
|
267
|
-
.string()
|
|
268
|
-
.describe("The backup location to restore from, one of the agents of that backup"),
|
|
269
|
-
password: z
|
|
270
|
-
.string()
|
|
271
|
-
.optional()
|
|
272
|
-
.describe("Encryption password, required for protected backups"),
|
|
273
|
-
restore_homeassistant: z
|
|
274
|
-
.boolean()
|
|
275
|
-
.optional()
|
|
276
|
-
.describe("Restore the Home Assistant configuration (default true)"),
|
|
277
|
-
restore_database: z
|
|
278
|
-
.boolean()
|
|
279
|
-
.optional()
|
|
280
|
-
.describe("Restore the recorder database (default true)"),
|
|
281
|
-
restore_folders: z
|
|
282
|
-
.array(folderSchema)
|
|
283
|
-
.optional()
|
|
284
|
-
.describe("Folders to restore (Supervisor only)"),
|
|
285
|
-
restore_addons: z
|
|
286
|
-
.array(z.string())
|
|
287
|
-
.optional()
|
|
288
|
-
.describe("Add-on slugs to restore (Supervisor only)"),
|
|
289
|
-
}, async ({ backup_id, agent_id, password, restore_homeassistant, restore_database, restore_folders, restore_addons, }) => {
|
|
290
|
-
try {
|
|
291
|
-
const payload = { backup_id, agent_id };
|
|
292
|
-
if (password !== undefined)
|
|
293
|
-
payload.password = password;
|
|
294
|
-
if (restore_homeassistant !== undefined)
|
|
295
|
-
payload.restore_homeassistant = restore_homeassistant;
|
|
296
|
-
if (restore_database !== undefined)
|
|
297
|
-
payload.restore_database = restore_database;
|
|
298
|
-
if (restore_folders !== undefined)
|
|
299
|
-
payload.restore_folders = restore_folders;
|
|
300
|
-
if (restore_addons !== undefined)
|
|
301
|
-
payload.restore_addons = restore_addons;
|
|
302
|
-
await ws.command("backup/restore", payload);
|
|
303
|
-
return ok({ backup_id, agent_id, restore: "started", note: notes.restore });
|
|
304
|
-
}
|
|
305
|
-
catch (e) {
|
|
306
|
-
return err(e);
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
server.tool("check_backup_password", "Check whether a password decrypts a protected backup via the WebSocket API (backup/can_decrypt_on_download). Returns { can_decrypt: true } or { can_decrypt: false } for a wrong password; errors if the backup is not found or on-the-fly decryption is not supported for it. Changes nothing — use it before restore_backup to avoid a failed restore. Requires an admin token.", {
|
|
310
|
-
backup_id: z.string().describe("Backup id from list_backups"),
|
|
311
|
-
agent_id: z.string().describe("The backup location holding the backup"),
|
|
312
|
-
password: z.string().describe("The password to test"),
|
|
313
|
-
}, async ({ backup_id, agent_id, password }) => {
|
|
314
|
-
try {
|
|
315
|
-
try {
|
|
316
|
-
await ws.command("backup/can_decrypt_on_download", { backup_id, agent_id, password });
|
|
317
|
-
}
|
|
318
|
-
catch (e) {
|
|
319
|
-
if (e.code === "password_incorrect") {
|
|
320
|
-
return ok({ backup_id, agent_id, can_decrypt: false });
|
|
321
|
-
}
|
|
322
|
-
throw e;
|
|
323
|
-
}
|
|
324
|
-
return ok({ backup_id, agent_id, can_decrypt: true });
|
|
325
|
-
}
|
|
326
|
-
catch (e) {
|
|
327
|
-
return err(e);
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
server.tool("get_supervisor_update_options", "Get the 'back up before updating' options of Home Assistant OS / Supervised installs via the WebSocket API (hassio/update/config/info): { add_on_backup_before_update, add_on_backup_retain_copies, core_backup_before_update }. Only available when the Supervisor integration is loaded; on Core/Container installs Home Assistant reports an unknown command. Requires an admin token.", {}, async () => {
|
|
331
|
-
try {
|
|
332
|
-
return ok(await ws.command("hassio/update/config/info"));
|
|
333
|
-
}
|
|
334
|
-
catch (e) {
|
|
335
|
-
return err(e);
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
server.tool("set_supervisor_update_options", "Change the 'back up before updating' options of Home Assistant OS / Supervised installs via the WebSocket API (hassio/update/config/update): whether a backup is taken automatically before updating an add-on or Home Assistant Core, and how many add-on backups are kept. Only the given fields change; at least one is required. Config only — it does not install any update. Not available on Core/Container installs. Requires an admin token.", {
|
|
339
|
-
add_on_backup_before_update: z
|
|
340
|
-
.boolean()
|
|
341
|
-
.optional()
|
|
342
|
-
.describe("Back up an add-on before updating it"),
|
|
343
|
-
add_on_backup_retain_copies: z
|
|
344
|
-
.number()
|
|
345
|
-
.int()
|
|
346
|
-
.min(1)
|
|
347
|
-
.optional()
|
|
348
|
-
.describe("How many pre-update backups to keep per add-on"),
|
|
349
|
-
core_backup_before_update: z
|
|
350
|
-
.boolean()
|
|
351
|
-
.optional()
|
|
352
|
-
.describe("Back up Home Assistant Core before updating it"),
|
|
353
|
-
}, async ({ add_on_backup_before_update, add_on_backup_retain_copies, core_backup_before_update, }) => {
|
|
354
|
-
try {
|
|
355
|
-
const payload = {};
|
|
356
|
-
if (add_on_backup_before_update !== undefined) {
|
|
357
|
-
payload.add_on_backup_before_update = add_on_backup_before_update;
|
|
358
|
-
}
|
|
359
|
-
if (add_on_backup_retain_copies !== undefined) {
|
|
360
|
-
payload.add_on_backup_retain_copies = add_on_backup_retain_copies;
|
|
361
|
-
}
|
|
362
|
-
if (core_backup_before_update !== undefined) {
|
|
363
|
-
payload.core_backup_before_update = core_backup_before_update;
|
|
364
|
-
}
|
|
365
|
-
if (Object.keys(payload).length === 0) {
|
|
366
|
-
throw new Error("Provide at least one of 'add_on_backup_before_update', 'add_on_backup_retain_copies' or 'core_backup_before_update'");
|
|
367
|
-
}
|
|
368
|
-
await ws.command("hassio/update/config/update", payload);
|
|
369
|
-
return ok({ updated: payload });
|
|
370
|
-
}
|
|
371
|
-
catch (e) {
|
|
372
|
-
return err(e);
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
}
|