Skip to content

Commit b36dc21

Browse files
author
xlyric
committed
ajout commandes HA switch
1 parent 6118806 commit b36dc21

File tree

6 files changed

+105
-38
lines changed

6 files changed

+105
-38
lines changed

src/config/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ constexpr size_t NTP_UPDATE_INTERVAL_MS = 3600000; /// synch de l'heure toute le
154154
// #define ARDUINO_RUNNING_CORE 1
155155
#endif
156156

157-
#define RELEASE "Version 20250908"
157+
#define RELEASE "Version 20250912"
158158
constexpr const int FS_RELEASE = 20250908;
159159
constexpr const char* FS_UPDATE = "<br>!! FS pas à jour !!"; /// paramettre FS pas à jour
160160

src/config/enums.h

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -628,14 +628,22 @@ struct Dallas {
628628
struct HA {
629629
/* HA */
630630
private:String name;
631-
public:void Set_name(String setter) {name=setter; }
631+
private:const String node_mac = WiFi.macAddress().substring(12,14)+ WiFi.macAddress().substring(15,17);
632+
private:const String node_id = String("PvRouter-") + node_mac;
633+
public:String topic = "homeassistant/sensor/"+ node_id +"/";
634+
635+
public:void Set_name(String setter) {
636+
name=setter;
637+
}
638+
632639
public:String Get_name() {return name;}
633640

634641
private:String dev_cla;
642+
635643
public:void Set_dev_cla(String setter) {
636644
dev_cla=setter;
637645
if (setter=="switch") {
638-
topic = "homeassistant/switch/"+ String(node_id) +"/";
646+
topic = "homeassistant/switch/"+ node_id +"/";
639647
}
640648
}
641649

@@ -659,18 +667,17 @@ struct HA {
659667
icon = R"("ic": ")" + setter + R"(", )";
660668
}
661669

662-
bool cmd_t;
670+
// private:IPAddress IPaddress;
671+
// private:String state_topic;
672+
// private:String stat_t;
673+
// private:String cmd_t;
674+
//private:String avty_t;
663675

664-
private:IPAddress IPaddress;
665-
private:String state_topic;
666-
private:String stat_t;
667-
private:String avty_t;
668-
669-
private:const String node_mac = WiFi.macAddress().substring(12,14)+ WiFi.macAddress().substring(15,17);
670-
private:const String node_id = String("PvRouter-") + node_mac;
671-
public:String topic = "homeassistant/sensor/"+ node_id +"/";
676+
//public:String Get_cmd_t() {return cmd_t;}
677+
//public:String Get_stat_t() {return stat_t;}
672678

673679
private:String device_declare() {
680+
IPAddress IPaddress = WiFi.localIP() ;
674681
String info = R"(
675682
"dev": {
676683
"ids": ")" + String(node_id) + R"(",
@@ -688,15 +695,20 @@ struct HA {
688695

689696
public:void discovery() {
690697
String dev_switch= "";
698+
String stat_t = topic +"state"+name;
699+
String cmd_t = topic +"command";
691700

692-
if (stat_cla == nullptr) { //rien faire
693-
}
694-
else if (dev_cla =="switch" ) {
701+
702+
// if (stat_cla.isEmpty()) { //rien faire
703+
//}
704+
if (dev_cla =="switch" ) {
695705
dev_switch = R"(
696-
"pl_on": "{ \")" + name + R"(\" : \"1\" }",
697-
"pl_off": "{ \")" + name + R"(\" : \"0\" }",
706+
"pl_on": 1,
707+
"pl_off": 0,
698708
"stat_on": 1,
699709
"stat_off": 0,
710+
"cmd_t": ")" + cmd_t + R"(",
711+
"cmd_tpl": "{ \")" + name + R"(\": {{ value }} }",
700712
)";
701713
}
702714
else {
@@ -706,20 +718,19 @@ struct HA {
706718
)";
707719

708720
}
721+
if (stat_cla.isEmpty() && dev_cla !="switch") { //rien faire
722+
dev_switch= "";
723+
}
709724

710-
IPaddress = WiFi.localIP() ;
725+
711726
String device= "{ \"dev_cla\": \""+dev_cla+"\","
712727
"\"name\": \""+ name +"-"+ node_mac + "\","
713-
// "\"state_topic\": \""+ topic +"state\","
714-
"\"stat_t\": \""+ topic +"state"+name+"\","
715728
"\"avty_t\": \""+ topic +"status\","
716729
"\"uniq_id\": \""+ node_mac + "-" + name +"\", "
717730
"\"val_tpl\": \"{{ value_json."+name +" }}\", "
718-
+ dev_switch +
719-
"\"cmd_t\": \""+ topic +"command\","
720-
"\"cmd_tpl\": \"{{ value_json."+name +" }}\", "
721-
"\"exp_aft\": \""+ MQTT_INTERVAL +"\", "
722-
+ icon
731+
+ dev_switch
732+
+ R"("stat_t": ")" + stat_t + R"(", )"
733+
+ "\"exp_aft\": \""+ MQTT_INTERVAL +"\", "
723734
+ device_declare() +
724735
"}";
725736

@@ -734,15 +745,23 @@ struct HA {
734745
else {
735746
snprintf(final_topic, sizeof(final_topic), "%sconfig", topic.c_str());
736747
client.publish(final_topic, device.c_str() , true); // déclaration autoconf routeur
737-
}
748+
}
749+
client.loop();
750+
delay(10);
738751
} // discovery
739752

740753
public:void send(const char* value) {
741754
// vérification que value est un nombre
755+
// client.loop();
756+
//delay(50);
742757
char message[150];
743-
snprintf(message, sizeof(message), " { \"%s\" : \"%s\" } ", name.c_str(), value); // NOSONAR
758+
snprintf(message, sizeof(message), "{ \"%s\": \"%s\" }", name.c_str(), value); // NOSONAR
759+
760+
bool result = client.publish((topic +"state"+name).c_str() , message, false); // false for exp_aft in discovery
761+
// Serial.println(stat_t.c_str());
762+
// Serial.println(message);
763+
// Serial.println(result ? "SUCCESS" : "FAILED");
744764

745-
client.publish((topic+"state"+name).c_str() , message, false); // false for exp_aft in discovery
746765
} // send()
747766

748767
void sendInt(int value) {

src/functions/Mqtt_http_Functions.h

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern HA device_dimmer_boost;
4646
extern HA device_dimmer_alarm_temp;
4747
extern xSemaphoreHandle mutex;
4848

49+
extern bool boost();
4950
//***********************************
5051
//************* Variables locales
5152
//***********************************
@@ -66,7 +67,7 @@ WiFiClient espClient;
6667
}
6768

6869
const String pvname = String("PvRouter-") + WiFi.macAddress().substring(12,14)+ WiFi.macAddress().substring(15,17);
69-
const String topic = "homeassistant/sensor/"+ pvname +"/status";
70+
String topic = "homeassistant/sensor/"+ pvname +"/status";
7071
// Loop until we're reconnected
7172
while (!client.connected()) {
7273
logging.clean_log_init();
@@ -83,6 +84,8 @@ WiFiClient espClient;
8384
if (client.connect(pvname.c_str(), configmqtt.username, configmqtt.password)) {
8485
// Once connected, publish online to the availability topic
8586
client.publish(topic.c_str(), "online", true);
87+
topic = "homeassistant/switch/"+ pvname +"/status";
88+
client.publish(topic.c_str(), "online", true);
8689
client.setKeepAlive(15);
8790
client.setBufferSize(1024);
8891
logging.Set_log_init(MQTT_connected,true);
@@ -140,12 +143,15 @@ WiFiClient espClient;
140143
// Ajout d'un espace pour le caractère nul // NOSONAR
141144
char arrivage[length+1];
142145
int recup = 0;
146+
//Serial.println("MQTT callback : topic = "+String(topic));
147+
//Serial.println(strstr(topic, "command") != NULL ? "MATCH" : "NO MATCH");
143148

144149
for (int i=0;i<length;i++) {
145150
arrivage[i] = (char)payload[i];
146151
}
147152
arrivage[length] = '\0'; // Ajouter le caractère nul à la fin
148153

154+
/// gestion du topic Shelly
149155
if (strcmp( topic, config.topic_Shelly ) == 0 ) {
150156
if (strcmp( arrivage , "unavailable" ) == 0 ) {
151157
gDisplayValues.Shelly = -2;
@@ -162,9 +168,42 @@ WiFiClient espClient;
162168
} else {
163169
DEBUG_PRINTLN("Erreur : Conversion de la chaîne en virgule flottante a échoué");
164170
}
165-
} // if (strcmp( arrivage , "unavailable" ) == 0 )
166-
} // if (strcmp( topic, config.topic_Shelly ) == 0 )
171+
}
172+
}
173+
174+
/// gestion des switchs
175+
if (strstr(topic, "command") != NULL ) {
176+
Serial.println("MQTT callback : dimmer = "+String(arrivage));
177+
String returnkey;
178+
179+
JsonDocument doc;
180+
deserializeJson(doc, arrivage);
181+
182+
// Traitement simple et propre
183+
if (!doc["Boost"].isNull()) {
184+
Serial.println("MQTT callback : Boost = "+String(doc["Boost"].as<bool>()));
185+
boost();
186+
returnkey = "Boost";
187+
188+
}
189+
190+
if (!doc["Relay1"].isNull()) {
191+
Serial.println("MQTT callback : Relay1 = "+String(doc["Relay1"].as<bool>()));
192+
digitalWrite(RELAY1, doc["Relay1"].as<bool>());
193+
returnkey = "Relay1";
194+
}
195+
196+
if (!doc["Relay2"].isNull()) {
197+
Serial.println("MQTT callback : Relay2 = "+String(doc["Relay2"].as<bool>()));
198+
digitalWrite(RELAY2, doc["Relay2"].as<bool>());
199+
returnkey = "Relay2";
200+
}
201+
202+
//envoie de la commande de confirmation au broker
203+
client.publish((device_dimmer_boost.topic+"state"+returnkey).c_str(), String(arrivage).c_str(), true);
204+
}
167205

206+
/// récupération des anciennes valeurs de consommation
168207
if (strcmp( topic, ("memory/"+compteur_grid.topic+compteur_grid.Get_name()).c_str() ) == 0 ) {
169208
Serial.println("MQTT callback : compteur_grid = "+String(arrivage));
170209
// Utiliser strtol pour une conversion plus robuste
@@ -180,8 +219,9 @@ WiFiClient espClient;
180219
} else {
181220
DEBUG_PRINTLN("Erreur : Conversion de la chaîne en virgule flottante a échoué");
182221
}
183-
} // if (strcmp( topic, ("memory/"+compteur_grid.topic+compteur_grid.Get_name()).c_str() ) == 0 )
222+
}
184223

224+
/// récupération des anciennes valeurs d'injection
185225
if (strcmp( topic, ("memory/"+compteur_inject.topic+compteur_inject.Get_name()).c_str() ) == 0 ) {
186226
Serial.println("MQTT callback : compteur_inject = "+String(arrivage));
187227
// Utiliser strtol pour une conversion plus robuste
@@ -198,12 +238,11 @@ WiFiClient espClient;
198238
else {
199239
DEBUG_PRINTLN("Erreur : Conversion de la chaîne en virgule flottante a échoué");
200240
}
201-
} // if (strcmp( topic, ("memory/"+compteur_inject.topic+compteur_inject.Get_name()).c_str() ) == 0 )
202-
if (WHtempgrid != 0 && WHtempinject !=0 ) {
203-
client.unsubscribe(("memory/"+compteur_grid.topic+"#").c_str());
204-
}
241+
}
205242

206-
// TODO, faire la récupération pour le mode boost
243+
if (WHtempgrid != 0 && WHtempinject !=0 ) {
244+
client.unsubscribe(("memory/"+compteur_grid.topic+"#").c_str());
245+
}
207246
}
208247

209248

@@ -232,6 +271,7 @@ WiFiClient espClient;
232271
Serial.println("memory/"+compteur_grid.topic+compteur_grid.Get_name()) ;
233272

234273
client.subscribe(("memory/"+compteur_grid.topic+"#").c_str());
274+
235275
client.loop();
236276

237277
if (config.IDXdimmer != 0 ) {

src/functions/appweb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ inline const char* stringBool(bool myBool) {
178178
return myBool ? "true" : "false";
179179
}
180180

181+
inline const char* stringInt(bool myBool) {
182+
return myBool ? "1" : "0";
183+
}
181184

182185
//***********************************
183186
//************* getServermode

src/functions/homeassistant.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110

111111
device_dimmer_boost.Set_name("Boost");
112112
device_dimmer_boost.Set_dev_cla("switch");
113+
// subscription au topic commande des switchs
114+
bool sub_result = client.subscribe((device_dimmer_boost.topic+"command").c_str());
115+
Serial.println("Souscription switch: " + String(sub_result ? "OK" : "FAILED"));
116+
client.loop();
117+
delay(10);
113118

114119
device_routeur.discovery();
115120
device_routed.discovery();

src/tasks/send-mqtt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void send_to_mqtt(void * parameter) { // NOSONAR
102102
device_dimmer.sendInt(unified_dimmer.get_power()); // Modif RV - pour être plus en accord avec le nommage sur les dimmers
103103
switch_relay1.sendInt(digitalRead(RELAY1));
104104
switch_relay2.sendInt(digitalRead(RELAY2));
105-
device_dimmer_boost.send(stringBool(programme_marche_forcee.run));
105+
device_dimmer_boost.send(stringInt(programme_marche_forcee.run));
106106
// si dallas.security actif alors on envoie l'alarme
107107
if (dallas.security) {
108108
device_dimmer_alarm_temp.send("Ballon Chaud");

0 commit comments

Comments
 (0)