Letβs start with smartthings-core-sdk/virtualdevices.ts at main Β· SmartThingsCommunity/smartthings-core-sdk Β· GitHub as we know that we are interested in virtual devices. So we already know the endpoint we are interested in is likely to be https://api.smartthings.com/virtualdevices
otherwise theyβd have called the file something else.
So letβs look for something in the file about creating events. It is on lines 82-89. I canβt read TypeScript/JavaScript that brilliantly but line 89 suggests to me we are doing a POST to https://api.smartthings.com/virtualdevices/{{deviceId}}/events
and the body is, in TypeScript/JavaScript terms, { deviceEvents }
. We also know from the previous line that deviceEvents
is an array of whatever DeviceEvent
is.
So effectively it is { deviceEvents: [ DeviceEvent ] }
and now we have to find DeviceEvent
.
Line 3 shows that DeviceEvent
is actually found in smartthings-core-sdk/devices.ts at main Β· SmartThingsCommunity/smartthings-core-sdk Β· GitHub. If we go to lines 511-522 we find:
export interface DeviceEvent {
value: unknown
component: string
capability: string
attribute: string
unit?: string
data?: { [name: string]: object }
}
export interface DeviceEventList {
deviceEvents: DeviceEvent[]
}
So there we have the definition of deviceEvents as a list of DeviceEvent and above it is the definition of DeviceEvent. So letβs glue them together.
{
deviceEvents: [
{
value: unknown
component: string
capability: string
attribute: string
unit?: string
data?: { [name: string]: object }
}
]
}
That is now starting to resemble an event. We have the component, capability and attribute, a value, an optional unit and some optional extra data (where they used to stick buttonNumber
and things like that). So letβs fill in some real data. In the same order I just gave (having moved value
) we have main
, presenceSensor
, presence
, present
and we donβt need the other stuff. So we have:
{
deviceEvents: [
{
component: "main",
capability: "presenceSensor",
attribute: "presence",
value: "present"
}
]
}
Unsurprisingly JSON, the JavaScript Object Notation, is pretty similar to that. It just needs the quotes around the keys too.
{
"deviceEvents": [
{
"component": "main",
"capability": "presenceSensor",
"attribute": "presence",
"value": "present"
}
]
}
As I say though, if you know the CLI command you can get places quicker.
> set SMARTTHINGS_DEBUG=true
> smartthings virtualdevices:events {{deviceId}} presenceSensor:presence present
That first fetched the capability so it can check you are asking something sensible and it will then pop up:
[2022-10-13T19:44:55.552] [DEBUG] rest-client - making axios request: {"url":"https://api.smartthings.com/virtualdevices/{{deviceId}}/events","method":"post","headers":{"Content-Type":"application/json;charset=utf-8","Accept":"application/json","User-Agent":"@smartthings/cli/1.0.0-beta.20 win32-x64 node-v16.16.0","Accept-Language":"en-GB","Authorization":"Bearer {{Personal Access Token}"},"data":{"deviceEvents":[{"component":"main","capability":"presenceSensor","attribute":"presence","value":"present"}]}}