Consolidate MqttPublishTeleState

Consolidate MqttPublishTeleState
This commit is contained in:
Theo Arends 2019-03-20 14:05:43 +01:00
parent 76149ca1fb
commit 878205335f
2 changed files with 12 additions and 19 deletions

View File

@ -1552,11 +1552,7 @@ void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
#ifdef USE_KNX #ifdef USE_KNX
KnxUpdatePowerState(device, power); KnxUpdatePowerState(device, power);
#endif // USE_KNX #endif // USE_KNX
if (publish_power && Settings.flag3.hass_tele_on_power) { if (publish_power && Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); }
mqtt_data[0] = '\0';
MqttShowState();
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN);
}
if (device <= MAX_PULSETIMERS) { // Restart PulseTime if powered On 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); 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()); 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) bool MqttShowSensor(void)
{ {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_JSON_TIME "\":\"%s\""), mqtt_data, GetDateAndTime(DT_LOCAL).c_str()); 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) { if (tele_period >= Settings.tele_period) {
tele_period = 0; tele_period = 0;
mqtt_data[0] = '\0'; MqttPublishTeleState();
MqttShowState();
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN);
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
if (MqttShowSensor()) { if (MqttShowSensor()) {

View File

@ -793,11 +793,7 @@ void LightPreparePower(void)
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
DomoticzUpdatePowerState(light_device); DomoticzUpdatePowerState(light_device);
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
if (Settings.flag3.hass_tele_on_power) { if (Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); }
mqtt_data[0] = '\0';
MqttShowState();
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN);
}
LightState(0); LightState(0);
} }
@ -1456,11 +1452,7 @@ bool LightCommand(void)
LightPowerOn(); LightPowerOn();
strip_timer_counter = 0; strip_timer_counter = 0;
// Publish state message for Hass // Publish state message for Hass
if (Settings.flag3.hass_tele_on_power) { if (Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); }
mqtt_data[0] = '\0';
MqttShowState();
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN);
}
} }
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme); snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme);
} }