Datetime date initial value fix (#6483)

This commit is contained in:
RFDarter 2024-04-08 09:36:23 +02:00 committed by GitHub
parent 6f71363d9b
commit d6352b3be4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View file

@ -8,6 +8,9 @@ from esphome.const import (
CONF_OPTIMISTIC, CONF_OPTIMISTIC,
CONF_RESTORE_VALUE, CONF_RESTORE_VALUE,
CONF_SET_ACTION, CONF_SET_ACTION,
CONF_DAY,
CONF_MONTH,
CONF_YEAR,
) )
from esphome.core import coroutine_with_priority from esphome.core import coroutine_with_priority
@ -82,7 +85,13 @@ async def to_code(config):
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE])) cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
if initial_value := config.get(CONF_INITIAL_VALUE): if initial_value := config.get(CONF_INITIAL_VALUE):
cg.add(var.set_initial_value(initial_value)) date_struct = cg.StructInitializer(
cg.ESPTime,
("day_of_month", initial_value[CONF_DAY]),
("month", initial_value[CONF_MONTH]),
("year", initial_value[CONF_YEAR]),
)
cg.add(var.set_initial_value(date_struct))
if CONF_SET_ACTION in config: if CONF_SET_ACTION in config:
await automation.build_automation( await automation.build_automation(

View file

@ -914,7 +914,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat
std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_config) { std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_config) {
return json::build_json([obj, start_config](JsonObject root) { return json::build_json([obj, start_config](JsonObject root) {
set_json_id(root, obj, "date-" + obj->get_object_id(), start_config); set_json_id(root, obj, "date-" + obj->get_object_id(), start_config);
std::string value = str_sprintf("%d-%d-%d", obj->year, obj->month, obj->day); std::string value = str_sprintf("%d-%02d-%02d", obj->year, obj->month, obj->day);
root["value"] = value; root["value"] = value;
root["state"] = value; root["state"] = value;
}); });