From e95b70efebbba8622194211244fd6647d4c614df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hm?= Date: Fri, 26 Jul 2024 20:37:40 +0200 Subject: [PATCH] Feature: expose 'full solar passsthrough active' via MQTT (#1136) --- include/MqttHandlePowerLimiterHass.h | 1 + include/PowerLimiter.h | 1 + src/MqttHandlePowerLimiter.cpp | 1 + src/MqttHandlePowerLimiterHass.cpp | 45 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/include/MqttHandlePowerLimiterHass.h b/include/MqttHandlePowerLimiterHass.h index 283f8ad8b..45bd80299 100644 --- a/include/MqttHandlePowerLimiterHass.h +++ b/include/MqttHandlePowerLimiterHass.h @@ -15,6 +15,7 @@ class MqttHandlePowerLimiterHassClass { void publish(const String& subtopic, const String& payload); void publishNumber(const char* caption, const char* icon, const char* category, const char* commandTopic, const char* stateTopic, const char* unitOfMeasure, const int16_t min, const int16_t max, const float step); void publishSelect(const char* caption, const char* icon, const char* category, const char* commandTopic, const char* stateTopic); + void publishBinarySensor(const char* caption, const char* icon, const char* stateTopic, const char* payload_on, const char* payload_off); void createDeviceInfo(JsonDocument& root); Task _loopTask; diff --git a/include/PowerLimiter.h b/include/PowerLimiter.h index c5727fe5c..8da2628f9 100644 --- a/include/PowerLimiter.h +++ b/include/PowerLimiter.h @@ -44,6 +44,7 @@ class PowerLimiterClass { uint8_t getInverterUpdateTimeouts() const { return _inverterUpdateTimeouts; } uint8_t getPowerLimiterState(); int32_t getLastRequestedPowerLimit() { return _lastRequestedPowerLimit; } + bool getFullSolarPassThroughEnabled() const { return _fullSolarPassThroughEnabled; } enum class Mode : unsigned { Normal = 0, diff --git a/src/MqttHandlePowerLimiter.cpp b/src/MqttHandlePowerLimiter.cpp index bb3dd00a8..f01a9b146 100644 --- a/src/MqttHandlePowerLimiter.cpp +++ b/src/MqttHandlePowerLimiter.cpp @@ -91,6 +91,7 @@ void MqttHandlePowerLimiterClass::loop() MqttSettings.publish("powerlimiter/status/threshold/voltage/stop", String(config.PowerLimiter.VoltageStopThreshold)); if (config.Vedirect.Enabled) { + MqttSettings.publish("powerlimiter/status/full_solar_passthrough_active", String(PowerLimiter.getFullSolarPassThroughEnabled())); MqttSettings.publish("powerlimiter/status/threshold/voltage/full_solar_passthrough_start", String(config.PowerLimiter.FullSolarPassThroughStartVoltage)); MqttSettings.publish("powerlimiter/status/threshold/voltage/full_solar_passthrough_stop", String(config.PowerLimiter.FullSolarPassThroughStopVoltage)); } diff --git a/src/MqttHandlePowerLimiterHass.cpp b/src/MqttHandlePowerLimiterHass.cpp index 66c8566d1..e5706cd85 100644 --- a/src/MqttHandlePowerLimiterHass.cpp +++ b/src/MqttHandlePowerLimiterHass.cpp @@ -75,6 +75,10 @@ void MqttHandlePowerLimiterHassClass::publishConfig() "config", "threshold/voltage/stop", "threshold/voltage/stop", "V", 16, 60, 0.1); if (config.Vedirect.Enabled) { + publishBinarySensor("full solar passthrough active", + "mdi:transmission-tower-import", + "full_solar_passthrough_active", "1", "0"); + publishNumber("DPL full solar passthrough start voltage", "mdi:transmission-tower-import", "config", "threshold/voltage/full_solar_passthrough_start", @@ -187,6 +191,47 @@ void MqttHandlePowerLimiterHassClass::publishNumber( publish(configTopic, buffer); } +void MqttHandlePowerLimiterHassClass::publishBinarySensor( + const char* caption, const char* icon, + const char* stateTopic, const char* payload_on, const char* payload_off) +{ + + String numberId = caption; + numberId.replace(" ", "_"); + numberId.toLowerCase(); + + const String configTopic = "binary_sensor/" + MqttHandleHass.getDtuUniqueId() + "/" + numberId + "/config"; + + const String statTopic = MqttSettings.getPrefix() + "powerlimiter/status/" + stateTopic; + + JsonDocument root; + + root["name"] = caption; + root["uniq_id"] = MqttHandleHass.getDtuUniqueId() + "_" + numberId; + if (strcmp(icon, "")) { + root["ic"] = icon; + } + root["stat_t"] = statTopic; + root["pl_on"] = payload_on; + root["pl_off"] = payload_off; + + auto const& config = Configuration.get(); + if (config.Mqtt.Hass.Expire) { + root["exp_aft"] = config.Mqtt.PublishInterval * 3; + } + + createDeviceInfo(root); + + if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { + return; + } + + String buffer; + serializeJson(root, buffer); + publish(configTopic, buffer); +} + + void MqttHandlePowerLimiterHassClass::createDeviceInfo(JsonDocument& root) { JsonObject object = root["dev"].to();