Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Created April 30, 2021 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/86f6d6796a3c88c3c54a05f70d507b0c to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/86f6d6796a3c88c3c54a05f70d507b0c to your computer and use it in GitHub Desktop.
Lambda-SmartAppConnector
const SmartApp = require('@smartthings/smartapp');
const deviceStates = {switch: 'off'}//1-10
module.exports = new SmartApp()
.appId('smartApp connector')
.disableCustomDisplayName(true)
.permissions(['w:devices:*', 'r:locations:*','x:devices:*', 'i:deviceprofiles:*', 'r:devices:*'])
.page('mainPage', (ctx, page, configData) => {
page.section('Device label:', section => {
section
.textSetting('deviceLabel')
.description('')
.name('Set label')
})
})
.installed(async (ctx, updateData) => {
// Get device label from Input
let deviceLabel = ctx.config.deviceLabel[0].stringConfig.value;
// Device request payload
let deviceOpt = {
label: deviceLabel,
locationId: ctx.locationId,
app: {
// It is recommended to use a PUBLISHED device
// profile. To do so, configure SmartThings CLI
// and use `deviceprofiles:publish` interface.
// - https://github.com/SmartThingsCommunity/smartthings-cli/releases
profileId: '95d359ec-2437-4ed2-afb4-6579d12b1c9e',
installedAppId: updateData.installedApp.installedAppId
}
};
// Call API
// https://smartthings.developer.samsung.com/docs/api-ref/st-api.html#operation/installDevice
let deviceReq = await ctx.api.devices.create(deviceOpt)
//device info
console.log(deviceReq);
//send initial status
const devStatus = [
{
component: 'main',
capability: 'switch',
attribute: 'switch',
value: deviceStates.switch
}
]
await ctx.api.devices.sendEvents(deviceReq.deviceId,devStatus)
})
.deviceCommandHandler(async (ctx, deviceCommandInfo) => {
console.log("deviceId _ command",deviceCommandInfo)
let devStatus=[]
let newStatus = {
component: deviceCommandInfo.commands[0].componentId,
capability: deviceCommandInfo.commands[0].capability
}
switch (deviceCommandInfo.commands[0].capability) {
case 'switch': {
newStatus.attribute='switch'
newStatus.value = deviceStates.switch = deviceCommandInfo.commands[0].command === 'on' ? 'on' : 'off';
break;
}
}
devStatus.push(newStatus)
await ctx.api.devices.sendEvents(deviceCommandInfo.deviceId,devStatus)
})
.uninstalled(async (ctx, uninstallData) => {
await ctx.api.devices.delete(ctx.config.deviceId); // delete device
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment