Skip to content

Commit 406a817

Browse files
authored
IR MQTT Server: HA multi output discovery (#1947)
When using the server to control more than one AC with Home Assistant, this will send a discovery message for each configured output. The naming and ID matching uses the same as the MQTT implementation (ie: 0 has no extension, 1 has _1 etc). Co-authored-by: Sillyfrog <[email protected]>
1 parent 721c5b8 commit 406a817

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

examples/IRMQTTServer/IRMQTTServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ const uint16_t kJsonAcStateMaxSize = 1024; // Bytes
299299
// ----------------- End of User Configuration Section -------------------------
300300

301301
// Constants
302-
#define _MY_VERSION_ "v1.8.0"
302+
#define _MY_VERSION_ "v1.8.1"
303303

304304
const uint8_t kRebootTime = 15; // Seconds
305305
const uint8_t kQuickDisplayTime = 2; // Seconds

examples/IRMQTTServer/IRMQTTServer.ino

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ void init_vars(void) {
22062206
MqttClimateCmnd = MqttClimate + '/' + MQTT_CLIMATE_CMND + '/';
22072207
// Sub-topic for the climate stat topics.
22082208
#if MQTT_DISCOVERY_ENABLE
2209-
MqttDiscovery = "homeassistant/climate/" + String(Hostname) + "/config";
2209+
MqttDiscovery = "homeassistant/climate/" + String(Hostname);
22102210
MqttUniqueId = WiFi.macAddress();
22112211
MqttUniqueId.replace(":", "");
22122212
#endif // MQTT_DISCOVERY_ENABLE
@@ -2525,7 +2525,11 @@ void handleSendMqttDiscovery(void) {
25252525
" is sent.</p>") +
25262526
addJsReloadUrl(kUrlRoot, kRebootTime, true) +
25272527
htmlEnd());
2528-
sendMQTTDiscovery(MqttDiscovery.c_str());
2528+
for (uint16_t i = 0; i < kNrOfIrTxGpios; i++) {
2529+
String channel_id = "";
2530+
if (i > 0) channel_id = "_" + String(i);
2531+
sendMQTTDiscovery(MqttDiscovery.c_str(), channel_id);
2532+
}
25292533
}
25302534
#endif // MQTT_DISCOVERY_ENABLE
25312535

@@ -2698,12 +2702,13 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
26982702
}
26992703

27002704
#if MQTT_DISCOVERY_ENABLE
2701-
void sendMQTTDiscovery(const char *topic) {
2705+
void sendMQTTDiscovery(const char *topic, String channel_id) {
2706+
String pub_topic = String(topic) + channel_id + F("/config");
27022707
if (mqtt_client.publish(
2703-
topic, String(
2704-
String(F("{"
2705-
"\"~\":\"")) + MqttClimate + F("\","
2706-
"\"name\":\"") + MqttHAName + F("\","
2708+
pub_topic.c_str(), String(
2709+
F("{"
2710+
"\"~\":\"") + MqttClimate + channel_id + F("\","
2711+
"\"name\":\"") + MqttHAName + channel_id + F("\","
27072712
#if (!MQTT_CLIMATE_HA_MODE)
27082713
// Typically we don't need or use the power command topic if we are using
27092714
// our Home Assistant Climate compatiblity mode. It causes odd behaviour
@@ -2729,9 +2734,9 @@ void sendMQTTDiscovery(const char *topic) {
27292734
"\"swing_modes\":[\"" D_STR_OFF "\",\"" D_STR_AUTO "\",\"" D_STR_HIGHEST
27302735
"\",\"" D_STR_HIGH "\",\"" D_STR_MIDDLE "\",\""
27312736
D_STR_LOW "\",\"" D_STR_LOWEST "\"],"
2732-
"\"uniq_id\":\"") + MqttUniqueId + F("\","
2737+
"\"uniq_id\":\"") + MqttUniqueId + channel_id + F("\","
27332738
"\"device\":{"
2734-
"\"identifiers\":[\"") + MqttUniqueId + F("\"],"
2739+
"\"identifiers\":[\"") + MqttUniqueId + channel_id + F("\"],"
27352740
"\"connections\":[[\"mac\",\"") + WiFi.macAddress() + F("\"]],"
27362741
"\"manufacturer\":\"IRremoteESP8266\","
27372742
"\"model\":\"IRMQTTServer\","

0 commit comments

Comments
 (0)