-
Notifications
You must be signed in to change notification settings - Fork 3
/
smartapp.js
43 lines (37 loc) · 1.44 KB
/
smartapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { SmartApp } = require('@smartthings/smartapp')
/* Define the SmartApp */
module.exports = new SmartApp()
.enableEventLogging(2) // logs all lifecycle event requests/responses as pretty-printed JSON. Omit in production
.configureI18n() // auto-create i18n files for localizing config pages
// Configuration page definition
.page('mainPage', (context, page, configData) => {
// prompts user to select a contact sensor
page.section('sensors', section => {
section
.deviceSetting('contactSensor')
.capabilities(['contactSensor'])
.required(true)
})
// prompts users to select one or more switch devices
page.section('lights', section => {
section
.deviceSetting('lights')
.capabilities(['switch'])
.required(true)
.multiple(true)
.permissions('rx')
})
})
// Handler called whenever app is installed or updated
// Called for both INSTALLED and UPDATED lifecycle events if there is
// no separate installed() handler
.updated(async (context, updateData) => {
await context.api.subscriptions.delete()
await context.api.subscriptions.subscribeToDevices(context.config.contactSensor,
'contactSensor', 'contact', 'openCloseHandler')
})
// Handler called when the configured open/close sensor opens or closes
.subscribedEventHandler('openCloseHandler', (context, event) => {
const value = event.value === 'open' ? 'on' : 'off'
context.api.devices.sendCommands(context.config.lights, 'switch', value)
})