Skip to content

Commit 1f6f0c1

Browse files
0xW3bJun6l30xW3bJun6l3
authored andcommitted
One Control to Rule Them all
1 parent f6afa13 commit 1f6f0c1

19 files changed

Lines changed: 7174 additions & 1024 deletions

Classes/Brain.js

Lines changed: 2261 additions & 1021 deletions
Large diffs are not rendered by default.

Classes/CO2.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class CO2 extends Device {
2+
constructor(name) {
3+
super(name, "co2"); // Setze den Gerätetyp auf "co2"
4+
this.targetCO2 = 0; // Zielwert für CO2 (ppm)
5+
this.currentCO2 = 0; // Aktueller CO2-Wert (ppm)
6+
this.autoRegulate = false; // Automatische Steuerung
7+
}
8+
9+
init() {
10+
// Initialisierungen, falls notwendig
11+
}
12+
13+
setTargetCO2(target) {
14+
if (target !== this.targetCO2) {
15+
this.targetCO2 = target;
16+
}
17+
}
18+
19+
enableAutoRegulation() {
20+
if (!this.enableAutoRegulation) {
21+
this.autoRegulate = true;
22+
}
23+
}
24+
25+
disableAutoRegulation() {
26+
if (this.enableAutoRegulation) {
27+
this.autoRegulate = false;
28+
}
29+
}
30+
31+
updateCurrentCO2(value) {
32+
if (value !== this.currentCO2) {
33+
this.currentCO2 = value;
34+
}
35+
}
36+
37+
evalAction(context) {
38+
if (this.action === "unchanged") return false;
39+
40+
if (this.autoRegulate && this.currentCO2 < this.targetCO2) {
41+
this.action = "increased";
42+
return true;
43+
} else if (this.autoRegulate && this.currentCO2 > this.targetCO2) {
44+
this.action = "reduced";
45+
return true;
46+
}
47+
48+
return this.action !== "unchanged";
49+
}
50+
51+
runAction(context) {
52+
if (!this.evalAction(context)) {
53+
return { CO2: `${this.name}`, Action: "noChangesNeeded" };
54+
}
55+
56+
switch (this.action) {
57+
case "increased":
58+
node.warn(`${this.name}: CO2-Zufuhr wird erhöht.`);
59+
return { entity_id: this.switches[0], action: "on" };
60+
61+
case "reduced":
62+
node.warn(`${this.name}: CO2-Zufuhr wird gestoppt.`);
63+
return { entity_id: this.switches[0], action: "off" };
64+
65+
case "on":
66+
node.warn(`${this.name}: CO2-Zufuhr wird aktiviert.`);
67+
return { entity_id: this.switches[0], action: "on" };
68+
69+
case "off":
70+
node.warn(`${this.name}: CO2-Zufuhr wird deaktiviert.`);
71+
return { entity_id: this.switches[0], action: "off" };
72+
73+
default:
74+
node.warn(`${this.name}: Unbekannte Aktion.`);
75+
return { CO2: `${this.name}`, Action: "UnknownAction" };
76+
}
77+
}
78+
}

Classes/Climate.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
class Climate extends Device {
2+
constructor(name) {
3+
super(name, "climate");
4+
this.currentHAVOC = "off"; // Standardzustand
5+
this.havocs = {
6+
dry: "dry",
7+
cool: "cool",
8+
hot: "hot",
9+
heat: "heat",
10+
wind: "wind",
11+
off: "off",
12+
};
13+
this.isRunning = false; // Status der Klimaanlage
14+
}
15+
16+
setData(data, context) {
17+
this.setFromtent(context.tentName);
18+
this.identifyIfFromAmbient();
19+
//this.data = { ...this.data, ...data };
20+
this.updateChangedData(this.data, data);
21+
this.identifySwitchesAndSensors();
22+
this.updateIsRunningState();
23+
this.identifyCurrentHavoc();
24+
}
25+
26+
identifyCurrentHavoc() {
27+
const havocDevice = this.switches[0];
28+
if (!havocDevice) {
29+
console.warn(`${this.name}: Keine Switches definiert, um HAVOC zu identifizieren.`);
30+
return;
31+
}
32+
33+
const havocValue = this.data[havocDevice];
34+
35+
if (havocValue && Object.values(this.havocs).includes(havocValue)) {
36+
this.currentHAVOC = havocValue;
37+
console.warn(`${this.name}: HAVOC-Modus gesetzt auf "${this.currentHAVOC}".`);
38+
} else {
39+
console.warn(`${this.name}: Ungültiger HAVOC-Wert "${havocValue}", Standardwert "off" wird verwendet.`);
40+
}
41+
}
42+
43+
runAction() {
44+
if (!this.needChange || !this.action || typeof this.action !== "object") {
45+
console.warn(`${this.name}: Keine Änderungen erforderlich.`);
46+
return null;
47+
}
48+
49+
const { mode, action } = this.action;
50+
const lowerMode = mode.toLowerCase(); // Konvertiere in Kleinbuchstaben
51+
const validMode = this.havocs[lowerMode]; // Vergleiche mit `havocs` Mapping
52+
53+
console.warn(`Aktion empfangen: Mode = "${mode}", Action = "${action}", ValidMode = "${validMode}", CurrentHAVOC = "${this.currentHAVOC}"`);
54+
55+
// Prüfen, ob der Modus ungültig ist
56+
if (!validMode) {
57+
console.warn(`${this.name}: Ungültiger Modus "${mode}" erhalten. Standardwert "off" wird verwendet.`);
58+
return { entity_id: this.switches[0], action: "invalid_mode", received_mode: mode };
59+
}
60+
61+
// Prüfen, ob der Modus bereits läuft
62+
if (this.isRunning && this.currentHAVOC.toLowerCase() === validMode) {
63+
console.warn(`${this.name}: Keine Änderungen notwendig. Der Modus "${validMode}" ist bereits aktiv.`);
64+
return { entity_id: this.switches[0], action: "AllReady_ON", climate_mode: this.currentHAVOC };
65+
}
66+
67+
if (action === "off") {
68+
return this.turnOFF();
69+
}
70+
71+
if (!this.isRunning) {
72+
return this.turnON(validMode);
73+
}
74+
75+
return this.changeMode(validMode);
76+
}
77+
78+
turnON(mode) {
79+
this.isRunning = true;
80+
if (this.currentHAVOC !== mode) {
81+
this.currentHAVOC = mode;
82+
return { entity_id: this.switches[0], action: "climate", climate_mode: mode };
83+
} else {
84+
return { entity_id: this.switches[0], action: "AllReady_Running", climate_mode: mode };
85+
}
86+
87+
}
88+
89+
turnOFF() {
90+
if (this.isRunning) {
91+
const previousMode = this.currentHAVOC;
92+
93+
if (this.isRunning === true) {
94+
this.isRunning = false;
95+
this.currentHAVOC = "off";
96+
return { entity_id: this.switches[0], action: "off", previous_mode: previousMode };
97+
} else {
98+
return { entity_id: this.switches[0], action: "AllreadyOFF", previous_mode: previousMode };
99+
}
100+
}
101+
return { entity_id: this.switches[0], action: "already_off" };
102+
}
103+
104+
changeMode(mode) {
105+
106+
if (this.currentHAVOC !== mode) {
107+
this.currentHAVOC = mode;
108+
return { entity_id: this.switches[0], action: "climate", climate_mode: mode };
109+
} else {
110+
return { entity_id: this.switches[0], action: "AllReady_OFF", climate_mode: mode };
111+
}
112+
113+
}
114+
}

Classes/Cooler.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Cooler extends Device {
2+
constructor(name) {
3+
super(name, "cooler");
4+
}
5+
6+
runAction() {
7+
if (!this.needChange) {
8+
return { entity_id: this.switches[0], action: "noChangesNeeded" };
9+
}
10+
11+
switch (this.action) {
12+
case "off":
13+
return this.turnOFF();
14+
case "on":
15+
return this.turnON();
16+
case "increased":
17+
return this.turnON();
18+
case "reduced":
19+
return this.turnOFF();
20+
case "unchanged":
21+
return { entity_id: this.switches[0], action: "UNCHANGED" };
22+
default:
23+
node.warn(`${this.name}: Unbekannte Aktion "${this.action}".`);
24+
return { entity_id: this.switches[0], action: "Unknown Action" };
25+
}
26+
}
27+
28+
turnON() {
29+
const entity = this.switches[0];
30+
if (!this.isRunning) {
31+
this.isRunning = true;
32+
node.warn(`${this.name}: Luftentfeuchter eingeschaltet in ${this.inRoomName}`);
33+
return { entity_id: entity, action: "on" };
34+
}
35+
return { entity_id: entity, action: "Already ON" };
36+
}
37+
38+
turnOFF() {
39+
const entity = this.switches[0];
40+
if (this.isRunning) {
41+
this.isRunning = false;
42+
node.warn(`${this.name}: Luftentfeuchter ausgeschaltet in ${this.inRoomName}`);
43+
return { entity_id: entity, action: "off" };
44+
}
45+
return { entity_id: entity, action: "Already OFF" };
46+
}
47+
}

Classes/Dehumidifier.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
class Dehumidifier extends Device {
2+
constructor(name) {
3+
super(name, "dehumidifier");
4+
this.realHumidifier = false; // Erkennung eines echten Luftentfeuchters
5+
this.isSimpleSwitch = false; // Standardmäßig ein einfacher Schalter
6+
this.hasModes = false; // Erkennung von Modis
7+
this.currentMode = null; // Aktueller Modus des Luftentfeuchters
8+
}
9+
10+
setData(data, context) {
11+
this.setFromtent(context.tentName);
12+
this.identifyIfFromAmbient();
13+
this.updateChangedData(this.data, data);
14+
this.identifySwitchesAndSensors();
15+
this.updateIsRunningState();
16+
this.identifyDehumidifierType();
17+
}
18+
19+
20+
21+
identifyDehumidifierType() {
22+
if (this.data) {
23+
if (Object.keys(this.data).some(key => key.startsWith("humidifier."))) {
24+
this.realHumidifier = true;
25+
this.isSimpleSwitch = false;
26+
this.hasModes = true;
27+
this.currentMode = "dry"; // Standardmodus
28+
} else if (Object.keys(this.data).some(key => key.startsWith("switch."))) {
29+
this.realHumidifier = false;
30+
this.isSimpleSwitch = true;
31+
this.hasModes = false;
32+
this.currentMode = null;
33+
} else {
34+
console.log(`${this.name}: Keine gültigen Daten gefunden, Standard auf einfacher Schalter.`);
35+
this.realHumidifier = false;
36+
this.isSimpleSwitch = true;
37+
this.hasModes = false;
38+
this.currentMode = null;
39+
}
40+
}
41+
}
42+
43+
runAction() {
44+
// Wenn keine Änderung nötig ist, gib aktuellen Zustand zurück
45+
if (!this.needChange) {
46+
if (this.action === "reduced" && !this.isRunning) {
47+
return { entity_id: this.switches[0], action: "Already OFF" };
48+
} else if (this.action === "increased" && this.isRunning) {
49+
return { entity_id: this.switches[0], action: "Already ON" };
50+
}
51+
return { entity_id: this.switches[0], action: "noChangesNeeded" };
52+
}
53+
54+
// Aktionen für einfache Schalter
55+
if (this.isSimpleSwitch) {
56+
if (this.action === "reduced") {
57+
return this.turnOFF();
58+
} else if (this.action === "increased") {
59+
return this.turnON();
60+
}
61+
}
62+
63+
// Aktionen für echte Luftentfeuchter
64+
if (this.realHumidifier) {
65+
if (this.action === "reduced") {
66+
return this.turnOFF();
67+
} else if (this.action === "increased") {
68+
return this.activateDryMode();
69+
}
70+
}
71+
72+
// Wenn keine passende Aktion gefunden wurde
73+
console.log(`${this.name}: Keine passenden Aktionen gefunden.`);
74+
return { entity_id: this.switches[0], action: "No Action Found" };
75+
}
76+
77+
78+
activateDryMode() {
79+
const entity = this.sensors.find(sensor => sensor.startsWith("humidifier."));
80+
if (!this.isRunning) {
81+
this.isRunning = true;
82+
console.log(`${this.name}: Luftentfeuchter im Modus "dry" eingeschaltet in ${this.inRoomName}`);
83+
return [
84+
{ entity_id: this.switches[0], action: "on" }, // Gerät einschalten
85+
{ entity_id: entity, action: "select", option: "dry" }, // Modus auf "dry" setzen
86+
];
87+
}
88+
return { entity_id: this.switches[0], action: "Already ON" };
89+
}
90+
91+
92+
turnON() {
93+
const entity = this.switches[0];
94+
if (!this.isRunning) {
95+
this.isRunning = true;
96+
console.log(`${this.name}: Luftentfeuchter eingeschaltet in ${this.inRoomName}`);
97+
return { entity_id: entity, action: "on" };
98+
}else{
99+
return { entity_id: entity, action: "Already ON" };
100+
}
101+
102+
}
103+
104+
turnOFF() {
105+
const entity = this.switches[0];
106+
if(this.isRunning) {
107+
this.isRunning = false;
108+
console.log(`${this.name}: Luftentfeuchter ausgeschaltet in ${this.inRoomName}`);
109+
return { entity_id: entity, action: "off" };
110+
}else{
111+
return { entity_id: entity, action: "Already OFF" };
112+
}
113+
114+
}
115+
}

0 commit comments

Comments
 (0)