forked from SmartThingsCommunity/smartthings-core-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installedapps.ts
528 lines (475 loc) · 14.3 KB
/
installedapps.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig, HttpClientParams } from '../endpoint-client'
import { Count, Owner, PrincipalType, Status, SuccessStatusValue } from '../types'
export interface StringConfig {
/**
* A config value
*/
value: string
}
export interface DeviceConfig {
/**
* The ID of the device.
*/
deviceId: string
/**
* The component ID on the device.
*/
componentId: string
permissions: string[]
}
export interface PermissionConfig {
permissions: string[]
}
export interface ModeConfig {
/**
* The ID of the mode.
*/
modeId: string
}
export interface SceneConfig {
/**
* The ID of the scene.
*/
sceneId: string
permissions: string[]
}
export interface MessageConfig {
/**
* The key value of the message group.
*/
messageGroupKey: string
}
export enum ConfigValueType {
STRING = 'STRING',
DEVICE = 'DEVICE',
PERMISSION = 'PERMISSION',
MODE = 'MODE',
SCENE = 'SCENE',
MESSAGE = 'MESSAGE',
}
export interface ConfigEntry {
/**
* The value type.
*/
valueType: ConfigValueType
/**
* The config if valueType is STRING, meaningless otherwise
*/
stringConfig?: StringConfig
/**
* The config if valueType is DEVICE, meaningless otherwise
*/
deviceConfig?: DeviceConfig
/**
* The config if valueType is PERMISSION, meaningless otherwise
*/
permissionConfig?: PermissionConfig
/**
* The config if valueType is MODE, meaningless otherwise
*/
modeConfig?: ModeConfig
/**
* The config if valueType is SCENE, meaningless otherwise
*/
sceneConfig?: SceneConfig
/**
* The config if valueType is MESSAGE, meaningless otherwise
*/
messageConfig?: MessageConfig
}
export enum InstalledAppType {
LAMBDA_SMART_APP = 'LAMBDA_SMART_APP',
WEBHOOK_SMART_APP = 'WEBHOOK_SMART_APP',
API_ONLY = 'API_ONLY',
BEHAVIOR = 'BEHAVIOR',
}
export enum InstallConfigurationStatus {
STAGED = 'STAGED',
DONE = 'DONE',
AUTHORIZED = 'AUTHORIZED',
REVOKED = 'REVOKED',
}
export enum InstalledAppStatus {
PENDING = 'PENDING',
AUTHORIZED = 'AUTHORIZED',
REVOKED = 'REVOKED',
DISABLED = 'DISABLED',
}
export interface InstalledAppUi {
pluginId?: string
pluginUri?: string
dashboardCardsEnabled: boolean
preInstallDashboardCardsEnabled: boolean
}
export interface InstalledAppIconImage {
/**
* A default icon image url for an app. https url required.
*/
url?: string
}
export enum InstalledAppClassification {
AUTOMATION = 'AUTOMATION',
SERVICE = 'SERVICE',
DEVICE = 'DEVICE',
CONNECTED_SERVICE = 'CONNECTED_SERVICE',
HIDDEN = 'HIDDEN',
}
export interface InstalledApp {
/**
* The ID of the installed app.
*/
installedAppId: string
installedAppType: InstalledAppType
installedAppStatus: InstalledAppStatus
/**
* A user defined name for the installed app. May be null.
*/
displayName?: string
/**
* The ID of the app.
*/
appId: string
/**
* A reference to an upstream system. For example, Behaviors would
* reference the behaviorId. May be null.
*/
referenceId?: string
/**
* The ID of the location to which the installed app may belong.
*/
locationId?: string
owner: Owner
/**
* A UTC ISO-8601 Date-Time String
*/
createdDate: string
/**
* A UTC ISO-8601 Date-Time String
*/
lastUpdatedDate: string
ui?: InstalledAppUi
iconImage?: InstalledAppIconImage
/**
* An App maybe associated to many classifications. A classification drives
* how the integration is presented to the user in the SmartThings mobile
* clients. These classifications include: * AUTOMATION - Denotes an
* integration that should display under the \"Automation\" tab in mobile
* clients. * SERVICE - Denotes an integration that is classified as a
* \"Service\". * DEVICE - Denotes an integration that should display under
* the \"Device\" tab in mobile clients. * CONNECTED_SERVICE - Denotes an
* integration that should display under the \"Connected Services\" menu in
* mobile clients. * HIDDEN - Denotes an integration that should not display
* in mobile clients
*/
classifications?: InstalledAppClassification[]
/**
* Denotes the principal type to be used with the app. Default is LOCATION.
*/
principalType: PrincipalType
/**
* Inform the installation systems that the associated app can only be
* installed once within a user's account.
*/
singleInstance: boolean
restrictionTier?: number
}
export interface ConfigurationRequest {
appId: string
locationId: string
installedAppType: InstalledAppType
configurationStatus: InstallConfigurationStatus
config: {[name: string]: ConfigEntry[]}
}
export interface InstalledAppConfiguration {
installedAppId: string
configurationId: string
configurationStatus: InstallConfigurationStatus
createdDate: string
lastUpdatedDate: string
config: {[name: string]: ConfigEntry}
}
export interface InstalledAppConfigItem {
installedAppId: string
configurationId: string
configurationStatus: InstallConfigurationStatus
createdDate: string
lastUpdatedDate: string
}
export interface InstalledAppResponse {
installedApp: InstalledApp
configurationDetail: InstalledAppConfiguration
}
export interface TokenInformation {
installedAppId: string
locationId: string
scope: string[]
}
export interface InstalledAppUpdateRequest {
displayName: string
}
export interface ConfigurationUpdateRequest {
config: {[name: string]: ConfigEntry[]}
}
export interface ConfigurationPatchRequest {
removals: string[]
upserts: {[name: string]: ConfigEntry[]}
}
export interface ConfigurationItemsList {
items: InstalledAppConfigItem[]
_links?: {
next?: {
href: string
}
previous?: {
href: string
}
}
}
export enum DashboardLifecycleEventType {
CREATE = 'CREATE',
UPDATE = 'UPDATE',
DELETE = 'DELETE'
}
export interface SmartAppEvent {
name: string
attributes: {[name: string]: string}
}
export interface SmartAppDashboardEvent {
cardId: string
lifecycle: DashboardLifecycleEventType
}
export interface InstalledAppEvents {
smartAppEvents?: SmartAppEvent[]
smartAppDashboardEvents?: SmartAppDashboardEvent[]
}
export enum InstalledAppMessageType {
PREDEFINED = 'PREDEFINED',
ADHOC = 'ADHOC'
}
export interface PredefinedMessage {
messageTemplateKey: string
defaultVariables: {[name: string]: string}
localeVariables: [
{
localeTag: string
variables: {[name: string]: string}
}
]
}
export interface AdhocMessage {
fallbackLocale: string
defaultVariables: {[name: string]: string}
templates: [
{
localeTag: string
template: string
variables: {[name: string]: string}
}
]
}
export interface InstalledAppMessage {
messageGroupKey: string
messageType: InstalledAppMessageType
predefinedMessage?: PredefinedMessage
adhocMessage?: AdhocMessage
}
// TODO -- tags??
export interface InstalledAppListOptions {
locationId?: string | string[]
installedAppStatus?: InstalledAppStatus | InstalledAppStatus[]
installedAppType?: InstalledAppType | InstalledAppType[]
appId?: string | string[]
modeId?: string | string[]
deviceId?: string | string[]
max?: number
page?: number
}
export interface ConfigurationListOptions {
configurationStatus?: InstallConfigurationStatus
max?: number
page?: number
}
export class InstalledAppsEndpoint extends Endpoint{
constructor(config: EndpointClientConfig) {
super(new EndpointClient('installedapps', config))
}
/**
* Returns a list of installed app instances matching the query options or all instances accessible by the principal
* (i.e. user) if no options are specified.
*
* @param options query options, locationId, installedAppStatus, installedAppType, deviceId. These can
* be single values or arrays.
*/
public async list(options: InstalledAppListOptions = {}): Promise<InstalledApp[]> {
const params: HttpClientParams = {}
if ('locationId' in options && options.locationId) {
params.locationId = options.locationId
} else if (this.client.config.locationId) {
params.locationId = this.client.config.locationId
}
if ('installedAppStatus' in options && options.installedAppStatus) {
params.installedAppStatus = options.installedAppStatus
}
if ('installedAppType' in options && options.installedAppType) {
params.installedAppType = options.installedAppType
}
if ('deviceId' in options && options.deviceId) {
params.deviceId = options.deviceId
}
if ('appId' in options && options.appId) {
params.appId = options.appId
}
if ('modeId' in options && options.modeId) {
params.modeId = options.modeId
}
if ('max' in options && options.max) {
params.max = options.max
}
if ('page' in options && options.page) {
params.page = options.page
}
return this.client.getPagedItems<InstalledApp>(undefined, params)
}
/**
* Returns the specified installed app definition
* @param id UUID of the installed app
*/
public get(id?: string): Promise<InstalledApp> {
return this.client.get<InstalledApp>(this.installedAppId(this.installedAppId(id)))
}
/**
* Returns the token info for an installed app principal
*/
public tokenInfo(): Promise<TokenInformation> {
return this.client.get<TokenInformation>('me')
}
/**
* Creates an installed app instance
* @param data configuration data for the app instance
*/
public create(data: ConfigurationRequest): Promise<InstalledAppResponse> {
return this.client.post<InstalledAppResponse>(undefined, data)
}
/**
* Updates the display name of an installled app instance
* @param id UUID of the installed app
* @param data request containing the display name
*/
public update(id: string, data: InstalledAppUpdateRequest): Promise<InstalledApp> {
return this.client.put<InstalledApp>(id, data)
}
/**
* List configurations of an installed app instance
* @param id UUID of the installed app
* @param options including the desired configuration status
*/
public listConfigurations(id: string, options: ConfigurationListOptions = {}): Promise<InstalledAppConfigItem[]> {
const params: HttpClientParams = {}
if ('configurationStatus' in options && options.configurationStatus) {
params.configurationStatus = options.configurationStatus
}
if ('max' in options && options.max) {
params.max = options.max
}
if ('page' in options && options.page) {
params.page = options.page
}
return this.client.getPagedItems<InstalledAppConfigItem>(`${id}/configs`, params)
}
/**
* Returns a specific installed app configuration
* @param id UUID of the installed app
* @param configurationId UUID of the configuration
*/
public getConfiguration(id: string, configurationId: string): Promise<InstalledAppConfiguration> {
return this.client.get<InstalledAppConfiguration>(`${id}/configs/${configurationId}`)
}
/**
* Returns the most recent configuration, authorized or not
* @param id The installedAppId
*/
public async getLatestConfiguration(id: string): Promise<InstalledAppConfiguration | undefined> {
const items = (await this.listConfigurations(id)).sort((a, b) => {
return a.lastUpdatedDate === b.lastUpdatedDate ? 0 : a.lastUpdatedDate < b.lastUpdatedDate ? 1 : -1
})
if (items.length > 0) {
const item = items[0]
return this.getConfiguration(item.installedAppId, item.configurationId)
}
return undefined
}
/**
* Returns the current authorized configuration, or undefined if there is no authorized configuration
* @param id UUID of the installed app
*/
public async getAuthorizedConfiguration(id: string): Promise<InstalledAppConfiguration | undefined> {
const items = await this.listConfigurations(id, {configurationStatus: InstallConfigurationStatus.AUTHORIZED})
if (items.length > 0) {
const item = items[0]
return this.getConfiguration(item.installedAppId, item.configurationId)
}
return undefined
}
/**
* Returns the current authorized configuration, or the latest configuration of any status if none are authorized
* @param id UUID of the installed app
*/
public async getCurrentConfiguration(id: string): Promise<InstalledAppConfiguration | undefined> {
let item = await this.getAuthorizedConfiguration(id)
if (!item) {
item = await this.getLatestConfiguration(id)
}
return item
}
/**
* Updates an Installed App configuration. Call implicitly operates on the latest 'STAGED' configuration.
* @param id UUID of the installed app
* @param data the new configuration
*/
public updateConfiguration(id: string, data: ConfigurationUpdateRequest): Promise<InstalledAppConfiguration> {
return this.client.put<InstalledAppConfiguration>(`${id}/configs`, data)
}
/**
* Allows specific configuration keys to be removed / upserted from any configuration that may already exist.
* This operation is only supported on install configurations in status of 'STAGED'. Useful for iteratively
* configuring an installed app.
* @param id UUID of the installed app
* @param configurationId UUID of the configuration
* @param data requests containing upserts and removals of configuration items
*/
public patchConfiguration(id: string, configurationId: string, data: ConfigurationPatchRequest): Promise<InstalledAppConfiguration> {
return this.client.put<InstalledAppConfiguration>(`${id}/configs/${configurationId}`, data)
}
/**
* Deletes an installed app instance. If the client is configured with an installedApp ID this value can be
* omitted.
* @param id UUID of the installed app
*/
public async delete(id?: string): Promise<Status> {
await this.client.delete<Count>(this.installedAppId(id))
return SuccessStatusValue
}
/**
* Create events for an installed app. Note that this method is here in support of future functionality not yet
* available in the SmartThings platform.
* @param data object contain lists of events
* @param id UUID of the installed app. This value does not need to be specified if the client is configured with
* an installed app ID
*/
public async createEvent(data: InstalledAppEvents, id?: string): Promise<Status> {
await this.client.post<Status>(`${this.installedAppId(id)}/events`, data)
return SuccessStatusValue
}
/**
* Send a message to a message group. Note that this method is here in support of future functionality not yet
* available in the SmartThings platform.
* @param data the message
* @param id UUID of the installed app. This value does not need to be specified if the client is configured with
* an installed app ID
*/
public async sendMessage(data: InstalledAppMessage, id?: string): Promise<Status> {
await this.client.post<Status>(`${this.installedAppId(id)}/send-message`, data)
return SuccessStatusValue
}
}