@raineworks/ha-dashboards 0.0.0-stage → 1.1.1
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 +16 -2
- package/dist/house.js +461 -0
- package/dist/mcp.js +1252 -0
- package/dist/types/kit/src/house.d.ts +8 -0
- package/dist/types/ui/src/lib/bedtime.d.ts +122 -0
- package/dist/types/ui/src/lib/color.d.ts +4 -0
- package/dist/types/ui/src/lib/hass-store.d.ts +46 -0
- package/dist/types/ui/src/lib/house-config.d.ts +97 -0
- package/dist/types/ui/src/lib/household.d.ts +33 -0
- package/dist/types/ui/src/lib/labels.d.ts +11 -0
- package/dist/types/ui/src/lib/lights.d.ts +204 -0
- package/dist/types/ui/src/lib/music.d.ts +489 -0
- package/dist/types/ui/src/lib/people.d.ts +119 -0
- package/dist/types/ui/src/lib/service-calls.d.ts +19 -0
- package/dist/types/ui/src/lib/text.d.ts +15 -0
- package/dist/types/ui/src/lib/time.d.ts +33 -0
- package/dist/types/ui/src/lib/tv.d.ts +156 -0
- package/dist/types/ui/src/lib/values.d.ts +11 -0
- package/dist/types/ui/src/types/homeassistant.d.ts +130 -0
- package/package.json +35 -5
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,1252 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// ../ha-client/src/index.ts
|
|
5
|
+
import { existsSync, readFileSync } from "fs";
|
|
6
|
+
import { resolve } from "path";
|
|
7
|
+
var REPO_ENV = resolve(import.meta.dir, "../../../.env");
|
|
8
|
+
function parseEnv(text) {
|
|
9
|
+
const env = {};
|
|
10
|
+
for (const line of text.split(`
|
|
11
|
+
`)) {
|
|
12
|
+
const match = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(line);
|
|
13
|
+
if (!match || line.trim().startsWith("#"))
|
|
14
|
+
continue;
|
|
15
|
+
env[match[1]] = match[2].replace(/^(['"])(.*)\1$/, "$2");
|
|
16
|
+
}
|
|
17
|
+
return env;
|
|
18
|
+
}
|
|
19
|
+
var repoEnv;
|
|
20
|
+
function setting(name) {
|
|
21
|
+
if (Bun.env[name])
|
|
22
|
+
return Bun.env[name];
|
|
23
|
+
repoEnv ??= existsSync(REPO_ENV) ? parseEnv(readFileSync(REPO_ENV, "utf8")) : {};
|
|
24
|
+
return repoEnv[name] || undefined;
|
|
25
|
+
}
|
|
26
|
+
var homeAssistantUrl = () => setting("HA_URL")?.replace(/\/+$/, "");
|
|
27
|
+
function homeAssistantToken() {
|
|
28
|
+
const token = setting("HA_TOKEN");
|
|
29
|
+
if (token)
|
|
30
|
+
return token;
|
|
31
|
+
const config = setting("HA_MCP_CONFIG");
|
|
32
|
+
if (!config || !existsSync(config))
|
|
33
|
+
return;
|
|
34
|
+
const parsed = JSON.parse(readFileSync(config, "utf8"));
|
|
35
|
+
return parsed?.mcpServers?.homeassistant?.env?.HOMEASSISTANT_TOKEN ?? undefined;
|
|
36
|
+
}
|
|
37
|
+
function connectHomeAssistant(timeoutMs = 1e4) {
|
|
38
|
+
const url = homeAssistantUrl();
|
|
39
|
+
const token = homeAssistantToken();
|
|
40
|
+
if (!url)
|
|
41
|
+
return Promise.reject(new Error("Set HA_URL to your Home Assistant (in the environment or .env)"));
|
|
42
|
+
if (!token)
|
|
43
|
+
return Promise.reject(new Error("Set HA_TOKEN to a long-lived Home Assistant token"));
|
|
44
|
+
return new Promise((resolveConnection, rejectConnection) => {
|
|
45
|
+
const ws = new WebSocket(`${url.replace(/^http/, "ws")}/api/websocket`);
|
|
46
|
+
let nextId = 1;
|
|
47
|
+
const pending = new Map;
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
ws.close();
|
|
50
|
+
rejectConnection(new Error(`No answer from Home Assistant at ${url}`));
|
|
51
|
+
}, timeoutMs);
|
|
52
|
+
const connection = {
|
|
53
|
+
call: (message) => new Promise((resolve, reject) => {
|
|
54
|
+
const id = nextId++;
|
|
55
|
+
pending.set(id, { resolve, reject });
|
|
56
|
+
ws.send(JSON.stringify({ ...message, id }));
|
|
57
|
+
}),
|
|
58
|
+
close: () => ws.close()
|
|
59
|
+
};
|
|
60
|
+
ws.onmessage = (event) => {
|
|
61
|
+
const msg = JSON.parse(String(event.data));
|
|
62
|
+
if (msg.type === "auth_required")
|
|
63
|
+
ws.send(JSON.stringify({ type: "auth", access_token: token }));
|
|
64
|
+
else if (msg.type === "auth_invalid") {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
ws.close();
|
|
67
|
+
rejectConnection(new Error("Home Assistant refused the token"));
|
|
68
|
+
} else if (msg.type === "auth_ok") {
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
resolveConnection(connection);
|
|
71
|
+
} else if (msg.type === "result" && pending.has(msg.id)) {
|
|
72
|
+
const call = pending.get(msg.id);
|
|
73
|
+
pending.delete(msg.id);
|
|
74
|
+
if (msg.success)
|
|
75
|
+
call?.resolve(msg.result);
|
|
76
|
+
else
|
|
77
|
+
call?.reject(new Error(msg.error?.message ?? "Home Assistant refused the command"));
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
ws.onerror = () => {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
rejectConnection(new Error(`Couldn't reach Home Assistant at ${url}`));
|
|
83
|
+
};
|
|
84
|
+
ws.onclose = () => {
|
|
85
|
+
for (const call of pending.values())
|
|
86
|
+
call.reject(new Error("Home Assistant closed the connection"));
|
|
87
|
+
pending.clear();
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ../mcp/src/server.ts
|
|
93
|
+
var PROTOCOL_VERSION = "2025-06-18";
|
|
94
|
+
var METHOD_NOT_FOUND = -32601;
|
|
95
|
+
var INVALID_PARAMS = -32602;
|
|
96
|
+
var INVALID_REQUEST = -32600;
|
|
97
|
+
function createServer(tools, info) {
|
|
98
|
+
const byName = new Map(tools.map((tool) => [tool.name, tool]));
|
|
99
|
+
async function handle(message) {
|
|
100
|
+
if (message.id === undefined)
|
|
101
|
+
return;
|
|
102
|
+
const id = message.id;
|
|
103
|
+
const ok = (result) => ({ jsonrpc: "2.0", id, result });
|
|
104
|
+
const fail = (code, text) => ({ jsonrpc: "2.0", id, error: { code, message: text } });
|
|
105
|
+
switch (message.method) {
|
|
106
|
+
case "initialize":
|
|
107
|
+
return ok({
|
|
108
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
109
|
+
capabilities: { tools: {} },
|
|
110
|
+
serverInfo: { name: info.name, version: info.version },
|
|
111
|
+
...info.instructions ? { instructions: info.instructions } : {}
|
|
112
|
+
});
|
|
113
|
+
case "ping":
|
|
114
|
+
return ok({});
|
|
115
|
+
case "tools/list":
|
|
116
|
+
return ok({
|
|
117
|
+
tools: tools.map(({ name, description, inputSchema, writes }) => ({
|
|
118
|
+
name,
|
|
119
|
+
description,
|
|
120
|
+
inputSchema,
|
|
121
|
+
annotations: { readOnlyHint: !writes }
|
|
122
|
+
}))
|
|
123
|
+
});
|
|
124
|
+
case "tools/call": {
|
|
125
|
+
const name = message.params?.name;
|
|
126
|
+
const tool = typeof name === "string" ? byName.get(name) : undefined;
|
|
127
|
+
if (!tool)
|
|
128
|
+
return fail(INVALID_PARAMS, `No tool named ${String(name)}`);
|
|
129
|
+
const args = message.params?.arguments ?? {};
|
|
130
|
+
for (const key of tool.inputSchema.required ?? [])
|
|
131
|
+
if (args[key] === undefined)
|
|
132
|
+
return fail(INVALID_PARAMS, `${tool.name} needs ${key}`);
|
|
133
|
+
try {
|
|
134
|
+
return ok({ content: [{ type: "text", text: await tool.run(args) }] });
|
|
135
|
+
} catch (err) {
|
|
136
|
+
return ok({ content: [{ type: "text", text: err.message }], isError: true });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
case undefined:
|
|
140
|
+
return fail(INVALID_REQUEST, "No method");
|
|
141
|
+
default:
|
|
142
|
+
return fail(METHOD_NOT_FOUND, `Unknown method ${message.method}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return { handle };
|
|
146
|
+
}
|
|
147
|
+
async function serveStdio(handle) {
|
|
148
|
+
const decoder = new TextDecoder;
|
|
149
|
+
let buffered = "";
|
|
150
|
+
const reply = (response) => {
|
|
151
|
+
if (response)
|
|
152
|
+
process.stdout.write(`${JSON.stringify(response)}
|
|
153
|
+
`);
|
|
154
|
+
};
|
|
155
|
+
const reader = Bun.stdin.stream().getReader();
|
|
156
|
+
for (;; ) {
|
|
157
|
+
const { value: chunk, done } = await reader.read();
|
|
158
|
+
if (done)
|
|
159
|
+
break;
|
|
160
|
+
buffered += decoder.decode(chunk, { stream: true });
|
|
161
|
+
let newline = buffered.indexOf(`
|
|
162
|
+
`);
|
|
163
|
+
while (newline >= 0) {
|
|
164
|
+
const line = buffered.slice(0, newline).trim();
|
|
165
|
+
buffered = buffered.slice(newline + 1);
|
|
166
|
+
if (line) {
|
|
167
|
+
let message;
|
|
168
|
+
try {
|
|
169
|
+
message = JSON.parse(line);
|
|
170
|
+
} catch {
|
|
171
|
+
reply({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
|
|
172
|
+
}
|
|
173
|
+
if (message)
|
|
174
|
+
handle(message).then(reply);
|
|
175
|
+
}
|
|
176
|
+
newline = buffered.indexOf(`
|
|
177
|
+
`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ../ha-client/src/snapshot.ts
|
|
183
|
+
var byKey = (rows, key) => Object.fromEntries(rows.map((row) => [String(row[key]), row]));
|
|
184
|
+
async function readSnapshot(ha) {
|
|
185
|
+
const [states, entities, devices, areas, floors, services, config] = await Promise.all([
|
|
186
|
+
ha.call({ type: "get_states" }),
|
|
187
|
+
ha.call({ type: "config/entity_registry/list" }),
|
|
188
|
+
ha.call({ type: "config/device_registry/list" }),
|
|
189
|
+
ha.call({ type: "config/area_registry/list" }),
|
|
190
|
+
ha.call({ type: "config/floor_registry/list" }).catch(() => []),
|
|
191
|
+
ha.call({ type: "get_services" }),
|
|
192
|
+
ha.call({ type: "get_config" })
|
|
193
|
+
]);
|
|
194
|
+
return {
|
|
195
|
+
states: byKey(states, "entity_id"),
|
|
196
|
+
entities: byKey(entities.map((e) => ({
|
|
197
|
+
entity_id: e.entity_id,
|
|
198
|
+
name: e.name ?? e.original_name ?? null,
|
|
199
|
+
area_id: e.area_id ?? null,
|
|
200
|
+
device_id: e.device_id ?? null,
|
|
201
|
+
labels: e.labels ?? [],
|
|
202
|
+
hidden: e.hidden_by != null,
|
|
203
|
+
platform: e.platform
|
|
204
|
+
})), "entity_id"),
|
|
205
|
+
devices: byKey(devices, "id"),
|
|
206
|
+
areas: byKey(areas, "area_id"),
|
|
207
|
+
floors: byKey(floors, "floor_id"),
|
|
208
|
+
services,
|
|
209
|
+
config
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ../ui/src/lib/household.ts
|
|
214
|
+
var members = null;
|
|
215
|
+
var hasTracker = (person) => Array.isArray(person.attributes.device_trackers) && person.attributes.device_trackers.length > 0;
|
|
216
|
+
function isHouseholdMember(person) {
|
|
217
|
+
if (!person?.entity_id.startsWith("person."))
|
|
218
|
+
return false;
|
|
219
|
+
return members ? members.has(person.entity_id) : hasTracker(person);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ../ui/src/lib/values.ts
|
|
223
|
+
var trimmedString = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : undefined;
|
|
224
|
+
|
|
225
|
+
// ../ui/src/lib/lights.ts
|
|
226
|
+
var HOUSEHOLD_LIGHT_LABEL = "household_light";
|
|
227
|
+
var COLOR_MODES = new Set(["hs", "xy", "rgb", "rgbw", "rgbww", "color_temp"]);
|
|
228
|
+
var NO_EFFECT = new Set(["", "off", "none", "solid", "unknown"]);
|
|
229
|
+
var RGB_MODES = new Set(["hs", "xy", "rgb", "rgbw", "rgbww"]);
|
|
230
|
+
function entityArea(hass, entityId) {
|
|
231
|
+
const entry = hass.entities?.[entityId];
|
|
232
|
+
if (entry?.area_id)
|
|
233
|
+
return entry.area_id;
|
|
234
|
+
const device = entry?.device_id ? hass.devices?.[entry.device_id] : undefined;
|
|
235
|
+
return device?.area_id ?? undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ../ui/src/lib/labels.ts
|
|
239
|
+
var labelsInUse = new WeakMap;
|
|
240
|
+
function labelInUse(entities, label) {
|
|
241
|
+
if (!entities)
|
|
242
|
+
return false;
|
|
243
|
+
let used = labelsInUse.get(entities);
|
|
244
|
+
if (!used) {
|
|
245
|
+
used = new Set(Object.values(entities).flatMap((e) => e.labels ?? []));
|
|
246
|
+
labelsInUse.set(entities, used);
|
|
247
|
+
}
|
|
248
|
+
return used.has(label);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ../ui/src/lib/service-calls.ts
|
|
252
|
+
var RUN_DOMAINS = new Set(["script", "scene", "automation"]);
|
|
253
|
+
|
|
254
|
+
// ../ui/src/lib/music.ts
|
|
255
|
+
var MEDIA_FEATURE = {
|
|
256
|
+
PAUSE: 1,
|
|
257
|
+
SEEK: 2,
|
|
258
|
+
VOLUME_SET: 4,
|
|
259
|
+
VOLUME_MUTE: 8,
|
|
260
|
+
PREVIOUS_TRACK: 16,
|
|
261
|
+
NEXT_TRACK: 32,
|
|
262
|
+
PLAY_MEDIA: 512,
|
|
263
|
+
CLEAR_PLAYLIST: 8192,
|
|
264
|
+
PLAY: 16384,
|
|
265
|
+
SHUFFLE_SET: 32768,
|
|
266
|
+
REPEAT_SET: 262144,
|
|
267
|
+
GROUPING: 524288
|
|
268
|
+
};
|
|
269
|
+
var TV_AUDIO_FEATURES = MEDIA_FEATURE.VOLUME_SET | MEDIA_FEATURE.VOLUME_MUTE | MEDIA_FEATURE.GROUPING;
|
|
270
|
+
var QUEUE_ONLY_FEATURES = MEDIA_FEATURE.PAUSE | MEDIA_FEATURE.SEEK | MEDIA_FEATURE.PREVIOUS_TRACK | MEDIA_FEATURE.NEXT_TRACK | MEDIA_FEATURE.CLEAR_PLAYLIST | MEDIA_FEATURE.SHUFFLE_SET | MEDIA_FEATURE.REPEAT_SET;
|
|
271
|
+
var STATES = new Set(["playing", "paused", "idle", "off"]);
|
|
272
|
+
var PAUSED_INDICATOR_MS = 60 * 60 * 1000;
|
|
273
|
+
var MEDIA_TYPES = new Set(["artist", "album", "track", "playlist", "radio", "podcast"]);
|
|
274
|
+
|
|
275
|
+
// ../ui/src/lib/people.ts
|
|
276
|
+
var num = (value) => {
|
|
277
|
+
const n = typeof value === "number" ? value : Number.parseFloat(String(value));
|
|
278
|
+
return Number.isFinite(n) ? n : null;
|
|
279
|
+
};
|
|
280
|
+
function initialsFor(name) {
|
|
281
|
+
const words = name.trim().split(/\s+/).filter(Boolean);
|
|
282
|
+
if (words.length === 0)
|
|
283
|
+
return "?";
|
|
284
|
+
return (words.length === 1 ? words[0].slice(0, 2) : `${words[0][0]}${words[words.length - 1][0]}`).toUpperCase();
|
|
285
|
+
}
|
|
286
|
+
function haversineMeters(lat1, lon1, lat2, lon2) {
|
|
287
|
+
const toRad = (d) => d * Math.PI / 180;
|
|
288
|
+
const dLat = toRad(lat2 - lat1);
|
|
289
|
+
const dLon = toRad(lon2 - lon1);
|
|
290
|
+
const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
|
|
291
|
+
return 2 * 6371000 * Math.asin(Math.sqrt(a));
|
|
292
|
+
}
|
|
293
|
+
function batteryForTracker(trackerId, states) {
|
|
294
|
+
if (!trackerId)
|
|
295
|
+
return null;
|
|
296
|
+
const device = trackerId.split(".")[1];
|
|
297
|
+
const tracker = states[trackerId];
|
|
298
|
+
const level = num(states[`sensor.${device}_battery_level`]?.state) ?? num(tracker?.attributes.battery_level);
|
|
299
|
+
if (level == null)
|
|
300
|
+
return null;
|
|
301
|
+
const rawState = states[`sensor.${device}_battery_state`]?.state ?? tracker?.attributes.battery_status;
|
|
302
|
+
const batteryState = String(rawState ?? "").toLowerCase().replace(/[\s-]+/g, "_");
|
|
303
|
+
const full = batteryState === "full";
|
|
304
|
+
const charging = batteryState === "charging" || full && states[`sensor.${device}_charger_type`]?.state !== "none";
|
|
305
|
+
return { level: Math.round(level), charging, full };
|
|
306
|
+
}
|
|
307
|
+
function zoneFor(person, states) {
|
|
308
|
+
const zoneName = (z) => z.attributes.friendly_name ?? z.entity_id.split(".")[1];
|
|
309
|
+
const inZones = (Array.isArray(person.attributes.in_zones) ? person.attributes.in_zones : []).map((id) => states[String(id)]).filter((z) => Boolean(z)).sort((a, b) => (num(a.attributes.radius) ?? 0) - (num(b.attributes.radius) ?? 0));
|
|
310
|
+
if (inZones[0])
|
|
311
|
+
return { entityId: inZones[0].entity_id, name: zoneName(inZones[0]) };
|
|
312
|
+
if (person.state === "home")
|
|
313
|
+
return { entityId: "zone.home", name: "Home" };
|
|
314
|
+
if (["not_home", "unknown", "unavailable"].includes(person.state))
|
|
315
|
+
return null;
|
|
316
|
+
const byName = Object.values(states).find((s) => s.entity_id.startsWith("zone.") && zoneName(s).toLowerCase() === person.state.toLowerCase());
|
|
317
|
+
return { entityId: byName?.entity_id ?? "", name: byName ? zoneName(byName) : person.state };
|
|
318
|
+
}
|
|
319
|
+
function getPersonView(person, states) {
|
|
320
|
+
const name = person.attributes.friendly_name ?? person.entity_id.split(".")[1];
|
|
321
|
+
const trackers = Array.isArray(person.attributes.device_trackers) ? person.attributes.device_trackers : [];
|
|
322
|
+
const source = typeof person.attributes.source === "string" ? person.attributes.source : String(trackers[0] ?? "");
|
|
323
|
+
const zone = zoneFor(person, states);
|
|
324
|
+
const isHome = person.state === "home";
|
|
325
|
+
const latitude = num(person.attributes.latitude);
|
|
326
|
+
const longitude = num(person.attributes.longitude);
|
|
327
|
+
const home = states["zone.home"];
|
|
328
|
+
const homeLat = num(home?.attributes.latitude);
|
|
329
|
+
const homeLon = num(home?.attributes.longitude);
|
|
330
|
+
const distanceFromHome = latitude != null && longitude != null && homeLat != null && homeLon != null ? haversineMeters(homeLat, homeLon, latitude, longitude) : null;
|
|
331
|
+
let locationLabel = "Unknown";
|
|
332
|
+
if (zone)
|
|
333
|
+
locationLabel = zone.name;
|
|
334
|
+
else if (person.state === "not_home")
|
|
335
|
+
locationLabel = "Away";
|
|
336
|
+
return {
|
|
337
|
+
entityId: person.entity_id,
|
|
338
|
+
name,
|
|
339
|
+
shortName: name.split(/\s+/)[0] ?? name,
|
|
340
|
+
initials: initialsFor(name),
|
|
341
|
+
picture: person.attributes.entity_picture,
|
|
342
|
+
isHome,
|
|
343
|
+
zone,
|
|
344
|
+
locationLabel,
|
|
345
|
+
latitude,
|
|
346
|
+
longitude,
|
|
347
|
+
distanceFromHome,
|
|
348
|
+
battery: batteryForTracker(source || undefined, states),
|
|
349
|
+
updated: states[source]?.last_updated ?? person.last_updated
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function discoverPeople(states) {
|
|
353
|
+
return Object.values(states).filter(isHouseholdMember).map((s) => getPersonView(s, states)).sort((a, b) => a.name.localeCompare(b.name));
|
|
354
|
+
}
|
|
355
|
+
var GEOCODE_RETRY_MS = 5 * 60000;
|
|
356
|
+
var placeCache = new Map;
|
|
357
|
+
var queue = Promise.resolve();
|
|
358
|
+
|
|
359
|
+
// ../ui/src/lib/text.ts
|
|
360
|
+
var MINOR_WORDS = new Set([
|
|
361
|
+
"a",
|
|
362
|
+
"an",
|
|
363
|
+
"and",
|
|
364
|
+
"as",
|
|
365
|
+
"at",
|
|
366
|
+
"but",
|
|
367
|
+
"by",
|
|
368
|
+
"for",
|
|
369
|
+
"in",
|
|
370
|
+
"nor",
|
|
371
|
+
"of",
|
|
372
|
+
"on",
|
|
373
|
+
"or",
|
|
374
|
+
"the",
|
|
375
|
+
"to",
|
|
376
|
+
"via",
|
|
377
|
+
"vs"
|
|
378
|
+
]);
|
|
379
|
+
|
|
380
|
+
// ../ui/src/lib/tv.ts
|
|
381
|
+
var TV_PLATFORM = "googletv";
|
|
382
|
+
var CONSOLE_KINDS = new Set([
|
|
383
|
+
"xbox",
|
|
384
|
+
"playstation",
|
|
385
|
+
"nintendo_switch",
|
|
386
|
+
"nintendo_wii",
|
|
387
|
+
"steam",
|
|
388
|
+
"console"
|
|
389
|
+
]);
|
|
390
|
+
var FEATURED_APPS = [
|
|
391
|
+
["com.google.android.youtube.tv"],
|
|
392
|
+
["com.google.android.youtube.tvunplugged"],
|
|
393
|
+
["com.netflix.ninja"],
|
|
394
|
+
["com.amazon.amazonvideo.livingroom"],
|
|
395
|
+
["com.apple.atve.androidtv.appletv", "com.apple.atve.sony.appletv"],
|
|
396
|
+
["com.wbd.stream"],
|
|
397
|
+
["com.hulu.livingroomplus"],
|
|
398
|
+
["com.disney.disneyplus"]
|
|
399
|
+
];
|
|
400
|
+
var featuredRank = (app) => {
|
|
401
|
+
const rank = FEATURED_APPS.findIndex((packages) => packages.includes(app.packageName));
|
|
402
|
+
return rank < 0 ? FEATURED_APPS.length : rank;
|
|
403
|
+
};
|
|
404
|
+
var INPUT_APPS = new Set(["com.tcl.tv", "com.sony.dtv.tvx", "com.google.android.tv"]);
|
|
405
|
+
var launcherApp = (app) => !INPUT_APPS.has(app.packageName) && app.name !== app.packageName;
|
|
406
|
+
var OTHER_APPS = {
|
|
407
|
+
"com.google.android.apps.tv.launcherx": "Home screen",
|
|
408
|
+
"com.google.android.tvlauncher": "Home screen",
|
|
409
|
+
"com.google.android.apps.tv.dreamx": "Screensaver",
|
|
410
|
+
"com.google.android.apps.mediashell": "Casting"
|
|
411
|
+
};
|
|
412
|
+
var ACTIVITIES = new Set(["off", "screensaver", "home", "app", "playing", "paused", "input"]);
|
|
413
|
+
function siblingOf(tv, domain, suffix, states, registries) {
|
|
414
|
+
const entities = registries?.entities;
|
|
415
|
+
const deviceId = entities?.[tv.entity_id]?.device_id;
|
|
416
|
+
if (deviceId && entities) {
|
|
417
|
+
const sameDevice = Object.values(entities).find((e) => e.device_id === deviceId && e.entity_id.startsWith(`${domain}.`) && e.entity_id.endsWith(suffix) && (e.entity_id in states));
|
|
418
|
+
if (sameDevice)
|
|
419
|
+
return sameDevice.entity_id;
|
|
420
|
+
}
|
|
421
|
+
const byName = `${domain}.${tv.entity_id.slice("media_player.".length)}${suffix}`;
|
|
422
|
+
return byName in states ? byName : undefined;
|
|
423
|
+
}
|
|
424
|
+
var remoteFor = (tv, states, registries) => siblingOf(tv, "remote", "", states, registries);
|
|
425
|
+
var activityFor = (tv, states, registries) => siblingOf(tv, "sensor", "_activity", states, registries);
|
|
426
|
+
var TV_FEATURE = {
|
|
427
|
+
...MEDIA_FEATURE,
|
|
428
|
+
TURN_ON: 128,
|
|
429
|
+
TURN_OFF: 256,
|
|
430
|
+
VOLUME_STEP: 1024,
|
|
431
|
+
SELECT_SOURCE: 2048
|
|
432
|
+
};
|
|
433
|
+
function toApps(value) {
|
|
434
|
+
if (!Array.isArray(value))
|
|
435
|
+
return [];
|
|
436
|
+
return value.map((item) => {
|
|
437
|
+
const a = item;
|
|
438
|
+
const packageName = trimmedString(a?.package);
|
|
439
|
+
if (!packageName)
|
|
440
|
+
return;
|
|
441
|
+
return { packageName, name: trimmedString(a.name) ?? packageName, icon: trimmedString(a.icon) };
|
|
442
|
+
}).filter((app) => !!app && launcherApp(app)).sort((a, b) => featuredRank(a) - featuredRank(b) || a.name.localeCompare(b.name));
|
|
443
|
+
}
|
|
444
|
+
function toInputs(value) {
|
|
445
|
+
if (!Array.isArray(value))
|
|
446
|
+
return [];
|
|
447
|
+
return value.map((item) => {
|
|
448
|
+
const i = item;
|
|
449
|
+
const id = trimmedString(i?.id);
|
|
450
|
+
if (!id)
|
|
451
|
+
return;
|
|
452
|
+
const port = typeof i.port === "number" ? i.port : 0;
|
|
453
|
+
return {
|
|
454
|
+
id,
|
|
455
|
+
label: trimmedString(i.name) ?? (port ? `HDMI ${port}` : id),
|
|
456
|
+
port,
|
|
457
|
+
kind: trimmedString(i.kind) ?? "hdmi",
|
|
458
|
+
device: trimmedString(i.device)
|
|
459
|
+
};
|
|
460
|
+
}).filter((input) => !!input).sort((a, b) => a.port - b.port);
|
|
461
|
+
}
|
|
462
|
+
function toNowPlaying(entity) {
|
|
463
|
+
if (entity.state !== "playing" && entity.state !== "paused")
|
|
464
|
+
return;
|
|
465
|
+
const a = entity.attributes;
|
|
466
|
+
const title = trimmedString(a.media_title);
|
|
467
|
+
if (!title)
|
|
468
|
+
return;
|
|
469
|
+
return {
|
|
470
|
+
state: entity.state,
|
|
471
|
+
title,
|
|
472
|
+
subtitle: trimmedString(a.media_artist),
|
|
473
|
+
artwork: trimmedString(a.entity_picture)
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function toTv(entity, activityState) {
|
|
477
|
+
const a = entity.attributes;
|
|
478
|
+
const state = entity.state === "unavailable" ? "unavailable" : entity.state === "off" ? "off" : "on";
|
|
479
|
+
const on = state === "on";
|
|
480
|
+
const apps = toApps(a.apps);
|
|
481
|
+
const inputs = toInputs(a.inputs);
|
|
482
|
+
const appId = on ? trimmedString(a.app_id) : undefined;
|
|
483
|
+
const input = on ? inputs.find((i) => i.id === a.input) : undefined;
|
|
484
|
+
const app = appId ? apps.find((x) => x.packageName === appId) : undefined;
|
|
485
|
+
const reported = trimmedString(a.app_name);
|
|
486
|
+
const activity = activityState && ACTIVITIES.has(activityState) ? activityState : undefined;
|
|
487
|
+
const appName = !on ? undefined : input ? input.label : activity === "screensaver" ? "Screensaver" : activity === "home" ? "Home screen" : app?.name ?? (appId && OTHER_APPS[appId]) ?? (reported && reported !== appId ? reported : undefined) ?? appId;
|
|
488
|
+
return {
|
|
489
|
+
entityId: entity.entity_id,
|
|
490
|
+
name: trimmedString(a.friendly_name) ?? entity.entity_id,
|
|
491
|
+
state,
|
|
492
|
+
activity,
|
|
493
|
+
appId,
|
|
494
|
+
appName,
|
|
495
|
+
app: input ? undefined : app,
|
|
496
|
+
apps,
|
|
497
|
+
inputs,
|
|
498
|
+
input,
|
|
499
|
+
nowPlaying: on && !input ? toNowPlaying(entity) : undefined,
|
|
500
|
+
volume: on && typeof a.volume_level === "number" ? Math.round(a.volume_level * 100) : undefined,
|
|
501
|
+
muted: on && a.is_volume_muted === true,
|
|
502
|
+
features: typeof a.supported_features === "number" ? a.supported_features : 0
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
var TV_LABEL = "household_tv";
|
|
506
|
+
function discoverTvs(states, registries) {
|
|
507
|
+
const registry = registries?.entities;
|
|
508
|
+
const labeled = labelInUse(registry, TV_LABEL);
|
|
509
|
+
return Object.values(states).filter((s) => {
|
|
510
|
+
if (!s.entity_id.startsWith("media_player.") || s.attributes.device_class !== "tv")
|
|
511
|
+
return false;
|
|
512
|
+
if (s.attributes.mass_player_type != null)
|
|
513
|
+
return false;
|
|
514
|
+
if (!registry)
|
|
515
|
+
return true;
|
|
516
|
+
const entry = registry[s.entity_id];
|
|
517
|
+
if (labeled && !entry?.labels?.includes(TV_LABEL))
|
|
518
|
+
return false;
|
|
519
|
+
return entry?.platform === TV_PLATFORM && !entry.hidden;
|
|
520
|
+
}).map((s) => {
|
|
521
|
+
const activity = activityFor(s, states, registries);
|
|
522
|
+
return {
|
|
523
|
+
...toTv(s, activity ? states[activity]?.state : undefined),
|
|
524
|
+
remoteEntityId: remoteFor(s, states, registries)
|
|
525
|
+
};
|
|
526
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ../ui/src/lib/bedtime.ts
|
|
530
|
+
var BEDTIME_LABEL = "bedtime";
|
|
531
|
+
function discoverBedtimeToggles(hass) {
|
|
532
|
+
const byArea = {};
|
|
533
|
+
for (const entry of Object.values(hass.entities ?? {})) {
|
|
534
|
+
const isSwitch = entry.entity_id.startsWith("switch.");
|
|
535
|
+
if (!(isSwitch || entry.entity_id.startsWith("input_boolean.")) || entry.hidden)
|
|
536
|
+
continue;
|
|
537
|
+
if (!entry.labels?.includes(BEDTIME_LABEL))
|
|
538
|
+
continue;
|
|
539
|
+
const areaId = entityArea(hass, entry.entity_id);
|
|
540
|
+
if (!areaId)
|
|
541
|
+
continue;
|
|
542
|
+
if (!byArea[areaId] || isSwitch && !byArea[areaId].startsWith("switch."))
|
|
543
|
+
byArea[areaId] = entry.entity_id;
|
|
544
|
+
}
|
|
545
|
+
return byArea;
|
|
546
|
+
}
|
|
547
|
+
var BEDTIME_STEP_KINDS = [
|
|
548
|
+
"rooms",
|
|
549
|
+
"lights",
|
|
550
|
+
"media",
|
|
551
|
+
"speaker",
|
|
552
|
+
"fan",
|
|
553
|
+
"covers",
|
|
554
|
+
"locks",
|
|
555
|
+
"climate",
|
|
556
|
+
"alert",
|
|
557
|
+
"panels"
|
|
558
|
+
];
|
|
559
|
+
var HOME_ROUTINE = {
|
|
560
|
+
zone: "The whole house",
|
|
561
|
+
steps: [
|
|
562
|
+
{ kind: "rooms", text: "Puts every room still awake to bed, each with its own routine", actions: [] },
|
|
563
|
+
{
|
|
564
|
+
kind: "lights",
|
|
565
|
+
text: "Turns off every household light and TV, and stops the music (not what stays on at bedtime)",
|
|
566
|
+
actions: ["light.turn_off", "media_player.turn_off", "media_player.media_stop"]
|
|
567
|
+
},
|
|
568
|
+
{ kind: "covers", text: "Closes every curtain, blind and garage door", actions: ["cover.close_cover"] },
|
|
569
|
+
{
|
|
570
|
+
kind: "locks",
|
|
571
|
+
text: "Locks the outside doors (an open door locks once it closes)",
|
|
572
|
+
actions: ["script.turn_on"]
|
|
573
|
+
},
|
|
574
|
+
{ kind: "climate", text: "Sets the thermostats to Sleep", actions: ["input_select.select_option"] },
|
|
575
|
+
{
|
|
576
|
+
kind: "alert",
|
|
577
|
+
text: "A minute later, tells everyone's phones about any door, window or garage still open",
|
|
578
|
+
actions: ["script.notify_people"]
|
|
579
|
+
},
|
|
580
|
+
{ kind: "panels", text: "Dims the wall panel lights to 10% until bedtime ends", actions: [] }
|
|
581
|
+
]
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
// ../ui/src/lib/house-config.ts
|
|
585
|
+
var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
586
|
+
var text = (value) => typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
587
|
+
var entityOf = (value, domain) => {
|
|
588
|
+
const id = text(value);
|
|
589
|
+
if (!id || !/^[a-z_]+\.[a-z0-9_]+$/.test(id))
|
|
590
|
+
return;
|
|
591
|
+
return !domain || id.startsWith(`${domain}.`) ? id : undefined;
|
|
592
|
+
};
|
|
593
|
+
var list = (value) => Array.isArray(value) ? value : [];
|
|
594
|
+
function appliance(value, name) {
|
|
595
|
+
if (!isObject(value))
|
|
596
|
+
return;
|
|
597
|
+
const machineState = entityOf(value.machine_state, "sensor");
|
|
598
|
+
const jobState = entityOf(value.job_state, "sensor");
|
|
599
|
+
const timeRemaining = entityOf(value.time_remaining, "sensor");
|
|
600
|
+
if (!machineState || !jobState || !timeRemaining)
|
|
601
|
+
return;
|
|
602
|
+
return { name: text(value.name) ?? name, machineState, jobState, timeRemaining };
|
|
603
|
+
}
|
|
604
|
+
function routine(value) {
|
|
605
|
+
if (!isObject(value))
|
|
606
|
+
return;
|
|
607
|
+
const zone = text(value.zone);
|
|
608
|
+
if (!zone)
|
|
609
|
+
return;
|
|
610
|
+
const steps = list(value.steps).flatMap((step) => {
|
|
611
|
+
if (!isObject(step))
|
|
612
|
+
return [];
|
|
613
|
+
const kind = text(step.kind);
|
|
614
|
+
const line = text(step.text);
|
|
615
|
+
if (!kind || !BEDTIME_STEP_KINDS.includes(kind) || !line)
|
|
616
|
+
return [];
|
|
617
|
+
const actions = list(step.actions).filter((a) => typeof a === "string");
|
|
618
|
+
return [{ kind, text: line, actions }];
|
|
619
|
+
});
|
|
620
|
+
return { zone, steps };
|
|
621
|
+
}
|
|
622
|
+
function parseHouseConfig(raw) {
|
|
623
|
+
const config = isObject(raw) ? raw : {};
|
|
624
|
+
const tts = isObject(config.tts) ? config.tts : {};
|
|
625
|
+
const laundry = isObject(config.laundry) ? config.laundry : {};
|
|
626
|
+
const thermostats = list(config.thermostats).flatMap((zone) => {
|
|
627
|
+
const entityId = entityOf(isObject(zone) ? zone.entity : zone, "climate");
|
|
628
|
+
return entityId ? [{ entityId, name: isObject(zone) ? text(zone.name) : undefined }] : [];
|
|
629
|
+
});
|
|
630
|
+
const routines = {};
|
|
631
|
+
if (isObject(config.bedtime_routines))
|
|
632
|
+
for (const [toggle, value] of Object.entries(config.bedtime_routines)) {
|
|
633
|
+
const parsed = (entityOf(toggle, "switch") || entityOf(toggle, "input_boolean")) && routine(value);
|
|
634
|
+
if (parsed)
|
|
635
|
+
routines[toggle] = parsed;
|
|
636
|
+
}
|
|
637
|
+
return {
|
|
638
|
+
announceSpeaker: entityOf(config.announce_speaker, "media_player"),
|
|
639
|
+
weather: entityOf(config.weather, "weather"),
|
|
640
|
+
alarmPanel: entityOf(config.alarm_panel, "alarm_control_panel"),
|
|
641
|
+
thermostats: thermostats.length ? thermostats : undefined,
|
|
642
|
+
laundry: { washer: appliance(laundry.washer, "Washer"), dryer: appliance(laundry.dryer, "Dryer") },
|
|
643
|
+
guestWifiQr: entityOf(config.guest_wifi_qr, "image"),
|
|
644
|
+
liveInputs: list(config.live_inputs).flatMap((input) => {
|
|
645
|
+
if (!isObject(input))
|
|
646
|
+
return [];
|
|
647
|
+
const name = text(input.name);
|
|
648
|
+
const uri = text(input.uri);
|
|
649
|
+
return name && uri ? [{ name, uri, signal: entityOf(input.signal, "binary_sensor") }] : [];
|
|
650
|
+
}),
|
|
651
|
+
tts: {
|
|
652
|
+
engine: entityOf(tts.engine, "tts"),
|
|
653
|
+
voice: text(tts.voice),
|
|
654
|
+
streaming: tts.streaming === true,
|
|
655
|
+
fallback: entityOf(tts.fallback, "tts")
|
|
656
|
+
},
|
|
657
|
+
birthdayCalendar: entityOf(config.birthday_calendar, "calendar"),
|
|
658
|
+
bedtimeRoutines: routines,
|
|
659
|
+
modules: isObject(config.modules) ? Object.fromEntries(Object.entries(config.modules).filter((entry) => typeof entry[1] === "boolean")) : {}
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
var current = parseHouseConfig(undefined);
|
|
663
|
+
var listeners = new Set;
|
|
664
|
+
function mergeHouseSources(integration, card) {
|
|
665
|
+
return { ...isObject(integration) ? integration : {}, ...isObject(card) ? card : {} };
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// ../ui/src/lib/covers.ts
|
|
669
|
+
var WINDOW_COVER_CLASSES = ["curtain", "blind", "shade", "shutter"];
|
|
670
|
+
var objectId = (entityId) => entityId.slice(entityId.indexOf(".") + 1);
|
|
671
|
+
var groupMembers = (state) => Array.isArray(state.attributes.entity_id) ? state.attributes.entity_id.filter((id) => typeof id === "string") : [];
|
|
672
|
+
function discoverWindowCovers(hass, states) {
|
|
673
|
+
const covers = Object.values(states).filter((s) => s.entity_id.startsWith("cover.") && WINDOW_COVER_CLASSES.includes(s.attributes.device_class) && !hass.entities?.[s.entity_id]?.hidden);
|
|
674
|
+
const inGroups = new Set(covers.flatMap(groupMembers));
|
|
675
|
+
return covers.filter((s) => !inGroups.has(s.entity_id)).map((s) => {
|
|
676
|
+
const areaId = entityArea(hass, s.entity_id);
|
|
677
|
+
const members = groupMembers(s);
|
|
678
|
+
return {
|
|
679
|
+
entityId: s.entity_id,
|
|
680
|
+
name: s.attributes.friendly_name ?? objectId(s.entity_id),
|
|
681
|
+
kind: s.attributes.device_class,
|
|
682
|
+
area: areaId ? hass.areas?.[areaId]?.name ?? undefined : undefined,
|
|
683
|
+
motors: members.length > 0 ? members : [s.entity_id]
|
|
684
|
+
};
|
|
685
|
+
}).sort((a, b) => (a.area ?? "").localeCompare(b.area ?? "") || a.name.localeCompare(b.name));
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// ../ui/src/lib/diffusers.ts
|
|
689
|
+
var DIFFUSER_PLATFORM = "everscent";
|
|
690
|
+
function discoverDiffusers(hass, states) {
|
|
691
|
+
return Object.keys(states).filter((id) => id.startsWith("switch.") && id.endsWith("_power") && hass.entities?.[id]?.platform === DIFFUSER_PLATFORM && !hass.entities?.[id]?.hidden).map((power) => {
|
|
692
|
+
const prefix = power.slice("switch.".length, -"_power".length);
|
|
693
|
+
const deviceId = hass.entities?.[power]?.device_id;
|
|
694
|
+
const device = deviceId ? hass.devices?.[deviceId] : undefined;
|
|
695
|
+
const duration = Object.values(hass.entities ?? {}).find((e) => e.device_id === deviceId && e.platform === DIFFUSER_PLATFORM && e.entity_id.startsWith("number.") && e.entity_id.endsWith("_quick_scent_duration"))?.entity_id;
|
|
696
|
+
return {
|
|
697
|
+
power,
|
|
698
|
+
name: device?.name_by_user || device?.name || prefix.replaceAll("_", " "),
|
|
699
|
+
fragrance: `sensor.${prefix}_fragrance`,
|
|
700
|
+
category: `sensor.${prefix}_fragrance_category`,
|
|
701
|
+
concentration: `sensor.${prefix}_concentration`,
|
|
702
|
+
low: `binary_sensor.${prefix}_fragrance_low`,
|
|
703
|
+
online: `binary_sensor.${prefix}_online`,
|
|
704
|
+
fan: `binary_sensor.${prefix}_fan`,
|
|
705
|
+
quickScent: `button.${prefix}_quick_scent`,
|
|
706
|
+
quickScentDuration: duration ?? `number.${prefix}_quick_scent_duration`
|
|
707
|
+
};
|
|
708
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// ../ui/src/lib/fans.ts
|
|
712
|
+
var objectId2 = (entityId) => entityId.slice(entityId.indexOf(".") + 1);
|
|
713
|
+
function discoverFans(hass, states) {
|
|
714
|
+
return Object.values(states).filter((s) => s.entity_id.startsWith("fan.") && !hass.entities?.[s.entity_id]?.hidden).map((s) => {
|
|
715
|
+
const areaId = entityArea(hass, s.entity_id);
|
|
716
|
+
return {
|
|
717
|
+
entityId: s.entity_id,
|
|
718
|
+
name: s.attributes.friendly_name ?? objectId2(s.entity_id),
|
|
719
|
+
area: areaId ? hass.areas?.[areaId]?.name ?? undefined : undefined
|
|
720
|
+
};
|
|
721
|
+
}).sort((a, b) => (a.area ?? "").localeCompare(b.area ?? "") || a.name.localeCompare(b.name));
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// ../ui/src/lib/pets.ts
|
|
725
|
+
var UNAVAILABLE = new Set(["unavailable", "unknown", ""]);
|
|
726
|
+
var num2 = (entity) => {
|
|
727
|
+
if (!entity || UNAVAILABLE.has(entity.state))
|
|
728
|
+
return null;
|
|
729
|
+
const n = Number.parseFloat(entity.state);
|
|
730
|
+
return Number.isFinite(n) ? n : null;
|
|
731
|
+
};
|
|
732
|
+
var measurement = (entity) => {
|
|
733
|
+
const value = num2(entity);
|
|
734
|
+
return value == null ? null : { value, unit: String(entity?.attributes.unit_of_measurement ?? "") };
|
|
735
|
+
};
|
|
736
|
+
function discoverPets(states) {
|
|
737
|
+
return Object.values(states).filter((s) => {
|
|
738
|
+
if (!s.entity_id.startsWith("person."))
|
|
739
|
+
return false;
|
|
740
|
+
const trackers = s.attributes.device_trackers;
|
|
741
|
+
if (Array.isArray(trackers) && trackers.length > 0)
|
|
742
|
+
return false;
|
|
743
|
+
const id = s.entity_id.slice("person.".length);
|
|
744
|
+
return Boolean(states[`sensor.${id}_visits_today`] || states[`sensor.${id}_weight`]);
|
|
745
|
+
}).map((s) => {
|
|
746
|
+
const id = s.entity_id.slice("person.".length);
|
|
747
|
+
const name = s.attributes.friendly_name ?? id;
|
|
748
|
+
const visits = num2(states[`sensor.${id}_visits_today`]);
|
|
749
|
+
return {
|
|
750
|
+
entityId: s.entity_id,
|
|
751
|
+
id,
|
|
752
|
+
name,
|
|
753
|
+
initials: initialsFor(name),
|
|
754
|
+
picture: s.attributes.entity_picture,
|
|
755
|
+
visitsToday: visits == null ? null : Math.round(visits),
|
|
756
|
+
weight: measurement(states[`sensor.${id}_weight`])
|
|
757
|
+
};
|
|
758
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
759
|
+
}
|
|
760
|
+
var CYCLE_STATUS = new Set(["ccp", "cst", "ec"]);
|
|
761
|
+
|
|
762
|
+
// ../ui/src/lib/printer.ts
|
|
763
|
+
var PRINTER_PLATFORM = "escpos_printer";
|
|
764
|
+
var entityName = (id) => id.split(".")[1] ?? id;
|
|
765
|
+
function discoverPrinters(hass) {
|
|
766
|
+
const entities = Object.values(hass.entities ?? {});
|
|
767
|
+
return entities.filter((e) => e.entity_id.startsWith("notify.") && e.platform === PRINTER_PLATFORM && !e.hidden).map((e) => {
|
|
768
|
+
const device = e.device_id ? hass.devices?.[e.device_id] : undefined;
|
|
769
|
+
const area = hass.areas?.[e.area_id ?? device?.area_id ?? ""]?.name;
|
|
770
|
+
const sibling = (domain, suffix) => entities.find((s) => s.device_id === e.device_id && s.platform === PRINTER_PLATFORM && s.entity_id.startsWith(`${domain}.`) && s.entity_id.endsWith(suffix))?.entity_id;
|
|
771
|
+
return {
|
|
772
|
+
entityId: e.entity_id,
|
|
773
|
+
name: area ? `${area} printer` : device?.name_by_user || device?.name || entityName(e.entity_id).replaceAll("_", " "),
|
|
774
|
+
...e.device_id && {
|
|
775
|
+
online: sibling("binary_sensor", "_online"),
|
|
776
|
+
paper: sibling("sensor", "_paper_status"),
|
|
777
|
+
cover: sibling("binary_sensor", "_cover_open")
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// ../ui/src/lib/security.ts
|
|
784
|
+
function findAlarmPanel(hass, states, configured) {
|
|
785
|
+
if (configured)
|
|
786
|
+
return configured;
|
|
787
|
+
const panels = Object.keys(states).filter((id) => id.startsWith("alarm_control_panel.")).sort();
|
|
788
|
+
return panels.find((id) => hass.entities?.[id]?.platform === "alarmo") ?? panels[0];
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// ../ui/src/lib/modules.ts
|
|
792
|
+
var hasDomain = (states, domain) => {
|
|
793
|
+
const prefix = `${domain}.`;
|
|
794
|
+
for (const id in states)
|
|
795
|
+
if (id.startsWith(prefix))
|
|
796
|
+
return true;
|
|
797
|
+
return false;
|
|
798
|
+
};
|
|
799
|
+
var hasService = (hass, domain, service) => !!hass.services?.[domain] && (!service || !!hass.services[domain]?.[service]);
|
|
800
|
+
var states = (hass) => hass.states;
|
|
801
|
+
var registries = (hass) => ({ entities: hass.entities, devices: hass.devices, areas: hass.areas });
|
|
802
|
+
var MODULES = [
|
|
803
|
+
{
|
|
804
|
+
id: "lighting",
|
|
805
|
+
name: "Lighting",
|
|
806
|
+
description: "A tile per room by floor (its lights, scenes, dimmers and colors a tap away) and All off",
|
|
807
|
+
needs: `Lights labeled "Household Light" (${HOUSEHOLD_LIGHT_LABEL}) in areas on floors; Stateful Scenes for scenes`,
|
|
808
|
+
watch: ["light", "switch"],
|
|
809
|
+
detect: ({ hass }) => Object.values(hass.entities ?? {}).some((entry) => entry.labels?.includes(HOUSEHOLD_LIGHT_LABEL))
|
|
810
|
+
},
|
|
811
|
+
{
|
|
812
|
+
id: "bedtime",
|
|
813
|
+
name: "Bedtime",
|
|
814
|
+
description: "Each room's bedtime toggle and Home Bedtime, asking before lights come on in a room asleep",
|
|
815
|
+
needs: `The integration's Bedtime rooms (its options), or input_booleans labeled "bedtime" in each room's area with input_boolean.home_bedtime`,
|
|
816
|
+
watch: ["input_boolean", "switch"],
|
|
817
|
+
detect: ({ hass }) => Object.keys(discoverBedtimeToggles(registries(hass))).length > 0
|
|
818
|
+
},
|
|
819
|
+
{
|
|
820
|
+
id: "security",
|
|
821
|
+
name: "Security",
|
|
822
|
+
description: "Door locks with their contact sensors, garage doors, and every opening left open",
|
|
823
|
+
needs: "lock.* entities, garage covers, door/window binary sensors",
|
|
824
|
+
watch: ["lock", "cover", "binary_sensor"],
|
|
825
|
+
detect: ({ hass }) => hasDomain(hass.states, "lock") || hasDomain(hass.states, "cover")
|
|
826
|
+
},
|
|
827
|
+
{
|
|
828
|
+
id: "alarm",
|
|
829
|
+
name: "Alarm",
|
|
830
|
+
description: "Arm and disarm with a PIN pad, exit and entry countdowns, and the arming check",
|
|
831
|
+
needs: "The Alarmo integration (an alarm_control_panel and the alarmo.arm action)",
|
|
832
|
+
watch: ["alarm_control_panel"],
|
|
833
|
+
detect: ({ hass, house }) => !!findAlarmPanel(hass, hass.states, house.alarmPanel) && hasService(hass, "alarmo")
|
|
834
|
+
},
|
|
835
|
+
{
|
|
836
|
+
id: "cameras",
|
|
837
|
+
name: "Cameras",
|
|
838
|
+
description: "Live views (WebRTC), snapshots, smart detections, talkback and recorded clips",
|
|
839
|
+
needs: "camera.* entities; UniFi Protect for detections, talkback and clips",
|
|
840
|
+
watch: ["camera"],
|
|
841
|
+
detect: ({ hass }) => hasDomain(hass.states, "camera")
|
|
842
|
+
},
|
|
843
|
+
{
|
|
844
|
+
id: "climate",
|
|
845
|
+
name: "Thermostats",
|
|
846
|
+
description: "Each thermostat's setpoint and mode",
|
|
847
|
+
needs: "climate.* entities (house.yaml `thermostats` to pick and name them)",
|
|
848
|
+
watch: ["climate"],
|
|
849
|
+
detect: ({ hass, house }) => !!house.thermostats?.length || hasDomain(hass.states, "climate")
|
|
850
|
+
},
|
|
851
|
+
{
|
|
852
|
+
id: "thermostat_presets",
|
|
853
|
+
name: "Thermostat presets",
|
|
854
|
+
description: "A house-wide Home / Away / Sleep preset over the thermostats",
|
|
855
|
+
needs: "input_select.thermostat_preset and script.adjust_thermostat_temperature, with the preset automation",
|
|
856
|
+
watch: ["input_select"],
|
|
857
|
+
detect: ({ hass }) => !!hass.states["input_select.thermostat_preset"]
|
|
858
|
+
},
|
|
859
|
+
{
|
|
860
|
+
id: "covers",
|
|
861
|
+
name: "Curtains & blinds",
|
|
862
|
+
description: "Every curtain, blind and shade, with positions estimated while they move",
|
|
863
|
+
needs: "cover.* entities with device class curtain, blind, shade or shutter",
|
|
864
|
+
watch: ["cover"],
|
|
865
|
+
detect: ({ hass }) => discoverWindowCovers(registries(hass), states(hass)).length > 0
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
id: "fans",
|
|
869
|
+
name: "Fans",
|
|
870
|
+
description: "Every fan: speed, oscillation and presets",
|
|
871
|
+
needs: "fan.* entities",
|
|
872
|
+
watch: ["fan"],
|
|
873
|
+
detect: ({ hass }) => discoverFans(registries(hass), states(hass)).length > 0
|
|
874
|
+
},
|
|
875
|
+
{
|
|
876
|
+
id: "diffusers",
|
|
877
|
+
name: "Scent diffusers",
|
|
878
|
+
description: "Whole-home diffusers: oil, fragrance and Quick scent",
|
|
879
|
+
needs: "The EverScent integration (raine-works/ha-everscent)",
|
|
880
|
+
watch: ["switch", "sensor", "binary_sensor", "button"],
|
|
881
|
+
detect: ({ hass }) => discoverDiffusers(registries(hass), states(hass)).length > 0
|
|
882
|
+
},
|
|
883
|
+
{
|
|
884
|
+
id: "weather",
|
|
885
|
+
name: "Weather",
|
|
886
|
+
description: "Now in the header; hourly charts, details and the 10-day forecast in its dialog",
|
|
887
|
+
needs: "A weather.* entity with forecasts",
|
|
888
|
+
watch: ["weather"],
|
|
889
|
+
detect: ({ hass, house }) => house.weather ? !!hass.states[house.weather] : hasDomain(hass.states, "weather")
|
|
890
|
+
},
|
|
891
|
+
{
|
|
892
|
+
id: "calendar",
|
|
893
|
+
name: "Calendar",
|
|
894
|
+
description: "The next event in the header; a month grid and day agenda",
|
|
895
|
+
needs: "calendar.* entities",
|
|
896
|
+
watch: ["calendar"],
|
|
897
|
+
detect: ({ hass }) => hasDomain(hass.states, "calendar")
|
|
898
|
+
},
|
|
899
|
+
{
|
|
900
|
+
id: "todo",
|
|
901
|
+
name: "To-do",
|
|
902
|
+
description: "To-do lists with swipes, drag to reorder, due dates and assignment to household members",
|
|
903
|
+
needs: "todo.* entities (Local To-do, Shopping list\u2026)",
|
|
904
|
+
watch: ["todo"],
|
|
905
|
+
detect: ({ hass }) => hasDomain(hass.states, "todo")
|
|
906
|
+
},
|
|
907
|
+
{
|
|
908
|
+
id: "notifications",
|
|
909
|
+
name: "Notifications",
|
|
910
|
+
description: "Home Assistant's persistent notifications, swiped away",
|
|
911
|
+
needs: "Nothing: part of Home Assistant",
|
|
912
|
+
watch: []
|
|
913
|
+
},
|
|
914
|
+
{
|
|
915
|
+
id: "people",
|
|
916
|
+
name: "People",
|
|
917
|
+
description: "Who's home in the header; a map of everyone with their phones' batteries",
|
|
918
|
+
needs: "person.* entities with a device tracker (the Companion app)",
|
|
919
|
+
watch: ["person"],
|
|
920
|
+
detect: ({ hass }) => discoverPeople(states(hass)).length > 0
|
|
921
|
+
},
|
|
922
|
+
{
|
|
923
|
+
id: "pets",
|
|
924
|
+
name: "Pets",
|
|
925
|
+
description: "Litter-Robot and Feeder-Robot: visits, weight and food charts, Cycle and Give snack",
|
|
926
|
+
needs: "The Litter-Robot integration, with a person.* per pet",
|
|
927
|
+
watch: ["person", "sensor"],
|
|
928
|
+
detect: ({ hass }) => discoverPets(states(hass)).length > 0
|
|
929
|
+
},
|
|
930
|
+
{
|
|
931
|
+
id: "music",
|
|
932
|
+
name: "Music",
|
|
933
|
+
description: "Now playing, browse and search, the queue, speaker groups, live inputs and Party mode",
|
|
934
|
+
needs: "The Music Assistant integration",
|
|
935
|
+
watch: ["media_player"],
|
|
936
|
+
detect: ({ hass }) => Object.values(hass.states).some((s) => s?.attributes?.mass_player_type != null)
|
|
937
|
+
},
|
|
938
|
+
{
|
|
939
|
+
id: "tvs",
|
|
940
|
+
name: "TVs",
|
|
941
|
+
description: "Power, what's on with its poster, volume, the TV's apps and inputs, and a remote",
|
|
942
|
+
needs: "The Raineworks TV integration (raine-works/ha-googletv)",
|
|
943
|
+
watch: ["media_player"],
|
|
944
|
+
detect: ({ hass }) => discoverTvs(states(hass), hass).length > 0
|
|
945
|
+
},
|
|
946
|
+
{
|
|
947
|
+
id: "messages",
|
|
948
|
+
name: "Messages",
|
|
949
|
+
description: "Announce on the speakers (typed or recorded), message phones, print notes",
|
|
950
|
+
needs: "Music Assistant speakers, or script.notify_people for phones, or a receipt printer",
|
|
951
|
+
watch: ["media_player"],
|
|
952
|
+
detect: (ctx) => !!MODULES.find((m) => m.id === "music")?.detect?.(ctx) || hasService(ctx.hass, "script", "notify_people") || discoverPrinters(registries(ctx.hass)).length > 0
|
|
953
|
+
},
|
|
954
|
+
{
|
|
955
|
+
id: "printer",
|
|
956
|
+
name: "Receipt printer",
|
|
957
|
+
description: "Print to-do lists and notes",
|
|
958
|
+
needs: "An ESC/POS printer (the escpos_printer notify platform)",
|
|
959
|
+
watch: ["sensor", "binary_sensor"],
|
|
960
|
+
detect: ({ hass }) => discoverPrinters(registries(hass)).length > 0
|
|
961
|
+
},
|
|
962
|
+
{
|
|
963
|
+
id: "laundry",
|
|
964
|
+
name: "Laundry",
|
|
965
|
+
description: "The washer and dryer in the header while they run, and a toast when one is done",
|
|
966
|
+
needs: "house.yaml `laundry`: each machine\u2019s machine state, job state and time remaining sensors (SmartThings)",
|
|
967
|
+
watch: ["sensor"],
|
|
968
|
+
detect: ({ hass, house }) => [house.laundry.washer, house.laundry.dryer].some((machine) => machine && hass.states[machine.machineState])
|
|
969
|
+
},
|
|
970
|
+
{
|
|
971
|
+
id: "energy",
|
|
972
|
+
name: "Energy",
|
|
973
|
+
description: "Live draw split by circuit, today / week / month charts, and where it went",
|
|
974
|
+
needs: "Home Assistant's Energy settings with a grid meter (and devices for circuits)",
|
|
975
|
+
watch: []
|
|
976
|
+
},
|
|
977
|
+
{
|
|
978
|
+
id: "daily",
|
|
979
|
+
name: "Daily",
|
|
980
|
+
description: "The daily brief (weather, today's events, the news), a daily affirmation and a scripture study",
|
|
981
|
+
needs: "The daily brief package: sensor.daily_brief and script.daily_brief_generate (an AI task and a conversation agent that can search)",
|
|
982
|
+
watch: ["sensor"],
|
|
983
|
+
detect: ({ hass }) => !!hass.states["sensor.daily_brief"] && hasService(hass, "script", "daily_brief_generate")
|
|
984
|
+
},
|
|
985
|
+
{
|
|
986
|
+
id: "assist",
|
|
987
|
+
name: "Assist",
|
|
988
|
+
description: "Voice and typed commands through Home Assistant's preferred Assist pipeline, with the glow",
|
|
989
|
+
needs: "An Assist pipeline with speech to text (and text to speech for spoken answers)",
|
|
990
|
+
watch: [],
|
|
991
|
+
detect: ({ hass }) => !hass.config?.components || hass.config.components.includes("assist_pipeline")
|
|
992
|
+
},
|
|
993
|
+
{
|
|
994
|
+
id: "holidays",
|
|
995
|
+
name: "Holidays",
|
|
996
|
+
description: "Easter eggs in the days around each holiday and on birthdays: characters, confetti, sounds",
|
|
997
|
+
needs: 'Nothing (birthdays: a calendar named "Birthdays")',
|
|
998
|
+
watch: []
|
|
999
|
+
},
|
|
1000
|
+
{
|
|
1001
|
+
id: "guest_wifi",
|
|
1002
|
+
name: "Guest Wi-Fi",
|
|
1003
|
+
description: "The guest network's QR code, with its name and password for a laptop, on the wall panels",
|
|
1004
|
+
needs: "house.yaml `guest_wifi_qr`: an image entity with the QR code (UniFi\u2019s, enabled)",
|
|
1005
|
+
watch: ["image"],
|
|
1006
|
+
detect: ({ hass, house }) => !!house.guestWifiQr && !!hass.states[house.guestWifiQr]
|
|
1007
|
+
}
|
|
1008
|
+
];
|
|
1009
|
+
var BY_ID = new Map(MODULES.map((m) => [m.id, m]));
|
|
1010
|
+
var moduleById = (id) => {
|
|
1011
|
+
const found = BY_ID.get(id);
|
|
1012
|
+
if (!found)
|
|
1013
|
+
throw new Error(`Unknown dashboard module: ${id}`);
|
|
1014
|
+
return found;
|
|
1015
|
+
};
|
|
1016
|
+
function moduleState(id, ctx) {
|
|
1017
|
+
const module = moduleById(id);
|
|
1018
|
+
if (ctx.house.modules[id] === false)
|
|
1019
|
+
return "off";
|
|
1020
|
+
return !module.detect || module.detect(ctx) ? "on" : "missing";
|
|
1021
|
+
}
|
|
1022
|
+
var moduleReport = (ctx) => MODULES.map(({ id, name, description, needs }) => ({ id, name, description, needs, state: moduleState(id, ctx) }));
|
|
1023
|
+
|
|
1024
|
+
// ../mcp/src/tools.ts
|
|
1025
|
+
var DOMAIN = "raineworks_dashboards";
|
|
1026
|
+
var json = (value) => JSON.stringify(value, null, 2);
|
|
1027
|
+
async function withHass(connect, work) {
|
|
1028
|
+
const ha = await connect();
|
|
1029
|
+
try {
|
|
1030
|
+
return await work(ha);
|
|
1031
|
+
} finally {
|
|
1032
|
+
ha.close();
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
var yamlOf = (yaml) => typeof yaml === "string" && yaml.trim() ? Bun.YAML.parse(yaml) : undefined;
|
|
1036
|
+
async function integrationHouse(ha) {
|
|
1037
|
+
const info = await ha.call({ type: `${DOMAIN}/info` }).catch(() => {
|
|
1038
|
+
return;
|
|
1039
|
+
});
|
|
1040
|
+
return info?.house;
|
|
1041
|
+
}
|
|
1042
|
+
function unlabeledLights(snapshot) {
|
|
1043
|
+
return Object.values(snapshot.entities).filter((e) => {
|
|
1044
|
+
const id = String(e.entity_id);
|
|
1045
|
+
return id.startsWith("light.") && !e.hidden && !(e.labels ?? []).includes(HOUSEHOLD_LIGHT_LABEL);
|
|
1046
|
+
}).map((e) => {
|
|
1047
|
+
const device = e.device_id ? snapshot.devices[String(e.device_id)] : undefined;
|
|
1048
|
+
const areaId = e.area_id ?? device?.area_id ?? null;
|
|
1049
|
+
const area = areaId ? snapshot.areas[areaId] : undefined;
|
|
1050
|
+
const name = String(snapshot.states[String(e.entity_id)]?.attributes.friendly_name ?? e.name ?? e.entity_id);
|
|
1051
|
+
return {
|
|
1052
|
+
entity_id: String(e.entity_id),
|
|
1053
|
+
name,
|
|
1054
|
+
area: area?.name ?? null,
|
|
1055
|
+
floor: area?.floor_id ?? null,
|
|
1056
|
+
device: device?.name_by_user ?? device?.name ?? null,
|
|
1057
|
+
model: device?.model ?? null,
|
|
1058
|
+
looks_like_an_indicator: /\b(led|indicator|status|night ?light|backlight)\b/i.test(`${name} ${device?.model ?? ""}`)
|
|
1059
|
+
};
|
|
1060
|
+
}).sort((a, b) => `${a.area ?? "~"} ${a.name}`.localeCompare(`${b.area ?? "~"} ${b.name}`));
|
|
1061
|
+
}
|
|
1062
|
+
function checkHouse(yaml) {
|
|
1063
|
+
const raw = Bun.YAML.parse(yaml) ?? {};
|
|
1064
|
+
if (typeof raw !== "object" || Array.isArray(raw))
|
|
1065
|
+
return { understood: [], ignored: ["(not a mapping)"] };
|
|
1066
|
+
const house = parseHouseConfig(raw);
|
|
1067
|
+
const count = (v) => Array.isArray(v) ? v.length : v && typeof v === "object" ? Object.keys(v).length : 0;
|
|
1068
|
+
const used = {
|
|
1069
|
+
announce_speaker: !!house.announceSpeaker,
|
|
1070
|
+
weather: !!house.weather,
|
|
1071
|
+
alarm_panel: !!house.alarmPanel,
|
|
1072
|
+
thermostats: (house.thermostats?.length ?? 0) === count(raw.thermostats) && count(raw.thermostats) > 0,
|
|
1073
|
+
laundry: !!(house.laundry.washer || house.laundry.dryer),
|
|
1074
|
+
guest_wifi_qr: !!house.guestWifiQr,
|
|
1075
|
+
live_inputs: house.liveInputs.length === count(raw.live_inputs) && count(raw.live_inputs) > 0,
|
|
1076
|
+
tts: !!(house.tts.engine || house.tts.voice),
|
|
1077
|
+
birthday_calendar: !!house.birthdayCalendar,
|
|
1078
|
+
bedtime_routines: Object.keys(house.bedtimeRoutines).length === count(raw.bedtime_routines),
|
|
1079
|
+
modules: Object.keys(house.modules).length === count(raw.modules)
|
|
1080
|
+
};
|
|
1081
|
+
const keys = Object.keys(raw).filter((k) => raw[k] != null);
|
|
1082
|
+
return { understood: keys.filter((k) => used[k]), ignored: keys.filter((k) => !used[k]) };
|
|
1083
|
+
}
|
|
1084
|
+
function dashboardConfig(card) {
|
|
1085
|
+
return { views: [{ title: "Home", path: "home", panel: true, cards: [card] }] };
|
|
1086
|
+
}
|
|
1087
|
+
function setupTools(connect) {
|
|
1088
|
+
return [
|
|
1089
|
+
{
|
|
1090
|
+
name: "integration_status",
|
|
1091
|
+
description: "Whether the Raineworks Dashboards integration is installed and set up on this Home Assistant, its version, the household it found, and which dashboards use its cards. Start here.",
|
|
1092
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
1093
|
+
run: () => withHass(connect, async (ha) => {
|
|
1094
|
+
const entries = await ha.call({
|
|
1095
|
+
type: "config_entries/get",
|
|
1096
|
+
domain: DOMAIN
|
|
1097
|
+
});
|
|
1098
|
+
const info = entries.some((e) => e.state === "loaded") ? await ha.call({ type: `${DOMAIN}/info` }).catch(() => null) : null;
|
|
1099
|
+
const dashboards = await ha.call({
|
|
1100
|
+
type: "lovelace/dashboards/list"
|
|
1101
|
+
});
|
|
1102
|
+
return json({
|
|
1103
|
+
installed: entries.length > 0,
|
|
1104
|
+
state: entries[0]?.state ?? "not set up",
|
|
1105
|
+
version: info?.version ?? null,
|
|
1106
|
+
household: info?.household ?? null,
|
|
1107
|
+
dashboards: dashboards.map((d) => ({ url_path: d.url_path, title: d.title, mode: d.mode })),
|
|
1108
|
+
next: entries.length ? undefined : 'Install "Raineworks Dashboards" from the Home Assistant Marketplace (Settings \u2192 Marketplace; custom repository https://github.com/raine-works/ha-dashboards), restart, then add the integration.'
|
|
1109
|
+
});
|
|
1110
|
+
})
|
|
1111
|
+
},
|
|
1112
|
+
{
|
|
1113
|
+
name: "capability_report",
|
|
1114
|
+
description: "Which dashboard features (modules) this Home Assistant has, which are missing and what each missing one needs, and which the house settings turn off. Uses the integration's house settings; pass house_yaml to add a card's own (they win key by key).",
|
|
1115
|
+
inputSchema: {
|
|
1116
|
+
type: "object",
|
|
1117
|
+
properties: { house_yaml: { type: "string", description: "A card's `house` settings, as YAML" } },
|
|
1118
|
+
additionalProperties: false
|
|
1119
|
+
},
|
|
1120
|
+
run: (args) => withHass(connect, async (ha) => {
|
|
1121
|
+
const [snapshot, fromIntegration] = await Promise.all([readSnapshot(ha), integrationHouse(ha)]);
|
|
1122
|
+
const house = parseHouseConfig(mergeHouseSources(fromIntegration, yamlOf(args.house_yaml)));
|
|
1123
|
+
const rows = moduleReport({ hass: snapshot, house });
|
|
1124
|
+
return json({
|
|
1125
|
+
on: rows.filter((r) => r.state === "on").map((r) => r.name),
|
|
1126
|
+
missing: rows.filter((r) => r.state === "missing").map((r) => ({ module: r.name, needs: r.needs })),
|
|
1127
|
+
off: rows.filter((r) => r.state === "off").map((r) => r.name)
|
|
1128
|
+
});
|
|
1129
|
+
})
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
name: "list_floors",
|
|
1133
|
+
description: "The floors and their areas, for a wall panel's `floor` option and for checking every room's lights are in an area on a floor.",
|
|
1134
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
1135
|
+
run: () => withHass(connect, async (ha) => {
|
|
1136
|
+
const snapshot = await readSnapshot(ha);
|
|
1137
|
+
const areas = Object.values(snapshot.areas);
|
|
1138
|
+
return json({
|
|
1139
|
+
floors: Object.values(snapshot.floors).map((f) => ({
|
|
1140
|
+
floor_id: f.floor_id,
|
|
1141
|
+
name: f.name,
|
|
1142
|
+
level: f.level ?? null,
|
|
1143
|
+
areas: areas.filter((a) => a.floor_id === f.floor_id).map((a) => a.name)
|
|
1144
|
+
})),
|
|
1145
|
+
areas_without_a_floor: areas.filter((a) => !a.floor_id).map((a) => a.name)
|
|
1146
|
+
});
|
|
1147
|
+
})
|
|
1148
|
+
},
|
|
1149
|
+
{
|
|
1150
|
+
name: "find_unlabeled_lights",
|
|
1151
|
+
description: 'Lights without the "Household Light" label, which the lighting section lists by floor and room. Room lights (lamps, ceiling lights, light switches) should get it; indicators, status LEDs and panel lights should not (looks_like_an_indicator flags likely ones). Ask the user before labeling.',
|
|
1152
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
1153
|
+
run: () => withHass(connect, async (ha) => json(unlabeledLights(await readSnapshot(ha))))
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
name: "label_household_lights",
|
|
1157
|
+
description: 'Gives these lights the "Household Light" label (creating the label if needed), so the dashboards list them. Changes Home Assistant: confirm the list with the user first.',
|
|
1158
|
+
writes: true,
|
|
1159
|
+
inputSchema: {
|
|
1160
|
+
type: "object",
|
|
1161
|
+
properties: { entity_ids: { type: "array", items: { type: "string" }, description: "light.* entity ids" } },
|
|
1162
|
+
required: ["entity_ids"],
|
|
1163
|
+
additionalProperties: false
|
|
1164
|
+
},
|
|
1165
|
+
run: (args) => withHass(connect, async (ha) => {
|
|
1166
|
+
const ids = args.entity_ids.filter((id) => typeof id === "string");
|
|
1167
|
+
if (!ids.length || ids.some((id) => !id.startsWith("light.")))
|
|
1168
|
+
throw new Error("entity_ids must be light.* entity ids");
|
|
1169
|
+
const labels = await ha.call({ type: "config/label_registry/list" });
|
|
1170
|
+
if (!labels.some((l) => l.label_id === HOUSEHOLD_LIGHT_LABEL))
|
|
1171
|
+
await ha.call({ type: "config/label_registry/create", name: "Household Light", icon: "mdi:lightbulb" });
|
|
1172
|
+
const labeled = [];
|
|
1173
|
+
for (const id of ids) {
|
|
1174
|
+
const entry = await ha.call({ type: "config/entity_registry/get", entity_id: id });
|
|
1175
|
+
const current = entry.labels ?? [];
|
|
1176
|
+
if (!current.includes(HOUSEHOLD_LIGHT_LABEL)) {
|
|
1177
|
+
await ha.call({
|
|
1178
|
+
type: "config/entity_registry/update",
|
|
1179
|
+
entity_id: id,
|
|
1180
|
+
labels: [...current, HOUSEHOLD_LIGHT_LABEL]
|
|
1181
|
+
});
|
|
1182
|
+
labeled.push(id);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return json({ labeled, already_labeled: ids.filter((id) => !labeled.includes(id)) });
|
|
1186
|
+
})
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
name: "create_dashboard",
|
|
1190
|
+
description: 'Creates a dashboard hosting one of the cards: "home" (desktops and phones) or "wall_panel" (a wall tablet, with its name, floor and optional timer). Changes Home Assistant: confirm with the user first.',
|
|
1191
|
+
writes: true,
|
|
1192
|
+
inputSchema: {
|
|
1193
|
+
type: "object",
|
|
1194
|
+
properties: {
|
|
1195
|
+
title: { type: "string", description: "Its name in the sidebar" },
|
|
1196
|
+
url_path: { type: "string", description: "Its URL, with a hyphen (dashboard-kitchen)" },
|
|
1197
|
+
card: { type: "string", enum: ["home", "wall_panel"] },
|
|
1198
|
+
name: { type: "string", description: `wall_panel: the panel's name ("Kitchen wall panel")` },
|
|
1199
|
+
floor: { type: "string", description: "wall_panel: a floor_id from list_floors" },
|
|
1200
|
+
timer: { type: "boolean", description: "wall_panel: a countdown timer in the header" }
|
|
1201
|
+
},
|
|
1202
|
+
required: ["title", "url_path", "card"],
|
|
1203
|
+
additionalProperties: false
|
|
1204
|
+
},
|
|
1205
|
+
run: (args) => withHass(connect, async (ha) => {
|
|
1206
|
+
const urlPath = String(args.url_path);
|
|
1207
|
+
if (!/^[a-z0-9]+(-[a-z0-9]+)+$/.test(urlPath))
|
|
1208
|
+
throw new Error("url_path must be lowercase words joined by hyphens, with at least one hyphen");
|
|
1209
|
+
const card = args.card === "wall_panel" ? {
|
|
1210
|
+
type: "custom:wall-panel-dashboard",
|
|
1211
|
+
...args.name ? { name: String(args.name) } : {},
|
|
1212
|
+
...args.floor ? { floor: String(args.floor) } : {},
|
|
1213
|
+
...args.timer ? { timer: true } : {}
|
|
1214
|
+
} : { type: "custom:home-dashboard" };
|
|
1215
|
+
await ha.call({
|
|
1216
|
+
type: "lovelace/dashboards/create",
|
|
1217
|
+
url_path: urlPath,
|
|
1218
|
+
title: String(args.title),
|
|
1219
|
+
mode: "storage",
|
|
1220
|
+
icon: args.card === "wall_panel" ? "mdi:tablet" : "mdi:home",
|
|
1221
|
+
show_in_sidebar: true,
|
|
1222
|
+
require_admin: false
|
|
1223
|
+
});
|
|
1224
|
+
await ha.call({ type: "lovelace/config/save", url_path: urlPath, config: dashboardConfig(card) });
|
|
1225
|
+
return json({
|
|
1226
|
+
created: `/${urlPath}/home`,
|
|
1227
|
+
card,
|
|
1228
|
+
next: args.card === "wall_panel" ? "Point the tablet at this dashboard, then add it on the integration's page (Add wall panel) for its status light and wake." : undefined
|
|
1229
|
+
});
|
|
1230
|
+
})
|
|
1231
|
+
},
|
|
1232
|
+
{
|
|
1233
|
+
name: "check_house_settings",
|
|
1234
|
+
description: "Which keys of a card's `house` settings (YAML) the dashboards understood. A value of the wrong shape is ignored without an error, so check settings this way before relying on them.",
|
|
1235
|
+
inputSchema: {
|
|
1236
|
+
type: "object",
|
|
1237
|
+
properties: { yaml: { type: "string", description: "The house settings, as YAML" } },
|
|
1238
|
+
required: ["yaml"],
|
|
1239
|
+
additionalProperties: false
|
|
1240
|
+
},
|
|
1241
|
+
run: async (args) => json(checkHouse(String(args.yaml)))
|
|
1242
|
+
}
|
|
1243
|
+
];
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// ../mcp/src/index.ts
|
|
1247
|
+
var server = createServer(setupTools(connectHomeAssistant), {
|
|
1248
|
+
name: "raineworks-dashboards",
|
|
1249
|
+
version: "1.0.0",
|
|
1250
|
+
instructions: "Sets up Raineworks Dashboards on a Home Assistant. Start with integration_status, then capability_report. Tools that change Home Assistant (label_household_lights, create_dashboard) need the user to agree first. Docs: https://github.com/raine-works/ha-dashboards/tree/master/docs"
|
|
1251
|
+
});
|
|
1252
|
+
await serveStdio(server.handle);
|