-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathcreate.ts
294 lines (251 loc) · 10.4 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
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
import { Errors } from '@oclif/core'
import inquirer from 'inquirer'
import {
DeviceProfile,
DeviceProfileRequest, PresentationDeviceConfig,
PresentationDeviceConfigCreate,
SmartThingsClient,
} from '@smartthings/core-sdk'
import { APIOrganizationCommand, inputAndOutputItem, userInputProcessor } from '@smartthings/cli-lib'
import { buildTableOutput } from '../deviceprofiles'
import { DeviceDefinitionRequest } from './view'
import { CapabilityId, chooseCapabilityFiltered } from '../capabilities'
const capabilitiesWithoutPresentations = ['healthCheck', 'execute']
export async function generateDefaultConfig(client: SmartThingsClient, deviceProfileId: string,
deviceProfile: DeviceProfileRequest | DeviceDefinitionRequest): Promise<PresentationDeviceConfigCreate> {
// Generate the default config
const deviceConfig = await client.presentation.generate(deviceProfileId)
// Edit the dashboard entries to include only the first capability in the profile
if (deviceProfile.components) {
if (deviceConfig.dashboard) {
const firstComponent = deviceProfile.components[0]
if (firstComponent.capabilities && firstComponent.capabilities.length > 0) {
const firstCapability = firstComponent.capabilities[0]
const capability = await client.capabilities.get(firstCapability.id, firstCapability.version || 1)
if (capability.attributes && Object.keys(capability.attributes).length > 0) {
deviceConfig.dashboard.states = deviceConfig.dashboard.states.filter(state =>
state.component === firstComponent.id && state.capability === firstCapability.id)
} else {
deviceConfig.dashboard.states = []
}
if (capability.commands && Object.keys(capability.commands).length > 0) {
deviceConfig.dashboard.actions = deviceConfig.dashboard.actions.filter(action =>
action.component === firstComponent.id && action.capability === firstCapability.id)
} else {
deviceConfig.dashboard.actions = []
}
}
} else {
deviceConfig.dashboard = {
states: [],
actions: [],
}
}
}
// Filter capabilities with no UI
if (deviceConfig.detailView) {
deviceConfig.detailView = deviceConfig.detailView.filter(viewItem =>
!(capabilitiesWithoutPresentations.includes(viewItem.capability)))
}
// Filter automation entries
if (deviceConfig.automation) {
// Filter out conditions for capabilities that don't have attributes
if (deviceConfig.automation.conditions) {
const capabilities = await Promise.all(deviceConfig.automation.conditions.map(condition => {
return client.capabilities.get(condition.capability, condition.version || 1)
}))
deviceConfig.automation.conditions = deviceConfig.automation.conditions.filter((_v, index) => {
const capability = capabilities[index]
return capability.attributes && Object.keys(capability.attributes).length > 0 && !(capabilitiesWithoutPresentations.includes(capability.id || ''))
})
}
// Filter out automation actions for capabilities that don't have commands
if (deviceConfig.automation.actions) {
const capabilities = await Promise.all(deviceConfig.automation.actions.map(action => {
return client.capabilities.get(action.capability, action.version || 1)
}))
deviceConfig.automation.actions = deviceConfig.automation.actions.filter((_v, index) => {
const capability = capabilities[index]
return capability.commands && Object.keys(capability.commands).length > 0 && !(capabilitiesWithoutPresentations.includes(capability.id || ''))
})
}
}
return deviceConfig
}
export interface DeviceProfileAndConfig {
deviceProfile: DeviceProfile
deviceConfig: PresentationDeviceConfig
}
export async function createWithDefaultConfig(client: SmartThingsClient, data: DeviceDefinitionRequest): Promise<DeviceProfileAndConfig> {
// Create the profile
let deviceProfile = await client.deviceProfiles.create(cleanupRequest(data))
// Generate the default config
const deviceConfigData = await generateDefaultConfig(client, deviceProfile.id, deviceProfile)
// Create the config using the default
const deviceConfig = await client.presentation.create(deviceConfigData)
// Update the profile to use the vid from the config
const profileId = deviceProfile.id
cleanupRequest(deviceProfile)
// TODO: I'm guessing name should be optional in PresentationDeviceConfigEntry
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete deviceProfile.name
if (!deviceProfile.metadata) {
deviceProfile.metadata = {}
}
deviceProfile.metadata.vid = deviceConfig.presentationId
deviceProfile.metadata.mnmn = deviceConfig.manufacturerName
// Update the profile with the vid and mnmn
deviceProfile = await client.deviceProfiles.update(profileId, deviceProfile)
// Return the composite object
return { deviceProfile, deviceConfig }
}
// Cleanup is done so that the result of a device profile get can be modified and
// used in an update operation without having to delete the status, owner, and
// component name fields, which aren't accepted in the update API call.
export function cleanupRequest(deviceProfileRequest: Partial<DeviceProfile & { restrictions: unknown }>): DeviceProfileRequest {
delete deviceProfileRequest.id
delete deviceProfileRequest.status
delete deviceProfileRequest.restrictions
if (deviceProfileRequest.components) {
for (const component of deviceProfileRequest.components) {
delete component.label
}
}
return deviceProfileRequest
}
export default class DeviceProfileCreateCommand extends APIOrganizationCommand {
static description = 'Create a new device profile\n' +
'Creates a new device profile. If a vid field is not present in the meta\n' +
'then a default device presentation will be created for this profile and the\n' +
'vid set to reference it.'
static flags = {
...APIOrganizationCommand.flags,
...inputAndOutputItem.flags,
}
static examples = [
'$ smartthings deviceprofiles:create -i myprofile.json # create a device profile from the JSON file definition',
'$ smartthings deviceprofiles:create -i myprofile.yaml # create a device profile from the YAML file definition',
'$ smartthings deviceprofiles:create # create a device profile with interactive dialog',
]
static aliases = ['device-profiles:create']
async run(): Promise<void> {
const { args, argv, flags } = await this.parse(DeviceProfileCreateCommand)
await super.setup(args, argv, flags)
const createDeviceProfile = async (_: void, data: DeviceDefinitionRequest): Promise<DeviceProfile> => {
if (data.view) {
throw new Error('Input contains "view" property. Use deviceprofiles:view:create instead.')
}
if (!data.metadata?.vid) {
const profileAndConfig = await createWithDefaultConfig(this.client, data)
return profileAndConfig.deviceProfile
}
return await this.client.deviceProfiles.create(cleanupRequest(data))
}
await inputAndOutputItem(this,
{ buildTableOutput: data => buildTableOutput(this.tableGenerator, data) },
createDeviceProfile, userInputProcessor(this))
}
// TODO - update once capability versions are supported
protected async capabilityDefined(idStr:string): Promise<boolean> {
try {
const capability = await this.client.capabilities.get(idStr, 1)
return !!capability
} catch (e) {
return false
}
}
protected async promptAndAddCapability(deviceProfile: DeviceProfileRequest, componentId: string, prompt = 'Capability ID'): Promise<CapabilityId> {
let capabilityId: CapabilityId = { id: '', version: 0 }
const idStr = (await inquirer.prompt({
type: 'input',
name: 'id',
message: `${prompt} (type ? for a list):`,
validate: async (input) => {
return (input.endsWith('?') || input === '' || await this.capabilityDefined(input))
? true
: `Invalid ID "${input}". Please enter a valid capability ID or ? for a list of available capabilities.`
},
})).id
if (idStr) {
if (idStr.endsWith('?')) {
capabilityId = await chooseCapabilityFiltered(this, `${prompt}:`, idStr.slice(0, idStr.length - 1))
} else {
// TODO - update once capability versions are supported
capabilityId = {id: idStr, version: 1}
}
}
if (capabilityId && capabilityId.id) {
const component = deviceProfile.components?.find(it => it.id === componentId)
if (component) {
component.capabilities?.push(capabilityId)
} else {
throw new Errors.CLIError(`Component ${componentId} not defined in profile`)
}
}
return capabilityId
}
protected async promptAndAddComponent(deviceProfile: DeviceProfileRequest, previousComponentId: string): Promise<string> {
const components = deviceProfile.components || []
let componentId: string = (await inquirer.prompt({
type: 'input',
name: 'componentId',
message: 'ComponentId ID: ',
validate: (input) => {
return (new RegExp(/^[0-9a-zA-Z]{1,100}$/).test(input) && !components.find(it => it.id === input)) || 'Invalid component name'
},
})).componentId
if (componentId) {
components.push({id: componentId, capabilities: []})
} else {
componentId = previousComponentId
}
return componentId
}
async getInputFromUser(): Promise<DeviceProfileRequest> {
const name = (await inquirer.prompt({
type: 'input',
name: 'deviceProfileName',
message: 'Device Profile Name:',
validate: (input: string) => {
return new RegExp(/^(?!\s)[-_!.~'() *0-9a-zA-Z]{1,100}(?<!\s)$/).test(input) || 'Invalid device profile name'
},
})).deviceProfileName
const deviceProfile: DeviceProfileRequest = {
name,
components: [
{
id: 'main',
capabilities: [],
},
],
}
let primaryCapabilityId: CapabilityId
do {
primaryCapabilityId = await this.promptAndAddCapability(deviceProfile, 'main', 'Primary capability ID')
} while (!primaryCapabilityId.id)
const enum Action {
ADD_CAPABILITY = 'Add another capability to this component',
ADD_COMPONENT = 'Add another component',
FINISH = 'Finish & Create',
}
let action: string
let componentId = 'main'
const choices = [Action.ADD_CAPABILITY, Action.ADD_COMPONENT, Action.FINISH]
do {
action = (await inquirer.prompt({
type: 'list',
name: 'action',
message: 'Select an action...',
choices,
})).action
if (action === Action.ADD_CAPABILITY) {
await this.promptAndAddCapability(deviceProfile, componentId)
} else if (action === Action.ADD_COMPONENT) {
componentId = await this.promptAndAddComponent(deviceProfile, componentId)
await this.promptAndAddCapability(deviceProfile, componentId)
}
} while (action !== Action.FINISH)
return deviceProfile
}
}