Skip to content

Instantly share code, notes, and snippets.

@erickvneri

erickvneri/smartapp.js

Last active Apr 1, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@erickvneri erickvneri commented Feb 4, 2021

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