Last active
April 1, 2021 18:10
-
-
Save erickvneri/d84c7e5953324f860c47594e54b71c7e to your computer and use it in GitHub Desktop.
SmartApp Instance to create a virtual device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/* | |
* This demo SmartApp can be registered | |
* as a SmartApp Automation or SmartApp | |
* Connector. | |
* | |
* Either way, a virtual switch will be | |
* created and will respond to device | |
* commands. | |
* */ | |
const { SmartApp } = require('@smartthings/smartapp'); | |
const deviceSmartApp = new SmartApp() | |
.appId('demo') | |
.disableCustomDisplayName(true) | |
.permissions(['w:devices:*', 'r:locations:*', 'i:deviceprofiles:*']) | |
.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: { | |
profileId: '', | |
installedAppId: updateData.installedApp.installedAppId | |
} | |
}; | |
let deviceReq = await ctx.api.devices.create(deviceOpt) | |
}).deviceCommandHandler(async (ctx, cmdData) => { | |
let event = []; | |
let statusRes = { | |
component: cmdData.commands[0].componentId, | |
capability: cmdData.commands[0].capability, | |
attribute: 'switch', | |
value: cmdData.commands[0].command === 'on' ? 'on' : 'off' | |
}; | |
event.push(statusRes); | |
await ctx.api.devices.sendEvents(cmdData.deviceId, event); // send device response | |
}).uninstalled(async (ctx, uninstallData) => { | |
await ctx.api.devices.delete(ctx.config.deviceId); // delete device | |
}) | |
module.exports = deviceSmartApp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To publish a device profile, run the following command at the SmartThings CLI: