MCPcopy Index your code
hub / github.com/SmartThingsCommunity/smartthings-cli / handler

Function handler

src/commands/devices.ts:121–181  ·  view source on GitHub ↗
(argv: ArgumentsCamelCase<CommandArgs>)

Source from the content-addressed store, hash-verified

119export type OutputDevice = Device & WithNamedRoom & Pick<DeviceStatus, 'healthState'>
120
121const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => {
122 const command = await apiCommand(argv)
123
124 const listTableFieldDefinitions: TableFieldDefinition<OutputDevice>[] = ['label', 'name', 'type', 'deviceId']
125
126 if (argv.verbose) {
127 listTableFieldDefinitions.splice(3, 0, 'location', 'room')
128 }
129
130 if (argv.health) {
131 listTableFieldDefinitions.splice(3, 0, {
132 path: 'healthState.state',
133 label: 'Health',
134 })
135 }
136
137 const config: OutputItemOrListConfig<OutputDevice> = {
138 primaryKeyName: 'deviceId',
139 sortKeyName: 'label',
140 listTableFieldDefinitions,
141 buildTableOutput: (data: Device) => buildTableOutput(command.tableGenerator, data),
142 }
143
144 const deviceGetOptions: DeviceGetOptions = {
145 includeStatus: argv.status,
146 }
147
148 const deviceListOptions: DeviceListOptions = {
149 capability: argv.capability,
150 capabilitiesMode: argv.capabilitiesMode === 'or' ? 'or' : 'and',
151 locationId: argv.location,
152 deviceId: argv.device,
153 installedAppId: argv.installedApp,
154 type: argv.type as DeviceIntegrationType[] | undefined,
155 includeHealth: argv.health,
156 ...deviceGetOptions,
157 }
158
159 await outputItemOrList<OutputDevice>(command, config, argv.idOrIndex,
160 async () => {
161 const devices = await command.client.devices.list(deviceListOptions)
162 if (argv.verbose) {
163 return await withLocationsAndRooms(command.client, devices)
164 }
165 return devices
166 },
167 async id => {
168 let chosenDevice: OutputDevice = await command.client.devices.get(id, deviceGetOptions)
169 if (argv.verbose) {
170 chosenDevice = await withLocationAndRoom(command.client, chosenDevice)
171 }
172 // Note -- we have to do this explicitly because the API does not honor the includeHealth
173 // parameter for individual devices
174 if (argv.health) {
175 const healthState = await command.client.devices.getHealth(id)
176 chosenDevice = { ...chosenDevice, healthState }
177 }
178 return chosenDevice

Callers

nothing calls this directly

Calls 4

apiCommandFunction · 0.85
withLocationsAndRoomsFunction · 0.85
withLocationAndRoomFunction · 0.85
buildTableOutputFunction · 0.50

Tested by

no test coverage detected