mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-26 08:13:06 +05:00
fix: adapt Home Assistant discovery to v9
This commit is contained in:
1 parent
e56dd1de11
commit
72a8cd0eb1
4 files changed
+36
-21
No files matched your search
+2
-1
@@ -7,6 +7,7 @@
|
|||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@@ -123,7 +124,7 @@ std::string mqttTopic(const Parts&... parts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Home Assistant discovery
|
// Home Assistant discovery
|
||||||
extern HA::Discovery haDiscovery;
|
extern std::unique_ptr<HA::Discovery> haDiscovery;
|
||||||
bool publishHaEntity(HaEntity entityId, bool remove);
|
bool publishHaEntity(HaEntity entityId, bool remove);
|
||||||
void publishDynamicSensor(
|
void publishDynamicSensor(
|
||||||
const Sensors::Settings& sensor,
|
const Sensors::Settings& sensor,
|
||||||
|
|||||||
+1
-2
@@ -12,8 +12,7 @@ lib_deps = ESP32Async/AsyncTCP@^3.5.0
|
|||||||
bblanchon/ArduinoJson@^7.4.3
|
bblanchon/ArduinoJson@^7.4.3
|
||||||
https://github.com/Laxilef/ESP32OT
|
https://github.com/Laxilef/ESP32OT
|
||||||
https://github.com/mathieucarbou/MycilaMQTT
|
https://github.com/mathieucarbou/MycilaMQTT
|
||||||
;https://github.com/mathieucarbou/MycilaHADiscovery
|
https://github.com/mathieucarbou/MycilaHADiscovery
|
||||||
https://github.com/Laxilef/MycilaHADiscovery#feature/discovery-publication-results
|
|
||||||
gyverlibs/FileData@^1.0.3
|
gyverlibs/FileData@^1.0.3
|
||||||
gyverlibs/GyverPID@^3.3.2
|
gyverlibs/GyverPID@^3.3.2
|
||||||
gyverlibs/GyverBlinker@^1.1.1
|
gyverlibs/GyverBlinker@^1.1.1
|
||||||
|
|||||||
+11
-11
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
HA::Discovery haDiscovery;
|
std::unique_ptr<HA::Discovery> haDiscovery;
|
||||||
|
|
||||||
bool publishHaEntity(HaEntity entityId, bool remove) {
|
bool publishHaEntity(HaEntity entityId, bool remove) {
|
||||||
std::unique_ptr<HA::Component> entity;
|
std::unique_ptr<HA::Component> entity;
|
||||||
@@ -842,10 +842,10 @@ bool publishHaEntity(HaEntity entityId, bool remove) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (remove) {
|
if (remove) {
|
||||||
return haDiscovery.unpublish(std::move(entity));
|
return haDiscovery->unpublish(std::move(entity)) == HA::Error::SUCCESS;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return haDiscovery.publish(std::move(entity));
|
return haDiscovery->publish(std::move(entity)) == HA::Error::SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1044,7 +1044,7 @@ void publishDynamicSensor(const Sensors::Settings& sensor, Sensors::ValueType va
|
|||||||
entity->stateTopic = mqttTopic(S_SENSORS, sensor.slug, S_VALUE);
|
entity->stateTopic = mqttTopic(S_SENSORS, sensor.slug, S_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
haDiscovery.publish(std::move(entity));
|
haDiscovery->publish(std::move(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishDynamicManualSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
void publishDynamicManualSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
||||||
@@ -1136,7 +1136,7 @@ void publishDynamicManualSensor(const Sensors::Settings& sensor, bool enabledByD
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
haDiscovery.publish(std::move(entity));
|
haDiscovery->publish(std::move(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteDynamicSensor(const Sensors::Settings& sensor, Sensors::ValueType valueType) {
|
void deleteDynamicSensor(const Sensors::Settings& sensor, Sensors::ValueType valueType) {
|
||||||
@@ -1146,11 +1146,11 @@ void deleteDynamicSensor(const Sensors::Settings& sensor, Sensors::ValueType val
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sensor.type == Sensors::Type::MANUAL) {
|
if (sensor.type == Sensors::Type::MANUAL) {
|
||||||
haDiscovery.unpublish<HA::Number>(std::move(id), "");
|
haDiscovery->unpublish<HA::Number>(std::move(id), "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
haDiscovery.unpublish<HA::Gauge>(std::move(id), "");
|
haDiscovery->unpublish<HA::Gauge>(std::move(id), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishConnectionDynamicSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
void publishConnectionDynamicSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
||||||
@@ -1164,11 +1164,11 @@ void publishConnectionDynamicSensor(const Sensors::Settings& sensor, bool enable
|
|||||||
entity->payloadOff = S_FALSE;
|
entity->payloadOff = S_FALSE;
|
||||||
entity->enabledByDefault = enabledByDefault;
|
entity->enabledByDefault = enabledByDefault;
|
||||||
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
|
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
|
||||||
haDiscovery.publish(std::move(entity));
|
haDiscovery->publish(std::move(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteConnectionDynamicSensor(const Sensors::Settings& sensor) {
|
void deleteConnectionDynamicSensor(const Sensors::Settings& sensor) {
|
||||||
haDiscovery.unpublish<HA::State>(std::string(sensor.slug) + "_connected", "");
|
haDiscovery->unpublish<HA::State>(std::string(sensor.slug) + "_connected", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishSignalQualityDynamicSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
void publishSignalQualityDynamicSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
|
||||||
@@ -1181,9 +1181,9 @@ void publishSignalQualityDynamicSensor(const Sensors::Settings& sensor, bool ena
|
|||||||
entity->icon = "mdi:signal";
|
entity->icon = "mdi:signal";
|
||||||
entity->enabledByDefault = enabledByDefault;
|
entity->enabledByDefault = enabledByDefault;
|
||||||
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
|
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
|
||||||
haDiscovery.publish(std::move(entity));
|
haDiscovery->publish(std::move(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteSignalQualityDynamicSensor(const Sensors::Settings& sensor) {
|
void deleteSignalQualityDynamicSensor(const Sensors::Settings& sensor) {
|
||||||
haDiscovery.unpublish<HA::Gauge>(std::string(sensor.slug) + "_signal_quality", "");
|
haDiscovery->unpublish<HA::Gauge>(std::string(sensor.slug) + "_signal_quality", "");
|
||||||
}
|
}
|
||||||
+22
-7
@@ -13,6 +13,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@@ -493,8 +494,8 @@ void stopMqtt(bool destroy = false) {
|
|||||||
if (mqttClient) {
|
if (mqttClient) {
|
||||||
mqttClient->end();
|
mqttClient->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
mqttConnectedAt.store(0);
|
mqttConnectedAt.store(0);
|
||||||
haDiscovery.end();
|
|
||||||
mqttHaPendingEntities.reset();
|
mqttHaPendingEntities.reset();
|
||||||
mqttHaPendingSensors.reset();
|
mqttHaPendingSensors.reset();
|
||||||
|
|
||||||
@@ -532,6 +533,7 @@ void stopMqtt(bool destroy = false) {
|
|||||||
mqttPublishHaTask = nullptr;
|
mqttPublishHaTask = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
haDiscovery.reset();
|
||||||
delete mqttClient;
|
delete mqttClient;
|
||||||
mqttClient = nullptr;
|
mqttClient = nullptr;
|
||||||
|
|
||||||
@@ -709,7 +711,12 @@ static void connect() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mqttPrefix != settings.mqtt.prefix || haDiscovery.getDiscoveryTopic() != settings.mqtt.homeAssistantDiscoveryPrefix) {
|
// Compare the applied prefixes before recreating discovery.
|
||||||
|
const bool resubscribe = mqttPrefix != settings.mqtt.prefix || !haDiscovery ||
|
||||||
|
haDiscovery->getDiscoveryTopic() != settings.mqtt.homeAssistantDiscoveryPrefix;
|
||||||
|
|
||||||
|
// Update the local subscriptions; MQTT applies them when connected.
|
||||||
|
if (resubscribe) {
|
||||||
for (const auto& topic : mqttClient->getSubscribedTopics()) {
|
for (const auto& topic : mqttClient->getSubscribedTopics()) {
|
||||||
mqttClient->unsubscribe(topic.c_str());
|
mqttClient->unsubscribe(topic.c_str());
|
||||||
}
|
}
|
||||||
@@ -718,10 +725,9 @@ static void connect() {
|
|||||||
subscribe();
|
subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
haDiscovery.setDiscoveryTopic(settings.mqtt.homeAssistantDiscoveryPrefix);
|
// Recreate discovery to apply the current device metadata and MQTT prefix.
|
||||||
haDiscovery.setWillTopic("~/status");
|
haDiscovery = std::make_unique<HA::Discovery>(
|
||||||
haDiscovery.begin(
|
HA::Device{
|
||||||
{
|
|
||||||
.id = mqttPrefix,
|
.id = mqttPrefix,
|
||||||
.name = PROJECT_NAME,
|
.name = PROJECT_NAME,
|
||||||
.version = BUILD_VERSION,
|
.version = BUILD_VERSION,
|
||||||
@@ -731,9 +737,15 @@ static void connect() {
|
|||||||
},
|
},
|
||||||
mqttPrefix,
|
mqttPrefix,
|
||||||
[](const char* topic, const std::string& payload) {
|
[](const char* topic, const std::string& payload) {
|
||||||
return mqttClient->publish(topic, payload, true);
|
return mqttClient->publish(topic, payload, true)
|
||||||
|
? HA::Error::SUCCESS
|
||||||
|
: HA::Error::ERR_MQTT_PUBLISHER;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
haDiscovery->setDiscoveryTopic(settings.mqtt.homeAssistantDiscoveryPrefix);
|
||||||
|
haDiscovery->setWillTopic("~/status");
|
||||||
|
|
||||||
|
// Start the connection after configuring discovery and subscriptions.
|
||||||
mqttClient->begin({
|
mqttClient->begin({
|
||||||
.server = settings.mqtt.server,
|
.server = settings.mqtt.server,
|
||||||
.port = settings.mqtt.port,
|
.port = settings.mqtt.port,
|
||||||
@@ -1101,7 +1113,10 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) {
|
|||||||
mqttPublishSensorsTask = task;
|
mqttPublishSensorsTask = task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Refresh settings every ten configured intervals to reduce repeated configuration traffic.
|
||||||
mqttPublishSettingsTask->setInterval(settings.mqtt.interval * 10000u);
|
mqttPublishSettingsTask->setInterval(settings.mqtt.interval * 10000u);
|
||||||
|
|
||||||
|
// Publish state at the configured interval (seconds converted to milliseconds).
|
||||||
mqttPublishStateTask->setInterval(settings.mqtt.interval * 1000u);
|
mqttPublishStateTask->setInterval(settings.mqtt.interval * 1000u);
|
||||||
|
|
||||||
// Call end() to reset the connection
|
// Call end() to reset the connection
|
||||||
|
|||||||
Reference in new issue
Block a user