ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
mqtt_date.cpp
Go to the documentation of this file.
1#include "mqtt_date.h"
2
3#include <utility>
4#include "esphome/core/log.h"
5
6#include "mqtt_const.h"
7
8#ifdef USE_MQTT
9#ifdef USE_DATETIME_DATE
10
11namespace esphome {
12namespace mqtt {
13
14static const char *const TAG = "mqtt.datetime";
15
16using namespace esphome::datetime;
17
19
21 this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
22 auto call = this->date_->make_call();
23 if (root.containsKey("year")) {
24 call.set_year(root["year"]);
25 }
26 if (root.containsKey("month")) {
27 call.set_month(root["month"]);
28 }
29 if (root.containsKey("day")) {
30 call.set_day(root["day"]);
31 }
32 call.perform();
33 });
35 [this]() { this->publish_state(this->date_->year, this->date_->month, this->date_->day); });
36}
37
39 ESP_LOGCONFIG(TAG, "MQTT Date '%s':", this->date_->get_name().c_str());
40 LOG_MQTT_COMPONENT(true, true)
41}
42
43std::string MQTTDateComponent::component_type() const { return "date"; }
44const EntityBase *MQTTDateComponent::get_entity() const { return this->date_; }
45
47 // Nothing extra to add here
48}
50 if (this->date_->has_state()) {
51 return this->publish_state(this->date_->year, this->date_->month, this->date_->day);
52 } else {
53 return true;
54 }
55}
56bool MQTTDateComponent::publish_state(uint16_t year, uint8_t month, uint8_t day) {
57 return this->publish_json(this->get_state_topic_(), [year, month, day](JsonObject root) {
58 root["year"] = year;
59 root["month"] = month;
60 root["day"] = day;
61 });
62}
63
64} // namespace mqtt
65} // namespace esphome
66
67#endif // USE_DATETIME_DATE
68#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
void add_on_state_callback(std::function< void()> &&callback)
bool has_state() const
Return whether this Datetime has gotten a full state yet.
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
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.
MQTTDateComponent(datetime::DateEntity *date)
Construct this MQTTDateComponent instance with the provided friendly_name and date.
Definition mqtt_date.cpp:18
const EntityBase * get_entity() const override
Definition mqtt_date.cpp:44
datetime::DateEntity * date_
Definition mqtt_date.h:38
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition mqtt_date.cpp:46
bool publish_state(uint16_t year, uint8_t month, uint8_t day)
Definition mqtt_date.cpp:56
void setup() override
Override setup.
Definition mqtt_date.cpp:20
std::string component_type() const override
Definition mqtt_date.cpp:43
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
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().