Set default value for input_datetime (#21919)

* Set default value for input_datetime

If no initial value is set and no value is available to be restored, set the default value as specified in the docs to 1970-01-01 00:00.

* Use regular if statement

Ternary statements can be tricky if you try to keep the value the same if not something

* Add test for default values

Check that if no initial value is set, state returns 1970-01-01 at 00:00

* Fix tests - was passing wrong args to time/date

* Verify we get a timestamp attribute for input_datetime

This adds a check that when using the default timestamp of 1970-1-1 00:00:00, we
get a timestamp attribute. This is waht prompted this PR in the first place, as
when specifying an automation trying to access the timestamp attribute for a non-
initialized input_datetime HASS wouldn't start.

* Simplify the change for a default value

Based on @balloob comment. Simplifying the code

* Revert "Simplify the change for a default value"

This reverts commit c2d67f19a686b141672d619be62e3f53890f1328.
This commit is contained in:
Tsvi Mostovicz authored and Paulus Schoutsen committed 2019-04-18 21:00:35 -07:00
1 parent 7a84cfb0be
commit eac2388d49
2 files changed
+47 -7

No files matched your search

@@ -20,6 +20,8 @@ CONF_HAS_DATE = 'has_date'
CONF_HAS_TIME = 'has_time'
CONF_INITIAL = 'initial'
DEFAULT_VALUE = '1970-01-01 00:00:00'
ATTR_DATE = 'date'
ATTR_TIME = 'time'
@@ -120,13 +122,18 @@ class InputDatetime(RestoreEntity):
if old_state is not None:
restore_val = old_state.state
if restore_val is not None:
if not self.has_date:
self._current_datetime = dt_util.parse_time(restore_val)
elif not self.has_time:
self._current_datetime = dt_util.parse_date(restore_val)
else:
self._current_datetime = dt_util.parse_datetime(restore_val)
if not self.has_date:
if not restore_val:
restore_val = DEFAULT_VALUE.split()[1]
self._current_datetime = dt_util.parse_time(restore_val)
elif not self.has_time:
if not restore_val:
restore_val = DEFAULT_VALUE.split()[0]
self._current_datetime = dt_util.parse_date(restore_val)
else:
if not restore_val:
restore_val = DEFAULT_VALUE
self._current_datetime = dt_util.parse_datetime(restore_val)
@property
def should_poll(self):