@orellbuehler/homeassistant-mcp 0.8.1 → 1.0.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.
@@ -0,0 +1,216 @@
1
+ import { z } from "zod";
2
+ import { buildQS, ok, err } from "../hass/format.js";
3
+ const calendarEntity = z.string().describe("Calendar entity_id, e.g. 'calendar.family'");
4
+ const recurrenceId = z
5
+ .string()
6
+ .optional()
7
+ .describe("recurrence_id of one occurrence of a recurring event (from get_calendar_events). Omit to address the whole event.");
8
+ const recurrenceRange = z
9
+ .string()
10
+ .optional()
11
+ .describe("Which occurrences with recurrence_id: 'THISANDFUTURE' for that occurrence and all later ones; omit for just that occurrence.");
12
+ const eventShape = {
13
+ summary: z.string().describe("Event title"),
14
+ start: z
15
+ .string()
16
+ .describe("Start: 'YYYY-MM-DD' for an all-day event, otherwise an ISO 8601 datetime like '2026-12-01T09:00:00' or '2026-12-01T09:00:00+01:00' (no offset = Home Assistant's time zone). Sent as dtstart."),
17
+ end: z
18
+ .string()
19
+ .describe("End, same kind as start and with the same offset. Must be after start; for an all-day event it is the day after the last day (a one-day event ends the next day). Sent as dtend."),
20
+ description: z.string().optional().describe("Event description"),
21
+ location: z.string().optional().describe("Event location"),
22
+ rrule: z
23
+ .string()
24
+ .optional()
25
+ .describe("Recurrence rule, e.g. 'FREQ=WEEKLY;BYDAY=MO'; FREQ must be DAILY, WEEKLY, MONTHLY or YEARLY"),
26
+ };
27
+ function buildEvent({ summary, start, end, description, location, rrule }) {
28
+ const event = { dtstart: start, dtend: end, summary };
29
+ if (description !== undefined)
30
+ event.description = description;
31
+ if (location !== undefined)
32
+ event.location = location;
33
+ if (rrule !== undefined)
34
+ event.rrule = rrule;
35
+ return event;
36
+ }
37
+ const todoEntity = z.string().describe("To-do list entity_id, e.g. 'todo.shopping_list'");
38
+ const shoppingItemId = z.string().describe("The item 'id' from list_shopping_list_items");
39
+ export function registerListTools(server, ws, client) {
40
+ server.tool("list_calendars", "List the calendar entities readable by the token (GET /api/calendars) as [{ name, entity_id }], sorted by name. Use the entity_id with get_calendar_events and the calendar event tools.", {}, async () => {
41
+ try {
42
+ return ok(await client.fetch("/api/calendars"));
43
+ }
44
+ catch (e) {
45
+ return err(e);
46
+ }
47
+ });
48
+ server.tool("get_calendar_events", "Get the events of a calendar entity between two instants (GET /api/calendars/{entity_id}?start=&end=). Returns [{ summary, start, end, description, location, uid, recurrence_id, rrule, status }] where start/end are { date } (all-day) or { dateTime } objects in Home Assistant's time zone. Calendar integrations return recurring events as individual occurrences. Both start and end are required; Home Assistant answers a missing or invalid range, start after end, or an entity that is not a calendar with a bare '400 Bad Request'.", {
49
+ entity_id: calendarEntity,
50
+ start: z
51
+ .string()
52
+ .describe("Range start as ISO 8601 datetime, e.g. '2026-12-01T00:00:00+01:00' (no offset = Home Assistant's time zone)"),
53
+ end: z.string().describe("Range end as ISO 8601 datetime, e.g. '2026-12-08T00:00:00+01:00'"),
54
+ }, async ({ entity_id, start, end }) => {
55
+ try {
56
+ return ok(await client.fetch(`/api/calendars/${encodeURIComponent(entity_id)}${buildQS({ start, end })}`));
57
+ }
58
+ catch (e) {
59
+ return err(e);
60
+ }
61
+ });
62
+ server.tool("create_calendar_event", "Create an event on a calendar entity via the calendar/event/create WebSocket command. Only calendars that support event creation work (e.g. Local Calendar); read-only calendars fail with 'Calendar does not support event creation'. Changes calendar data only, it does not control any device. Home Assistant returns no result for the new event, so call get_calendar_events afterwards to find its uid.", {
63
+ entity_id: calendarEntity,
64
+ ...eventShape,
65
+ }, async ({ entity_id, ...fields }) => {
66
+ try {
67
+ const event = buildEvent(fields);
68
+ await ws.command("calendar/event/create", { entity_id, event });
69
+ return ok({ entity_id, created: true });
70
+ }
71
+ catch (e) {
72
+ return err(e);
73
+ }
74
+ });
75
+ server.tool("update_calendar_event", "Update an event on a calendar entity via calendar/event/update. summary, start and end are always required (Home Assistant validates the full event), so read the event with get_calendar_events first and resend the unchanged fields; whether an omitted description/location/rrule is cleared or kept depends on the calendar integration. Only calendars that support event updates work. For one occurrence of a recurring event pass recurrence_id (and recurrence_range 'THISANDFUTURE' to also change all later ones). Get the uid from get_calendar_events.", {
76
+ entity_id: calendarEntity,
77
+ uid: z.string().describe("The event uid from get_calendar_events"),
78
+ recurrence_id: recurrenceId,
79
+ recurrence_range: recurrenceRange,
80
+ ...eventShape,
81
+ }, async ({ entity_id, uid, recurrence_id, recurrence_range, ...fields }) => {
82
+ try {
83
+ const payload = { entity_id, uid, event: buildEvent(fields) };
84
+ if (recurrence_id !== undefined)
85
+ payload.recurrence_id = recurrence_id;
86
+ if (recurrence_range !== undefined)
87
+ payload.recurrence_range = recurrence_range;
88
+ await ws.command("calendar/event/update", payload);
89
+ return ok({ entity_id, uid, updated: true });
90
+ }
91
+ catch (e) {
92
+ return err(e);
93
+ }
94
+ });
95
+ server.tool("delete_calendar_event", "Delete an event from a calendar entity via calendar/event/delete. Only calendars that support event deletion work. Irreversible. For one occurrence of a recurring event pass recurrence_id (and recurrence_range 'THISANDFUTURE' to also delete all later ones); without recurrence_id the whole event is addressed. Get the uid from get_calendar_events.", {
96
+ entity_id: calendarEntity,
97
+ uid: z.string().describe("The event uid from get_calendar_events"),
98
+ recurrence_id: recurrenceId,
99
+ recurrence_range: recurrenceRange,
100
+ }, async ({ entity_id, uid, recurrence_id, recurrence_range }) => {
101
+ try {
102
+ const payload = { entity_id, uid };
103
+ if (recurrence_id !== undefined)
104
+ payload.recurrence_id = recurrence_id;
105
+ if (recurrence_range !== undefined)
106
+ payload.recurrence_range = recurrence_range;
107
+ await ws.command("calendar/event/delete", payload);
108
+ return ok({ entity_id, uid, deleted: true });
109
+ }
110
+ catch (e) {
111
+ return err(e);
112
+ }
113
+ });
114
+ server.tool("list_todo_items", "List the items of a to-do list entity via todo/item/list: { items: [{ uid, summary, status ('needs_action' or 'completed'), due, description, completed }] } in list order (empty fields are omitted). Find to-do entities with list_entities (domain 'todo'). Adding, renaming, completing and removing items are Home Assistant services (todo.add_item, todo.update_item, todo.remove_item, todo.remove_completed_items, todo.get_items), not WebSocket commands, so they are not part of this tool group.", {
115
+ entity_id: todoEntity,
116
+ }, async ({ entity_id }) => {
117
+ try {
118
+ return ok(await ws.command("todo/item/list", { entity_id }));
119
+ }
120
+ catch (e) {
121
+ return err(e);
122
+ }
123
+ });
124
+ server.tool("move_todo_item", "Move an item within a to-do list entity via todo/item/move, to the position right after previous_uid or to the first position when previous_uid is omitted. Only lists that support reordering work; others fail with 'To-do list does not support To-do item reordering'. Get uids from list_todo_items.", {
125
+ entity_id: todoEntity,
126
+ uid: z.string().describe("uid of the item to move"),
127
+ previous_uid: z
128
+ .string()
129
+ .optional()
130
+ .describe("uid of the item it should come after; omit to move it to the top"),
131
+ }, async ({ entity_id, uid, previous_uid }) => {
132
+ try {
133
+ const payload = { entity_id, uid };
134
+ if (previous_uid !== undefined)
135
+ payload.previous_uid = previous_uid;
136
+ await ws.command("todo/item/move", payload);
137
+ return ok({ entity_id, uid, moved: true });
138
+ }
139
+ catch (e) {
140
+ return err(e);
141
+ }
142
+ });
143
+ server.tool("list_shopping_list_items", "List all items of the Shopping list integration via shopping_list/items: [{ id, name, complete }] in list order. Requires the Shopping list integration to be set up; the same list is also exposed as a to-do entity (typically todo.shopping_list, with uid = id and summary = name).", {}, async () => {
144
+ try {
145
+ return ok(await ws.command("shopping_list/items"));
146
+ }
147
+ catch (e) {
148
+ return err(e);
149
+ }
150
+ });
151
+ server.tool("add_shopping_list_item", "Add an item to the shopping list via shopping_list/items/add. Appends it as not complete and returns the new item { id, name, complete }. Does not de-duplicate: check list_shopping_list_items first if the name may already be on the list.", {
152
+ name: z.string().describe("Item name, e.g. 'Milk'"),
153
+ }, async ({ name }) => {
154
+ try {
155
+ return ok(await ws.command("shopping_list/items/add", { name }));
156
+ }
157
+ catch (e) {
158
+ return err(e);
159
+ }
160
+ });
161
+ server.tool("update_shopping_list_item", "Rename a shopping list item and/or tick it off or untick it via shopping_list/items/update. At least one of name or complete is required. Returns the updated item { id, name, complete }; an unknown id fails with 'Item not found'.", {
162
+ item_id: shoppingItemId,
163
+ name: z.string().optional().describe("New item name"),
164
+ complete: z
165
+ .boolean()
166
+ .optional()
167
+ .describe("true to mark the item as bought/completed, false to put it back on the list"),
168
+ }, async ({ item_id, name, complete }) => {
169
+ try {
170
+ if (name === undefined && complete === undefined) {
171
+ throw new Error("Provide at least one of 'name' or 'complete' to update the item");
172
+ }
173
+ const payload = { item_id };
174
+ if (name !== undefined)
175
+ payload.name = name;
176
+ if (complete !== undefined)
177
+ payload.complete = complete;
178
+ return ok(await ws.command("shopping_list/items/update", payload));
179
+ }
180
+ catch (e) {
181
+ return err(e);
182
+ }
183
+ });
184
+ server.tool("remove_shopping_list_item", "Remove one item from the shopping list via shopping_list/items/remove. Returns the removed item { id, name, complete }; an unknown id fails with 'Item not found'. To drop every ticked-off item at once use clear_completed_shopping_items.", {
185
+ item_id: shoppingItemId,
186
+ }, async ({ item_id }) => {
187
+ try {
188
+ return ok(await ws.command("shopping_list/items/remove", { item_id }));
189
+ }
190
+ catch (e) {
191
+ return err(e);
192
+ }
193
+ });
194
+ server.tool("clear_completed_shopping_items", "Remove all completed (ticked-off) items from the shopping list via shopping_list/items/clear. Irreversible; incomplete items are untouched. Check list_shopping_list_items first to see what will go.", {}, async () => {
195
+ try {
196
+ await ws.command("shopping_list/items/clear");
197
+ return ok({ cleared: true });
198
+ }
199
+ catch (e) {
200
+ return err(e);
201
+ }
202
+ });
203
+ server.tool("reorder_shopping_list", "Reorder the shopping list via shopping_list/items/reorder: the items come out in the order of item_ids. item_ids must contain EVERY incomplete item (otherwise Home Assistant rejects it with 'doesn't contain all the unchecked shopping list items'); completed items may be omitted and are then appended at the end in their current order. An unknown id fails with 'One or more item id(s) not found'. To move a single item use move_todo_item on the shopping list's to-do entity instead.", {
204
+ item_ids: z
205
+ .array(z.string())
206
+ .describe("Item ids from list_shopping_list_items in the desired order, first = top"),
207
+ }, async ({ item_ids }) => {
208
+ try {
209
+ await ws.command("shopping_list/items/reorder", { item_ids });
210
+ return ok({ reordered: true, count: item_ids.length });
211
+ }
212
+ catch (e) {
213
+ return err(e);
214
+ }
215
+ });
216
+ }