-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-Smartthings.js
executable file
·219 lines (191 loc) · 6.06 KB
/
MMM-Smartthings.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* global Module */
/* Magic Mirror
* Module: MMM-Smartthings
*
* By BuzzKC
* MIT Licensed.
*/
Module.register("MMM-Smartthings", {
deviceStatuses: [],
defaults: {
updateInterval: 30000, //API rate limit: A maximum of 250 executions per minute is allowed for each installed SmartApp or Device Handler.
personalAccessToken: '', //setup personal access token at https://account.smartthings.com/tokens,
capabilities: [],
title: 'Devices',
excludedDeviceNames: [],
tempLowValue: '65',
tempHighValue: '80'
},
/*
Capabilities statuses implemented:
"switch"
"contactSensor"
"lock"
"temperatureMeasurement"
"relativeHumidityMeasurement"
"motionSensor"
Other capabilities reference: https://docs.smartthings.com/en/latest/capabilities-reference.html
*/
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function() {
let self = this;
//Flag for check if module is loaded
this.loaded = false;
this.sendConfig();
this.getData();
// Schedule update timer.
this.scheduleUpdate(2000);
},
sendConfig: function() {
this.sendSocketNotification('SEND_CONFIG', this.config);
},
/*
* getData
* function example return data and show it in the module wrapper
* get a URL request
*
*/
getData: function() {
this.sendSocketNotification("GET_DEVICES", null);
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
let nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad ;
let self = this;
setTimeout(function() {
self.updateDom();
self.getData();
self.scheduleUpdate();
}, nextLoad);
},
getDom: function() {
let self = this;
const wrapper = document.createElement('div');
if (this.deviceStatuses === null || this.deviceStatuses.length === 0) {
wrapper.innerHTML =
'<div class="loading"><span class="zmdi zmdi-rotate-right zmdi-hc-spin"></span> Loading...</div>';
//retry ui update in a few seconds, data may still be loading
setTimeout(function() {
self.updateDom();
}, 5000);
return wrapper;
}
this.deviceStatuses = this.deviceStatuses.sort(this.compareDeviceNames); //sort device names
this.deviceStatuses = this.deviceStatuses.sort(this.compareDeviceTypes); //sort by device types
const deviceKeys = Object.keys(this.deviceStatuses) || [];
wrapper.innerHTML = `
<span class="title">${this.config.title}</span>
<ul class="sensors">
${deviceKeys
.map(sensorKey => {
const device = this.deviceStatuses[sensorKey];
let iconClass = 'zmdi';
let rowClass = '';
if (device.value === 'locked' || device.value === 'closed') {
iconClass = `${iconClass} zmdi-lock`;
rowClass = `${rowClass} ok`;
} else if (device.value === 'unlocked' || device.value === 'open') {
iconClass = `${iconClass} zmdi-lock-open`;
rowClass = `${rowClass} error`;
} else if (device.value === 'on') {
iconClass = `${iconClass} zmdi-power`;
rowClass = `${rowClass} error`;
} else if (device.value === 'off') {
iconClass = `${iconClass} zmdi-minus-circle-outline`;
rowClass = `${rowClass} ok`;
} else if (device.deviceType === 'temperatureMeasurement') {
if (device.value <= this.config.tempLowValue) {
iconClass = `sensor-temp-low fa fa-thermometer-empty`;
} else if (device.value >= this.config.tempHighValue) {
iconClass = `sensor-temp-high fa fa-thermometer-full`;
} else {
iconClass = `sensor-temp fa fa-thermometer-half`;
}
} else if (device.deviceType === 'relativeHumidityMeasurement') {
iconClass = `${iconClass} zmdi-grain`;
} else if (device.deviceType === 'motionSensor') {
if(device.value === 'active') {
iconClass = `sensor-motion ${iconClass} zmdi-run`;
} else {
iconClass = `${iconClass} zmdi-run`;
}
}
return `
<li class="sensor ${rowClass}">
<span class="sensor-icon ${device.deviceType}"></span>
<span class="sensor-name">${device.deviceName}</span>
<span class="sensor-status-icon ${iconClass}"></span>
<span class="sensor-status-name">${device.value}</span>
</li>
`;
})
.join('')}
</ul>
`;
return wrapper;
},
compareDeviceNames: function (a, b) {
// Use toUpperCase() to ignore character casing
const deviceNameA = a.deviceName.toUpperCase();
const deviceNameB = b.deviceName.toUpperCase();
let comparison = 0;
if (deviceNameA > deviceNameB) {
comparison = 1;
} else if (deviceNameA < deviceNameB) {
comparison = -1;
}
return comparison;
},
compareDeviceTypes: function (a, b) {
// Use toUpperCase() to ignore character casing
const deviceTypeA = a.deviceType.toUpperCase();
const deviceTypeB = b.deviceType.toUpperCase();
let comparison = 0;
if (deviceTypeA > deviceTypeB) {
comparison = 1;
} else if (deviceTypeA < deviceTypeB) {
comparison = -1;
}
return comparison;
},
getScripts: function() {
return [
'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js',
];
},
getStyles: function () {
return [
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css',
"MMM-Smartthings.css",
];
},
// Load translations files
getTranslations: function() {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if(notification === "DEVICE_STATUS_FOUND") {
this.deviceStatuses = payload;
}
//messages to display in console from node_helper and other backend processes.
if (notification === "ConsoleOutput") {
console.log("OUTPUT_LOG:");
console.log(payload);
}
}
});