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:
parent
7a84cfb0be
commit
eac2388d49
2 changed files with 47 additions and 7 deletions
|
@ -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', {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue