Skip to content

Commit

Permalink
Merge pull request #4566 from emontnemery/hass_fix_button_switch
Browse files Browse the repository at this point in the history
Fix reversed logic when announcing buttons / switches
  • Loading branch information
arendst authored Dec 8, 2018
2 parents 4c21107 + 9b5e90e commit 92e209b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions sonoff/xdrv_12_home_assistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,16 @@ void HAssAnnounceRelayLight(void)

void HAssAnnounceButtonSwitch(byte device, char* topic, byte present, byte key, byte toggle)
{
// key 0 = button
// key 1 = switch
char stopic[TOPSZ];
char sidx[8];

mqtt_data[0] = '\0'; // Clear retained message

// Clear or Set topic
snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/binary_sensor/%s_%s_%d/config"),
topic, key?"BTN":"SW",device+1);
topic, key?"SW":"BTN",device+1);

if (Settings.flag.hass_discovery && present) {
char name[33];
Expand All @@ -320,9 +322,9 @@ void HAssAnnounceButtonSwitch(byte device, char* topic, byte present, byte key,
char *availability_topic = _availability_topic;

if (device+1 > MAX_FRIENDLYNAMES) {
snprintf_P(name, sizeof(name), PSTR("%s %s %d"), Settings.friendlyname[0], key?"BTN":"SW", device+1);
snprintf_P(name, sizeof(name), PSTR("%s %s %d"), Settings.friendlyname[0], key?"SW":"BTN", device+1);
} else {
snprintf_P(name, sizeof(name), PSTR("%s %s"), Settings.friendlyname[device], key?"BTN":"SW");
snprintf_P(name, sizeof(name), PSTR("%s %s"), Settings.friendlyname[device], key?"SW":"BTN");
}
GetPowerDevice(value_template, device+1, sizeof(value_template),
key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by Settings.flag.device_index_enable
Expand Down Expand Up @@ -373,7 +375,7 @@ void HAssAnnounceSwitches(void)
toggle = 0; // MQTT message will be ON/OFF
}

HAssAnnounceButtonSwitch(switch_index, sw_topic, switch_present, 0, toggle);
HAssAnnounceButtonSwitch(switch_index, sw_topic, switch_present, 1, toggle);
}
}
}
Expand Down Expand Up @@ -406,7 +408,7 @@ void HAssAnnounceButtons(void)
toggle = 0; // MQTT message will be ON/OFF
}

HAssAnnounceButtonSwitch(button_index, key_topic, button_present, 1, toggle);
HAssAnnounceButtonSwitch(button_index, key_topic, button_present, 0, toggle);
}
}
}
Expand Down

0 comments on commit 92e209b

Please sign in to comment.