Skip to content

Latest commit

 

History

History
107 lines (100 loc) · 6 KB

index.md

File metadata and controls

107 lines (100 loc) · 6 KB

SmartApp SDK Reference

The SmartApp class handles all SmartApp lifecycle events and callbacks. It is instantiated and configured with handlers where appropriate and is invoked in response to either web server HTTP requests, or in response to 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);
});