ESPHome 2025.5.0
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 {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.update";
13
14using namespace esphome::update;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20 if (payload == "INSTALL") {
21 this->update_->perform();
22 } else {
23 ESP_LOGW(TAG, "'%s': Received unknown update payload: %s", this->friendly_name().c_str(), payload.c_str());
24 this->status_momentary_warning("state", 5000);
25 }
26 });
27
28 this->update_->add_on_state_callback([this]() { this->defer("send", [this]() { this->publish_state(); }); });
29}
30
32 return this->publish_json(this->get_state_topic_(), [this](JsonObject root) {
33 root["installed_version"] = this->update_->update_info.current_version;
34 root["latest_version"] = this->update_->update_info.latest_version;
35 root["title"] = this->update_->update_info.title;
36 if (!this->update_->update_info.summary.empty())
37 root["release_summary"] = this->update_->update_info.summary;
38 if (!this->update_->update_info.release_url.empty())
39 root["release_url"] = this->update_->update_info.release_url;
40 });
41}
42
44 root["schema"] = "json";
45 root[MQTT_PAYLOAD_INSTALL] = "INSTALL";
46}
47
49
51 ESP_LOGCONFIG(TAG, "MQTT Update '%s': ", this->update_->get_name().c_str());
52 LOG_MQTT_COMPONENT(true, true);
53}
54
55std::string MQTTUpdateComponent::component_type() const { return "update"; }
56const EntityBase *MQTTUpdateComponent::get_entity() const { return this->update_; }
57
58} // namespace mqtt
59} // namespace esphome
60
61#endif // USE_UPDATE
62#endif // USE_MQTT
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTUpdateComponent(update::UpdateEntity *update)
std::string component_type() const override
"update" component type.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
update::UpdateEntity * update_
Definition mqtt_update.h:34
const EntityBase * get_entity() const override
void add_on_state_callback(std::function< void()> &&callback)
const UpdateInfo & update_info
constexpr const char *const MQTT_PAYLOAD_INSTALL
Definition mqtt_const.h:144
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().