ESPHome 2026.11.0b6
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
13ESPHOME_LOG_TAG(TAG, "mqtt.datetime.datetime");
14
15using namespace esphome::datetime;
16
18
20 this->subscribe_json(this->command_topic_source_(), [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
48void MQTTDateTimeComponent::dump_config() { LOG_MQTT_COMPONENT(true, true); }
49
51const EntityBase *MQTTDateTimeComponent::get_entity() const { return this->datetime_; }
52
54 // Home Assistant requires timezone-aware ISO datetimes; the device works in local time, which is
55 // assumed to match the Home Assistant time zone
56 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
57 root[MQTT_VALUE_TEMPLATE] =
58 ESPHOME_F("{{ ('%04d-%02d-%02dT%02d:%02d:%02d' | format(value_json.year, value_json.month, value_json.day, "
59 "value_json.hour, value_json.minute, value_json.second)) | as_datetime | as_local }}");
60 root[MQTT_COMMAND_TEMPLATE] =
61 ESPHOME_F("{% set v = value | as_local %}{\"year\":{{ v.year }},\"month\":{{ v.month }},\"day\":{{ v.day }},"
62 "\"hour\":{{ v.hour }},\"minute\":{{ v.minute }},\"second\":{{ v.second }}}");
63}
65 if (this->datetime_->has_state()) {
66 return this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day,
67 this->datetime_->hour, this->datetime_->minute, this->datetime_->second);
68 } else {
69 return true;
70 }
71}
72bool MQTTDateTimeComponent::publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
73 uint8_t second) {
74 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
75 return this->publish_json(this->get_state_topic_to_(topic_buf),
76 [year, month, day, hour, minute, second](JsonObject root) {
77 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
78 root[ESPHOME_F("year")] = year;
79 root[ESPHOME_F("month")] = month;
80 root[ESPHOME_F("day")] = day;
81 root[ESPHOME_F("hour")] = hour;
82 root[ESPHOME_F("minute")] = minute;
83 root[ESPHOME_F("second")] = second;
84 });
85}
86
87} // namespace esphome::mqtt
88
89#endif // USE_DATETIME_DATETIME
90#endif // USE_MQTT
bool has_state() const
void add_on_state_callback(F &&callback)
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
void subscribe_json(T &&topic, F &&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).
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
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().