-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.ts
351 lines (313 loc) · 10.1 KB
/
app.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/// <reference types="@google/local-home-sdk" />
import App = smarthome.App;
import Constants = smarthome.Constants;
import DataFlow = smarthome.DataFlow;
import Execute = smarthome.Execute;
import Intents = smarthome.Intents;
import IntentFlow = smarthome.IntentFlow;
import HttpResponseData = smarthome.DataFlow.HttpResponseData;
import ErrorCode = IntentFlow.ErrorCode;
type ReqRes<S> = S extends IntentFlow.IntentHandler<infer REQ, infer RES>
? { request: REQ; response: RES }
: never;
type Requests = {
// [Intents.EVENT]: ReqRes<IntentFlow.EventHandler>;
[Intents.EXECUTE]: ReqRes<IntentFlow.ExecuteHandler>;
[Intents.IDENTIFY]: ReqRes<IntentFlow.IdentifyHandler>;
// [Intents.INDICATE]: ReqRes<IntentFlow.IndicateHandler>;
// [Intents.PARSE_NOTIFICATION]: ReqRes<IntentFlow.ParseNotificationHandler>;
// [Intents.PROVISION]: ReqRes<IntentFlow.ProvisionHandler>;
[Intents.PROXY_SELECTED]: ReqRes<IntentFlow.ProxySelectedHandler>;
[Intents.QUERY]: ReqRes<IntentFlow.QueryHandler>;
[Intents.REACHABLE_DEVICES]: ReqRes<IntentFlow.ReachableDevicesHandler>;
// [Intents.REGISTER]: ReqRes<IntentFlow.RegisterHandler>;
// [Intents.UNPROVISION]: ReqRes<IntentFlow.UnprovisionHandler>;
// [Intents.UPDATE]: ReqRes<IntentFlow.UpdateHandler>;
};
interface HassCustomDeviceData {
webhookId: string;
httpPort: number;
uuid?: string;
}
const VERSION = "2.1.6";
class RequestResponseHandler<T extends keyof Requests> {
_deviceManager?: smarthome.DeviceManager;
haVersion?: string;
constructor(
public intent: T,
public request: Requests[T]["request"],
public options: {
supportedHAVersion?: [number, number];
extractHAVersion?: (
deviceManager: smarthome.DeviceManager
) => string | undefined;
} = {}
) {
this.logMessage("Processing", request);
}
async getDeviceManager() {
if (this._deviceManager) {
return this._deviceManager;
}
this._deviceManager = await app.getDeviceManager();
this.haVersion = (
this.options.extractHAVersion || getHAVersionFromProxyDevice
)(this._deviceManager);
return this._deviceManager;
}
createResponse(payload: smarthome.IntentResponse["payload"]): any {
return {
requestId: this.request.requestId,
payload,
};
}
/** Create and log the error. */
createError(errorCode: ErrorCode, msg: string, ...extraLog: any[]) {
this.logError(`Error ${errorCode}`, msg, ...extraLog);
if (this.haVersion) {
msg += ` (HA/${this.haVersion})`;
}
return new IntentFlow.HandlerError(this.request.requestId, errorCode, msg);
}
/** Get Home Assistant info stored in device custom data. */
getHassCustomData(
deviceManager: smarthome.DeviceManager
): HassCustomDeviceData {
for (const device of deviceManager.getRegisteredDevices()) {
const customData = device.customData as HassCustomDeviceData | undefined;
if (customData && "webhookId" in customData && "httpPort" in customData) {
return customData;
}
}
throw this.createError(
ErrorCode.DEVICE_VERIFICATION_FAILED,
`Unable to find HASS connection info.`,
deviceManager.getRegisteredDevices()
);
}
get logPrefix() {
let prefix = `${
this.intent.startsWith("action.devices.")
? this.intent.substring("action.devices.".length)
: this.intent
}-${this.request.requestId.substring(0, 5)}`;
if (this.haVersion) {
prefix += ` @ HA/${this.haVersion}`;
}
return `[${prefix}]`;
}
logMessage(msg: string, ...extraLog: any[]) {
if (extraLog.length > 0) {
msg += "\n";
}
console.log(this.logPrefix, msg, ...extraLog);
}
logError(msg: string, ...extraLog: any[]) {
if (extraLog.length > 0) {
msg += "\n";
}
console.error(this.logPrefix, msg, ...extraLog);
}
async forwardRequest(
targetDeviceId: string,
isRetry = false
): Promise<Requests[T]["response"]> {
const deviceManager = await this.getDeviceManager();
this.logMessage(`Sending to HA`, this.request);
const haVersion = this.haVersion;
if (
this.options.supportedHAVersion &&
(!haVersion ||
!atleastVersion(haVersion, ...this.options.supportedHAVersion))
) {
this.logMessage(
"Intent not supported by HA version. Returning empty response"
);
return this.createResponse({} as any);
}
const deviceData = this.getHassCustomData(deviceManager);
const command = new DataFlow.HttpRequestData();
command.method = Constants.HttpOperation.POST;
command.requestId = this.request.requestId;
command.deviceId = targetDeviceId;
command.port = deviceData.httpPort;
command.path = `/api/webhook/${deviceData.webhookId}`;
command.data = JSON.stringify(this.request);
command.dataType = "application/json";
command.additionalHeaders = {
"HA-Cloud-Version": VERSION,
};
// this.logMessage("Sending", command);
let rawResponse: HttpResponseData;
try {
rawResponse = (await deviceManager.send(command)) as HttpResponseData;
} catch (err) {
this.logError("Error making request", err);
// Errors coming out of `deviceManager.send` are already Google errors.
throw err;
}
// Detect the response if the webhook is not registered.
// This can happen if user logs out from cloud while Google still
// has devices synced or if Home Assistant is restarting and Google Assistant
// integration is not yet initialized.
if (
rawResponse.httpResponse.statusCode === 200 &&
!rawResponse.httpResponse.body
) {
// Retry in case it's because of initialization.
if (
!isRetry &&
[
Intents.IDENTIFY,
Intents.PROXY_SELECTED,
Intents.REACHABLE_DEVICES,
Intents.QUERY,
].includes(this.intent)
) {
return await this.forwardRequest(targetDeviceId, true);
}
throw this.createError(ErrorCode.GENERIC_ERROR, "Webhook not registered");
}
let response: Requests[T]["response"];
try {
response = JSON.parse(rawResponse.httpResponse.body as string);
} catch (err) {
this.logError(
"Invalid JSON in response",
rawResponse.httpResponse.body,
err
);
throw this.createError(
ErrorCode.GENERIC_ERROR,
`Error parsing body: ${rawResponse.httpResponse.body}`,
rawResponse.httpResponse.body
);
}
this.logMessage("Response", response);
return response;
}
}
// The types are wrong. The registered proxy device (HA) includes unparsed mdns props.
interface RegisteredDeviceMdnsScanData extends IntentFlow.MdnsScanData {
texts: string[];
}
const extractHAVersionFromMdnsRecords = (
texts: string[]
): string | undefined => {
for (const text of texts) {
if (text.startsWith("version=")) {
return text.split("=")[1];
}
}
return undefined;
};
const getHAVersionFromProxyDevice = (
deviceManager: smarthome.DeviceManager
): string | undefined => {
const proxyDevice = deviceManager.getRegisteredDevices().find(
(dev) =>
// Only the proxy device has scanData
(dev.scanData?.mdnsScanData as RegisteredDeviceMdnsScanData)?.texts
);
if (!proxyDevice) {
return undefined;
}
return extractHAVersionFromMdnsRecords(
(proxyDevice.scanData!.mdnsScanData as RegisteredDeviceMdnsScanData).texts
);
};
const atleastVersion = (
haVersion: string,
major: number,
minor: number
): boolean => {
const parts = haVersion.split(".");
if (parts.length < 2) {
return false;
}
let numbers: [number, number];
try {
numbers = [parseInt(parts[0]), parseInt(parts[1])];
} catch (err) {
return false;
}
return (
// If major version is higher
numbers[0] > major ||
// same major, higher or equal minor
(numbers[0] == major && numbers[1] >= minor)
);
};
const app = new App(VERSION);
app
.onIdentify(async (request) => {
const handler = new RequestResponseHandler(Intents.IDENTIFY, request, {
extractHAVersion: () =>
extractHAVersionFromMdnsRecords(
request.inputs[0].payload.device.mdnsScanData?.data || []
),
});
const deviceManager = await handler.getDeviceManager();
const deviceToIdentify = request.inputs[0].payload.device;
if (
!deviceToIdentify.mdnsScanData ||
deviceToIdentify.mdnsScanData.data.length === 0
) {
throw handler.createError(
ErrorCode.DEVICE_NOT_IDENTIFIED,
"No usable mdns scan data"
);
}
if (
!deviceToIdentify.mdnsScanData.serviceName.endsWith(
"._home-assistant._tcp.local"
)
) {
throw handler.createError(
ErrorCode.DEVICE_NOT_IDENTIFIED,
`Not Home Assistant type: ${deviceToIdentify.mdnsScanData.serviceName}`
);
}
const customData = handler.getHassCustomData(deviceManager);
if (
deviceToIdentify.mdnsScanData.txt.uuid &&
customData.uuid &&
deviceToIdentify.mdnsScanData.txt.uuid !== customData.uuid
) {
throw handler.createError(
ErrorCode.DEVICE_VERIFICATION_FAILED,
`UUID does not match.`,
deviceManager.getRegisteredDevices()
);
}
return await handler.forwardRequest("");
})
// Intents targeting the proxy device
// This used to fix things, in June 2022 it breaks things?
// .onProxySelected((request) =>
// new RequestResponseHandler(Intents.PROXY_SELECTED, request, {
// supportedHAVersion: [2022, 3],
// }).forwardRequest(request.inputs[0].payload.device.id!)
// )
.onReachableDevices((request) =>
new RequestResponseHandler(
Intents.REACHABLE_DEVICES,
request
).forwardRequest(request.inputs[0].payload.device.id!)
)
// Intents targeting a device in Home Assistant
.onQuery((request) =>
new RequestResponseHandler(Intents.QUERY, request).forwardRequest(
request.inputs[0].payload.devices[0].id
)
)
.onExecute((request) =>
new RequestResponseHandler(Intents.EXECUTE, request).forwardRequest(
request.inputs[0].payload.commands[0].devices[0].id
)
)
.listen()
.then(() => {
console.log("Ready!");
})
.catch((e: Error) => console.error(e));