ESPHome  2024.4.2
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 
11 namespace esphome {
12 namespace mqtt {
13 
14 static const char *const TAG = "mqtt.datetime.time";
15 
16 using 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 
43 std::string MQTTTimeComponent::component_type() const { return "time"; }
44 const 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 }
56 bool 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
uint8_t minute
Definition: time_entity.h:148
std::string component_type() const override
Definition: mqtt_time.cpp:43
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_time.cpp:46
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
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.
void setup() override
Override setup.
Definition: mqtt_time.cpp:20
bool has_state() const
Return whether this Datetime has gotten a full state yet.
Definition: datetime_base.h:14
Simple Helper struct used for Home Assistant MQTT send_discovery().
uint8_t second
Definition: time_entity.h:149
void add_on_state_callback(std::function< void()> &&callback)
Definition: datetime_base.h:18
constexpr const char * c_str() const
Definition: string_ref.h:68
uint8_t hour
Definition: time_entity.h:147
const EntityBase * get_entity() const override
Definition: mqtt_time.cpp:44
MQTTTimeComponent(datetime::TimeEntity *time)
Construct this MQTTTimeComponent instance with the provided friendly_name and time.
Definition: mqtt_time.cpp:18
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
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.
datetime::TimeEntity * time_
Definition: mqtt_time.h:38
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool send_initial_state() override
Definition: mqtt_time.cpp:49
bool publish_state(uint8_t hour, uint8_t minute, uint8_t second)
Definition: mqtt_time.cpp:56