-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
296 lines (255 loc) · 7.9 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
/// <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;
const findHassCustomDeviceDataByMdnsData = (
requestId: string,
devices: Array<{ customData?: unknown }>,
mdnsScanData: { [key: string]: string }
) => {
let device;
device = devices.find((dev) => {
const customData = dev.customData as HassCustomDeviceData;
return (
customData &&
"webhookId" in customData &&
(!mdnsScanData.uuid || customData.uuid === mdnsScanData.uuid) &&
(!mdnsScanData.baseUrl || customData.baseUrl === mdnsScanData.baseUrl)
);
});
// backwards compatibility for HA < 0.109
if (!device) {
device = devices.find(
(dev) =>
dev.customData &&
"webhookId" in (dev.customData as HassCustomDeviceData)
);
}
if (!device) {
console.log(requestId, "Unable to find HASS connection info.", devices);
throw new IntentFlow.HandlerError(
requestId,
"invalidRequest",
"Unable to find HASS connection info."
);
}
return device.customData as HassCustomDeviceData;
};
const findHassCustomDeviceDataByDeviceId = (
requestId: string,
devices: Array<{ customData?: unknown }>,
deviceId?: string
) => {
let device;
device = devices.find((dev) => {
const customData = dev.customData as HassCustomDeviceData;
return (
customData &&
"webhookId" in customData &&
customData.proxyDeviceId === deviceId
);
});
if (!device) {
console.log(requestId, "Unable to find HASS connection info.", devices);
throw new IntentFlow.HandlerError(
requestId,
"invalidRequest",
"Unable to find HASS connection info."
);
}
return device.customData as HassCustomDeviceData;
};
const createResponse = (
request: smarthome.IntentRequest,
payload: smarthome.IntentResponse["payload"]
): any => ({
intent: request.inputs[0].intent,
requestId: request.requestId,
payload,
});
interface HassCustomDeviceData {
webhookId: string;
httpPort: number;
httpSSL: boolean;
baseUrl?: string;
uuid?: string;
proxyDeviceId: string;
}
class UnknownInstance extends Error {
constructor(public requestId: string) {
super();
}
throwHandlerError() {
throw new IntentFlow.HandlerError(
this.requestId,
"invalidRequest",
"Unknown Instance"
);
}
}
const forwardRequest = async (
hassDeviceData: HassCustomDeviceData,
targetDeviceId: string,
request: smarthome.IntentRequest
) => {
const command = new DataFlow.HttpRequestData();
command.method = Constants.HttpOperation.POST;
command.requestId = request.requestId;
command.deviceId = targetDeviceId;
command.isSecure = hassDeviceData.httpSSL;
command.port = hassDeviceData.httpPort;
command.path = `/api/webhook/${hassDeviceData.webhookId}`;
command.data = JSON.stringify(request);
command.dataType = "application/json";
console.log(request.requestId, "Sending", command);
const deviceManager = await app.getDeviceManager();
let resp: HttpResponseData;
try {
resp = await new Promise<HttpResponseData>((resolve, reject) => {
setTimeout(() => reject(-1), 10000);
deviceManager
.send(command)
.then((response: any) => resolve(response as HttpResponseData), reject);
});
// resp = (await deviceManager.send(command)) as HttpResponseData;
console.log(request.requestId, "Raw Response", resp);
} catch (err) {
console.error(request.requestId, "Error making request", err);
throw new IntentFlow.HandlerError(
request.requestId,
"invalidRequest",
err === -1 ? "Timeout" : err.message
);
}
// Response if the webhook is not registered.
if (resp.httpResponse.statusCode === 200 && !resp.httpResponse.body) {
throw new UnknownInstance(request.requestId);
}
try {
const response = JSON.parse(resp.httpResponse.body as string);
// Local SDK wants this.
response.intent = request.inputs[0].intent;
console.log(request.requestId, "Response", response);
return response;
} catch (err) {
console.error(request.requestId, "Error parsing body", err);
throw new IntentFlow.HandlerError(
request.requestId,
"invalidRequest",
err.message
);
}
};
const identifyHandler = async (
request: IntentFlow.IdentifyRequest
): Promise<IntentFlow.IdentifyResponse> => {
console.log("IDENTIFY intent:", request);
const deviceToIdentify = request.inputs[0].payload.device;
if (!deviceToIdentify.mdnsScanData) {
console.error(request.requestId, "No usable mdns scan data");
return createResponse(request, {} as any);
}
if (
!deviceToIdentify.mdnsScanData.serviceName.endsWith(
"._home-assistant._tcp.local"
)
) {
console.error(request.requestId, "Not Home Assistant type");
return createResponse(request, {} as any);
}
try {
const hassCustomData = findHassCustomDeviceDataByMdnsData(
request.requestId,
request.devices,
deviceToIdentify.mdnsScanData.txt
);
return await forwardRequest(hassCustomData, "", request);
} catch (err) {
if (err instanceof UnknownInstance) {
return createResponse(request, {} as any);
}
throw err;
}
};
const reachableDevicesHandler = async (
request: IntentFlow.ReachableDevicesRequest
): Promise<IntentFlow.ReachableDevicesResponse> => {
console.log("REACHABLE_DEVICES intent:", request);
const hassCustomData = findHassCustomDeviceDataByDeviceId(
request.requestId,
request.devices,
request.inputs[0].payload.device.id
);
try {
return forwardRequest(
hassCustomData,
// Old code would sent it to the proxy ID: hassCustomData.proxyDeviceId
// But tutorial claims otherwise, but maybe it is not for hub devices??
// https://developers.google.com/assistant/smarthome/develop/local#implement_the_execute_handler
// Sending it to the device that has to receive the command as per the tutorial
request.inputs[0].payload.device.id as string,
request
);
} catch (err) {
if (err instanceof UnknownInstance) {
err.throwHandlerError();
}
throw err;
}
};
const executeHandler = async (
request: IntentFlow.ExecuteRequest
): Promise<IntentFlow.ExecuteResponse> => {
console.log("EXECUTE intent:", request);
const device = request.inputs[0].payload.commands[0].devices[0];
try {
return forwardRequest(
device.customData as HassCustomDeviceData,
device.id,
request
);
} catch (err) {
if (err instanceof UnknownInstance) {
err.throwHandlerError();
}
throw err;
}
};
const app = new App("1.0.0");
app
.onIdentify(identifyHandler)
.onReachableDevices(reachableDevicesHandler)
.onExecute(executeHandler)
// Undocumented in TypeScript
// Suggested by Googler, seems to work :shrug:
// https://github.com/actions-on-google/smart-home-local/issues/1#issuecomment-515706997
// @ts-ignore
.onProxySelected((req) => {
console.log("ProxySelected", req);
return createResponse(req, {} as any);
})
// @ts-ignore
.onIndicate((req) => console.log("Indicate", req))
// @ts-ignore
.onParseNotification((req) => console.log("ParseNotification", req))
// @ts-ignore
.onProvision((req) => console.log("Provision", req))
// @ts-ignore
.onQuery((req) => console.log("Query", req))
// @ts-ignore
.onRegister((req) => console.log("Register", req))
// @ts-ignore
.onUnprovision((req) => console.log("Unprovision", req))
// @ts-ignore
.onUpdate((req) => console.log("Update", req))
.listen()
.then(() => {
console.log("Ready!");
// Play audio to indicate that receiver is ready to inspect.
new Audio("https://www.pacdv.com/sounds/fart-sounds/fart-1.wav").play();
})
.catch((e: Error) => console.error(e));