mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-03-05 11:14:00 +00:00
Fixed config reload for HA and Domoticz if changed
This commit is contained in:
@@ -31,6 +31,10 @@ public:
|
||||
|
||||
uint8_t getFormat();
|
||||
|
||||
void setDomoticzConfig(DomoticzConfig config) {
|
||||
this->config = config;
|
||||
}
|
||||
|
||||
private:
|
||||
DomoticzConfig config;
|
||||
double energy = 0.0;
|
||||
|
||||
@@ -19,57 +19,9 @@ public:
|
||||
#else
|
||||
HomeAssistantMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) {
|
||||
#endif
|
||||
this->boardType = boardType;
|
||||
this->hw = hw;
|
||||
|
||||
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false;
|
||||
|
||||
topic = String(mqttConfig.publishTopic);
|
||||
|
||||
if(strlen(config.discoveryNameTag) > 0) {
|
||||
snprintf_P(buf, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag);
|
||||
deviceName = String(buf);
|
||||
snprintf_P(buf, 128, PSTR("[%s] "), config.discoveryNameTag);
|
||||
sensorNamePrefix = String(buf);
|
||||
} else {
|
||||
deviceName = F("AMS reader");
|
||||
sensorNamePrefix = "";
|
||||
}
|
||||
deviceModel = boardTypeToString(boardType);
|
||||
manufacturer = boardManufacturerToString(boardType);
|
||||
|
||||
char hostname[32];
|
||||
#if defined(ESP8266)
|
||||
strcpy(hostname, WiFi.hostname().c_str());
|
||||
#elif defined(ESP32)
|
||||
strcpy(hostname, WiFi.getHostname());
|
||||
#endif
|
||||
|
||||
stripNonAscii((uint8_t*) hostname, 32, false);
|
||||
deviceUid = String(hostname); // Maybe configurable in the future?
|
||||
|
||||
if(strlen(config.discoveryHostname) > 0) {
|
||||
if(strncmp_P(config.discoveryHostname, PSTR("http"), 4) == 0) {
|
||||
deviceUrl = String(config.discoveryHostname);
|
||||
} else {
|
||||
snprintf_P(buf, 128, PSTR("http://%s/"), config.discoveryHostname);
|
||||
deviceUrl = String(buf);
|
||||
}
|
||||
} else {
|
||||
snprintf_P(buf, 128, PSTR("http://%s.local/"), hostname);
|
||||
deviceUrl = String(buf);
|
||||
}
|
||||
|
||||
if(strlen(config.discoveryPrefix) > 0) {
|
||||
snprintf_P(json, 128, PSTR("%s/status"), config.discoveryPrefix);
|
||||
statusTopic = String(buf);
|
||||
|
||||
snprintf_P(buf, 128, PSTR("%s/sensor/"), config.discoveryPrefix);
|
||||
discoveryTopic = String(buf);
|
||||
} else {
|
||||
statusTopic = F("homeassistant/status");
|
||||
discoveryTopic = F("homeassistant/sensor/");
|
||||
}
|
||||
strcpy(this->mqttConfig.subscribeTopic, statusTopic.c_str());
|
||||
setHomeAssistantConfig(config);
|
||||
};
|
||||
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps);
|
||||
bool publishTemperatures(AmsConfiguration*, HwTools*);
|
||||
@@ -81,7 +33,10 @@ public:
|
||||
|
||||
uint8_t getFormat();
|
||||
|
||||
void setHomeAssistantConfig(HomeAssistantConfig config);
|
||||
private:
|
||||
uint8_t boardType;
|
||||
|
||||
String topic;
|
||||
|
||||
String deviceName;
|
||||
|
||||
@@ -23,7 +23,7 @@ struct HomeAssistantSensor {
|
||||
const uint8_t List1SensorCount PROGMEM = 2;
|
||||
const HomeAssistantSensor List1Sensors[List1SensorCount] PROGMEM = {
|
||||
{"Active import", "/power", "P", 30, "W", "power", "measurement"},
|
||||
{"Data timestamp", "/power", "t", 30, "", "timestamp", ""}
|
||||
{"Data timestamp", "/power", "t", 30, "", "timestamp", ""}
|
||||
};
|
||||
|
||||
const uint8_t List2SensorCount PROGMEM = 8;
|
||||
|
||||
@@ -19,6 +19,58 @@
|
||||
#include <esp_task_wdt.h>
|
||||
#endif
|
||||
|
||||
void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config) {
|
||||
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false;
|
||||
|
||||
topic = String(mqttConfig.publishTopic);
|
||||
|
||||
if(strlen(config.discoveryNameTag) > 0) {
|
||||
snprintf_P(json, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag);
|
||||
deviceName = String(json);
|
||||
snprintf_P(json, 128, PSTR("[%s] "), config.discoveryNameTag);
|
||||
sensorNamePrefix = String(json);
|
||||
} else {
|
||||
deviceName = F("AMS reader");
|
||||
sensorNamePrefix = "";
|
||||
}
|
||||
deviceModel = boardTypeToString(boardType);
|
||||
manufacturer = boardManufacturerToString(boardType);
|
||||
|
||||
char hostname[32];
|
||||
#if defined(ESP8266)
|
||||
strcpy(hostname, WiFi.hostname().c_str());
|
||||
#elif defined(ESP32)
|
||||
strcpy(hostname, WiFi.getHostname());
|
||||
#endif
|
||||
|
||||
stripNonAscii((uint8_t*) hostname, 32, false);
|
||||
deviceUid = String(hostname); // Maybe configurable in the future?
|
||||
|
||||
if(strlen(config.discoveryHostname) > 0) {
|
||||
if(strncmp_P(config.discoveryHostname, PSTR("http"), 4) == 0) {
|
||||
deviceUrl = String(config.discoveryHostname);
|
||||
} else {
|
||||
snprintf_P(json, 128, PSTR("http://%s/"), config.discoveryHostname);
|
||||
deviceUrl = String(json);
|
||||
}
|
||||
} else {
|
||||
snprintf_P(json, 128, PSTR("http://%s.local/"), hostname);
|
||||
deviceUrl = String(json);
|
||||
}
|
||||
|
||||
if(strlen(config.discoveryPrefix) > 0) {
|
||||
snprintf_P(json, 128, PSTR("%s/status"), config.discoveryPrefix);
|
||||
statusTopic = String(json);
|
||||
|
||||
snprintf_P(json, 128, PSTR("%s/sensor/"), config.discoveryPrefix);
|
||||
discoveryTopic = String(json);
|
||||
} else {
|
||||
statusTopic = F("homeassistant/status");
|
||||
discoveryTopic = F("homeassistant/sensor/");
|
||||
}
|
||||
strcpy(this->mqttConfig.subscribeTopic, statusTopic.c_str());
|
||||
}
|
||||
|
||||
bool HomeAssistantMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
|
||||
if(topic.isEmpty() || !mqtt.connected())
|
||||
return false;
|
||||
|
||||
@@ -598,14 +598,10 @@ void loop() {
|
||||
if (mqttEnabled || config.isMqttChanged()) {
|
||||
if(mqttHandler == NULL || !mqttHandler->connected() || config.isMqttChanged()) {
|
||||
if(mqttHandler != NULL && config.isMqttChanged()) {
|
||||
MqttConfig mqttConfig;
|
||||
if(config.getMqttConfig(mqttConfig)) {
|
||||
mqttHandler->disconnect();
|
||||
mqttHandler->setConfig(mqttConfig);
|
||||
config.ackMqttChange();
|
||||
}
|
||||
mqttHandler->disconnect();
|
||||
}
|
||||
MQTT_connect();
|
||||
config.ackMqttChange();
|
||||
}
|
||||
} else if(mqttHandler != NULL) {
|
||||
mqttHandler->disconnect();
|
||||
@@ -1497,6 +1493,22 @@ void MQTT_connect() {
|
||||
mqttHandler = NULL;
|
||||
} else if(config.isMqttChanged()) {
|
||||
mqttHandler->setConfig(mqttConfig);
|
||||
switch(mqttConfig.payloadFormat) {
|
||||
case 3: {
|
||||
DomoticzConfig domo;
|
||||
config.getDomoticzConfig(domo);
|
||||
DomoticzMqttHandler* dmh = (DomoticzMqttHandler*) &mqttHandler;
|
||||
dmh->setDomoticzConfig(domo);
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
HomeAssistantConfig haconf;
|
||||
config.getHomeAssistantConfig(haconf);
|
||||
HomeAssistantMqttHandler* hamh = (HomeAssistantMqttHandler*) &mqttHandler;
|
||||
hamh->setHomeAssistantConfig(haconf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user