-
Notifications
You must be signed in to change notification settings - Fork 103
/
create.ts
81 lines (70 loc) · 2.82 KB
/
create.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
import { APIOrganizationCommand, inputAndOutputItem } from '@smartthings/cli-lib'
import { createWithDefaultConfig } from '../../../lib/commands/deviceprofiles/create-util'
import { buildTableOutput, cleanupForCreate, DeviceDefinition, DeviceDefinitionRequest } from '../../../lib/commands/deviceprofiles-util'
import {
prunePresentation,
augmentPresentation,
} from '../../../lib/commands/deviceprofiles/view-util'
export default class DeviceDefCreateCommand extends APIOrganizationCommand<typeof DeviceDefCreateCommand.flags> {
static description = 'create a new device profile and device configuration\n' +
'Creates a new device profile and device configuration. Unlike deviceprofiles:create, ' +
'this command accepts a consolidated object that can include a device configuration ' +
'in a property named "view".' +
this.apiDocsURL('createDeviceProfile', 'createDeviceConfiguration', 'updateDeviceProfile', 'generateDeviceConfig')
static flags = {
...APIOrganizationCommand.flags,
...inputAndOutputItem.flags,
}
static examples = [
'$ smartthings deviceprofiles:view:create -i test.json',
'',
'This test.json file defines a switch that cannot be controlled by the automations builder:',
'',
'name: Test Switch',
'components:',
' - id: main',
' capabilities:',
' - id: switch',
'view:',
' dashboard:',
' states:',
' - capability: switch',
' actions:',
' - capability: switch',
' detailView:',
' - capability: switch',
' automation:',
' conditions:',
' - capability: switch ',
]
private async createWithCustomConfig(data: DeviceDefinitionRequest): Promise<DeviceDefinition> {
if (!data.view) {
throw Error('View property not defined')
}
// create the device config from the view data
const deviceConfig = await this.client.presentation.create(augmentPresentation(data.view))
// Set the vid and mnmn from the config
if (!data.metadata) {
data.metadata = {}
}
data.metadata.vid = deviceConfig.presentationId
data.metadata.mnmn = deviceConfig.manufacturerName
delete data.view
// Create the profile
const profile = await this.client.deviceProfiles.create(cleanupForCreate(data))
// Return the composite object
return { ...profile, view: prunePresentation(deviceConfig) }
}
async run(): Promise<void> {
const createDeviceDefinition = async (_: void, data: DeviceDefinitionRequest): Promise<DeviceDefinition> => {
if (data.view) {
return this.createWithCustomConfig(data)
}
const profileAndConfig = await createWithDefaultConfig(this.client, data)
return { ...profileAndConfig.deviceProfile, view: prunePresentation(profileAndConfig.deviceConfig) }
}
await inputAndOutputItem(this,
{ buildTableOutput: data => buildTableOutput(this.tableGenerator, data, { includeViewInfo: true }) },
createDeviceDefinition)
}
}