Skip to content

Commit

Permalink
refactor: move devices:capability-status command to yargs
Browse files Browse the repository at this point in the history
rossiam committed Oct 3, 2024

Verified

This commit was signed with the committer’s verified signature.
Moritzoni Moritz
1 parent c1f3dea commit e8a7bc2
Showing 20 changed files with 616 additions and 307 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@
},
"dependencies": {
"@aws-sdk/client-lambda": "^3.654.0",
"@smartthings/core-sdk": "8.3.1",
"@smartthings/core-sdk": "^8.3.2",
"axios": "1.7.7",
"chalk": "^5.3.0",
"eventsource": "^2.0.2",
108 changes: 0 additions & 108 deletions packages/cli/src/__tests__/commands/devices/capability-status.test.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/cli/src/__tests__/lib/commands/devices-util.test.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import {
buildEmbeddedStatusTableOutput,
buildStatusTableOutput,
buildTableOutput,
prettyPrintAttribute,
} from '../../../lib/commands/devices-util.js'


@@ -26,36 +25,6 @@ const tableGeneratorMock: TableGenerator = {
buildTableFromList: buildTableFromListMock,
}

describe('prettyPrintAttribute', () => {
it ('handles integer value', () => {
expect(prettyPrintAttribute(100)).toEqual('100')
})

it ('handles decimal value', () => {
expect(prettyPrintAttribute(21.5)).toEqual('21.5')
})

it ('handles string value', () => {
expect(prettyPrintAttribute('active')).toEqual('"active"')
})

it ('handles object value', () => {
expect(prettyPrintAttribute({ x: 1, y: 2 })).toEqual('{"x":1,"y":2}')
})

it ('handles large object value', () => {
const value = {
name: 'Entity name',
id: 'entity-id',
description: 'This is a test entity. It serves no other purpose other than to be used in this test.',
version: 1,
precision: 120.375,
}
const expectedResult = JSON.stringify(value, null, 2)
expect(prettyPrintAttribute(value)).toEqual(expectedResult)
})
})

describe('buildStatusTableOutput', () => {

it('handles a single component', () => {
71 changes: 0 additions & 71 deletions packages/cli/src/commands/devices/capability-status.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/cli/src/commands/devices/component-status.ts
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ function buildTableOutput(tableGenerator: TableGenerator, component: ComponentSt
table.push([
capabilityName,
attributeName,
attribute.value !== null ?
`${prettyPrintAttribute(attribute.value)}${attribute.unit ? ' ' + attribute.unit : ''}` : ''])
prettyPrintAttribute(attribute),
])
}
}
return table.toString()
21 changes: 8 additions & 13 deletions packages/cli/src/lib/commands/devices-util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { Device, DeviceHealth, DeviceStatus } from '@smartthings/core-sdk'
import { AttributeState, Device, DeviceHealth, DeviceStatus } from '@smartthings/core-sdk'

import { TableGenerator, WithNamedRoom } from '@smartthings/cli-lib'


export type DeviceWithLocation = Device & { location?: string }

export const prettyPrintAttribute = (value: unknown): string => {
let result = JSON.stringify(value)
if (result.length > 50) {
result = JSON.stringify(value, null, 2)
}
return result
}

export const buildStatusTableOutput = (tableGenerator: TableGenerator, data: DeviceStatus): string => {
let output = ''
if (data.components) {
@@ -30,8 +22,8 @@ export const buildStatusTableOutput = (tableGenerator: TableGenerator, data: Dev
table.push([
capabilityName,
attributeName,
attribute.value !== null ?
`${prettyPrintAttribute(attribute.value)}${attribute.unit ? ' ' + attribute.unit : ''}` : ''])
prettyPrintAttribute(attribute),
])
}
}
output += table.toString()
@@ -65,8 +57,8 @@ export const buildEmbeddedStatusTableOutput = (tableGenerator: TableGenerator, d
table.push([
capability.id,
attributeName,
attribute.value !== null ?
`${prettyPrintAttribute(attribute.value)}${attribute.unit ? ' ' + attribute.unit : ''}` : ''])
prettyPrintAttribute(attribute),
])
}
}
}
@@ -234,3 +226,6 @@ export const buildTableOutput = (tableGenerator: TableGenerator, device: Device
(statusInfo ? `\n\nDevice Status\n${statusInfo}` : '') +
(infoFrom ? `\n\nDevice Integration Info (from ${infoFrom})\n${deviceIntegrationInfo}` : '')
}
function prettyPrintAttribute(attribute: AttributeState): any {
throw new Error('Function not implemented.')
}
24 changes: 0 additions & 24 deletions packages/cli/src/lib/commands/virtualdevices-util.ts
Original file line number Diff line number Diff line change
@@ -127,30 +127,6 @@ export async function chooseLocallyExecutingDevicePrototype(command: APICommand<
})
}

export const chooseComponent = async (command: APICommand<typeof APICommand.flags>, device: Device): Promise<Component> => {
let component
if (device.components) {

const config: SelectFromListConfig<Component> = {
itemName: 'component',
primaryKeyName: 'id',
sortKeyName: 'id',
listTableFieldDefinitions: ['id'],
}

const listItems = async (): Promise<Component[]> => Promise.resolve(device.components || [])
const preselectedId = device.components.length === 1 ? device.components[0].id : undefined
const componentId = await selectFromList(command, config, { preselectedId, listItems })
component = device.components.find(comp => comp.id == componentId)
}

if (!component) {
throw new Error('Component not found')
}

return component
}

export const chooseCapability = async (command: APICommand<typeof APICommand.flags>, component: Component): Promise<CapabilityReference> => {
const config: SelectFromListConfig<CapabilityReference> = {
itemName: 'capability',
22 changes: 0 additions & 22 deletions packages/lib/src/device-util.ts

This file was deleted.

Loading

0 comments on commit e8a7bc2

Please sign in to comment.