From 817f72c7bd09abca2f306658e9a9a3408a722414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Bo=CC=88hm?= Date: Sat, 27 Jul 2024 01:04:41 +0200 Subject: [PATCH] first try on getting `publishBinarySensor` centralised --- include/MqttHandleBatteryHass.h | 5 ++- include/MqttHassPublisher.h | 3 ++ src/MqttHandleBatteryHass.cpp | 62 +++++++++++++++++++-------------- src/MqttHassPublisher.cpp | 48 ++++++++++++++++++++++++- 4 files changed, 90 insertions(+), 28 deletions(-) diff --git a/include/MqttHandleBatteryHass.h b/include/MqttHandleBatteryHass.h index 7b066cbfe..0b849ed6d 100644 --- a/include/MqttHandleBatteryHass.h +++ b/include/MqttHandleBatteryHass.h @@ -11,8 +11,11 @@ class MqttHandleBatteryHassClass { private: void loop(); void publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off); + void publishBinarySensor2(const char* caption, const char* icon, const char* sensorId, const char* subTopic); void publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL); - void createDeviceInfo(JsonDocument& root); + JsonObject createDeviceInfo(); + + String configTopicPrefix(); Task _loopTask; diff --git a/include/MqttHassPublisher.h b/include/MqttHassPublisher.h index 37b1cbfdf..08274b071 100644 --- a/include/MqttHassPublisher.h +++ b/include/MqttHassPublisher.h @@ -10,6 +10,9 @@ class MqttHassPublisher { public: static void publish(const String& subtopic, const JsonDocument& payload); static void createDeviceInfo(JsonDocument& doc, const String& name, const String& identifiers, const String& model, const String& sw_version, const bool& via_dtu); + static JsonObject createDeviceInfo(const String& name, const String& identifiers, const String& model, const String& sw_version, const bool& via_dtu); + + static void publishBinarySensor(const String& unique_dentifier, const String& name, const String& icon, const String& configTopicPrefix, const String& sensorId, const String& statSubTopic, const JsonObject& device_info); static String getDtuUniqueId(); diff --git a/src/MqttHandleBatteryHass.cpp b/src/MqttHandleBatteryHass.cpp index c37061767..cbc649b80 100644 --- a/src/MqttHandleBatteryHass.cpp +++ b/src/MqttHandleBatteryHass.cpp @@ -159,7 +159,9 @@ void MqttHandleBatteryHassClass::loop() publishSensor("Modules Blocking Charge", "mdi:counter", "modulesBlockingCharge"); publishSensor("Modules Blocking Discharge", "mdi:counter", "modulesBlockingDischarge"); - publishBinarySensor("Alarm Discharge current", "mdi:alert", "alarm/overCurrentDischarge", "1", "0"); + // publishBinarySensor("Alarm Discharge current", "mdi:alert", "alarm/overCurrentDischarge", "1", "0"); + publishBinarySensor2("Alarm Discharge current", "mdi:alert", "alarm_discharge_current", "battery/alarm/overCurrentDischarge"); + publishBinarySensor("Alarm High charge current", "mdi:alert", "alarm/overCurrentCharge", "1", "0"); publishBinarySensor("Alarm Voltage low", "mdi:alert", "alarm/underVoltage", "1", "0"); publishBinarySensor("Alarm Voltage high", "mdi:alert", "alarm/overVoltage", "1", "0"); @@ -186,6 +188,11 @@ void MqttHandleBatteryHassClass::loop() _doPublish = false; } +String MqttHandleBatteryHassClass::configTopicPrefix() +{ + return "dtu_battery_" + serial; +} + void MqttHandleBatteryHassClass::publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement ) { String sensorId = caption; @@ -218,7 +225,7 @@ void MqttHandleBatteryHassClass::publishSensor(const char* caption, const char* root["unit_of_meas"] = unitOfMeasurement; } - createDeviceInfo(root); + root["dev"] = createDeviceInfo(); if (Configuration.get().Mqtt.Hass.Expire) { root["exp_aft"] = Battery.getStats()->getMqttFullPublishIntervalMs() / 1000 * 3; @@ -237,6 +244,19 @@ void MqttHandleBatteryHassClass::publishSensor(const char* caption, const char* MqttHassPublisher::publish(configTopic, root); } +void MqttHandleBatteryHassClass::publishBinarySensor2(const char* caption, const char* icon, const char* sensorId, const char* subTopic) +{ + MqttHassPublisher::publishBinarySensor( + serial + "_" + sensorId, + caption, + icon, + configTopicPrefix(), + sensorId, + subTopic, + createDeviceInfo() + ); +} + void MqttHandleBatteryHassClass::publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off) { String sensorId = caption; @@ -247,38 +267,29 @@ void MqttHandleBatteryHassClass::publishBinarySensor(const char* caption, const sensorId.replace(":", ""); sensorId.toLowerCase(); - String configTopic = "binary_sensor/dtu_battery_" + serial + String configTopic = "dtu_battery_" + serial + "/" + sensorId + "/config"; - String statTopic = MqttSettings.getPrefix() + "battery/"; + String statTopic = "battery/"; // omit serial to avoid a breaking change // statTopic.concat(serial); // statTopic.concat("/"); statTopic.concat(subTopic); - JsonDocument root; - - root["name"] = caption; - root["uniq_id"] = serial + "_" + sensorId; - root["stat_t"] = statTopic; - root["pl_on"] = payload_on; - root["pl_off"] = payload_off; - - if (icon != NULL) { - root["icon"] = icon; - } - - createDeviceInfo(root); - - if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { - return; - } - - MqttHassPublisher::publish(configTopic, root); + // MqttHassPublisher::publishBinarySensor( + // serial + "_" + sensorId, + // caption, + // icon, + // configTopic, + // statTopic, + // payload_on, + // payload_off, + // createDeviceInfo() + // ); } -void MqttHandleBatteryHassClass::createDeviceInfo(JsonDocument& root) +JsonObject MqttHandleBatteryHassClass::createDeviceInfo() { String name = "Battery(" + serial + ")"; @@ -293,8 +304,7 @@ void MqttHandleBatteryHassClass::createDeviceInfo(JsonDocument& root) firmareVersion = __COMPILED_GIT_HASH__; } - MqttHassPublisher::createDeviceInfo( - root, + return MqttHassPublisher::createDeviceInfo( name, serial, Battery.getStats()->getManufacturer(), diff --git a/src/MqttHassPublisher.cpp b/src/MqttHassPublisher.cpp index 9a1293cc1..d5bdf6412 100644 --- a/src/MqttHassPublisher.cpp +++ b/src/MqttHassPublisher.cpp @@ -24,7 +24,21 @@ void MqttHassPublisher::createDeviceInfo( const String& model, const String& sw_version, const bool& via_dtu) { - auto object = doc["dev"].to(); + doc["dev"] = createDeviceInfo( + name, + identifiers, + model, + sw_version, + via_dtu + ); +} + +JsonObject MqttHassPublisher::createDeviceInfo( + const String& name, const String& identifiers, + const String& model, const String& sw_version, + const bool& via_dtu) +{ + JsonObject object; object["name"] = name; object["ids"] = identifiers; @@ -36,6 +50,38 @@ void MqttHassPublisher::createDeviceInfo( if (via_dtu) { object["via_device"] = getDtuUniqueId(); } + + return object; +} + +void MqttHassPublisher::publishBinarySensor( + const String& unique_dentifier, + const String& name, const String& icon, + const String& configTopicPrefix, const String& sensorId, const String& statSubTopic, + const JsonObject& device_info) +{ + JsonDocument root; + + root["name"] = name; + root["uniq_id"] = unique_dentifier; + root["stat_t"] = MqttSettings.getPrefix() + "/" + statSubTopic; + root["pl_on"] = "1"; + root["pl_off"] = "0"; + + if (icon != nullptr) { + root["icon"] = icon; + } + + root["dev"] = device_info; + + if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { + return; + } + + publish( + "binary_sensor/" + configTopicPrefix + "/" + sensorId + "/config", + root + ); } String MqttHassPublisher::getDtuUniqueId()