mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Validate initial value for input_datetime (#57256)
This commit is contained in:
parent
9dfb684002
commit
0e48985fc5
@ -81,6 +81,30 @@ def has_date_or_time(conf):
|
|||||||
raise vol.Invalid("Entity needs at least a date or a time")
|
raise vol.Invalid("Entity needs at least a date or a time")
|
||||||
|
|
||||||
|
|
||||||
|
def valid_initial(conf):
|
||||||
|
"""Check the initial value is valid."""
|
||||||
|
initial = conf.get(CONF_INITIAL)
|
||||||
|
if not initial:
|
||||||
|
return conf
|
||||||
|
|
||||||
|
if conf[CONF_HAS_DATE] and conf[CONF_HAS_TIME]:
|
||||||
|
parsed_value = dt_util.parse_datetime(initial)
|
||||||
|
if parsed_value is not None:
|
||||||
|
return conf
|
||||||
|
raise vol.Invalid(f"Initial value '{initial}' can't be parsed as a datetime")
|
||||||
|
|
||||||
|
if conf[CONF_HAS_DATE]:
|
||||||
|
parsed_value = dt_util.parse_date(initial)
|
||||||
|
if parsed_value is not None:
|
||||||
|
return conf
|
||||||
|
raise vol.Invalid(f"Initial value '{initial}' can't be parsed as a date")
|
||||||
|
|
||||||
|
parsed_value = dt_util.parse_time(initial)
|
||||||
|
if parsed_value is not None:
|
||||||
|
return conf
|
||||||
|
raise vol.Invalid(f"Initial value '{initial}' can't be parsed as a time")
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
DOMAIN: cv.schema_with_slug_keys(
|
DOMAIN: cv.schema_with_slug_keys(
|
||||||
@ -93,6 +117,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
vol.Optional(CONF_INITIAL): cv.string,
|
vol.Optional(CONF_INITIAL): cv.string,
|
||||||
},
|
},
|
||||||
has_date_or_time,
|
has_date_or_time,
|
||||||
|
valid_initial,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
@ -744,3 +744,30 @@ async def test_timestamp(hass):
|
|||||||
|
|
||||||
finally:
|
finally:
|
||||||
dt_util.set_default_time_zone(ORIG_TIMEZONE)
|
dt_util.set_default_time_zone(ORIG_TIMEZONE)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"config, error",
|
||||||
|
[
|
||||||
|
(
|
||||||
|
{"has_time": True, "has_date": True, "initial": "abc"},
|
||||||
|
"'abc' can't be parsed as a datetime",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{"has_time": False, "has_date": True, "initial": "abc"},
|
||||||
|
"'abc' can't be parsed as a date",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{"has_time": True, "has_date": False, "initial": "abc"},
|
||||||
|
"'abc' can't be parsed as a time",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_invalid_initial(hass, caplog, config, error):
|
||||||
|
"""Test configuration is rejected if the initial value is invalid."""
|
||||||
|
assert not await async_setup_component(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
{DOMAIN: {"test_date": config}},
|
||||||
|
)
|
||||||
|
assert error in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user