1 | /**
|
2 | * Copyright 2018 Google Inc. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | import { AppOptions, AppHandler, ServiceBaseApp } from '../../assistant';
|
17 | import { Headers, BuiltinFrameworkMetadata } from '../../framework';
|
18 | import * as Api from './api/v1';
|
19 | /**
|
20 | * @public
|
21 | * @deprecated Home Graph credentials are deprecated.
|
22 | * Use Google APIs Node.js Client for Home Graph:
|
23 | * https://www.npmjs.com/package/@googleapis/homegraph
|
24 | */
|
25 | export interface SmartHomeJwt {
|
26 | type: 'service_account';
|
27 | project_id: string;
|
28 | private_key_id: string;
|
29 | private_key: string;
|
30 | client_email: string;
|
31 | client_id: string;
|
32 | auth_uri: string;
|
33 | token_uri: string;
|
34 | auth_provider_x509_cert_url: string;
|
35 | client_x509_cert_url: string;
|
36 | }
|
37 | /** @public */
|
38 | export interface SmartHomeOptions extends AppOptions {
|
39 | /**
|
40 | * An API key to use the home graph API. See
|
41 | * https://console.cloud.google.com/apis/api/homegraph.googleapis.com/overview
|
42 | * to learn more.
|
43 | * @public
|
44 | * @deprecated Home Graph credentials are deprecated.
|
45 | * Use Google APIs Node.js Client for Home Graph:
|
46 | * https://www.npmjs.com/package/@googleapis/homegraph
|
47 | */
|
48 | key?: string;
|
49 | /**
|
50 | * A JWT (JSON Web Token) that is able to access the home graph API.
|
51 | * This is used for report state. See https://jwt.io/. A JWT can be
|
52 | * created through the Google Cloud Console: https://console.cloud.google.com/apis/credentials
|
53 | * @public
|
54 | * @deprecated Home Graph credentials are deprecated.
|
55 | * Use Google APIs Node.js Client for Home Graph:
|
56 | * https://www.npmjs.com/package/@googleapis/homegraph
|
57 | */
|
58 | jwt?: SmartHomeJwt;
|
59 | }
|
60 | /** @public */
|
61 | export interface SmartHomeHandler<TRequest extends Api.SmartHomeV1Request, TResponse extends Api.SmartHomeV1Response> {
|
62 | (body: TRequest, headers: Headers, framework: BuiltinFrameworkMetadata): TResponse | Promise<TResponse>;
|
63 | }
|
64 | /** @hidden */
|
65 | export interface SmartHomeHandlers {
|
66 | [intent: string]: SmartHomeHandler<Api.SmartHomeV1Request, Api.SmartHomeV1Response>;
|
67 | }
|
68 | /** @public */
|
69 | export interface SmartHomeApp extends ServiceBaseApp {
|
70 | /** @hidden */
|
71 | _intents: SmartHomeHandlers;
|
72 | /** @hidden */
|
73 | _intent(intent: Api.SmartHomeV1Intents, handler: SmartHomeHandler<Api.SmartHomeV1Request, Api.SmartHomeV1Response>): this;
|
74 | /**
|
75 | * Defines a function that will run when a SYNC request is received.
|
76 | *
|
77 | * @example
|
78 | * ```javascript
|
79 | *
|
80 | * const app = smarthome();
|
81 | * app.onSync((body, headers) => {
|
82 | * return {
|
83 | * requestId: 'ff36...',
|
84 | * payload: {
|
85 | * ...
|
86 | * }
|
87 | * }
|
88 | * })
|
89 | * ```
|
90 | *
|
91 | * @param handler The function that will run for a SYNC request. It should
|
92 | * return a valid response or a Promise that resolves to valid response.
|
93 | *
|
94 | * @public
|
95 | */
|
96 | onSync(handler: SmartHomeHandler<Api.SmartHomeV1SyncRequest, Api.SmartHomeV1SyncResponse>): this;
|
97 | /**
|
98 | * Defines a function that will run when a QUERY request is received.
|
99 | *
|
100 | * @example
|
101 | * ```javascript
|
102 | *
|
103 | * const app = smarthome();
|
104 | * app.onQuery((body, headers) => {
|
105 | * return {
|
106 | * requestId: 'ff36...',
|
107 | * payload: {
|
108 | * ...
|
109 | * }
|
110 | * }
|
111 | * })
|
112 | * ```
|
113 | *
|
114 | * @param handler The function that will run for a QUERY request. It should
|
115 | * return a valid response or a Promise that resolves to valid response.
|
116 | *
|
117 | * @public
|
118 | */
|
119 | onQuery(handler: SmartHomeHandler<Api.SmartHomeV1QueryRequest, Api.SmartHomeV1QueryResponse>): this;
|
120 | /**
|
121 | * Defines a function that will run when an EXECUTE request is received.
|
122 | *
|
123 | * @example
|
124 | * ```javascript
|
125 | *
|
126 | * const app = smarthome();
|
127 | * app.onExecute((body, headers) => {
|
128 | * return {
|
129 | * requestId: 'ff36...',
|
130 | * payload: {
|
131 | * ...
|
132 | * }
|
133 | * }
|
134 | * })
|
135 | * ```
|
136 | * @param handler The function that will run for an EXECUTE request. It should
|
137 | * return a valid response or a Promise that resolves to valid response.
|
138 | *
|
139 | * @public
|
140 | */
|
141 | onExecute(handler: SmartHomeHandler<Api.SmartHomeV1ExecuteRequest, Api.SmartHomeV1ExecuteResponse>): this;
|
142 | /**
|
143 | * Defines a function that will run when a DISCONNECT request is received.
|
144 | *
|
145 | * @example
|
146 | * ```javascript
|
147 | *
|
148 | * const app = smarthome();
|
149 | * app.onDisconnect((body, headers) => {
|
150 | * // User unlinked their account, stop reporting state for user
|
151 | * return {}
|
152 | * })
|
153 | * ```
|
154 | * @param handler The function that will run for an EXECUTE request. It should
|
155 | * return a valid response or a Promise that resolves to valid response.
|
156 | *
|
157 | * @public
|
158 | */
|
159 | onDisconnect(handler: SmartHomeHandler<Api.SmartHomeV1DisconnectRequest, Api.SmartHomeV1DisconnectResponse>): this;
|
160 | /**
|
161 | * Sends a request to the home graph to send a new SYNC request. This should
|
162 | * be called when a device is added or removed for a given user id.
|
163 | *
|
164 | * When calling this function, an API key needs to be provided as an option
|
165 | * in the constructor. See https://developers.google.com/actions/smarthome/create-app#request-sync
|
166 | * to learn more.
|
167 | *
|
168 | * @example
|
169 | * ```javascript
|
170 | *
|
171 | * const app = smarthome({
|
172 | * key: "123ABC"
|
173 | * });
|
174 | *
|
175 | * const addNewDevice = () => {
|
176 | * app.requestSync('user-123')
|
177 | * .then((res) => {
|
178 | * // Request sync was successful
|
179 | * })
|
180 | * .catch((res) => {
|
181 | * // Request sync failed
|
182 | * })
|
183 | * }
|
184 | *
|
185 | * // When request sync is called, a SYNC
|
186 | * // intent will be received soon after.
|
187 | * app.onSync(body => {
|
188 | * // ...
|
189 | * })
|
190 | * ```
|
191 | *
|
192 | * @param agentUserId The user identifier.
|
193 | *
|
194 | * @public
|
195 | * @deprecated Home Graph wrapper methods are deprecated.
|
196 | * Use Google APIs Node.js Client for Home Graph:
|
197 | * https://www.npmjs.com/package/@googleapis/homegraph
|
198 | */
|
199 | requestSync(agentUserId: string): Promise<string>;
|
200 | /**
|
201 | * Reports the current state of a device or set of devices to the home graph.
|
202 | * This may be done if the state of the device was changed locally, like a
|
203 | * light turning on through a light switch.
|
204 | *
|
205 | * When calling this function, a JWT (JSON Web Token) needs to be provided
|
206 | * as an option in the constructor.
|
207 | *
|
208 | * @example
|
209 | * ```javascript
|
210 | * const app = smarthome({
|
211 | * jwt: require('./jwt.json');
|
212 | * });
|
213 | *
|
214 | * const reportState = () => {
|
215 | * app.reportState({
|
216 | * requestId: '123ABC',
|
217 | * agentUserId: 'user-123',
|
218 | * payload: {
|
219 | * devices: {
|
220 | * states: {
|
221 | * "light-123": {
|
222 | * on: true
|
223 | * }
|
224 | * }
|
225 | * }
|
226 | * }
|
227 | * })
|
228 | * .then((res) => {
|
229 | * // Report state was successful
|
230 | * })
|
231 | * .catch((res) => {
|
232 | * // Report state failed
|
233 | * })
|
234 | * };
|
235 | * ```
|
236 | *
|
237 | * @param reportedState A payload containing a device or set of devices with their states
|
238 | *
|
239 | * @public
|
240 | * @deprecated Home Graph wrapper methods are deprecated.
|
241 | * Use Google APIs Node.js Client for Home Graph:
|
242 | * https://www.npmjs.com/package/@googleapis/homegraph
|
243 | */
|
244 | reportState(reportedState: Api.SmartHomeV1ReportStateRequest): Promise<string>;
|
245 | /**
|
246 | * @public
|
247 | * @deprecated Home Graph credentials are deprecated.
|
248 | * Use Google APIs Node.js Client for Home Graph:
|
249 | * https://www.npmjs.com/package/@googleapis/homegraph
|
250 | */
|
251 | key?: string;
|
252 | /**
|
253 | * @public
|
254 | * @deprecated Home Graph credentials are deprecated.
|
255 | * Use Google APIs Node.js Client for Home Graph:
|
256 | * https://www.npmjs.com/package/@googleapis/homegraph
|
257 | */
|
258 | jwt?: SmartHomeJwt;
|
259 | }
|
260 | /** @public */
|
261 | export interface SmartHome {
|
262 | (options?: SmartHomeOptions): AppHandler & SmartHomeApp;
|
263 | }
|
264 | /**
|
265 | *
|
266 | * @example
|
267 | * ```javascript
|
268 | *
|
269 | * const app = smarthome({
|
270 | * debug: true,
|
271 | * key: '<api-key>',
|
272 | * jwt: require('./key.json')
|
273 | * });
|
274 | *
|
275 | * app.onSync((body, headers) => {
|
276 | * return { ... }
|
277 | * });
|
278 | *
|
279 | * app.onQuery((body, headers) => {
|
280 | * return { ... }
|
281 | * });
|
282 | *
|
283 | * app.onExecute((body, headers) => {
|
284 | * return { ... }
|
285 | * });
|
286 | *
|
287 | * exports.smarthome = functions.https.onRequest(app);
|
288 | *
|
289 | * ```
|
290 | *
|
291 | * @public
|
292 | */
|
293 | export declare const smarthome: SmartHome;
|