Skip to content

Instantly share code, notes, and snippets.

@erickvneri
Last active April 1, 2021 18:10
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 erickvneri/d84c7e5953324f860c47594e54b71c7e to your computer and use it in GitHub Desktop.
Save erickvneri/d84c7e5953324f860c47594e54b71c7e to your computer and use it in GitHub Desktop.
SmartApp Instance to create a virtual device
'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;
@erickvneri
Copy link
Author

To publish a device profile, run the following command at the SmartThings CLI:

smartthings deviceprofiles:publish <device_profile_id>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment