fix: adapt Home Assistant discovery to v9

This commit is contained in:
Yurii committed 2026-11-25 22:23:59 +03:00
1 parent e56dd1de11
commit 72a8cd0eb1
4 files changed
+36 -21

No files matched your search

+2 -1
View File
@@ -7,6 +7,7 @@
#include <esp_log.h>
#include <functional>
#include <initializer_list>
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
@@ -123,7 +124,7 @@ std::string mqttTopic(const Parts&... parts) {
}
// Home Assistant discovery
extern HA::Discovery haDiscovery;
extern std::unique_ptr<HA::Discovery> haDiscovery;
bool publishHaEntity(HaEntity entityId, bool remove);
void publishDynamicSensor(
const Sensors::Settings& sensor,
+1 -2
View File
@@ -12,8 +12,7 @@ lib_deps = ESP32Async/AsyncTCP@^3.5.0
bblanchon/ArduinoJson@^7.4.3
https://github.com/Laxilef/ESP32OT
https://github.com/mathieucarbou/MycilaMQTT
;https://github.com/mathieucarbou/MycilaHADiscovery
https://github.com/Laxilef/MycilaHADiscovery#feature/discovery-publication-results
https://github.com/mathieucarbou/MycilaHADiscovery
gyverlibs/FileData@^1.0.3
gyverlibs/GyverPID@^3.3.2
gyverlibs/GyverBlinker@^1.1.1
+11 -11
View File
@@ -2,7 +2,7 @@
#include <algorithm>
HA::Discovery haDiscovery;
std::unique_ptr<HA::Discovery> haDiscovery;
bool publishHaEntity(HaEntity entityId, bool remove) {
std::unique_ptr<HA::Component> entity;
@@ -842,10 +842,10 @@ bool publishHaEntity(HaEntity entityId, bool remove) {
}
if (remove) {
return haDiscovery.unpublish(std::move(entity));
return haDiscovery->unpublish(std::move(entity)) == HA::Error::SUCCESS;
} 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);
}
haDiscovery.publish(std::move(entity));
haDiscovery->publish(std::move(entity));
}
void publishDynamicManualSensor(const Sensors::Settings& sensor, bool enabledByDefault) {
@@ -1136,7 +1136,7 @@ void publishDynamicManualSensor(const Sensors::Settings& sensor, bool enabledByD
break;
}
haDiscovery.publish(std::move(entity));
haDiscovery->publish(std::move(entity));
}
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) {
haDiscovery.unpublish<HA::Number>(std::move(id), "");
haDiscovery->unpublish<HA::Number>(std::move(id), "");
return;
}
haDiscovery.unpublish<HA::Gauge>(std::move(id), "");
haDiscovery->unpublish<HA::Gauge>(std::move(id), "");
}
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->enabledByDefault = enabledByDefault;
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
haDiscovery.publish(std::move(entity));
haDiscovery->publish(std::move(entity));
}
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) {
@@ -1181,9 +1181,9 @@ void publishSignalQualityDynamicSensor(const Sensors::Settings& sensor, bool ena
entity->icon = "mdi:signal";
entity->enabledByDefault = enabledByDefault;
entity->expireAfter = 2u * MQTT_SENSOR_REPUBLISH_INTERVAL / 1000u;
haDiscovery.publish(std::move(entity));
haDiscovery->publish(std::move(entity));
}
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
View File
@@ -13,6 +13,7 @@
#include <cstring>
#include <initializer_list>
#include <list>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
@@ -493,8 +494,8 @@ void stopMqtt(bool destroy = false) {
if (mqttClient) {
mqttClient->end();
}
mqttConnectedAt.store(0);
haDiscovery.end();
mqttHaPendingEntities.reset();
mqttHaPendingSensors.reset();
@@ -532,6 +533,7 @@ void stopMqtt(bool destroy = false) {
mqttPublishHaTask = nullptr;
}
haDiscovery.reset();
delete mqttClient;
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()) {
mqttClient->unsubscribe(topic.c_str());
}
@@ -718,10 +725,9 @@ static void connect() {
subscribe();
}
haDiscovery.setDiscoveryTopic(settings.mqtt.homeAssistantDiscoveryPrefix);
haDiscovery.setWillTopic("~/status");
haDiscovery.begin(
{
// Recreate discovery to apply the current device metadata and MQTT prefix.
haDiscovery = std::make_unique<HA::Discovery>(
HA::Device{
.id = mqttPrefix,
.name = PROJECT_NAME,
.version = BUILD_VERSION,
@@ -731,9 +737,15 @@ static void connect() {
},
mqttPrefix,
[](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({
.server = settings.mqtt.server,
.port = settings.mqtt.port,
@@ -1101,7 +1113,10 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) {
mqttPublishSensorsTask = task;
}
// Refresh settings every ten configured intervals to reduce repeated configuration traffic.
mqttPublishSettingsTask->setInterval(settings.mqtt.interval * 10000u);
// Publish state at the configured interval (seconds converted to milliseconds).
mqttPublishStateTask->setInterval(settings.mqtt.interval * 1000u);
// Call end() to reset the connection