From f4da1d1d869710c83054309c43790e00405b2a36 Mon Sep 17 00:00:00 2001 From: Adrian Scillato <35405447+ascillato@users.noreply.github.com> Date: Wed, 20 Mar 2019 00:57:26 -0300 Subject: [PATCH 1/2] Added HASS_STATE to command STATE The command STATE has a secondary mqtt message in the case that SETOPTION59 is 1 This adds a HASS_STATE mqtt message if HomeAssistant discovery is enabled. This is useful for syncing HomeAssistant with all the states of a Tasmota device when HA is restarted. The actual automation for HA explained in the wiki will also make use of this new message. --- sonoff/sonoff.ino | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 51b89031c..d1cf3adb1 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -615,6 +615,11 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) if (Settings.flag3.hass_tele_on_power) { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); } +#ifdef USE_HOME_ASSISTANT + if (Settings.flag.hass_discovery) { + HAssPublishStatus(); + } +#endif // USE_HOME_ASSISTANT } else if (CMND_SLEEP == command_code) { if ((payload >= 0) && (payload < 251)) { From 878205335ff6af018990f849b8d884941fab5b61 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 20 Mar 2019 14:05:43 +0100 Subject: [PATCH 2/2] Consolidate MqttPublishTeleState Consolidate MqttPublishTeleState --- sonoff/sonoff.ino | 19 ++++++++++--------- sonoff/xdrv_04_light.ino | 12 ++---------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index d1cf3adb1..74fbafee5 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -619,7 +619,7 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) if (Settings.flag.hass_discovery) { HAssPublishStatus(); } -#endif // USE_HOME_ASSISTANT +#endif // USE_HOME_ASSISTANT } else if (CMND_SLEEP == command_code) { if ((payload >= 0) && (payload < 251)) { @@ -1552,11 +1552,7 @@ void ExecuteCommandPower(uint8_t device, uint8_t state, int source) #ifdef USE_KNX KnxUpdatePowerState(device, power); #endif // USE_KNX - if (publish_power && Settings.flag3.hass_tele_on_power) { - mqtt_data[0] = '\0'; - MqttShowState(); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); - } + if (publish_power && Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); } if (device <= MAX_PULSETIMERS) { // Restart PulseTime if powered On SetPulseTimer(device -1, (((POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate) ? ~power : power) & mask) ? Settings.pulse_timer[device -1] : 0); } @@ -1783,6 +1779,13 @@ void MqttShowState(void) mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkCount(), WifiDowntime().c_str()); } +void MqttPublishTeleState(void) +{ + mqtt_data[0] = '\0'; + MqttShowState(); + MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); +} + bool MqttShowSensor(void) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_JSON_TIME "\":\"%s\""), mqtt_data, GetDateAndTime(DT_LOCAL).c_str()); @@ -1860,9 +1863,7 @@ void PerformEverySecond(void) if (tele_period >= Settings.tele_period) { tele_period = 0; - mqtt_data[0] = '\0'; - MqttShowState(); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); + MqttPublishTeleState(); mqtt_data[0] = '\0'; if (MqttShowSensor()) { diff --git a/sonoff/xdrv_04_light.ino b/sonoff/xdrv_04_light.ino index 1dc6e8630..532ecc42d 100644 --- a/sonoff/xdrv_04_light.ino +++ b/sonoff/xdrv_04_light.ino @@ -793,11 +793,7 @@ void LightPreparePower(void) #ifdef USE_DOMOTICZ DomoticzUpdatePowerState(light_device); #endif // USE_DOMOTICZ - if (Settings.flag3.hass_tele_on_power) { - mqtt_data[0] = '\0'; - MqttShowState(); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); - } + if (Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); } LightState(0); } @@ -1456,11 +1452,7 @@ bool LightCommand(void) LightPowerOn(); strip_timer_counter = 0; // Publish state message for Hass - if (Settings.flag3.hass_tele_on_power) { - mqtt_data[0] = '\0'; - MqttShowState(); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN); - } + if (Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); } } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme); }