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 c2d67f19a6.
This commit is contained in:
Tsvi Mostovicz 2019-04-19 04:00:35 +00:00 committed by Paulus Schoutsen
parent 7a84cfb0be
commit eac2388d49
2 changed files with 47 additions and 7 deletions

View file

@ -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):

View file

@ -199,6 +199,39 @@ def test_restore_state(hass):
assert state_bogus.state == str(initial)
@asyncio.coroutine
def test_default_value(hass):
"""Test default value if none has been set via inital or restore state."""
yield from async_setup_component(hass, DOMAIN, {
DOMAIN: {
'test_time': {
'has_time': True,
'has_date': False
},
'test_date': {
'has_time': False,
'has_date': True
},
'test_datetime': {
'has_time': True,
'has_date': True
},
}})
dt_obj = datetime.datetime(1970, 1, 1, 0, 0)
state_time = hass.states.get('input_datetime.test_time')
assert state_time.state == str(dt_obj.time())
assert state_time.attributes.get('timestamp') is not None
state_date = hass.states.get('input_datetime.test_date')
assert state_date.state == str(dt_obj.date())
assert state_date.attributes.get('timestamp') is not None
state_datetime = hass.states.get('input_datetime.test_datetime')
assert state_datetime.state == str(dt_obj)
assert state_datetime.attributes.get('timestamp') is not None
async def test_input_datetime_context(hass, hass_admin_user):
"""Test that input_datetime context works."""
assert await async_setup_component(hass, 'input_datetime', {