@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.
- 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/registry.js
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err, domainOf } from "../hass/format.js";
|
|
3
|
-
export function registerRegistryTools(server, ws) {
|
|
4
|
-
server.tool("list_registry_entities", "List ALL registered entities from the entity registry via the WebSocket API — including disabled and currently-unavailable entities that GET /api/states (list_entities) omits. Includes name, platform, area_id, device_id, entity_category and disabled_by. Optionally filter by domain.", {
|
|
5
|
-
domain: z
|
|
6
|
-
.string()
|
|
7
|
-
.optional()
|
|
8
|
-
.describe("Only entities in this domain, e.g. 'light', 'sensor'"),
|
|
9
|
-
include_disabled: z.boolean().optional().describe("Include disabled entities (default true)"),
|
|
10
|
-
}, async ({ domain, include_disabled }) => {
|
|
11
|
-
try {
|
|
12
|
-
const list = (await ws.command("config/entity_registry/list"));
|
|
13
|
-
let rows = list;
|
|
14
|
-
if (domain)
|
|
15
|
-
rows = rows.filter((e) => domainOf(e.entity_id) === domain);
|
|
16
|
-
if (include_disabled === false)
|
|
17
|
-
rows = rows.filter((e) => !e.disabled_by);
|
|
18
|
-
const entities = rows.map((e) => ({
|
|
19
|
-
entity_id: e.entity_id,
|
|
20
|
-
name: e.name ?? e.original_name ?? null,
|
|
21
|
-
platform: e.platform ?? null,
|
|
22
|
-
area_id: e.area_id ?? null,
|
|
23
|
-
device_id: e.device_id ?? null,
|
|
24
|
-
entity_category: e.entity_category ?? null,
|
|
25
|
-
disabled_by: e.disabled_by ?? null,
|
|
26
|
-
hidden_by: e.hidden_by ?? null,
|
|
27
|
-
}));
|
|
28
|
-
return ok({ count: entities.length, entities });
|
|
29
|
-
}
|
|
30
|
-
catch (e) {
|
|
31
|
-
return err(e);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
server.tool("rename_entity", "Rename an entity in the entity registry via the WebSocket API (config/entity_registry/update). Set 'name' to override the friendly name (pass null to revert to the integration's original_name) and/or 'new_entity_id' to change the entity_id itself (must keep the same domain). At least one of name or new_entity_id is required. This edits registry config only — it does not control any device. Returns the updated entity_entry.", {
|
|
35
|
-
entity_id: z
|
|
36
|
-
.string()
|
|
37
|
-
.describe("Current entity_id to rename, e.g. 'cover.storen_wohnzimmer_links'"),
|
|
38
|
-
name: z
|
|
39
|
-
.string()
|
|
40
|
-
.nullable()
|
|
41
|
-
.optional()
|
|
42
|
-
.describe("New friendly name; null reverts to the integration's original_name"),
|
|
43
|
-
new_entity_id: z
|
|
44
|
-
.string()
|
|
45
|
-
.optional()
|
|
46
|
-
.describe("New entity_id; must keep the same domain, e.g. 'cover.wohnzimmer_links'"),
|
|
47
|
-
}, async ({ entity_id, name, new_entity_id }) => {
|
|
48
|
-
try {
|
|
49
|
-
if (name === undefined && new_entity_id === undefined) {
|
|
50
|
-
throw new Error("Provide at least one of 'name' or 'new_entity_id' to rename the entity");
|
|
51
|
-
}
|
|
52
|
-
if (new_entity_id !== undefined && domainOf(new_entity_id) !== domainOf(entity_id)) {
|
|
53
|
-
throw new Error(`new_entity_id must stay in the '${domainOf(entity_id)}' domain (got '${new_entity_id}')`);
|
|
54
|
-
}
|
|
55
|
-
const payload = { entity_id };
|
|
56
|
-
if (name !== undefined)
|
|
57
|
-
payload.name = name;
|
|
58
|
-
if (new_entity_id !== undefined)
|
|
59
|
-
payload.new_entity_id = new_entity_id;
|
|
60
|
-
return ok(await ws.command("config/entity_registry/update", payload));
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
return err(e);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
server.tool("set_entity_enabled", "Enable or disable one or more entities in the entity registry via the WebSocket API (config/entity_registry/update with disabled_by = null to enable, 'user' to disable). This edits registry config only — it does not control any device. Each entity is updated independently: a failure on one is reported in its row and the rest still run. Enabling is NOT instant — HA only loads and starts polling a re-enabled entity after its config entry reloads (see 'reload_delay', usually 30 seconds) or, for integrations that cannot be unloaded, after a full restart ('require_restart'). Re-read list_registry_entities or list_entities after that delay.", {
|
|
67
|
-
entity_ids: z
|
|
68
|
-
.array(z.string())
|
|
69
|
-
.min(1)
|
|
70
|
-
.describe("entity_id(s) to enable or disable, e.g. ['sensor.temp', 'light.kitchen']"),
|
|
71
|
-
enabled: z
|
|
72
|
-
.boolean()
|
|
73
|
-
.describe("true to enable (disabled_by null), false to disable (disabled_by 'user')"),
|
|
74
|
-
}, async ({ entity_ids, enabled }) => {
|
|
75
|
-
try {
|
|
76
|
-
const results = [];
|
|
77
|
-
for (const entity_id of entity_ids) {
|
|
78
|
-
try {
|
|
79
|
-
const res = (await ws.command("config/entity_registry/update", {
|
|
80
|
-
entity_id,
|
|
81
|
-
disabled_by: enabled ? null : "user",
|
|
82
|
-
}));
|
|
83
|
-
const entry = res?.entity_entry ?? res;
|
|
84
|
-
const row = {
|
|
85
|
-
entity_id,
|
|
86
|
-
ok: true,
|
|
87
|
-
disabled_by: entry?.disabled_by ?? null,
|
|
88
|
-
};
|
|
89
|
-
if (res?.require_restart)
|
|
90
|
-
row.require_restart = true;
|
|
91
|
-
if (res?.reload_delay !== undefined)
|
|
92
|
-
row.reload_delay = res.reload_delay;
|
|
93
|
-
results.push(row);
|
|
94
|
-
}
|
|
95
|
-
catch (e) {
|
|
96
|
-
results.push({ entity_id, ok: false, error: String(e) });
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const failed = results.filter((r) => r.ok !== true).length;
|
|
100
|
-
const payload = {
|
|
101
|
-
enabled,
|
|
102
|
-
count: results.length,
|
|
103
|
-
succeeded: results.length - failed,
|
|
104
|
-
failed,
|
|
105
|
-
results,
|
|
106
|
-
};
|
|
107
|
-
if (enabled && failed < results.length) {
|
|
108
|
-
payload.note =
|
|
109
|
-
"Enabled entities are not available immediately: Home Assistant loads them after the config entry reloads ('reload_delay' seconds, usually 30) or after a restart where 'require_restart' is set.";
|
|
110
|
-
}
|
|
111
|
-
return ok(payload);
|
|
112
|
-
}
|
|
113
|
-
catch (e) {
|
|
114
|
-
return err(e);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
server.tool("remove_entity", "Remove an orphaned entity from the entity registry via the WebSocket API (config/entity_registry/remove). Only for leftover entries no integration provides any more — e.g. a deleted template entity or a stale device entity shown as restored/unavailable. Refuses to remove an entity that is still provided by a loaded integration (it has a live state that is not marked 'restored'); remove or reconfigure that integration instead. This deletes registry config only — it does not control any device — and cannot be undone.", {
|
|
118
|
-
entity_id: z.string().describe("entity_id to remove, e.g. 'light.licht_keller_wc'"),
|
|
119
|
-
}, async ({ entity_id }) => {
|
|
120
|
-
try {
|
|
121
|
-
const states = (await ws.command("get_states"));
|
|
122
|
-
const state = states.find((s) => s.entity_id === entity_id);
|
|
123
|
-
if (state && state.attributes?.restored !== true) {
|
|
124
|
-
throw new Error(`${entity_id} is still provided by a loaded integration (state '${state.state}'), so it is not orphaned and was not removed. Remove it from its integration or config first.`);
|
|
125
|
-
}
|
|
126
|
-
await ws.command("config/entity_registry/remove", { entity_id });
|
|
127
|
-
return ok({ removed: entity_id });
|
|
128
|
-
}
|
|
129
|
-
catch (e) {
|
|
130
|
-
return err(e);
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
server.tool("rename_device", "Rename a device in the device registry via the WebSocket API (config/device_registry/update). Set 'name_by_user' to override the device name (pass null to revert to the integration's name) and/or 'area_id' to move it to another area (pass null to clear the area). At least one of name_by_user or area_id is required. Entities with has_entity_name derive their friendly names from the device name, so this is how to fix their display names. This edits registry config only — it does not control any device. Returns the updated device entry.", {
|
|
134
|
-
device_id: z.string().describe("Device registry id, as returned by list_devices"),
|
|
135
|
-
name_by_user: z
|
|
136
|
-
.string()
|
|
137
|
-
.nullable()
|
|
138
|
-
.optional()
|
|
139
|
-
.describe("New user-facing device name; null reverts to the integration's name"),
|
|
140
|
-
area_id: z
|
|
141
|
-
.string()
|
|
142
|
-
.nullable()
|
|
143
|
-
.optional()
|
|
144
|
-
.describe("Area to assign, as returned by list_areas; null clears the area"),
|
|
145
|
-
}, async ({ device_id, name_by_user, area_id }) => {
|
|
146
|
-
try {
|
|
147
|
-
if (name_by_user === undefined && area_id === undefined) {
|
|
148
|
-
throw new Error("Provide at least one of 'name_by_user' or 'area_id' to update the device");
|
|
149
|
-
}
|
|
150
|
-
const payload = { device_id };
|
|
151
|
-
if (name_by_user !== undefined)
|
|
152
|
-
payload.name_by_user = name_by_user;
|
|
153
|
-
if (area_id !== undefined)
|
|
154
|
-
payload.area_id = area_id;
|
|
155
|
-
return ok(await ws.command("config/device_registry/update", payload));
|
|
156
|
-
}
|
|
157
|
-
catch (e) {
|
|
158
|
-
return err(e);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
server.tool("list_devices", "List devices from the device registry via the WebSocket API (id, name, name_by_user, manufacturer, model, area_id, config_entries). 'name' is the effective name (name_by_user if set, else the integration's name); 'name_by_user' is the user override or null; 'config_entries' are the entry_ids of the integrations providing the device (see list_config_entries). Useful for grouping entities by physical device when writing automations.", {}, async () => {
|
|
162
|
-
try {
|
|
163
|
-
const list = (await ws.command("config/device_registry/list"));
|
|
164
|
-
const devices = list.map((d) => ({
|
|
165
|
-
id: d.id,
|
|
166
|
-
name: d.name_by_user ?? d.name ?? null,
|
|
167
|
-
name_by_user: d.name_by_user ?? null,
|
|
168
|
-
manufacturer: d.manufacturer ?? null,
|
|
169
|
-
model: d.model ?? null,
|
|
170
|
-
area_id: d.area_id ?? null,
|
|
171
|
-
config_entries: d.config_entries ?? [],
|
|
172
|
-
}));
|
|
173
|
-
return ok({ count: devices.length, devices });
|
|
174
|
-
}
|
|
175
|
-
catch (e) {
|
|
176
|
-
return err(e);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
server.tool("list_areas", "List areas from the area registry via the WebSocket API (area_id, name, floor_id, icon). Areas group devices and entities by room or location.", {}, async () => {
|
|
180
|
-
try {
|
|
181
|
-
return ok(await ws.command("config/area_registry/list"));
|
|
182
|
-
}
|
|
183
|
-
catch (e) {
|
|
184
|
-
return err(e);
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
server.tool("list_labels", "List labels from the label registry via the WebSocket API (label_id, name, color, icon). Labels are cross-cutting tags applied to entities, devices and areas.", {}, async () => {
|
|
188
|
-
try {
|
|
189
|
-
return ok(await ws.command("config/label_registry/list"));
|
|
190
|
-
}
|
|
191
|
-
catch (e) {
|
|
192
|
-
return err(e);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
package/dist/tools/reload.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
export const RELOAD_TARGETS = {
|
|
4
|
-
all: { domain: "homeassistant", service: "reload_all" },
|
|
5
|
-
core: { domain: "homeassistant", service: "reload_core_config" },
|
|
6
|
-
automation: { domain: "automation", service: "reload" },
|
|
7
|
-
script: { domain: "script", service: "reload" },
|
|
8
|
-
scene: { domain: "scene", service: "reload" },
|
|
9
|
-
template: { domain: "template", service: "reload" },
|
|
10
|
-
group: { domain: "group", service: "reload" },
|
|
11
|
-
zone: { domain: "zone", service: "reload" },
|
|
12
|
-
person: { domain: "person", service: "reload" },
|
|
13
|
-
timer: { domain: "timer", service: "reload" },
|
|
14
|
-
schedule: { domain: "schedule", service: "reload" },
|
|
15
|
-
counter: { domain: "counter", service: "reload" },
|
|
16
|
-
input_boolean: { domain: "input_boolean", service: "reload" },
|
|
17
|
-
input_number: { domain: "input_number", service: "reload" },
|
|
18
|
-
input_select: { domain: "input_select", service: "reload" },
|
|
19
|
-
input_text: { domain: "input_text", service: "reload" },
|
|
20
|
-
input_datetime: { domain: "input_datetime", service: "reload" },
|
|
21
|
-
input_button: { domain: "input_button", service: "reload" },
|
|
22
|
-
rest_command: { domain: "rest_command", service: "reload" },
|
|
23
|
-
command_line: { domain: "command_line", service: "reload" },
|
|
24
|
-
mqtt: { domain: "mqtt", service: "reload" },
|
|
25
|
-
};
|
|
26
|
-
const targets = Object.keys(RELOAD_TARGETS);
|
|
27
|
-
export function registerReloadTools(server, client) {
|
|
28
|
-
server.tool("reload", "Reload a reloadable Home Assistant domain so YAML edits take effect without a full restart. Restricted to a fixed allowlist of safe reload services — it cannot control devices. Use 'all' (homeassistant.reload_all) to reload everything reloadable, or 'core' (homeassistant.reload_core_config) for core config like customize. Run check_config first.", {
|
|
29
|
-
target: z.enum(targets).describe(`What to reload. One of: ${targets.join(", ")}`),
|
|
30
|
-
}, async ({ target }) => {
|
|
31
|
-
try {
|
|
32
|
-
const { domain, service } = RELOAD_TARGETS[target];
|
|
33
|
-
const result = await client.fetch(`/api/services/${domain}/${service}`, { method: "POST" });
|
|
34
|
-
return ok({ reloaded: target, service: `${domain}.${service}`, result });
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
return err(e);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
package/dist/tools/resources.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
const resType = z
|
|
4
|
-
.enum(["module", "css", "js", "html"])
|
|
5
|
-
.describe("Resource type. Lovelace cards use 'module'; 'css' for stylesheets. Rarely 'js'/'html'.");
|
|
6
|
-
export function registerResourceTools(server, ws) {
|
|
7
|
-
server.tool("list_lovelace_resources", "List registered Lovelace (dashboard) resources (lovelace/resources) — the JS/CSS URLs, e.g. /hacsfiles/… bundles, loaded into every dashboard. Each item has an id (use it with update/delete), a type (module/css/js/html) and a url. Only works when the resource registry is in storage mode (lovelace: mode: storage); it is disabled if Lovelace is globally in YAML mode.", {}, async () => {
|
|
8
|
-
try {
|
|
9
|
-
return ok(await ws.command("lovelace/resources/list"));
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
return err(e);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
server.tool("create_lovelace_resource", "Register a new Lovelace resource (lovelace/resources/create) so a card bundle/stylesheet loads on every dashboard. Config authoring (like adding it under Settings → Dashboards → Resources); requires an admin token and storage-mode resources. Returns the created resource incl. its new id.", {
|
|
16
|
-
url: z
|
|
17
|
-
.string()
|
|
18
|
-
.describe("Resource URL, e.g. '/hacsfiles/my-card/my-card.js' or '/local/aurora-cards.js'."),
|
|
19
|
-
res_type: resType.optional().describe("Defaults to 'module' (the type Lovelace cards use)."),
|
|
20
|
-
}, async ({ url, res_type }) => {
|
|
21
|
-
try {
|
|
22
|
-
return ok(await ws.command("lovelace/resources/create", { url, res_type: res_type ?? "module" }));
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
return err(e);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
server.tool("update_lovelace_resource", "Update an existing Lovelace resource (lovelace/resources/update) — change its url and/or type. Config authoring; requires an admin token and storage-mode resources. Get the resource_id from list_lovelace_resources.", {
|
|
29
|
-
resource_id: z.string().describe("The resource id from list_lovelace_resources."),
|
|
30
|
-
url: z.string().optional().describe("New URL. Omit to keep the current one."),
|
|
31
|
-
res_type: resType.optional().describe("New type. Omit to keep the current one."),
|
|
32
|
-
}, async ({ resource_id, url, res_type }) => {
|
|
33
|
-
try {
|
|
34
|
-
const payload = { resource_id };
|
|
35
|
-
if (url !== undefined)
|
|
36
|
-
payload.url = url;
|
|
37
|
-
if (res_type !== undefined)
|
|
38
|
-
payload.res_type = res_type;
|
|
39
|
-
return ok(await ws.command("lovelace/resources/update", payload));
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
return err(e);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
server.tool("delete_lovelace_resource", "Delete a Lovelace resource (lovelace/resources/delete) by id — removes the JS/CSS URL from every dashboard. Config authoring; requires an admin token and storage-mode resources. Use this for manually-registered resources (e.g. /local/…) or a /hacsfiles/… resource that HACS did not auto-remove after remove_hacs_repository. Get the resource_id from list_lovelace_resources.", {
|
|
46
|
-
resource_id: z.string().describe("The resource id from list_lovelace_resources."),
|
|
47
|
-
}, async ({ resource_id }) => {
|
|
48
|
-
try {
|
|
49
|
-
await ws.command("lovelace/resources/delete", { resource_id });
|
|
50
|
-
return ok({ resource_id, deleted: true });
|
|
51
|
-
}
|
|
52
|
-
catch (e) {
|
|
53
|
-
return err(e);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
package/dist/tools/scripts.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
const SCRIPT_PREFIX = "script.";
|
|
4
|
-
async function resolveScriptKey(script, ws) {
|
|
5
|
-
if (!script.startsWith(SCRIPT_PREFIX))
|
|
6
|
-
return script;
|
|
7
|
-
try {
|
|
8
|
-
const entry = (await ws.command("config/entity_registry/get", {
|
|
9
|
-
entity_id: script,
|
|
10
|
-
}));
|
|
11
|
-
if (entry?.unique_id)
|
|
12
|
-
return String(entry.unique_id);
|
|
13
|
-
}
|
|
14
|
-
catch (e) {
|
|
15
|
-
if (e.code !== "not_found")
|
|
16
|
-
throw e;
|
|
17
|
-
}
|
|
18
|
-
return script.slice(SCRIPT_PREFIX.length);
|
|
19
|
-
}
|
|
20
|
-
async function resolveSceneId(scene, client) {
|
|
21
|
-
if (!scene.startsWith("scene."))
|
|
22
|
-
return scene;
|
|
23
|
-
const state = (await client.fetch(`/api/states/${encodeURIComponent(scene)}`));
|
|
24
|
-
const id = state.attributes?.id;
|
|
25
|
-
if (id === undefined || id === null || id === "") {
|
|
26
|
-
throw new Error(`Scene ${scene} has no 'id' attribute, so it is not stored in scenes.yaml and cannot be read or edited via the config API (it is defined elsewhere in YAML without an id, comes from an integration, or was made with scene.create)`);
|
|
27
|
-
}
|
|
28
|
-
return String(id);
|
|
29
|
-
}
|
|
30
|
-
const scriptPath = (key) => `/api/config/script/config/${encodeURIComponent(key)}`;
|
|
31
|
-
const scenePath = (id) => `/api/config/scene/config/${encodeURIComponent(id)}`;
|
|
32
|
-
const blueprintDomain = z
|
|
33
|
-
.enum(["automation", "script", "template"])
|
|
34
|
-
.describe("Blueprint domain: 'automation', 'script' or 'template'");
|
|
35
|
-
const blueprintFile = z
|
|
36
|
-
.string()
|
|
37
|
-
.describe("Blueprint path relative to config/blueprints/<domain>/, including .yaml, e.g. 'homeassistant/motion_light.yaml'");
|
|
38
|
-
export function registerScriptTools(server, client, ws) {
|
|
39
|
-
server.tool("get_script_config", "Get the stored configuration of a script (alias, description, icon, mode, fields, variables, sequence) from scripts.yaml via GET /api/config/script/config/{key} (requires an admin token). Accepts the script.* entity id (its key is resolved through the entity registry) or the script's key itself (the object id, e.g. 'goodnight' for script.goodnight). Only scripts stored in scripts.yaml (UI-created) are accessible; others return 404.", {
|
|
40
|
-
script: z
|
|
41
|
-
.string()
|
|
42
|
-
.describe("script.* entity id, or the script's key (the object id after 'script.')"),
|
|
43
|
-
}, async ({ script }) => {
|
|
44
|
-
try {
|
|
45
|
-
const key = await resolveScriptKey(script, ws);
|
|
46
|
-
return ok(await client.fetch(scriptPath(key)));
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
return err(e);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
server.tool("upsert_script", "Create a new script or replace an existing one via POST /api/config/script/config/{key} (requires an admin token). Home Assistant validates the config including the sequence's actions, writes it to scripts.yaml and reloads scripts automatically — no separate reload needed. The config REPLACES the stored one entirely, so to edit call get_script_config first and send back the modified object. The key is the script's object id: pass a new key (lowercase letters, digits and underscores, e.g. 'goodnight'; not reload, turn_on, turn_off or toggle) to create script.<key> — it cannot be renamed later — or an existing script.* entity id or key to replace that script. Writing a script does not run it; its sequence runs when an automation, scene or service call invokes it.", {
|
|
53
|
-
script: z
|
|
54
|
-
.string()
|
|
55
|
-
.describe("New script key (e.g. 'goodnight') to create script.<key>, or the script.* entity id / key of an existing script to replace"),
|
|
56
|
-
config: z
|
|
57
|
-
.record(z.unknown())
|
|
58
|
-
.describe("Full script config as stored in scripts.yaml: alias, sequence (list of actions), description?, icon?, mode? (single|restart|queued|parallel), max?, max_exceeded?, fields?, variables? — or a blueprint instance {use_blueprint: {path, input}, alias?, description?}"),
|
|
59
|
-
}, async ({ script, config }) => {
|
|
60
|
-
try {
|
|
61
|
-
const key = await resolveScriptKey(script, ws);
|
|
62
|
-
const result = await client.fetch(scriptPath(key), {
|
|
63
|
-
method: "POST",
|
|
64
|
-
body: JSON.stringify(config),
|
|
65
|
-
});
|
|
66
|
-
return ok({ key, result });
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
return err(e);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
server.tool("delete_script", "Delete a script from scripts.yaml via DELETE /api/config/script/config/{key} (requires an admin token). Accepts the script.* entity id or the key. Only scripts stored in scripts.yaml can be deleted; Home Assistant then removes the script's entity registry entry. Automations and scenes that call the script are not updated.", {
|
|
73
|
-
script: z
|
|
74
|
-
.string()
|
|
75
|
-
.describe("script.* entity id, or the script's key (the object id after 'script.')"),
|
|
76
|
-
}, async ({ script }) => {
|
|
77
|
-
try {
|
|
78
|
-
const key = await resolveScriptKey(script, ws);
|
|
79
|
-
return ok(await client.fetch(scriptPath(key), { method: "DELETE" }));
|
|
80
|
-
}
|
|
81
|
-
catch (e) {
|
|
82
|
-
return err(e);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
server.tool("get_scene_config", "Get the stored configuration of a scene (id, name, icon, entities, metadata) from scenes.yaml via GET /api/config/scene/config/{id} (requires an admin token). Accepts the scene.* entity id (the internal id is resolved automatically) or the internal id itself. Only scenes with an 'id' (UI-created, stored in scenes.yaml) are accessible.", {
|
|
86
|
-
scene: z
|
|
87
|
-
.string()
|
|
88
|
-
.describe("scene.* entity id, or the scene's internal id (the 'id' attribute)"),
|
|
89
|
-
}, async ({ scene }) => {
|
|
90
|
-
try {
|
|
91
|
-
const id = await resolveSceneId(scene, client);
|
|
92
|
-
return ok(await client.fetch(scenePath(id)));
|
|
93
|
-
}
|
|
94
|
-
catch (e) {
|
|
95
|
-
return err(e);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
server.tool("upsert_scene", "Create a new scene or replace an existing one via POST /api/config/scene/config/{id} (requires an admin token). Home Assistant validates the config, writes it to scenes.yaml and reloads scenes automatically — no separate reload needed. The config REPLACES the stored one entirely, so to edit call get_scene_config first and send back the modified object. Omit 'scene' to create a new one (an id is generated and returned); its entity id becomes scene.<slugified name>. Writing a scene does not activate it.", {
|
|
99
|
-
config: z
|
|
100
|
-
.record(z.unknown())
|
|
101
|
-
.describe("Full scene config as stored in scenes.yaml: name, entities (map of entity_id to a state string such as 'on', or an object {state, ...attributes} such as {state: 'on', brightness: 120}), icon?, metadata? (free-form object). An 'id' key, if present, must equal the scene's id"),
|
|
102
|
-
scene: z
|
|
103
|
-
.string()
|
|
104
|
-
.optional()
|
|
105
|
-
.describe("Existing scene.* entity id or internal id to replace; omit to create a new scene"),
|
|
106
|
-
}, async ({ config, scene }) => {
|
|
107
|
-
try {
|
|
108
|
-
const id = scene ? await resolveSceneId(scene, client) : String(Date.now());
|
|
109
|
-
const result = await client.fetch(scenePath(id), {
|
|
110
|
-
method: "POST",
|
|
111
|
-
body: JSON.stringify(config),
|
|
112
|
-
});
|
|
113
|
-
return ok({ id, created: !scene, result });
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
return err(e);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
server.tool("delete_scene", "Delete a scene from scenes.yaml via DELETE /api/config/scene/config/{id} (requires an admin token). Accepts the scene.* entity id or the internal id. Only UI-managed scenes (those stored in scenes.yaml) can be deleted; Home Assistant then removes the scene's entity registry entry. Automations and scripts that activate the scene are not updated.", {
|
|
120
|
-
scene: z
|
|
121
|
-
.string()
|
|
122
|
-
.describe("scene.* entity id, or the scene's internal id (the 'id' attribute)"),
|
|
123
|
-
}, async ({ scene }) => {
|
|
124
|
-
try {
|
|
125
|
-
const id = await resolveSceneId(scene, client);
|
|
126
|
-
return ok(await client.fetch(scenePath(id), { method: "DELETE" }));
|
|
127
|
-
}
|
|
128
|
-
catch (e) {
|
|
129
|
-
return err(e);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
server.tool("list_blueprints", "List the installed blueprints of one domain via the WebSocket API (blueprint/list, requires an admin token). Returns an object keyed by blueprint path relative to config/blueprints/<domain>/, e.g. 'homeassistant/motion_light.yaml' — use that exact path, including .yaml, as use_blueprint.path in an automation or script and in delete_blueprint / substitute_blueprint. Each value is {metadata: {name, description, domain, input, source_url, ...}} or {error: '...'} for a file that failed to load.", { domain: blueprintDomain }, async ({ domain }) => {
|
|
133
|
-
try {
|
|
134
|
-
return ok(await ws.command("blueprint/list", { domain }));
|
|
135
|
-
}
|
|
136
|
-
catch (e) {
|
|
137
|
-
return err(e);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
server.tool("import_blueprint", "Fetch a blueprint from a URL via the WebSocket API (blueprint/import, requires an admin token) WITHOUT saving it. Home Assistant itself downloads and validates the YAML (community forum topics, GitHub file and gist URLs, home-assistant.io blueprint URLs, or any URL that returns blueprint YAML; 10 second timeout). Returns suggested_filename (e.g. 'homeassistant/motion_light', without .yaml), raw_data (the YAML text), blueprint.metadata (name, domain, inputs), validation_errors (null when this Home Assistant supports the blueprint, otherwise a list of reasons) and exists (true when that filename is already installed). To install it, review raw_data and call save_blueprint with domain = blueprint.metadata.domain, path = suggested_filename, yaml = raw_data and source_url = this url.", {
|
|
141
|
-
url: z
|
|
142
|
-
.string()
|
|
143
|
-
.describe("URL of the blueprint, e.g. a GitHub file URL or a Home Assistant community topic"),
|
|
144
|
-
}, async ({ url }) => {
|
|
145
|
-
try {
|
|
146
|
-
return ok(await ws.command("blueprint/import", { url }));
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
return err(e);
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
server.tool("save_blueprint", "Save a blueprint file to config/blueprints/<domain>/<path> via the WebSocket API (blueprint/save, requires an admin token). Home Assistant parses and validates the YAML as a blueprint of that domain (a different blueprint.domain is rejected), rewrites it in its own normalized YAML (comments are dropped) and records source_url in the metadata when given. Fails with 'already_exists' unless allow_override is true; overriding an existing blueprint reloads the automations/scripts that use it. '.yaml' is appended to the path when missing. Saving only makes the blueprint available — it creates no automation or script. Returns {domain, path, overrides_existing}.", {
|
|
153
|
-
domain: blueprintDomain,
|
|
154
|
-
path: z
|
|
155
|
-
.string()
|
|
156
|
-
.describe("Path relative to config/blueprints/<domain>/, e.g. 'homeassistant/motion_light' (the suggested_filename from import_blueprint); '.yaml' is appended when missing"),
|
|
157
|
-
yaml: z.string().describe("The blueprint YAML text (raw_data from import_blueprint)"),
|
|
158
|
-
source_url: z
|
|
159
|
-
.string()
|
|
160
|
-
.optional()
|
|
161
|
-
.describe("URL the blueprint came from; stored in its metadata so the UI can offer re-import"),
|
|
162
|
-
allow_override: z
|
|
163
|
-
.boolean()
|
|
164
|
-
.optional()
|
|
165
|
-
.describe("Overwrite an existing blueprint at this path (default false: fail if it exists)"),
|
|
166
|
-
}, async ({ domain, path, yaml, source_url, allow_override }) => {
|
|
167
|
-
try {
|
|
168
|
-
const payload = { domain, path, yaml };
|
|
169
|
-
if (source_url !== undefined)
|
|
170
|
-
payload.source_url = source_url;
|
|
171
|
-
if (allow_override !== undefined)
|
|
172
|
-
payload.allow_override = allow_override;
|
|
173
|
-
const result = (await ws.command("blueprint/save", payload));
|
|
174
|
-
return ok({ domain, path: path.endsWith(".yaml") ? path : `${path}.yaml`, ...result });
|
|
175
|
-
}
|
|
176
|
-
catch (e) {
|
|
177
|
-
return err(e);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
server.tool("delete_blueprint", "Delete a blueprint file via the WebSocket API (blueprint/delete, requires an admin token). Home Assistant refuses with 'Blueprint in use' while an automation, script or template still uses it. Use the exact path from list_blueprints, including .yaml.", {
|
|
181
|
-
domain: blueprintDomain,
|
|
182
|
-
path: blueprintFile,
|
|
183
|
-
}, async ({ domain, path }) => {
|
|
184
|
-
try {
|
|
185
|
-
await ws.command("blueprint/delete", { domain, path });
|
|
186
|
-
return ok({ domain, path, deleted: true });
|
|
187
|
-
}
|
|
188
|
-
catch (e) {
|
|
189
|
-
return err(e);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
server.tool("substitute_blueprint", "Preview the configuration a blueprint instance generates via the WebSocket API (blueprint/substitute, requires an admin token) without saving or changing anything. Returns {substituted_config}: the automation/script/template config with the blueprint's !input placeholders filled in from 'input' (defaults fill the rest; a missing required input fails). Use it to inspect or hand-edit a blueprint-based automation: take use_blueprint.path and use_blueprint.input from get_automation_config and pass them here.", {
|
|
193
|
-
domain: blueprintDomain,
|
|
194
|
-
path: blueprintFile,
|
|
195
|
-
input: z
|
|
196
|
-
.record(z.unknown())
|
|
197
|
-
.describe("Blueprint input values by input name, as in use_blueprint.input"),
|
|
198
|
-
}, async ({ domain, path, input }) => {
|
|
199
|
-
try {
|
|
200
|
-
return ok(await ws.command("blueprint/substitute", { domain, path, input }));
|
|
201
|
-
}
|
|
202
|
-
catch (e) {
|
|
203
|
-
return err(e);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
package/dist/tools/services.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { ok, err } from "../hass/format.js";
|
|
3
|
-
export function registerServiceTools(server, client) {
|
|
4
|
-
server.tool("list_services", "List services that can be called — the actions available in automations and scripts. With no domain, returns a compact map of domain -> service names. Pass a domain to get full detail (description, fields, target, selectors) for that domain's services.", {
|
|
5
|
-
domain: z
|
|
6
|
-
.string()
|
|
7
|
-
.optional()
|
|
8
|
-
.describe("Only this domain, e.g. 'light', 'notify', 'automation'. Returns full field/target detail when set."),
|
|
9
|
-
}, async ({ domain }) => {
|
|
10
|
-
try {
|
|
11
|
-
const data = (await client.fetch("/api/services"));
|
|
12
|
-
if (domain) {
|
|
13
|
-
const entry = data.find((d) => d.domain === domain);
|
|
14
|
-
return ok(entry ?? { domain, services: {} });
|
|
15
|
-
}
|
|
16
|
-
const domains = data
|
|
17
|
-
.map((d) => ({ domain: d.domain, services: Object.keys(d.services ?? {}) }))
|
|
18
|
-
.sort((a, b) => a.domain.localeCompare(b.domain));
|
|
19
|
-
return ok({ count: domains.length, domains });
|
|
20
|
-
}
|
|
21
|
-
catch (e) {
|
|
22
|
-
return err(e);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
server.tool("list_events", "List the event types the instance is currently listening for, with listener counts. Useful for discovering events to use as automation triggers (the 'event' trigger platform).", {}, async () => {
|
|
26
|
-
try {
|
|
27
|
-
return ok(await client.fetch("/api/events"));
|
|
28
|
-
}
|
|
29
|
-
catch (e) {
|
|
30
|
-
return err(e);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|