ESPHome 2025.5.0
Loading...
Searching...
No Matches
mqtt_datetime.cpp
Go to the documentation of this file.
1#include "mqtt_datetime.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_DATETIME
10
11namespace esphome {
12namespace mqtt {
13
14static const char *const TAG = "mqtt.datetime.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->datetime_->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 if (root.containsKey("hour")) {
33 call.set_hour(root["hour"]);
34 }
35 if (root.containsKey("minute")) {
36 call.set_minute(root["minute"]);
37 }
38 if (root.containsKey("second")) {
39 call.set_second(root["second"]);
40 }
41 call.perform();
42 });
43 this->datetime_->add_on_state_callback([this]() {
44 this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day, this->datetime_->hour,
45 this->datetime_->minute, this->datetime_->second);
46 });
47}
48
50 ESP_LOGCONFIG(TAG, "MQTT DateTime '%s':", this->datetime_->get_name().c_str());
51 LOG_MQTT_COMPONENT(true, true)
52}
53
54std::string MQTTDateTimeComponent::component_type() const { return "datetime"; }
56
58 // Nothing extra to add here
59}
61 if (this->datetime_->has_state()) {
62 return this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day,
63 this->datetime_->hour, this->datetime_->minute, this->datetime_->second);
64 } else {
65 return true;
66 }
67}
68bool MQTTDateTimeComponent::publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
69 uint8_t second) {
70 return this->publish_json(this->get_state_topic_(), [year, month, day, hour, minute, second](JsonObject root) {
71 root["year"] = year;
72 root["month"] = month;
73 root["day"] = day;
74 root["hour"] = hour;
75 root["minute"] = minute;
76 root["second"] = second;
77 });
78}
79
80} // namespace mqtt
81} // namespace esphome
82
83#endif // USE_DATETIME_DATETIME
84#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
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.
std::string component_type() const override
bool publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
datetime::DateTimeEntity * datetime_
MQTTDateTimeComponent(datetime::DateTimeEntity *datetime)
Construct this MQTTDateTimeComponent instance with the provided friendly_name and time.
const EntityBase * get_entity() const override
void setup() override
Override setup.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
uint8_t second
uint8_t minute
uint8_t hour
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().