Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/HomeAssistantHelper/HomeAssistantHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ class HomeAssistantHelper {
return result;
}

bool publishClimateHeatingConfig(
const char* name,
const char* uniqSuffix,
const char* tempUnit = "C",
float minTemp = 18.0f,
float maxTemp = 28.0f,
float tempStep = 0.1f
) {
JsonDocument doc;

doc["name"] = name;
doc["uniq_id"] = this->getUniqueIdWithPrefix(uniqSuffix);
doc["obj_id"] = this->getUniqueIdWithPrefix(uniqSuffix);

doc["mode_cmd_t"] = this->getDeviceTopic("climate", "heating", "mode/set");
doc["mode_stat_t"] = this->getDeviceTopic("climate", "heating", "mode");
doc["modes"][0] = "off";
doc["modes"][1] = "heat";

doc["temp_cmd_t"] = this->getDeviceTopic("climate", "heating", "target/set");
doc["temp_stat_t"] = this->getDeviceTopic("climate", "heating", "target");
doc["curr_temp_t"] = this->getDeviceTopic("climate", "heating", "current");
doc["act_t"] = this->getDeviceTopic("climate", "heating", "action");

doc["min_temp"] = minTemp;
doc["max_temp"] = maxTemp;
doc["temp_step"] = tempStep;
doc["temp_unit"] = tempUnit;

String topic = this->makeConfigTopic("climate", uniqSuffix);
return this->publish(topic.c_str(), doc);
}

template <class CT, class NT>
String makeConfigTopic(CT category, NT name, char nameSeparator = '/') {
String topic = "";
Expand Down
33 changes: 4 additions & 29 deletions src/MqttTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ class MqttTask : public Task {
void setup() {
Log.sinfoln(FPSTR(L_MQTT), F("Started"));

// wificlient settings
#ifdef ARDUINO_ARCH_ESP8266
this->wifiClient->setSync(true);
this->wifiClient->setNoDelay(true);
#endif

// client settings
this->client->setTxPayloadSize(256);
#ifdef ARDUINO_ARCH_ESP8266
this->client->setConnectionTimeout(1000);
Expand All @@ -141,7 +139,6 @@ class MqttTask : public Task {
this->onMessage(topic, payload, length);
});

// writer settings
#ifdef ARDUINO_ARCH_ESP32
this->writer->setYieldCallback([this] {
this->delay(10);
Expand All @@ -155,7 +152,6 @@ class MqttTask : public Task {
::optimistic_yield(1000);
#endif

//this->client->poll();
this->delay(250);
});

Expand All @@ -171,7 +167,6 @@ class MqttTask : public Task {
});
#endif

// ha helper settings
this->haHelper->setDevicePrefix(settings.mqtt.prefix);
this->haHelper->setDeviceVersion(BUILD_VERSION);
this->haHelper->setDeviceModel(PROJECT_NAME);
Expand Down Expand Up @@ -219,7 +214,6 @@ class MqttTask : public Task {

this->client->poll();

// delay for publish data
if (!this->isReadyForSend()) {
return;
}
Expand All @@ -228,20 +222,17 @@ class MqttTask : public Task {
::optimistic_yield(1000);
#endif

// publish variables and status
if (this->newConnection || millis() - this->prevPubVarsTime > (settings.mqtt.interval * 1000u)) {
this->writer->publish(this->haHelper->getDeviceTopic(F("status")).c_str(), "online", false);
this->publishVariables(this->haHelper->getDeviceTopic(F("state")).c_str());
this->prevPubVarsTime = millis();
}

// publish settings
if (this->newConnection || millis() - this->prevPubSettingsTime > (settings.mqtt.interval * 10000u)) {
this->publishSettings(this->haHelper->getDeviceTopic(F("settings")).c_str());
this->prevPubSettingsTime = millis();
}

// publish sensors
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
if (!Sensors::hasEnabledAndValid(sensorId)) {
continue;
Expand All @@ -263,7 +254,6 @@ class MqttTask : public Task {
}
}

// publish ha entities if not published
if (settings.mqtt.homeAssistantDiscovery) {
if (this->newConnection || !this->currentHomeAssistantDiscovery || this->currentUnitSystem != settings.system.unitSystem) {
this->publishHaEntities();
Expand All @@ -272,15 +262,12 @@ class MqttTask : public Task {
this->currentUnitSystem = settings.system.unitSystem;

} else {
// publish non static ha entities
this->publishNonStaticHaEntities();
}

// rebuilding ha configs
for (auto& [sensorId, prevSettings] : this->queueReconfigureSensors) {
Log.sinfoln(FPSTR(L_MQTT_HA), F("Rebuilding config for sensor #%hhu '%s'"), sensorId, prevSettings.name);

// delete old config
if (strlen(prevSettings.name) && prevSettings.enabled) {
switch (prevSettings.type) {
case Sensors::Type::BLUETOOTH:
Expand All @@ -307,7 +294,6 @@ class MqttTask : public Task {
continue;
}

// make new config
auto& sSettings = Sensors::settings[sensorId];
switch (sSettings.type) {
case Sensors::Type::BLUETOOTH:
Expand All @@ -334,9 +320,7 @@ class MqttTask : public Task {
this->currentHomeAssistantDiscovery = false;
}

// reconfigure manual sensors
for (auto& [sensorId, prevSettings] : this->queueReconfigureSensors) {
// unsubscribe from old topic
if (strlen(prevSettings.name) && prevSettings.enabled) {
if (prevSettings.type == Sensors::Type::MANUAL) {
this->client->unsubscribe(
Expand All @@ -353,7 +337,6 @@ class MqttTask : public Task {
continue;
}

// subscribe to new topic
auto& sSettings = Sensors::settings[sensorId];
if (sSettings.type == Sensors::Type::MANUAL) {
this->client->subscribe(
Expand All @@ -366,7 +349,6 @@ class MqttTask : public Task {
}
}

// clear queue
this->queueReconfigureSensors.clear();

if (this->newConnection) {
Expand All @@ -383,7 +365,6 @@ class MqttTask : public Task {
this->client->subscribe(this->haHelper->getDeviceTopic(F("settings/set")).c_str());
this->client->subscribe(this->haHelper->getDeviceTopic(F("state/set")).c_str());

// subscribe to manual sensors
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
if (!Sensors::hasEnabledAndValid(sensorId)) {
continue;
Expand Down Expand Up @@ -448,7 +429,6 @@ class MqttTask : public Task {
}
doc.shrinkToFit();

// delete topic
this->writer->publish(topic.c_str(), nullptr, 0, true);

if (this->haHelper->getDeviceTopic(F("state/set")).equals(topic)) {
Expand All @@ -458,8 +438,10 @@ class MqttTask : public Task {

} else if (this->haHelper->getDeviceTopic(F("settings/set")).equals(topic)) {
if (safeJsonToSettings(doc, settings)) {
this->resetPublishedSettingsTime();
fsSettings.update();
this->resetPublishedSettingsTime();

Log.sinfoln(FPSTR(L_MQTT_MSG), F("Settings updated from MQTT"));
}

} else {
Expand All @@ -484,15 +466,13 @@ class MqttTask : public Task {
}

void publishHaEntities() {
// heating
this->haHelper->publishSwitchHeatingTurbo(false);
this->haHelper->publishSwitchHeatingHysteresis();
this->haHelper->publishInputHeatingHysteresis(settings.system.unitSystem);
this->haHelper->publishInputHeatingTurboFactor(false);
this->haHelper->publishInputHeatingMinTemp(settings.system.unitSystem);
this->haHelper->publishInputHeatingMaxTemp(settings.system.unitSystem);

// pid
this->haHelper->publishSwitchPid();
this->haHelper->publishInputPidFactorP(false);
this->haHelper->publishInputPidFactorI(false);
Expand All @@ -501,14 +481,12 @@ class MqttTask : public Task {
this->haHelper->publishInputPidMinTemp(settings.system.unitSystem, false);
this->haHelper->publishInputPidMaxTemp(settings.system.unitSystem, false);

// equitherm
this->haHelper->publishSwitchEquitherm();
this->haHelper->publishInputEquithermSlope(false);
this->haHelper->publishInputEquithermExponent(false);
this->haHelper->publishInputEquithermShift(false);
this->haHelper->publishInputEquithermTargetDiffFactor(false);

// states
this->haHelper->publishStatusState();
this->haHelper->publishEmergencyState();
this->haHelper->publishOpenthermConnectedState();
Expand All @@ -518,18 +496,15 @@ class MqttTask : public Task {
this->haHelper->publishDiagState();
this->haHelper->publishExternalPumpState(false);

// sensors
this->haHelper->publishFaultCode();
this->haHelper->publishDiagCode();
this->haHelper->publishNetworkRssi(false);
this->haHelper->publishUptime(false);

// buttons
this->haHelper->publishRestartButton(false);
this->haHelper->publishResetFaultButton();
this->haHelper->publishResetDiagButton();

// dynamic sensors
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
if (!Sensors::hasEnabledAndValid(sensorId)) {
continue;
Expand Down Expand Up @@ -648,4 +623,4 @@ class MqttTask : public Task {

return this->writer->publish(topic, doc, true);
}
};
};
Loading