ESPHome 2026.11.0-dev
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 // Nothing extra to add here
55}
57 if (this->datetime_->has_state()) {
58 return this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day,
59 this->datetime_->hour, this->datetime_->minute, this->datetime_->second);
60 } else {
61 return true;
62 }
63}
64bool MQTTDateTimeComponent::publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
65 uint8_t second) {
66 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
67 return this->publish_json(this->get_state_topic_to_(topic_buf),
68 [year, month, day, hour, minute, second](JsonObject root) {
69 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
70 root[ESPHOME_F("year")] = year;
71 root[ESPHOME_F("month")] = month;
72 root[ESPHOME_F("day")] = day;
73 root[ESPHOME_F("hour")] = hour;
74 root[ESPHOME_F("minute")] = minute;
75 root[ESPHOME_F("second")] = second;
76 });
77}
78
79} // namespace esphome::mqtt
80
81#endif // USE_DATETIME_DATETIME
82#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().