mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-26 12:13:10 +00:00
Various fixes for HA
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "HomeAssistantMqttHandler.h"
|
||||
#include "HomeAssistantStatic.h"
|
||||
#include "hexutils.h"
|
||||
#include "Uptime.h"
|
||||
#include "version.h"
|
||||
@@ -12,11 +11,13 @@
|
||||
#include "json/hadiscover_json.h"
|
||||
#include "json/realtime_json.h"
|
||||
|
||||
bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) {
|
||||
bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi) {
|
||||
if(topic.isEmpty() || !mqtt->connected())
|
||||
return false;
|
||||
|
||||
if(data->getListType() >= 3) { // publish energy counts
|
||||
publishList3Sensors();
|
||||
if(data->getActiveExportCounter() > 0.0) publishList3ExportSensors();
|
||||
snprintf_P(json, BufferSize, HA2_JSON,
|
||||
data->getActiveImportCounter(),
|
||||
data->getActiveExportCounter(),
|
||||
@@ -30,11 +31,14 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
String meterModel = data->getMeterModel();
|
||||
meterModel.replace("\\", "\\\\");
|
||||
if(data->getListType() == 1) { // publish power counts
|
||||
publishList1Sensors();
|
||||
snprintf_P(json, BufferSize, HA1_JSON,
|
||||
data->getActiveImportPower()
|
||||
);
|
||||
mqtt->publish(topic + "/power", json);
|
||||
} else if(data->getListType() <= 3) { // publish power counts and volts/amps
|
||||
publishList2Sensors();
|
||||
if(data->getActiveExportPower() > 0) publishList2ExportSensors();
|
||||
snprintf_P(json, BufferSize, HA3_JSON,
|
||||
data->getListId().c_str(),
|
||||
data->getMeterId().c_str(),
|
||||
@@ -52,6 +56,8 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
);
|
||||
mqtt->publish(topic + "/power", json);
|
||||
} else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
|
||||
publishList4Sensors();
|
||||
if(data->getL1ActiveExportPower() > 0 || data->getL2ActiveExportPower() > 0 || data->getL3ActiveExportPower() > 0) publishList4ExportSensors();
|
||||
snprintf_P(json, BufferSize, HA4_JSON,
|
||||
data->getListId().c_str(),
|
||||
data->getMeterId().c_str(),
|
||||
@@ -81,6 +87,8 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
}
|
||||
|
||||
if(ea->isInitialized()) {
|
||||
publishRealtimeSensors(ea, eapi);
|
||||
if(ea->getProducedThisHour() > 0.0 || ea->getProducedToday() > 0.0 || ea->getProducedThisMonth() > 0.0) publishRealtimeExportSensors(ea, eapi);
|
||||
String peaks = "";
|
||||
uint8_t peakCount = ea->getConfig()->hours;
|
||||
if(peakCount > 5) peakCount = 5;
|
||||
@@ -95,12 +103,15 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
ea->getUseThisHour(),
|
||||
ea->getCostThisHour(),
|
||||
ea->getProducedThisHour(),
|
||||
ea->getIncomeThisHour(),
|
||||
ea->getUseToday(),
|
||||
ea->getCostToday(),
|
||||
ea->getProducedToday(),
|
||||
ea->getIncomeToday(),
|
||||
ea->getUseThisMonth(),
|
||||
ea->getCostThisMonth(),
|
||||
ea->getProducedThisMonth()
|
||||
ea->getProducedThisMonth(),
|
||||
ea->getIncomeThisMonth()
|
||||
);
|
||||
mqtt->publish(topic + "/realtime", json);
|
||||
}
|
||||
@@ -110,7 +121,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
|
||||
bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) {
|
||||
int count = hw->getTempSensorCount();
|
||||
if(count == 0) return false;
|
||||
if(count < 2) return false;
|
||||
|
||||
int size = 32 + (count * 26);
|
||||
|
||||
@@ -121,11 +132,13 @@ bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwT
|
||||
TempSensorData* data = hw->getTempSensorData(i);
|
||||
if(data != NULL) {
|
||||
char* pos = buf+strlen(buf);
|
||||
String id = toHex(data->address, 8);
|
||||
snprintf(pos, 26, "\"%s\":%.2f,",
|
||||
toHex(data->address, 8).c_str(),
|
||||
id.c_str(),
|
||||
data->lastRead
|
||||
);
|
||||
data->changed = false;
|
||||
publishTemperatureSensor(i+1, id);
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
@@ -140,14 +153,16 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
|
||||
if(eapi->getValueForHour(0) == ENTSOE_NO_VALUE)
|
||||
return false;
|
||||
|
||||
publishPriceSensors(eapi);
|
||||
|
||||
time_t now = time(nullptr);
|
||||
|
||||
float min1hr = 0.0, min3hr = 0.0, min6hr = 0.0;
|
||||
int8_t min1hrIdx = -1, min3hrIdx = -1, min6hrIdx = -1;
|
||||
float min = INT16_MAX, max = INT16_MIN;
|
||||
float values[24];
|
||||
for(int i = 0;i < 24; i++) values[i] = ENTSOE_NO_VALUE;
|
||||
for(uint8_t i = 0; i < 24; i++) {
|
||||
float values[38];
|
||||
for(int i = 0;i < 38; i++) values[i] = ENTSOE_NO_VALUE;
|
||||
for(uint8_t i = 0; i < 38; i++) {
|
||||
float val = eapi->getValueForHour(now, i);
|
||||
values[i] = val;
|
||||
|
||||
@@ -234,6 +249,32 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
|
||||
values[9],
|
||||
values[10],
|
||||
values[11],
|
||||
values[12],
|
||||
values[13],
|
||||
values[14],
|
||||
values[15],
|
||||
values[16],
|
||||
values[17],
|
||||
values[18],
|
||||
values[19],
|
||||
values[20],
|
||||
values[21],
|
||||
values[22],
|
||||
values[23],
|
||||
values[24],
|
||||
values[25],
|
||||
values[26],
|
||||
values[27],
|
||||
values[28],
|
||||
values[29],
|
||||
values[30],
|
||||
values[31],
|
||||
values[32],
|
||||
values[33],
|
||||
values[34],
|
||||
values[35],
|
||||
values[36],
|
||||
values[37],
|
||||
min == INT16_MAX ? 0.0 : min,
|
||||
max == INT16_MIN ? 0.0 : max,
|
||||
ts1hr,
|
||||
@@ -247,6 +288,9 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, Energ
|
||||
if(topic.isEmpty() || !mqtt->connected())
|
||||
return false;
|
||||
|
||||
publishSystemSensors();
|
||||
if(hw->getTemperature() > -50) publishTemperatureSensor(0, "");
|
||||
|
||||
snprintf_P(json, BufferSize, JSONSYS_JSON,
|
||||
WiFi.macAddress().c_str(),
|
||||
clientId.c_str(),
|
||||
@@ -256,64 +300,207 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, Energ
|
||||
hw->getTemperature(),
|
||||
VERSION
|
||||
);
|
||||
mqtt->publish(topic + "/state", json);
|
||||
|
||||
if(!autodiscoverInit) {
|
||||
#if defined(ESP8266)
|
||||
String haUID = WiFi.hostname();
|
||||
#elif defined(ESP32)
|
||||
String haUID = WiFi.getHostname();
|
||||
#endif
|
||||
String haUrl = "http://" + haUID + ".local/";
|
||||
// Could this be necessary? haUID.replace("-", "_");
|
||||
uint8_t peakCount = ea->getConfig()->hours;
|
||||
if(peakCount > 5) peakCount = 5;
|
||||
|
||||
uint8_t peaks = 0;
|
||||
for(int i=0;i<HA_SENSOR_COUNT;i++) {
|
||||
HomeAssistantSensor sensor = HA_SENSORS[i];
|
||||
String uid = String(sensor.path);
|
||||
uid.replace(".", "");
|
||||
uid.replace("[", "");
|
||||
uid.replace("]", "");
|
||||
uid.replace("'", "");
|
||||
String uom = String(sensor.uom);
|
||||
if(strncmp(sensor.devcl, "monetary", 8) == 0) {
|
||||
if(eapi == NULL) continue;
|
||||
if(strncmp(sensor.path, "prices", 5) == 0) {
|
||||
uom = String(eapi->getCurrency()) + "/kWh";
|
||||
} else {
|
||||
uom = String(eapi->getCurrency());
|
||||
}
|
||||
}
|
||||
if(strncmp(sensor.path, "peaks[", 6) == 0) {
|
||||
if(peaks >= peakCount) continue;
|
||||
peaks++;
|
||||
}
|
||||
if(strncmp(sensor.path, "temp", 4) == 0) {
|
||||
if(hw->getTemperature() < 0) continue;
|
||||
}
|
||||
snprintf_P(json, BufferSize, HADISCOVER_JSON,
|
||||
sensor.name,
|
||||
topic.c_str(), sensor.topic,
|
||||
haUID.c_str(), uid.c_str(),
|
||||
haUID.c_str(), uid.c_str(),
|
||||
uom.c_str(),
|
||||
sensor.path,
|
||||
sensor.devcl,
|
||||
haUID.c_str(),
|
||||
haName.c_str(),
|
||||
haModel.c_str(),
|
||||
VERSION,
|
||||
haManuf.c_str(),
|
||||
haUrl.c_str(),
|
||||
strlen_P(sensor.stacl) > 0 ? ", \"stat_cla\" :" : "",
|
||||
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : ""
|
||||
);
|
||||
mqtt->publish(haTopic + haUID + "_" + uid.c_str() + "/config", json, true, 0);
|
||||
}
|
||||
|
||||
autodiscoverInit = true;
|
||||
}
|
||||
return true;
|
||||
return mqtt->publish(topic + "/state", json);
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor& sensor) {
|
||||
#if defined(ESP8266)
|
||||
String haUID = WiFi.hostname();
|
||||
#elif defined(ESP32)
|
||||
String haUID = WiFi.getHostname();
|
||||
#endif
|
||||
String haUrl = "http://" + haUID + ".local/";
|
||||
|
||||
String uid = String(sensor.path);
|
||||
uid.replace(".", "");
|
||||
uid.replace("[", "");
|
||||
uid.replace("]", "");
|
||||
uid.replace("'", "");
|
||||
String uom = String(sensor.uom);
|
||||
snprintf_P(json, BufferSize, HADISCOVER_JSON,
|
||||
sensor.name,
|
||||
topic.c_str(), sensor.topic,
|
||||
haUID.c_str(), uid.c_str(),
|
||||
haUID.c_str(), uid.c_str(),
|
||||
uom.c_str(),
|
||||
sensor.path,
|
||||
sensor.devcl,
|
||||
haUID.c_str(),
|
||||
haName.c_str(),
|
||||
haModel.c_str(),
|
||||
VERSION,
|
||||
haManuf.c_str(),
|
||||
haUrl.c_str(),
|
||||
strlen_P(sensor.stacl) > 0 ? ", \"stat_cla\" :" : "",
|
||||
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : ""
|
||||
);
|
||||
mqtt->publish(haTopic + haUID + "_" + uid.c_str() + "/config", json, true, 0);
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList1Sensors() {
|
||||
if(l1Init) return;
|
||||
for(uint8_t i = 0; i < List1SensorCount; i++) {
|
||||
publishSensor(List1Sensors[i]);
|
||||
}
|
||||
l1Init = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList2Sensors() {
|
||||
if(l2Init) return;
|
||||
for(uint8_t i = 0; i < List2SensorCount; i++) {
|
||||
publishSensor(List2Sensors[i]);
|
||||
}
|
||||
l2Init = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList2ExportSensors() {
|
||||
if(l2eInit) return;
|
||||
for(uint8_t i = 0; i < List2ExportSensorCount; i++) {
|
||||
publishSensor(List2ExportSensors[i]);
|
||||
}
|
||||
l2eInit = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList3Sensors() {
|
||||
if(l3Init) return;
|
||||
for(uint8_t i = 0; i < List3SensorCount; i++) {
|
||||
publishSensor(List3Sensors[i]);
|
||||
}
|
||||
l3Init = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList3ExportSensors() {
|
||||
if(l3eInit) return;
|
||||
for(uint8_t i = 0; i < List3ExportSensorCount; i++) {
|
||||
publishSensor(List3ExportSensors[i]);
|
||||
}
|
||||
l3eInit = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList4Sensors() {
|
||||
if(l4Init) return;
|
||||
for(uint8_t i = 0; i < List4SensorCount; i++) {
|
||||
publishSensor(List4Sensors[i]);
|
||||
}
|
||||
l4Init = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishList4ExportSensors() {
|
||||
if(l4eInit) return;
|
||||
for(uint8_t i = 0; i < List4ExportSensorCount; i++) {
|
||||
publishSensor(List4ExportSensors[i]);
|
||||
}
|
||||
l4eInit = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishRealtimeSensors(EnergyAccounting* ea, EntsoeApi* eapi) {
|
||||
if(rtInit) return;
|
||||
for(uint8_t i = 0; i < RealtimeSensorCount; i++) {
|
||||
HomeAssistantSensor sensor = RealtimeSensors[i];
|
||||
if(strncmp(sensor.devcl, "monetary", 8) == 0) {
|
||||
if(eapi == NULL) continue;
|
||||
sensor.uom = eapi->getCurrency();
|
||||
}
|
||||
publishSensor(sensor);
|
||||
}
|
||||
uint8_t peakCount = ea->getConfig()->hours;
|
||||
if(peakCount > 5) peakCount = 5;
|
||||
for(uint8_t i = 0; i < peakCount; i++) {
|
||||
char name[strlen(RealtimePeakSensor.name)];
|
||||
snprintf(name, strlen(RealtimePeakSensor.name), RealtimePeakSensor.name, i+1);
|
||||
char path[strlen(RealtimePeakSensor.path)];
|
||||
snprintf(path, strlen(RealtimePeakSensor.path), RealtimePeakSensor.path, i+1);
|
||||
HomeAssistantSensor sensor = {
|
||||
name,
|
||||
RealtimePeakSensor.topic,
|
||||
path,
|
||||
RealtimePeakSensor.uom,
|
||||
RealtimePeakSensor.devcl,
|
||||
RealtimePeakSensor.stacl
|
||||
};
|
||||
publishSensor(sensor);
|
||||
}
|
||||
rtInit = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishRealtimeExportSensors(EnergyAccounting* ea, EntsoeApi* eapi) {
|
||||
if(rteInit) return;
|
||||
for(uint8_t i = 0; i < RealtimeExportSensorCount; i++) {
|
||||
HomeAssistantSensor sensor = RealtimeExportSensors[i];
|
||||
if(strncmp(sensor.devcl, "monetary", 8) == 0) {
|
||||
if(eapi == NULL) continue;
|
||||
sensor.uom = eapi->getCurrency();
|
||||
}
|
||||
publishSensor(sensor);
|
||||
}
|
||||
rteInit = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishTemperatureSensor(uint8_t index, String id) {
|
||||
if(index > 32) return;
|
||||
if(tInit[index]) return;
|
||||
char name[strlen(TemperatureSensor.name)+id.length()];
|
||||
snprintf(name, strlen(TemperatureSensor.name)+id.length(), TemperatureSensor.name, id.c_str());
|
||||
|
||||
char path[strlen(TemperatureSensor.path)+id.length()];
|
||||
if(index == 0) {
|
||||
memcpy_P(path, PSTR("temp\0"), 5);
|
||||
} else {
|
||||
snprintf(path, strlen(TemperatureSensor.path)+id.length(), TemperatureSensor.path, id.c_str());
|
||||
}
|
||||
HomeAssistantSensor sensor = {
|
||||
name,
|
||||
index == 0 ? SystemSensors[0].topic : TemperatureSensor.topic,
|
||||
path,
|
||||
TemperatureSensor.uom,
|
||||
TemperatureSensor.devcl,
|
||||
TemperatureSensor.stacl
|
||||
};
|
||||
publishSensor(sensor);
|
||||
tInit[index] = true;
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishPriceSensors(EntsoeApi* eapi) {
|
||||
if(eapi == NULL) return;
|
||||
String uom = String(eapi->getCurrency()) + "/kWh";
|
||||
|
||||
if(!pInit) {
|
||||
for(uint8_t i = 0; i < PriceSensorCount; i++) {
|
||||
HomeAssistantSensor sensor = PriceSensors[i];
|
||||
if(strncmp(sensor.devcl, "monetary", 8) == 0) {
|
||||
sensor.uom = uom.c_str();
|
||||
}
|
||||
publishSensor(sensor);
|
||||
}
|
||||
pInit = true;
|
||||
}
|
||||
for(uint8_t i = 0; i < 38; i++) {
|
||||
if(prInit[i]) continue;
|
||||
float val = eapi->getValueForHour(i);
|
||||
if(val == ENTSOE_NO_VALUE) continue;
|
||||
|
||||
char name[strlen(PriceSensor.name)+2];
|
||||
snprintf(name, strlen(PriceSensor.name)+2, PriceSensor.name, i, i == 1 ? "hour" : "hours");
|
||||
char path[strlen(PriceSensor.path)+1];
|
||||
snprintf(path, strlen(PriceSensor.path)+1, PriceSensor.path, i);
|
||||
HomeAssistantSensor sensor = {
|
||||
i == 0 ? "Price current hour" : name,
|
||||
PriceSensor.topic,
|
||||
path,
|
||||
uom.c_str(),
|
||||
PriceSensor.devcl,
|
||||
PriceSensor.stacl
|
||||
};
|
||||
publishSensor(sensor);
|
||||
prInit[i] = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HomeAssistantMqttHandler::publishSystemSensors() {
|
||||
if(sInit) return;
|
||||
for(uint8_t i = 0; i < SystemSensorCount; i++) {
|
||||
publishSensor(SystemSensors[i]);
|
||||
}
|
||||
sInit = true;
|
||||
}
|
||||
Reference in New Issue
Block a user