Skip to content

Commit

Permalink
first try on getting publishBinarySensor centralised
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBoehm committed Jul 26, 2024
1 parent 67afe92 commit 817f72c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
5 changes: 4 additions & 1 deletion include/MqttHandleBatteryHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions include/MqttHassPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
62 changes: 36 additions & 26 deletions src/MqttHandleBatteryHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 + ")";

Expand All @@ -293,8 +304,7 @@ void MqttHandleBatteryHassClass::createDeviceInfo(JsonDocument& root)
firmareVersion = __COMPILED_GIT_HASH__;
}

MqttHassPublisher::createDeviceInfo(
root,
return MqttHassPublisher::createDeviceInfo(
name,
serial,
Battery.getStats()->getManufacturer(),
Expand Down
48 changes: 47 additions & 1 deletion src/MqttHassPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ void MqttHassPublisher::createDeviceInfo(
const String& model, const String& sw_version,
const bool& via_dtu)
{
auto object = doc["dev"].to<JsonObject>();
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;
Expand All @@ -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()
Expand Down

0 comments on commit 817f72c

Please sign in to comment.