Correct input_datetime initial value parsing (#10417)

* Correct input_datetime initial value parsing

* Correct input_datetime initial value parsing
This commit is contained in:
Colin Dunn 2017-11-16 18:16:22 +11:00 committed by Paulus Schoutsen
parent 1719fa7008
commit 3dbae5ca5b
2 changed files with 9 additions and 9 deletions

View file

@ -46,7 +46,7 @@ CONFIG_SCHEMA = vol.Schema({
vol.Required(CONF_HAS_DATE): cv.boolean, vol.Required(CONF_HAS_DATE): cv.boolean,
vol.Required(CONF_HAS_TIME): cv.boolean, vol.Required(CONF_HAS_TIME): cv.boolean,
vol.Optional(CONF_ICON): cv.icon, 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), }, cv.has_at_least_one_key_value((CONF_HAS_DATE, True),
(CONF_HAS_TIME, True)))}) (CONF_HAS_TIME, True)))})
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
@ -137,15 +137,15 @@ class InputDatetime(Entity):
old_state = yield from async_get_last_state(self.hass, old_state = yield from async_get_last_state(self.hass,
self.entity_id) self.entity_id)
if old_state is not None: 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 restore_val is not None:
if not self._has_date: 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: elif not self._has_time:
self._current_datetime = restore_val.date() self._current_datetime = dt_util.parse_date(restore_val)
else: else:
self._current_datetime = restore_val self._current_datetime = dt_util.parse_datetime(restore_val)
def has_date(self): def has_date(self):
"""Return whether the input datetime carries a date.""" """Return whether the input datetime carries a date."""

View file

@ -102,7 +102,7 @@ def test_set_datetime_time(hass):
@asyncio.coroutine @asyncio.coroutine
def test_set_invalid(hass): def test_set_invalid(hass):
"""Test set_datetime method with only time.""" """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, { yield from async_setup_component(hass, DOMAIN, {
DOMAIN: { DOMAIN: {
'test_date': { 'test_date': {
@ -124,7 +124,7 @@ def test_set_invalid(hass):
yield from hass.async_block_till_done() yield from hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == str(initial.date()) assert state.state == initial
@asyncio.coroutine @asyncio.coroutine
@ -159,8 +159,8 @@ def test_set_datetime_date(hass):
def test_restore_state(hass): def test_restore_state(hass):
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
mock_restore_cache(hass, ( mock_restore_cache(hass, (
State('input_datetime.test_time', '2017-09-07 19:46:00'), State('input_datetime.test_time', '19:46:00'),
State('input_datetime.test_date', '2017-09-07 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_datetime', '2017-09-07 19:46:00'),
State('input_datetime.test_bogus_data', 'this is not a date'), State('input_datetime.test_bogus_data', 'this is not a date'),
)) ))