mirror of
https://github.com/esphome/esphome.git
synced 2025-09-12 00:02:21 +01:00
Add datetime date entities (#6191)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
94
esphome/components/template/datetime/__init__.py
Normal file
94
esphome/components/template/datetime/__init__.py
Normal file
@@ -0,0 +1,94 @@
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import datetime
|
||||
from esphome.const import (
|
||||
CONF_INITIAL_VALUE,
|
||||
CONF_LAMBDA,
|
||||
CONF_OPTIMISTIC,
|
||||
CONF_RESTORE_VALUE,
|
||||
CONF_SET_ACTION,
|
||||
)
|
||||
|
||||
from esphome.core import coroutine_with_priority
|
||||
from .. import template_ns
|
||||
|
||||
CODEOWNERS = ["@rfdarter"]
|
||||
|
||||
|
||||
TemplateDate = template_ns.class_(
|
||||
"TemplateDate", datetime.DateEntity, cg.PollingComponent
|
||||
)
|
||||
|
||||
|
||||
def validate(config):
|
||||
config = config.copy()
|
||||
if CONF_LAMBDA in config:
|
||||
if config[CONF_OPTIMISTIC]:
|
||||
raise cv.Invalid("optimistic cannot be used with lambda")
|
||||
if CONF_INITIAL_VALUE in config:
|
||||
raise cv.Invalid("initial_value cannot be used with lambda")
|
||||
if CONF_RESTORE_VALUE in config:
|
||||
raise cv.Invalid("restore_value cannot be used with lambda")
|
||||
else:
|
||||
if CONF_RESTORE_VALUE not in config:
|
||||
config[CONF_RESTORE_VALUE] = False
|
||||
|
||||
if not config[CONF_OPTIMISTIC] and CONF_SET_ACTION not in config:
|
||||
raise cv.Invalid(
|
||||
"Either optimistic mode must be enabled, or set_action must be set, to handle the date and time being set."
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
_BASE_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
||||
}
|
||||
).extend(cv.polling_component_schema("60s"))
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.typed_schema(
|
||||
{
|
||||
"DATE": datetime.date_schema(TemplateDate)
|
||||
.extend(_BASE_SCHEMA)
|
||||
.extend(
|
||||
{
|
||||
cv.Optional(CONF_INITIAL_VALUE): cv.date_time(allowed_time=False),
|
||||
}
|
||||
),
|
||||
},
|
||||
upper=True,
|
||||
),
|
||||
validate,
|
||||
)
|
||||
|
||||
|
||||
@coroutine_with_priority(-100.0)
|
||||
async def to_code(config):
|
||||
var = await datetime.new_datetime(config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.ESPTime)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
else:
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
||||
|
||||
if initial_value := config.get(CONF_INITIAL_VALUE):
|
||||
cg.add(var.set_initial_value(initial_value))
|
||||
|
||||
if CONF_SET_ACTION in config:
|
||||
await automation.build_automation(
|
||||
var.get_set_trigger(),
|
||||
[(cg.ESPTime, "x")],
|
||||
config[CONF_SET_ACTION],
|
||||
)
|
||||
|
||||
await cg.register_component(var, config)
|
111
esphome/components/template/datetime/template_date.cpp
Normal file
111
esphome/components/template/datetime/template_date.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "template_date.h"
|
||||
|
||||
#ifdef USE_DATETIME_DATE
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.date";
|
||||
|
||||
void TemplateDate::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
ESPTime state{};
|
||||
|
||||
if (!this->restore_value_) {
|
||||
state = this->initial_value_;
|
||||
} else {
|
||||
datetime::DateEntityRestoreState temp;
|
||||
this->pref_ =
|
||||
global_preferences->make_preference<datetime::DateEntityRestoreState>(194434030U ^ this->get_object_id_hash());
|
||||
if (this->pref_.load(&temp)) {
|
||||
temp.apply(this);
|
||||
return;
|
||||
} else {
|
||||
// set to inital value if loading from pref failed
|
||||
state = this->initial_value_;
|
||||
}
|
||||
}
|
||||
|
||||
this->year_ = state.year;
|
||||
this->month_ = state.month;
|
||||
this->day_ = state.day_of_month;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDate::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->year_ = val->year;
|
||||
this->month_ = val->month;
|
||||
this->day_ = val->day_of_month;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDate::control(const datetime::DateCall &call) {
|
||||
bool has_year = call.get_year().has_value();
|
||||
bool has_month = call.get_month().has_value();
|
||||
bool has_day = call.get_day().has_value();
|
||||
|
||||
ESPTime value = {};
|
||||
if (has_year)
|
||||
value.year = *call.get_year();
|
||||
|
||||
if (has_month)
|
||||
value.month = *call.get_month();
|
||||
|
||||
if (has_day)
|
||||
value.day_of_month = *call.get_day();
|
||||
|
||||
this->set_trigger_->trigger(value);
|
||||
|
||||
if (this->optimistic_) {
|
||||
if (has_year)
|
||||
this->year_ = *call.get_year();
|
||||
if (has_month)
|
||||
this->month_ = *call.get_month();
|
||||
if (has_day)
|
||||
this->day_ = *call.get_day();
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
if (this->restore_value_) {
|
||||
datetime::DateEntityRestoreState temp = {};
|
||||
if (has_year) {
|
||||
temp.year = *call.get_year();
|
||||
} else {
|
||||
temp.year = this->year_;
|
||||
}
|
||||
if (has_month) {
|
||||
temp.month = *call.get_month();
|
||||
} else {
|
||||
temp.month = this->month_;
|
||||
}
|
||||
if (has_day) {
|
||||
temp.day = *call.get_day();
|
||||
} else {
|
||||
temp.day = this->day_;
|
||||
}
|
||||
|
||||
this->pref_.save(&temp);
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateDate::dump_config() {
|
||||
LOG_DATETIME_DATE("", "Template Date", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_DATETIME_DATE
|
46
esphome/components/template/datetime/template_date.h
Normal file
46
esphome/components/template/datetime/template_date.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_DATETIME_DATE
|
||||
|
||||
#include "esphome/components/datetime/date_entity.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "esphome/core/time.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateDate : public datetime::DateEntity, public PollingComponent {
|
||||
public:
|
||||
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<ESPTime> *get_set_trigger() const { return this->set_trigger_; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
|
||||
void set_initial_value(ESPTime initial_value) { this->initial_value_ = initial_value; }
|
||||
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
||||
|
||||
protected:
|
||||
void control(const datetime::DateCall &call) override;
|
||||
|
||||
bool optimistic_{false};
|
||||
ESPTime initial_value_{};
|
||||
bool restore_value_{false};
|
||||
Trigger<ESPTime> *set_trigger_ = new Trigger<ESPTime>();
|
||||
optional<std::function<optional<ESPTime>()>> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_DATETIME_DATE
|
Reference in New Issue
Block a user