-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathcomponent-status.ts
53 lines (43 loc) · 1.78 KB
/
component-status.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
import { ComponentStatus } from '@smartthings/core-sdk'
import { APICommand, chooseComponent, chooseDevice, formatAndWriteItem, TableGenerator } from '@smartthings/cli-lib'
import { prettyPrintAttribute } from '../../lib/commands/devices-util'
function buildTableOutput(tableGenerator: TableGenerator, component: ComponentStatus): string {
const table = tableGenerator.newOutputTable({ head: ['Capability', 'Attribute', 'Value'] })
for (const capabilityName of Object.keys(component)) {
const capability = component[capabilityName]
for (const attributeName of Object.keys(capability)) {
const attribute = capability[attributeName]
table.push([
capabilityName,
attributeName,
attribute.value !== null ?
`${prettyPrintAttribute(attribute.value)}${attribute.unit ? ' ' + attribute.unit : ''}` : ''])
}
}
return table.toString()
}
export default class DeviceComponentStatusCommand extends APICommand<typeof DeviceComponentStatusCommand.flags> {
static description = "get the current status of a device component's attributes" +
this.apiDocsURL('getDeviceComponentStatus')
static flags = {
...APICommand.flags,
...formatAndWriteItem.flags,
}
static args = [
{
name: 'id',
description: 'the device id',
},
{
name: 'component',
description: 'the component id',
},
]
async run(): Promise<void> {
const deviceId = await chooseDevice(this, this.args.id, { allowIndex: true })
const device = await this.client.devices.get(deviceId)
const componentName = await chooseComponent(this, this.args.component, device.components)
const componentStatus = await this.client.devices.getComponentStatus(deviceId, componentName)
await formatAndWriteItem(this, { buildTableOutput: data => buildTableOutput(this.tableGenerator, data) }, componentStatus)
}
}