-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
83 lines (66 loc) · 1.89 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {
createConnection,
createLongLivedTokenAuth,
} from "npm:[email protected]"
import {config} from "https://deno.land/x/dotenv/mod.ts";
const TOKEN = config().HASS_TOKEN;
const auth = createLongLivedTokenAuth(
"https://hass.coopermoses.com",
TOKEN,
);
async function conn() {
return await createConnection({ auth} );
}
async function doGetLovelace(): Promise<void> {
const connection = await conn();
const lovelace = await connection.sendMessagePromise({
type: "lovelace/config",
url_path: null,
force: false,
});
Deno.stdout.write(new TextEncoder().encode(JSON.stringify(lovelace)))
}
async function doGetEnergy(): Promise<void> {
const connection = await conn();
const lovelace = await connection.sendMessagePromise({
type: "energy/config",
url_path: null,
force: false,
});
Deno.stdout.write(new TextEncoder().encode(JSON.stringify(lovelace)))
}
async function doSetLovelace() {
const val = new TextDecoder().decode(await Deno.readAll(Deno.stdin));
const dash = JSON.parse(val)
const connection = await conn();
const ret = await connection.sendMessagePromise<{success: boolean}>({
type: "lovelace/config/save",
url_path: null,
config: dash,
})
if (!ret.success) {
console.error("Error: ${ret}");
} else {
console.log("Saved!");
}
}
(async () => {
if (!TOKEN) {
console.error("Set HASS_TOKEN in your environment or .env file");
Deno.exit(1);
}
const cmd = Deno.args[0]
switch (cmd) {
case "get":
await doGetLovelace();
Deno.exit(0);
break;
case "set":
await doSetLovelace();
Deno.exit(0);
break;
default:
console.error(`Unexpected command: ${cmd}`)
Deno.exit(1);
}
})();