Minor clenup.

This commit is contained in:
Blaz Kristan 2022-01-20 17:38:18 +01:00
parent 52c36ef6a4
commit 255347ab77

View File

@ -254,6 +254,7 @@ class MultiRelay : public Usermod {
mqtt->subscribe(subuf, 0); mqtt->subscribe(subuf, 0);
publishHomeAssistantAutodiscovery(); publishHomeAssistantAutodiscovery();
for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) { for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
if (_relay[i].pin<0) continue;
publishMqtt(i); //publish current state publishMqtt(i); //publish current state
} }
} }
@ -263,37 +264,37 @@ class MultiRelay : public Usermod {
for (uint8_t i = 0; i < MULTI_RELAY_MAX_RELAYS; i++) { for (uint8_t i = 0; i < MULTI_RELAY_MAX_RELAYS; i++) {
char uid[16], json_str[1024], buf[128]; char uid[16], json_str[1024], buf[128];
size_t payload_size; size_t payload_size;
sprintf(uid, "%s_sw%d", escapedMac.c_str(), i); sprintf_P(uid, PSTR("%s_sw%d"), escapedMac.c_str(), i);
if (_relay[i].pin >= 0 && _relay[i].external) { if (_relay[i].pin >= 0 && _relay[i].external) {
StaticJsonDocument<1024> json; StaticJsonDocument<1024> json;
sprintf(buf, "%s Switch %d", serverDescription, i); //max length: 33 + 8 + 3 = 44 sprintf_P(buf, PSTR("%s Switch %d"), serverDescription, i); //max length: 33 + 8 + 3 = 44
json[F("name")] = buf; json[F("name")] = buf;
sprintf(buf, "%s/relay/%d", mqttDeviceTopic, i); //max length: 33 + 7 + 3 = 43 sprintf_P(buf, PSTR("%s/relay/%d"), mqttDeviceTopic, i); //max length: 33 + 7 + 3 = 43
json["~"] = buf; json["~"] = buf;
strcat(buf, "/command"); strcat_P(buf, PSTR("/command"));
mqtt->subscribe(buf, 0); mqtt->subscribe(buf, 0);
json[F("stat_t")] = "~"; json[F("stat_t")] = "~";
json[F("cmd_t")] = "~/command"; json[F("cmd_t")] = F("~/command");
json[F("pl_off")] = F("off"); json[F("pl_off")] = F("off");
json[F("pl_on")] = F("on"); json[F("pl_on")] = F("on");
json[F("uniq_id")] = uid; json[F("uniq_id")] = uid;
strcpy(buf, mqttDeviceTopic); //max length: 33 + 7 = 40 strcpy(buf, mqttDeviceTopic); //max length: 33 + 7 = 40
strcat(buf, "/status"); strcat_P(buf, PSTR("/status"));
json[F("avty_t")] = buf; json[F("avty_t")] = buf;
json[F("pl_avail")] = F("online"); json[F("pl_avail")] = F("online");
json[F("pl_not_avail")] = F("offline"); json[F("pl_not_avail")] = F("offline");
//TODO: dev //TODO: dev
payload_size = serializeJson(json, json_str); payload_size = serializeJson(json, json_str);
} else { } else {
//Unpublish disabled or internal relays //Unpublish disabled or internal relays
json_str[0] = 0; json_str[0] = 0;
payload_size = 0; payload_size = 0;
} }
sprintf(buf, "homeassistant/switch/%s/config", uid); sprintf_P(buf, PSTR("homeassistant/switch/%s/config"), uid);
mqtt->publish(buf, 0, true, json_str, payload_size); mqtt->publish(buf, 0, true, json_str, payload_size);
} }
} }