mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Datetime date initial value fix (#6483)
This commit is contained in:
parent
6f71363d9b
commit
d6352b3be4
2 changed files with 11 additions and 2 deletions
|
@ -8,6 +8,9 @@ from esphome.const import (
|
|||
CONF_OPTIMISTIC,
|
||||
CONF_RESTORE_VALUE,
|
||||
CONF_SET_ACTION,
|
||||
CONF_DAY,
|
||||
CONF_MONTH,
|
||||
CONF_YEAR,
|
||||
)
|
||||
|
||||
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]))
|
||||
|
||||
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:
|
||||
await automation.build_automation(
|
||||
|
|
|
@ -914,7 +914,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat
|
|||
std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_config) {
|
||||
return json::build_json([obj, start_config](JsonObject root) {
|
||||
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["state"] = value;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue