-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathpreferences.ts
44 lines (37 loc) · 1.25 KB
/
preferences.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
import { DevicePreferenceResponse } from '@smartthings/core-sdk'
import { APICommand, chooseDevice, formatAndWriteItem, TableGenerator } from '@smartthings/cli-lib'
export function buildTableOutput(tableGenerator: TableGenerator, data: DevicePreferenceResponse): string {
let output = ''
if (data.values) {
const table = tableGenerator.newOutputTable({ head: ['Name', 'Type', 'Value'] })
const names = Object.keys(data.values).sort()
for (const name of names) {
const item = data.values[name]
if (item) {
table.push([
name,
item.preferenceType,
item.value,
])
}
}
output = table.toString()
}
return output
}
export default class DevicePreferencesCommand extends APICommand<typeof DevicePreferencesCommand.flags> {
static description = 'get the current preferences of a device'
static flags = {
...APICommand.flags,
...formatAndWriteItem.flags,
}
static args = [{
name: 'id',
description: 'the device id',
}]
async run(): Promise<void> {
const deviceId = await chooseDevice(this, this.args.id, { allowIndex: true })
const preferences = await this.client.devices.getPreferences(deviceId)
await formatAndWriteItem(this, { buildTableOutput: data => buildTableOutput(this.tableGenerator, data) }, preferences)
}
}