-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-Homematic-Heaters.js
290 lines (271 loc) · 9.66 KB
/
MMM-Homematic-Heaters.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Module.register("MMM-Homematic-Heaters",{
// Default module config.
defaults: {
devices: [],
ccu2IP: "homematic-ccu2",
xmlapiURL: "config/xmlapi",
updateInterval: 300000 //5min
},
homematicUrl: "",
moduleDisplay: "",
configurationSettings:[],
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
wrapper.innerHTML = this.moduleDisplay;
return wrapper;
},
// Define start sequence.
start: function() {
var self = this;
self.homematicUrl = this.config.ccu2IP + "/" + this.config.xmlapiURL;
self.configurationSettings = self.readConfiguration(this.config.devices);
setInterval(function() {
self.getDataFromCCU2();
self.updateDom();
}, this.config.updateInterval);
moment.locale(this.config.language);
self.getDataFromCCU2();
Log.info("Starting module: " + this.name + "accessing URL " + self.homematicUrl);
},
/**
* Send the notification request to the node_helper to get
* all settings of all requested devices
*/
getDataFromCCU2: function() {
var self = this;
this.sendSocketNotification(
'MMM_CCU2_REQUEST',
{
deviceList: self.createPayloadForRequest(),
url: self.homematicUrl,
}
);
},
/**
* Prepare the settings specified in the config.js
* and do some basic checks to prevent common errors
*/
readConfiguration : function(configSettings) {
var item = {};
for (var dev in configSettings) {
var settings = {};
var device = configSettings[dev];
var devId = device.id;
var devLabel = "";
var devSetTemp = false;
var devMode = false;
var devFaults = false;
if(device.label) { devLabel = device.label; }
if(device.showSetTemperature) { devSetTemp = device.showSetTemperature; }
if(device.showCurrentMode) { devMode = device.showCurrentMode; }
if(device.showFaultReporting) { devFaults = device.showFaultReporting; }
settings["configDeviceId"] = device.id;
settings["configDeviceLabel"] = devLabel;
settings["configDeviceShowSetTemperature"] = devSetTemp;
settings["configDeviceShowFaultReport"] = devFaults;
settings["configDeviceShowMode"] = devMode;
item[device.id] = settings;
}
return item;
},
/**
* Creates the payload for the socket notification
*/
createPayloadForRequest : function() {
var payload = [];
for (var dev in this.config.devices) {
var device = this.config.devices[dev];
payload.push(JSON.stringify({"deviceId": device.id}));
}
return payload;
},
/**
* Receives the notification with the response from the node_helper
*/
socketNotificationReceived: function(notification, payload) {
if (notification === 'MMM_CCU2_RESPONSE' ) {
if(payload && payload.content){
this.prepareOutputForDevices(payload.content);
}
}
},
/**
* Prepares the output for displaying the values in the mirror.
*/
prepareOutputForDevices: function(response) {
var deviceName = "";
for (var device in response) {
var deviceArray = response[device];
for(var deviceId in deviceArray){
var settingsArray = deviceArray[deviceId][0];
var deviceLabel = settingsArray["deviceName"];
var faultReport = this.prepareFaultReporting(settingsArray);
var currentMode = this.prepareControlModeOutput(settingsArray);
var actualTemperature = this.prepareAttribute("ACTUAL_TEMPERATURE", settingsArray);
var valveState = this.prepareAttribute("VALVE_STATE", settingsArray);
var setTemperature = this.prepareAttribute("SET_TEMPERATURE", settingsArray);
//Before displaying the settings double-check against the configurationSettings
//(config.js) to drive the desired behavior...
if(this.configurationSettings[deviceId]["configDeviceLabel"] !== "") {
deviceLabel = this.configurationSettings[deviceId]["configDeviceLabel"];
}
var configDeviceShowFaultReport = "";
if(this.configurationSettings[deviceId]["configDeviceShowFaultReport"] === true) {
configDeviceShowFaultReport = faultReport;
}
var configDeviceShowMode = "";
if(this.configurationSettings[deviceId]["configDeviceShowMode"] === true) {
configDeviceShowMode = "<span class='deviceMode'> ("+currentMode+")</span>";
}
var configDeviceShowSetTemperature = "";
if(this.configurationSettings[deviceId]["configDeviceShowSetTemperature"] === true) {
//style the divider (slash) with the same style as the label
configDeviceShowSetTemperature = "<span class='deviceLabel'> / </span><span class='deviceSetTemperature'>" + setTemperature + "</span>";
}
deviceLabel = "<span class='deviceLabel'>" + deviceLabel + ": </span>";
actualTemperature = "<span class='deviceActualTemperature'>" + actualTemperature + "</span>";
var newDevice = "<span class='deviceContainer'>" + deviceLabel + actualTemperature + configDeviceShowSetTemperature + configDeviceShowMode +"</span>";
newDevice += configDeviceShowFaultReport;
deviceName += newDevice;
}
}
this.moduleDisplay = deviceName;
this.updateDom();
},
/**
* Prepare the output of the given attribute. Reads the attributeName from the
* settingsArray and do further processing on it, i.e. to display the value with the
* unit (temperature, valve state) or anything else.
* Can be used in the future to prepare any other attributes for output.
*/
prepareAttribute: function(attributeName, settingsArray){
var preparedAttributeValue = "";
var attributeNameArray = settingsArray[attributeName];
switch(attributeName){
//As of now, all attributes below can be handled with the same logic
case "ACTUAL_TEMPERATURE":
case "SET_TEMPERATURE":
case "VALVE_STATE":
preparedAttributeValue = Number(parseFloat(attributeNameArray["value"]).toFixed(2)) + attributeNameArray["valueunit"];
break;
}
return preparedAttributeValue;
},
/**
* Prepare the control mode of the radiator specified in CONTROL_MODE
* parameter. Translate the returning number according to the Homematic
* documentation into the available values.
*/
prepareControlModeOutput: function(settingsArray) {
var controlMode = Number(settingsArray["CONTROL_MODE"]["value"]);
var valveState = Number(settingsArray["VALVE_STATE"]["value"]);
var valveStateDisplay = valveState + settingsArray["VALVE_STATE"]["valueunit"];
var translatedMode = this.translate("RADIATOR_OFF");
var modus = "AUTO"; //default
switch (controlMode){
case 0:
modus = "AUTO";
if(valveState !== 0) {
//Do not show word "auto" in auto mode: this.translate("RADIATOR_MODE_".concat(modus))
translatedMode = this.translate("HEATS_WITH") + " " + valveStateDisplay;
}
break;
case 1:
modus = "MANUAL";
if(valveState !== 0) {
translatedMode = this.translate("RADIATOR_MODE_".concat(modus)) + ", " + this.translate("HEATS_WITH") + " " + valveStateDisplay;
}
break;
case 2:
modus = "PARTY"; //Urlaubsmodus
var urlaubsEnde = moment().set({'year': settingsArray["PARTY_STOP_YEAR"]["value"],
'month': (settingsArray["PARTY_STOP_MONTH"]["value"] -1),
'date': settingsArray["PARTY_STOP_DAY"]["value"]});
translatedMode = this.translate("RADIATOR_MODE_".concat(modus)) + " " + this.translate("HOLIDAY_MODE_UNTIL") + " ";
translatedMode = translatedMode + urlaubsEnde.format("ddd, MMM Do Y");
break;
case 3:
modus = "BOOST";
translatedMode = this.translate("RADIATOR_MODE_".concat(modus));
}//switch
return translatedMode;
},
/**
* Translate the values of parameter CONTROL_MODE to their
* human readable values that can be used for i18n afterwards
* According Homematic documentation for datapoints
*/
prepareControlMode: function(controlModeParameter) {
var controlMode = Number(controlModeParameter["value"]);
var modus = "AUTO"; //default
switch (controlMode){
case 0:
modus = "AUTO";
break;
case 1:
modus = "MANUAL";
break;
case 2:
modus = "PARTY"; //Urlaubsmodus
break;
case 3:
modus = "BOOST";
}//switch
return modus;
},
/**
* Prepare the error (if any) for the FAULT_REPORTING parameter.
* Translate the states into their values (according Homematic documentation)
* to be able to use it in the translation files. Return an empty
* string if everything is fine.
*/
prepareFaultReporting: function(settingsArray) {
var faultCode = Number(settingsArray["FAULT_REPORTING"]["value"]);
var errorMsg = "";
switch (faultCode){
case 1:
errorMsg = this.translate("VALVE_TIGHT");
break;
case 2:
errorMsg = this.translate("ADJUSTING_RANGE_TOO_LARGE");
break;
case 3:
errorMsg = this.translate("ADJUSTING_RANGE_TOO_SMALL");
break;
case 4:
errorMsg = this.translate("COMMUNICATION_ERROR");
break;
case 5:
errorMsg = ""; //Not available - no documentation
break;
case 6:
errorMsg = this.translate("LOWBAT");
//Append the current battery state just for information
errorMsg += " ";// + Number(parseFloat(settingsArray["BATTERY_STATE"]["value"]).toFixed(2)) + settingsArray["BATTERY_STATE"]["valueunit"]
break;
case 7:
errorMsg = this.translate("VALVE_ERROR_POSITION");
}
//Return the translated i18n error message - if any
if(errorMsg !== "") {
errorMsg = "<span class='faultReporting'>"+ this.translate("WARNING") + errorMsg + "</span>";
}
return errorMsg;
},
// Define required style-sheet scripts
getStyles: function() {
return ["MMM-Homematic-Heaters.css"];
},
// Define required dependency scripts
getScripts: function() {
return ["moment.js"];
},
// Define required translation files
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
};
}
});