ESPHome 2026.2.1
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::mqtt {
12
13static const char *const TAG = "mqtt.datetime.datetime";
14
15using namespace esphome::datetime;
16
18
20 this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
21 auto call = this->datetime_->make_call();
22 if (root[ESPHOME_F("year")].is<uint16_t>()) {
23 call.set_year(root[ESPHOME_F("year")]);
24 }
25 if (root[ESPHOME_F("month")].is<uint8_t>()) {
26 call.set_month(root[ESPHOME_F("month")]);
27 }
28 if (root[ESPHOME_F("day")].is<uint8_t>()) {
29 call.set_day(root[ESPHOME_F("day")]);
30 }
31 if (root[ESPHOME_F("hour")].is<uint8_t>()) {
32 call.set_hour(root[ESPHOME_F("hour")]);
33 }
34 if (root[ESPHOME_F("minute")].is<uint8_t>()) {
35 call.set_minute(root[ESPHOME_F("minute")]);
36 }
37 if (root[ESPHOME_F("second")].is<uint8_t>()) {
38 call.set_second(root[ESPHOME_F("second")]);
39 }
40 call.perform();
41 });
42 this->datetime_->add_on_state_callback([this]() {
43 this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day, this->datetime_->hour,
44 this->datetime_->minute, this->datetime_->second);
45 });
46}
47
49 ESP_LOGCONFIG(TAG, "MQTT DateTime '%s':", this->datetime_->get_name().c_str());
50 LOG_MQTT_COMPONENT(true, true);
51}
52
54const EntityBase *MQTTDateTimeComponent::get_entity() const { return this->datetime_; }
55
57 // Nothing extra to add here
58}
60 if (this->datetime_->has_state()) {
61 return this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day,
62 this->datetime_->hour, this->datetime_->minute, this->datetime_->second);
63 } else {
64 return true;
65 }
66}
67bool MQTTDateTimeComponent::publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
68 uint8_t second) {
69 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
70 return this->publish_json(this->get_state_topic_to_(topic_buf),
71 [year, month, day, hour, minute, second](JsonObject root) {
72 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
73 root[ESPHOME_F("year")] = year;
74 root[ESPHOME_F("month")] = month;
75 root[ESPHOME_F("day")] = day;
76 root[ESPHOME_F("hour")] = hour;
77 root[ESPHOME_F("minute")] = minute;
78 root[ESPHOME_F("second")] = second;
79 });
80}
81
82} // namespace esphome::mqtt
83
84#endif // USE_DATETIME_DATETIME
85#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
void add_on_state_callback(std::function< void()> &&callback)
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.
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).
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
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.
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
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().