Default `input_datetime` to current date (#45052)

pull/45067/head
Philip Allgaier 2021-01-11 23:45:58 +01:00 committed by GitHub
parent bade98624d
commit cad2fa89ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -32,8 +32,6 @@ CONF_HAS_DATE = "has_date"
CONF_HAS_TIME = "has_time"
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)
ATTR_DATETIME = "datetime"
@ -218,7 +216,9 @@ class InputDatetime(RestoreEntity):
else:
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 current_datetime.tzinfo is not None:
@ -246,32 +246,36 @@ class InputDatetime(RestoreEntity):
if self.state is not None:
return
default_value = py_datetime.datetime.today().strftime("%Y-%m-%d 00:00:00")
# Priority 2: Old state
old_state = await self.async_get_last_state()
if old_state is None:
self._current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
self._current_datetime = dt_util.parse_datetime(default_value)
return
if self.has_date and self.has_time:
date_time = dt_util.parse_datetime(old_state.state)
if date_time is None:
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
current_datetime = dt_util.parse_datetime(default_value)
else:
current_datetime = date_time
elif self.has_date:
date = dt_util.parse_date(old_state.state)
if date is None:
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
current_datetime = dt_util.parse_datetime(default_value)
else:
current_datetime = py_datetime.datetime.combine(date, DEFAULT_TIME)
else:
time = dt_util.parse_time(old_state.state)
if time is None:
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
current_datetime = dt_util.parse_datetime(default_value)
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(
tzinfo=dt_util.DEFAULT_TIME_ZONE

View File

@ -320,7 +320,7 @@ async def test_restore_state(hass):
hass.state = CoreState.starting
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(
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")
assert state_time.state == dt_obj.strftime(FMT_TIME)
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_3 is None
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, "dt2") == f"{DOMAIN}.dt2"