-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-Netatmo-Thermostat.js
executable file
·184 lines (162 loc) · 4.44 KB
/
MMM-Netatmo-Thermostat.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
/* global Module */
/* Magic Mirror
* Module: MMM-Netatmo-Thermostat
*
* By
* MIT Licensed.
*/
Module.register("MMM-Netatmo-Thermostat", {
defaults: {
refreshToken: null,
clientId: null,
clientSecret: null,
updateInterval: 60000,
api: {
base: 'https://api.netatmo.com/',
authEndpoint: 'oauth2/token',
authPayload: 'grant_type=refresh_token&refresh_token={0}&client_id={1}&client_secret={2}',
dataEndpoint: 'api/getthermostatsdata',
dataPayload: 'access_token={0}'
},
color: {
off: "grey",
heating: "orange",
cooling: "blue"
}
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function() {
var self = this;
//Flag for check if module is loaded
this.loaded = false;
// Get initial data from netatmo connect api.
this.getData();
},
load: {
token: function() {
return Q($.ajax({
type: 'POST',
url: this.config.api.base + this.config.api.authEndpoint,
data: this.config.api.authPayload.format(
this.config.refreshToken,
this.config.clientId,
this.config.clientSecret
)
}));
},
data: function(data) {
this.config.refreshToken = data.refresh_token;
// call for station data
return Q($.ajax({
url: this.config.api.base + this.config.api.dataEndpoint,
data: this.config.api.dataPayload.format(data.access_token)
}));
}
},
renderAll: function(data) {
this.processData(data.body.devices[0]);
// render modules
return Q({});
},
renderError: function(reason) {
/* eslint-disable no-console */
console.log("error " + reason);
},
/*
* getData
* function example return data and show it in the module wrapper
* get a URL request
*
*/
getData: function() {
var self = this;
return Q.fcall(
this.load.token.bind(self),
this.renderError.bind(self)
).then(
this.load.data.bind(self),
this.renderError.bind(self)
).done(
this.renderAll.bind(self)
);
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad ;
var self = this;
setTimeout(function() {
self.getData();
}, nextLoad);
},
formatTemp: function (t) {
var tTxt = String(t);
if (tTxt.indexOf(".") != -1) {
var tempParts = tTxt.split(".");
return tempParts[0] + "<sup class='tempDecimal'>" + tempParts[1] + "</sup>";
} else {
return tTxt;
}
},
getDom: function() {
var self = this;
// create element wrapper for show into the module
var wrapper = document.createElement("div");
// Data from helper
if (this.data.modules) {
console.log(this.data);
var theName = document.createElement("div");
wrapper.id = "circle";
wrapper.innerHTML = this.formatTemp(this.data.modules[0].measured.temperature);
if (this.data.modules[0].setpoint.setpoint_mode === "off") {
wrapper.className = "off " + this.config.color.off;
} else {
if (this.data.modules[0].therm_relay_cmd !== 0 || this.data.modules[0].anticipating === true) {
wrapper.className = "heating " + this.config.color.heating;
} else {
wrapper.className = "cooling " + this.config.color.cooling;
}
}
wrapper.appendChild(theName);
}
return wrapper;
},
getScripts: function() {
return [
'q.min.js',
'String.format.js',
'jquery-3.2.1.min.js'
];
},
getStyles: function () {
return [
"MMM-Netatmo-Thermostat.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"
};
},
processData: function(data) {
var self = this;
this.data = data;
self.updateDom(self.config.animationSpeed);
this.loaded = true;
this.scheduleUpdate(self.config.updateInterval);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
},
});