-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartapp.js
108 lines (103 loc) · 3.27 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const SmartApp = require("@smartthings/smartapp");
const airQuality = require("./lib/airquality");
async function setColor(ctx)
{
const aqi = await airQuality.getAqi(ctx.configStringValue("location").replace(" ", ""));
if (aqi >= 100)
{
console.log(`Turning on the light and setting color to red...`);
await ctx.api.devices.sendCommands(ctx.config.colorLight, [
{
capability: "switch",
command: "on",
},
{
capability: "switchLevel",
command: "setLevel",
arguments: [25],
},
{
capability: "colorControl",
command: "setColor",
arguments: [
{
hue: 90,
saturation: 80,
},
],
},
]);
}
else if (aqi >= 50)
{
console.log(`Turning on the light and setting color to yellow...`);
await ctx.api.devices.sendCommands(ctx.config.colorLight, [
{
capability: "switch",
command: "on",
},
{
capability: "switchLevel",
command: "setLevel",
arguments: [25],
},
{
capability: "colorControl",
command: "setColor",
arguments: [
{
hue: 15,
saturation: 85,
},
],
},
]);
}
else
{
console.log(`Truning off the light...`);
await ctx.api.devices.sendCommands(ctx.config.colorLight, [
{
capability: "switch",
command: "off",
},
]);
}
}
module.exports = new SmartApp()
.enableEventLogging() // Log and pretty-print all lifecycle events and responses
.configureI18n() // Use files from locales directory for configuration page localization
.page("mainPage", (context, page, configData) =>
{
page.section("airquality", (section) =>
{
section.textSetting("location").required(true);
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("aqiHandler", `0/${ctx.configStringValue("scheduleInterval")} * * * ? *`, "UTC");
})
.scheduledEventHandler("aqiHandler", setColor);