Default `input_datetime` to current date (#45052)
parent
bade98624d
commit
cad2fa89ed
|
@ -32,8 +32,6 @@ CONF_HAS_DATE = "has_date"
|
||||||
CONF_HAS_TIME = "has_time"
|
CONF_HAS_TIME = "has_time"
|
||||||
CONF_INITIAL = "initial"
|
CONF_INITIAL = "initial"
|
||||||
|
|
||||||
DEFAULT_VALUE = "1970-01-01 00:00:00"
|
|
||||||
DEFAULT_DATE = py_datetime.date(1970, 1, 1)
|
|
||||||
DEFAULT_TIME = py_datetime.time(0, 0, 0)
|
DEFAULT_TIME = py_datetime.time(0, 0, 0)
|
||||||
|
|
||||||
ATTR_DATETIME = "datetime"
|
ATTR_DATETIME = "datetime"
|
||||||
|
@ -218,7 +216,9 @@ class InputDatetime(RestoreEntity):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
time = dt_util.parse_time(initial)
|
time = dt_util.parse_time(initial)
|
||||||
current_datetime = py_datetime.datetime.combine(DEFAULT_DATE, time)
|
current_datetime = py_datetime.datetime.combine(
|
||||||
|
py_datetime.date.today(), time
|
||||||
|
)
|
||||||
|
|
||||||
# If the user passed in an initial value with a timezone, convert it to right tz
|
# If the user passed in an initial value with a timezone, convert it to right tz
|
||||||
if current_datetime.tzinfo is not None:
|
if current_datetime.tzinfo is not None:
|
||||||
|
@ -246,32 +246,36 @@ class InputDatetime(RestoreEntity):
|
||||||
if self.state is not None:
|
if self.state is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
default_value = py_datetime.datetime.today().strftime("%Y-%m-%d 00:00:00")
|
||||||
|
|
||||||
# Priority 2: Old state
|
# Priority 2: Old state
|
||||||
old_state = await self.async_get_last_state()
|
old_state = await self.async_get_last_state()
|
||||||
if old_state is None:
|
if old_state is None:
|
||||||
self._current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
|
self._current_datetime = dt_util.parse_datetime(default_value)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.has_date and self.has_time:
|
if self.has_date and self.has_time:
|
||||||
date_time = dt_util.parse_datetime(old_state.state)
|
date_time = dt_util.parse_datetime(old_state.state)
|
||||||
if date_time is None:
|
if date_time is None:
|
||||||
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
|
current_datetime = dt_util.parse_datetime(default_value)
|
||||||
else:
|
else:
|
||||||
current_datetime = date_time
|
current_datetime = date_time
|
||||||
|
|
||||||
elif self.has_date:
|
elif self.has_date:
|
||||||
date = dt_util.parse_date(old_state.state)
|
date = dt_util.parse_date(old_state.state)
|
||||||
if date is None:
|
if date is None:
|
||||||
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
|
current_datetime = dt_util.parse_datetime(default_value)
|
||||||
else:
|
else:
|
||||||
current_datetime = py_datetime.datetime.combine(date, DEFAULT_TIME)
|
current_datetime = py_datetime.datetime.combine(date, DEFAULT_TIME)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
time = dt_util.parse_time(old_state.state)
|
time = dt_util.parse_time(old_state.state)
|
||||||
if time is None:
|
if time is None:
|
||||||
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
|
current_datetime = dt_util.parse_datetime(default_value)
|
||||||
else:
|
else:
|
||||||
current_datetime = py_datetime.datetime.combine(DEFAULT_DATE, time)
|
current_datetime = py_datetime.datetime.combine(
|
||||||
|
py_datetime.date.today(), time
|
||||||
|
)
|
||||||
|
|
||||||
self._current_datetime = current_datetime.replace(
|
self._current_datetime = current_datetime.replace(
|
||||||
tzinfo=dt_util.DEFAULT_TIME_ZONE
|
tzinfo=dt_util.DEFAULT_TIME_ZONE
|
||||||
|
|
|
@ -320,7 +320,7 @@ async def test_restore_state(hass):
|
||||||
hass.state = CoreState.starting
|
hass.state = CoreState.starting
|
||||||
|
|
||||||
initial = datetime.datetime(2017, 1, 1, 23, 42)
|
initial = datetime.datetime(2017, 1, 1, 23, 42)
|
||||||
default = datetime.datetime(1970, 1, 1, 0, 0)
|
default = datetime.datetime.combine(datetime.date.today(), DEFAULT_TIME)
|
||||||
|
|
||||||
await async_setup_component(
|
await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
|
@ -375,7 +375,7 @@ async def test_default_value(hass):
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
dt_obj = datetime.datetime(1970, 1, 1, 0, 0)
|
dt_obj = datetime.datetime.combine(datetime.date.today(), DEFAULT_TIME)
|
||||||
state_time = hass.states.get("input_datetime.test_time")
|
state_time = hass.states.get("input_datetime.test_time")
|
||||||
assert state_time.state == dt_obj.strftime(FMT_TIME)
|
assert state_time.state == dt_obj.strftime(FMT_TIME)
|
||||||
assert state_time.attributes.get("timestamp") is not None
|
assert state_time.attributes.get("timestamp") is not None
|
||||||
|
@ -477,7 +477,9 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||||
assert state_2 is not None
|
assert state_2 is not None
|
||||||
assert state_3 is None
|
assert state_3 is None
|
||||||
assert state_1.state == DEFAULT_TIME.strftime(FMT_TIME)
|
assert state_1.state == DEFAULT_TIME.strftime(FMT_TIME)
|
||||||
assert state_2.state == datetime.datetime(1970, 1, 1, 0, 0).strftime(FMT_DATETIME)
|
assert state_2.state == datetime.datetime.combine(
|
||||||
|
datetime.date.today(), DEFAULT_TIME
|
||||||
|
).strftime(FMT_DATETIME)
|
||||||
|
|
||||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
|
||||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") == f"{DOMAIN}.dt2"
|
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") == f"{DOMAIN}.dt2"
|
||||||
|
|
Loading…
Reference in New Issue