ESPHome 2026.2.4
Loading...
Searching...
No Matches
mqtt_update.cpp
Go to the documentation of this file.
1#include "mqtt_update.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_UPDATE
8
9namespace esphome::mqtt {
10
11static const char *const TAG = "mqtt.update";
12
13using namespace esphome::update;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
19 if (payload == "INSTALL") {
20 this->update_->perform();
21 } else {
22 ESP_LOGW(TAG, "'%s': Received unknown update payload: %s", this->friendly_name_().c_str(), payload.c_str());
23 this->status_momentary_warning("state", 5000);
24 }
25 });
26
27 this->update_->add_on_state_callback([this]() { this->defer("send", [this]() { this->publish_state(); }); });
28}
29
31 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
32 return this->publish_json(this->get_state_topic_to_(topic_buf), [this](JsonObject root) {
33 root[ESPHOME_F("installed_version")] = this->update_->update_info.current_version;
34 root[ESPHOME_F("latest_version")] = this->update_->update_info.latest_version;
35 root[ESPHOME_F("title")] = this->update_->update_info.title;
36 if (!this->update_->update_info.summary.empty())
37 root[ESPHOME_F("release_summary")] = this->update_->update_info.summary;
38 if (!this->update_->update_info.release_url.empty())
39 root[ESPHOME_F("release_url")] = this->update_->update_info.release_url;
40 });
41}
42
44 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
45 root[ESPHOME_F("schema")] = ESPHOME_F("json");
46 root[MQTT_PAYLOAD_INSTALL] = ESPHOME_F("INSTALL");
47}
48
50
52 ESP_LOGCONFIG(TAG, "MQTT Update '%s': ", this->update_->get_name().c_str());
53 LOG_MQTT_COMPONENT(true, true);
54}
55
57const EntityBase *MQTTUpdateComponent::get_entity() const { return this->update_; }
58
59} // namespace esphome::mqtt
60
61#endif // USE_UPDATE
62#endif // USE_MQTT
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:479
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
const StringRef & friendly_name_() const
Get the friendly name of this MQTT component.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTUpdateComponent(update::UpdateEntity *update)
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
update::UpdateEntity * update_
Definition mqtt_update.h:33
void add_on_state_callback(std::function< void()> &&callback)
const UpdateInfo & update_info
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().