Skip to content

Latest commit

 

History

History
108 lines (101 loc) · 5.82 KB

index.md

File metadata and controls

108 lines (101 loc) · 5.82 KB

SmartApp SDK Reference

The SmartApp class handles all SmartApp lifecycle events and callbacks. It is instantiated and configured with handlers for appropriate and invoked in response to either web-server HTTP requests or AWS Lambda function calls.

Instantiation and Initialization

const smartapp = new SmartApp()
    .enableEventLogging(2)
    .configureI18n()
    ....

Configuration Page Definition

    .page('mainPage', (context, page, configData) => {
        page.section('sensors', section => {
            section.deviceSetting('contactSensor')
                   .capabilities(['contactSensor'])
                   .required(false);
        })
    })

Event Handler Definition

Installation Events

    .updated(async (context, updateData) => {
        await context.api.subscriptions.unsubscribeAll()
        await context.api.subscriptions.subscribeToDevices(
            context.config.contactSensor, 'contactSensor', 'contact', 'deviceEventHandler');
    })

Subscribed and Scheduled Events

    .subscribedEventHandler('myDeviceEventHandler', (context, event) => {
        const value = event.value === 'open' ? 'on' : 'off';
        context.api.devices.sendCommands(context.config.lights, 'switch', value);
    })

Invocation from Web-Servers and Lambda Functions

server.post('/', (req, res) => {
  smartapp.handleHttpCallback(req, res);
});