Skip to content

Commit

Permalink
Correct input_datetime initial value parsing (#10417)
Browse files Browse the repository at this point in the history
* Correct input_datetime initial value parsing

* Correct input_datetime initial value parsing
  • Loading branch information
colindunn authored and balloob committed Nov 16, 2017
1 parent 1719fa7 commit 3dbae5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions homeassistant/components/input_datetime.py
Expand Up @@ -46,7 +46,7 @@
vol.Required(CONF_HAS_DATE): cv.boolean,
vol.Required(CONF_HAS_TIME): cv.boolean,
vol.Optional(CONF_ICON): cv.icon,
vol.Optional(CONF_INITIAL): cv.datetime,
vol.Optional(CONF_INITIAL): cv.string,
}, cv.has_at_least_one_key_value((CONF_HAS_DATE, True),
(CONF_HAS_TIME, True)))})
}, extra=vol.ALLOW_EXTRA)
Expand Down Expand Up @@ -137,15 +137,15 @@ def async_added_to_hass(self):
old_state = yield from async_get_last_state(self.hass,
self.entity_id)
if old_state is not None:
restore_val = dt_util.parse_datetime(old_state.state)
restore_val = old_state.state

if restore_val is not None:
if not self._has_date:
self._current_datetime = restore_val.time()
self._current_datetime = dt_util.parse_time(restore_val)
elif not self._has_time:
self._current_datetime = restore_val.date()
self._current_datetime = dt_util.parse_date(restore_val)
else:
self._current_datetime = restore_val
self._current_datetime = dt_util.parse_datetime(restore_val)

def has_date(self):
"""Return whether the input datetime carries a date."""
Expand Down
8 changes: 4 additions & 4 deletions tests/components/test_input_datetime.py
Expand Up @@ -102,7 +102,7 @@ def test_set_datetime_time(hass):
@asyncio.coroutine
def test_set_invalid(hass):
"""Test set_datetime method with only time."""
initial = datetime.datetime(2017, 1, 1, 0, 0)
initial = '2017-01-01'
yield from async_setup_component(hass, DOMAIN, {
DOMAIN: {
'test_date': {
Expand All @@ -124,7 +124,7 @@ def test_set_invalid(hass):
yield from hass.async_block_till_done()

state = hass.states.get(entity_id)
assert state.state == str(initial.date())
assert state.state == initial


@asyncio.coroutine
Expand Down Expand Up @@ -159,8 +159,8 @@ def test_set_datetime_date(hass):
def test_restore_state(hass):
"""Ensure states are restored on startup."""
mock_restore_cache(hass, (
State('input_datetime.test_time', '2017-09-07 19:46:00'),
State('input_datetime.test_date', '2017-09-07 19:46:00'),
State('input_datetime.test_time', '19:46:00'),
State('input_datetime.test_date', '2017-09-07'),
State('input_datetime.test_datetime', '2017-09-07 19:46:00'),
State('input_datetime.test_bogus_data', 'this is not a date'),
))
Expand Down

0 comments on commit 3dbae5c

Please sign in to comment.