From eac2388d4984f256eefe0dfd3b52b835fc4d6880 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Fri, 19 Apr 2019 04:00:35 +0000 Subject: [PATCH] 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. --- .../components/input_datetime/__init__.py | 21 ++++++++---- tests/components/input_datetime/test_init.py | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/input_datetime/__init__.py b/homeassistant/components/input_datetime/__init__.py index 34faffd2028..af0a28aa34a 100644 --- a/homeassistant/components/input_datetime/__init__.py +++ b/homeassistant/components/input_datetime/__init__.py @@ -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): diff --git a/tests/components/input_datetime/test_init.py b/tests/components/input_datetime/test_init.py index 2a4d0fef09d..03ad27e6048 100644 --- a/tests/components/input_datetime/test_init.py +++ b/tests/components/input_datetime/test_init.py @@ -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', {