diff --git a/lib/AmsMqttHandler/include/AmsMqttHandler.h b/lib/AmsMqttHandler/include/AmsMqttHandler.h index a957a52f..0c9a87fd 100644 --- a/lib/AmsMqttHandler/include/AmsMqttHandler.h +++ b/lib/AmsMqttHandler/include/AmsMqttHandler.h @@ -37,6 +37,7 @@ public: virtual bool publishPrices(EntsoeApi* eapi) { return false; }; virtual bool publishSystem(HwTools*, EntsoeApi*, EnergyAccounting*) { return false; }; virtual bool publishRaw(String data) { return false; }; + virtual void onMessage(String &topic, String &payload) {}; virtual ~AmsMqttHandler() { if(mqttClient != NULL) { diff --git a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp index d91afc11..c489aa5f 100644 --- a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp +++ b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp @@ -119,7 +119,14 @@ bool AmsMqttHandler::connect() { // Connect to a unsecure or secure MQTT server if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) || (strlen(mqttConfig.username) > 0 && mqtt.connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) { - if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully connected to MQTT!\n")); + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully connected to MQTT\n")); + mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2)); + if(strlen(mqttConfig.subscribeTopic) > 0) { + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR(" Subscribing to [%s]\n"), mqttConfig.subscribeTopic); + if(!mqtt.subscribe(mqttConfig.subscribeTopic)) { + if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscribeTopic); + } + } return true; } else { if (debugger->isActive(RemoteDebug::ERROR)) { diff --git a/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h b/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h index 1e22f2c6..05e0fb79 100644 --- a/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h +++ b/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h @@ -15,6 +15,8 @@ public: bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea); bool publishRaw(String data); + void onMessage(String &topic, String &payload); + uint8_t getFormat(); private: diff --git a/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp b/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp index 53ebb509..eb1aaee4 100644 --- a/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp +++ b/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp @@ -82,3 +82,6 @@ uint8_t DomoticzMqttHandler::getFormat() { bool DomoticzMqttHandler::publishRaw(String data) { return false; } + +void DomoticzMqttHandler::onMessage(String &topic, String &payload) { +} diff --git a/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h b/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h index e51643d2..2a1751d8 100644 --- a/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h +++ b/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h @@ -9,6 +9,7 @@ class HomeAssistantMqttHandler : public AmsMqttHandler { public: HomeAssistantMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) { this->hw = hw; + l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = false; topic = String(mqttConfig.publishTopic); @@ -46,18 +47,26 @@ public: } 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()); }; + bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi); bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishPrices(EntsoeApi*); bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea); bool publishRaw(String data); + void onMessage(String &topic, String &payload); + uint8_t getFormat(); private: @@ -69,6 +78,7 @@ private: String manufacturer; String deviceUrl; + String statusTopic; String discoveryTopic; String sensorNamePrefix; diff --git a/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp index c3475f05..4e8b409c 100644 --- a/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp +++ b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp @@ -547,3 +547,12 @@ uint8_t HomeAssistantMqttHandler::getFormat() { bool HomeAssistantMqttHandler::publishRaw(String data) { return false; } + +void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) { + if(topic.equals(statusTopic)) { + if(payload.equals("online")) { + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n")); + l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = false; + } + } +} diff --git a/lib/JsonMqttHandler/include/JsonMqttHandler.h b/lib/JsonMqttHandler/include/JsonMqttHandler.h index 124ad12f..4b475e71 100644 --- a/lib/JsonMqttHandler/include/JsonMqttHandler.h +++ b/lib/JsonMqttHandler/include/JsonMqttHandler.h @@ -14,6 +14,8 @@ public: bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea); bool publishRaw(String data); + void onMessage(String &topic, String &payload); + uint8_t getFormat(); private: diff --git a/lib/JsonMqttHandler/src/JsonMqttHandler.cpp b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp index f8b3b848..fbe38319 100644 --- a/lib/JsonMqttHandler/src/JsonMqttHandler.cpp +++ b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp @@ -362,3 +362,6 @@ uint8_t JsonMqttHandler::getFormat() { bool JsonMqttHandler::publishRaw(String data) { return false; } + +void JsonMqttHandler::onMessage(String &topic, String &payload) { +} diff --git a/lib/RawMqttHandler/include/RawMqttHandler.h b/lib/RawMqttHandler/include/RawMqttHandler.h index bf20394e..2f2bffe9 100644 --- a/lib/RawMqttHandler/include/RawMqttHandler.h +++ b/lib/RawMqttHandler/include/RawMqttHandler.h @@ -15,6 +15,8 @@ public: bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea); bool publishRaw(String data); + void onMessage(String &topic, String &payload); + uint8_t getFormat(); private: diff --git a/lib/RawMqttHandler/src/RawMqttHandler.cpp b/lib/RawMqttHandler/src/RawMqttHandler.cpp index 269ee774..933f106c 100644 --- a/lib/RawMqttHandler/src/RawMqttHandler.cpp +++ b/lib/RawMqttHandler/src/RawMqttHandler.cpp @@ -287,3 +287,6 @@ uint8_t RawMqttHandler::getFormat() { bool RawMqttHandler::publishRaw(String data) { return false; } + +void RawMqttHandler::onMessage(String &topic, String &payload) { +} diff --git a/src/PassthroughMqttHandler.cpp b/src/PassthroughMqttHandler.cpp index d78b786f..c89a1fe2 100644 --- a/src/PassthroughMqttHandler.cpp +++ b/src/PassthroughMqttHandler.cpp @@ -25,4 +25,7 @@ bool PassthroughMqttHandler::publishRaw(String data) { uint8_t PassthroughMqttHandler::getFormat() { return 255; -} \ No newline at end of file +} + +void PassthroughMqttHandler::onMessage(String &topic, String &payload) { +} diff --git a/src/PassthroughMqttHandler.h b/src/PassthroughMqttHandler.h index 188548ed..082582dc 100644 --- a/src/PassthroughMqttHandler.h +++ b/src/PassthroughMqttHandler.h @@ -12,6 +12,8 @@ public: bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea); bool publishRaw(String data); + void onMessage(String &topic, String &payload); + uint8_t getFormat(); }; #endif