Repository navigation
Expand file tree
/
Copy pathsmartapp.js
More file actions
67 lines (62 loc) · 2.36 KB
/
Copy pathsmartapp.js
File metadata and controls
67 lines (62 loc) · 2.36 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const SmartApp = require('@smartthings/smartapp');
const weather = require('./lib/weather');
async function setColor(ctx) {
const forecast = await weather.getForecast(ctx.configStringValue('zipCode'));
const color = weather.getColorForForecast(forecast, ctx.configNumberValue('forecastInterval'));
await ctx.api.devices.sendCommands(ctx.config.colorLight, [
{
capability: 'switch',
command: 'on'
},
{
capability: 'switchLevel',
command: 'setLevel',
arguments: [20]
},
{
capability: 'colorControl',
command: 'setColor',
arguments: [color]
}
]);
}
module.exports = new SmartApp()
.configureI18n()
.enableEventLogging(2) // logs all lifecycle event requests/responses as pretty-printed JSON. Omit in production
.page('mainPage', (context, page, configData) => {
page.section('forecast', section => {
section.numberSetting('zipCode')
.required(true);
section.enumSetting('forecastInterval')
.options([
{ id: "3", name: "3 Hours" },
{ id: "6", name: "6 Hours" },
{ id: "9", name: "9 Hours" },
{ id: "12", name: "12 Hours" }
])
.defaultValue("3");
section.enumSetting('scheduleInterval')
.options([
{ id: "15", name: "15 Minutes" },
{ id: "30", name: "30 Minutes" },
{ id: "45", name: "45 Minutes" },
{ id: "60", name: "60 Minutes" }
])
.defaultValue("15");
});
page.section('lights', section => {
section.deviceSetting('colorLight')
.capabilities(['colorControl', 'switch', 'switchLevel'])
.permissions('rx')
.required(true);
});
})
.updated(async ctx => {
// clear any previous configuration
await ctx.api.schedules.delete();
// switch light on to initial color
await setColor(ctx);
// schedule future changes
await ctx.api.schedules.schedule('weatherHandler', `0/${ctx.configStringValue('scheduleInterval')} * * * ? *`, 'UTC');
})
.scheduledEventHandler('weatherHandler', setColor);