1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 23:22:21 +01:00

Add datetime date entities (#6191)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
RFDarter
2024-03-10 19:52:22 +01:00
committed by GitHub
parent 4ec2b37cc6
commit 1e96a19d09
44 changed files with 1553 additions and 22 deletions

View File

@@ -113,6 +113,7 @@ MQTTSensorComponent = mqtt_ns.class_("MQTTSensorComponent", MQTTComponent)
MQTTSwitchComponent = mqtt_ns.class_("MQTTSwitchComponent", MQTTComponent)
MQTTTextSensor = mqtt_ns.class_("MQTTTextSensor", MQTTComponent)
MQTTNumberComponent = mqtt_ns.class_("MQTTNumberComponent", MQTTComponent)
MQTTDatetimeComponent = mqtt_ns.class_("MQTTDatetimeComponent", MQTTComponent)
MQTTTextComponent = mqtt_ns.class_("MQTTTextComponent", MQTTComponent)
MQTTSelectComponent = mqtt_ns.class_("MQTTSelectComponent", MQTTComponent)
MQTTButtonComponent = mqtt_ns.class_("MQTTButtonComponent", MQTTComponent)

View File

@@ -0,0 +1,68 @@
#include "mqtt_date.h"
#include <utility>
#include "esphome/core/log.h"
#include "mqtt_const.h"
#ifdef USE_MQTT
#ifdef USE_DATETIME_DATE
namespace esphome {
namespace mqtt {
static const char *const TAG = "mqtt.datetime";
using namespace esphome::datetime;
MQTTDateComponent::MQTTDateComponent(DateEntity *date) : date_(date) {}
void MQTTDateComponent::setup() {
this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
auto call = this->date_->make_call();
if (root.containsKey("year")) {
call.set_year(root["year"]);
}
if (root.containsKey("month")) {
call.set_month(root["month"]);
}
if (root.containsKey("day")) {
call.set_day(root["day"]);
}
call.perform();
});
this->date_->add_on_state_callback(
[this]() { this->publish_state(this->date_->year, this->date_->month, this->date_->day); });
}
void MQTTDateComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MQTT Date '%s':", this->date_->get_name().c_str());
LOG_MQTT_COMPONENT(true, true)
}
std::string MQTTDateComponent::component_type() const { return "date"; }
const EntityBase *MQTTDateComponent::get_entity() const { return this->date_; }
void MQTTDateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
// Nothing extra to add here
}
bool MQTTDateComponent::send_initial_state() {
if (this->date_->has_state()) {
return this->publish_state(this->date_->year, this->date_->month, this->date_->day);
} else {
return true;
}
}
bool MQTTDateComponent::publish_state(uint16_t year, uint8_t month, uint8_t day) {
return this->publish_json(this->get_state_topic_(), [year, month, day](JsonObject root) {
root["year"] = year;
root["month"] = month;
root["day"] = day;
});
}
} // namespace mqtt
} // namespace esphome
#endif // USE_DATETIME_DATE
#endif // USE_MQTT

View File

@@ -0,0 +1,45 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_MQTT
#ifdef USE_DATETIME_DATE
#include "esphome/components/datetime/date_entity.h"
#include "mqtt_component.h"
namespace esphome {
namespace mqtt {
class MQTTDateComponent : public mqtt::MQTTComponent {
public:
/** Construct this MQTTDatetimeComponent instance with the provided friendly_name and datetime
*
* @param datetime The datetime component.
*/
explicit MQTTDateComponent(datetime::DateEntity *date);
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
/// Override setup.
void setup() override;
void dump_config() override;
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override;
bool send_initial_state() override;
bool publish_state(uint16_t year, uint8_t month, uint8_t day);
protected:
std::string component_type() const override;
const EntityBase *get_entity() const override;
datetime::DateEntity *date_;
};
} // namespace mqtt
} // namespace esphome
#endif // USE_DATETIME_DATE
#endif // USE_MQTT