Skip to content

Commit

Permalink
Merge pull request arendst#4562 from emontnemery/force_local
Browse files Browse the repository at this point in the history
Enable forced local operation when button- or switchtopic is set
  • Loading branch information
arendst authored Dec 8, 2018
2 parents 5ea51e1 + 0836fe5 commit 7e7f39b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions sonoff/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#define MQTT_BUTTON_RETAIN 0 // [ButtonRetain] Button may send retain flag (0 = off, 1 = on)
#define MQTT_POWER_RETAIN 0 // [PowerRetain] Power status message may send retain flag (0 = off, 1 = on)
#define MQTT_SWITCH_RETAIN 0 // [SwitchRetain] Switch may send retain flag (0 = off, 1 = on)
#define MQTT_BUTTON_SWITCH_FORCE_LOCAL 0 // [SetOption61] Force local operation when button/switch topic is set (0 = off, 1 = on)

#define MQTT_STATUS_OFF "OFF" // [StateText1] Command or Status result when turned off (needs to be a string like "0" or "Off")
#define MQTT_STATUS_ON "ON" // [StateText2] Command or Status result when turned on (needs to be a string like "1" or "On")
Expand Down
2 changes: 1 addition & 1 deletion sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu
uint32_t receive_raw : 1; // bit 8 (v6.3.0.11)
uint32_t hass_tele_on_power : 1; // bit 9 (v6.3.0.13)
uint32_t sleep_normal : 1; // bit 10 (v6.3.0.15) - SetOption60 - Enable normal sleep instead of dynamic sleep
uint32_t spare11 : 1;
uint32_t button_switch_force_local : 1;// bit 11
uint32_t spare12 : 1;
uint32_t spare13 : 1;
uint32_t spare14 : 1;
Expand Down
1 change: 1 addition & 0 deletions sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ void SettingsDefaultSet2(void)
Settings.flag.mqtt_power_retain = MQTT_POWER_RETAIN;
Settings.flag.mqtt_button_retain = MQTT_BUTTON_RETAIN;
Settings.flag.mqtt_switch_retain = MQTT_SWITCH_RETAIN;
Settings.flag3.button_switch_force_local = MQTT_BUTTON_SWITCH_FORCE_LOCAL;
// Settings.flag.mqtt_sensor_retain = 0;
// Settings.flag.mqtt_offline = 0;
// Settings.flag.mqtt_serial = 0;
Expand Down
4 changes: 2 additions & 2 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ boolean SendKey(byte key, byte device, byte state)
if (9 == state) {
mqtt_data[0] = '\0';
} else {
if ((!strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic)) && (2 == state)) {
if ((Settings.flag3.button_switch_force_local || !strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic)) && (2 == state)) {
state = ~(power >> (device -1)) &1;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), GetStateText(state));
Expand All @@ -1328,7 +1328,7 @@ boolean SendKey(byte key, byte device, byte state)
#else
MqttPublishDirect(stopic, (key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain);
#endif // USE_DOMOTICZ
result = true;
result = !Settings.flag3.button_switch_force_local;
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":{\"State\":%d}}"), (key) ? "Switch" : "Button", device, state);
result = XdrvRulesProcess();
Expand Down
8 changes: 5 additions & 3 deletions sonoff/xdrv_12_home_assistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ void HAssAnnounceSwitches(void)

// Check if MQTT message will be ON/OFF or TOGGLE
if (Settings.switchmode[switch_index] == FOLLOW || Settings.switchmode[switch_index] == FOLLOW_INV ||
Settings.flag3.button_switch_force_local ||
!strcmp(mqtt_topic, sw_topic) || !strcmp(Settings.mqtt_grptopic, sw_topic))
{
toggle = 0;
toggle = 0; // MQTT message will be ON/OFF
}

HAssAnnounceButtonSwitch(switch_index, sw_topic, switch_present, 0, toggle);
Expand Down Expand Up @@ -399,9 +400,10 @@ void HAssAnnounceButtons(void)
}

// Check if MQTT message will be ON/OFF or TOGGLE
if (!strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic))
if (Settings.flag3.button_switch_force_local ||
!strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic))
{
toggle = 0;
toggle = 0; // MQTT message will be ON/OFF
}

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

0 comments on commit 7e7f39b

Please sign in to comment.