From 8ac38bb1f0f8b1b9fd0b613b80c2486a184e827b Mon Sep 17 00:00:00 2001
From: Darrell Turner
Date: Mon, 4 Apr 2022 15:04:10 -0400
Subject: [PATCH] MACCHINA_A0 Running->Charging
---
src/main.cpp | 10 +++++-----
src/main.h | 5 +++--
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/main.cpp b/src/main.cpp
index bbfc134..2a6ba83 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -34,8 +34,8 @@ bool sendTelemetry(int totalSeen, int totalFpSeen, int totalFpQueried, int total
&& sendDeleteDiscovery("switch", "OTA Update")
&& Motion::SendDiscovery(doc)
#ifdef MACCHINA_A0
- && sendTeleSensorDiscovery("Battery", "", "{{ value_json.batt }}", "%")
- && sendTeleBinarySensorDiscovery("Running", "", "{{ value_json.run }}", "running")
+ && sendTeleSensorDiscovery("Battery", "", "{{ value_json.batt }}", "%", "battery")
+ && sendTeleBinarySensorDiscovery("Charging", "", "{{ value_json.charging }}", "battery_charging")
#endif
#ifdef SENSORS
&& sendDiscoveryHumidity()
@@ -72,10 +72,10 @@ bool sendTelemetry(int totalSeen, int totalFpSeen, int totalFpQueried, int total
#ifdef MACCHINA_A0
auto mv = a0_read_batt_mv();
doc["mV"] = mv;
- bool run = (mv > 13200);
+ bool charging = (mv > 13200);
unsigned int soc = round(-13275.04 + 2.049731 * mv - (0.00007847975 * mv) * mv);
- doc["batt"] = run ? (unsigned int)100 : max((unsigned int)0, min((unsigned int)100, soc));
- doc["run"] = run ? "ON" : "OFF";
+ doc["batt"] = charging ? (unsigned int)100 : max((unsigned int)0, min((unsigned int)100, soc));
+ doc["charging"] = charging ? "ON" : "OFF";
#endif
#ifdef VERSION
doc["ver"] = String(VERSION);
diff --git a/src/main.h b/src/main.h
index cb58416..e69a037 100644
--- a/src/main.h
+++ b/src/main.h
@@ -379,14 +379,14 @@ bool sendTeleBinarySensorDiscovery(const String &name, const String &entityCateg
doc["stat_t"] = "~/telemetry";
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
doc["value_template"] = temp;
- doc["dev_cla"] = devClass;
+ if (!devClass.isEmpty()) doc["dev_cla"] = devClass;
serializeJson(doc, buffer);
String discoveryTopic = "homeassistant/binary_sensor/espresense_" + ESPMAC + "/" + slug + "/config";
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
-bool sendTeleSensorDiscovery(const String &name, const String &entityCategory, const String &temp, const String &units)
+bool sendTeleSensorDiscovery(const String &name, const String &entityCategory, const String &temp, const String &units, const String &devClass = "")
{
auto slug = slugify(name);
@@ -399,6 +399,7 @@ bool sendTeleSensorDiscovery(const String &name, const String &entityCategory, c
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
doc["value_template"] = temp;
if (!units.isEmpty()) doc["unit_of_measurement"] = units;
+ if (!devClass.isEmpty()) doc["dev_cla"] = devClass;
serializeJson(doc, buffer);
String discoveryTopic = "homeassistant/sensor/espresense_" + ESPMAC + "/" + slug + "/config";