ESPHome 2025.5.0
Loading...
Searching...
No Matches
mqtt_time.cpp
Go to the documentation of this file.
1#include "mqtt_time.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_TIME
10
11namespace esphome {
12namespace mqtt {
13
14static const char *const TAG = "mqtt.datetime.time";
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->time_->make_call();
23 if (root.containsKey("hour")) {
24 call.set_hour(root["hour"]);
25 }
26 if (root.containsKey("minute")) {
27 call.set_minute(root["minute"]);
28 }
29 if (root.containsKey("second")) {
30 call.set_second(root["second"]);
31 }
32 call.perform();
33 });
35 [this]() { this->publish_state(this->time_->hour, this->time_->minute, this->time_->second); });
36}
37
39 ESP_LOGCONFIG(TAG, "MQTT Time '%s':", this->time_->get_name().c_str());
40 LOG_MQTT_COMPONENT(true, true)
41}
42
43std::string MQTTTimeComponent::component_type() const { return "time"; }
44const EntityBase *MQTTTimeComponent::get_entity() const { return this->time_; }
45
47 // Nothing extra to add here
48}
50 if (this->time_->has_state()) {
51 return this->publish_state(this->time_->hour, this->time_->minute, this->time_->second);
52 } else {
53 return true;
54 }
55}
56bool MQTTTimeComponent::publish_state(uint8_t hour, uint8_t minute, uint8_t second) {
57 return this->publish_json(this->get_state_topic_(), [hour, minute, second](JsonObject root) {
58 root["hour"] = hour;
59 root["minute"] = minute;
60 root["second"] = second;
61 });
62}
63
64} // namespace mqtt
65} // namespace esphome
66
67#endif // USE_DATETIME_TIME
68#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.
MQTTTimeComponent(datetime::TimeEntity *time)
Construct this MQTTTimeComponent instance with the provided friendly_name and time.
Definition mqtt_time.cpp:18
std::string component_type() const override
Definition mqtt_time.cpp:43
datetime::TimeEntity * time_
Definition mqtt_time.h:38
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition mqtt_time.cpp:46
const EntityBase * get_entity() const override
Definition mqtt_time.cpp:44
void setup() override
Override setup.
Definition mqtt_time.cpp:20
bool publish_state(uint8_t hour, uint8_t minute, uint8_t second)
Definition mqtt_time.cpp:56
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().