Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for DD in time_period_str #73693

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 7 additions & 26 deletions homeassistant/helpers/config_validation.py
Expand Up @@ -95,7 +95,7 @@

# pylint: disable=invalid-name

TIME_PERIOD_ERROR = "offset {} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F'"
TIME_PERIOD_ERROR = "offset {} should be format 'HH:MM', 'HH:MM:SS', 'HH:MM:SS.F', 'DD HH:MM', 'DD HH:MM:SS', or 'DD HH:MM:SS.F'"

# Home Assistant types
byte = vol.All(vol.Coerce(int), vol.Range(min=0, max=255))
Expand Down Expand Up @@ -439,32 +439,13 @@ def time_period_str(value: str) -> timedelta:
if not isinstance(value, str):
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))

negative_offset = False
if value.startswith("-"):
negative_offset = True
value = value[1:]
elif value.startswith("+"):
value = value[1:]

parsed = value.split(":")
if len(parsed) not in (2, 3):
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
try:
hour = int(parsed[0])
minute = int(parsed[1])
try:
second = float(parsed[2])
except IndexError:
second = 0
except ValueError as err:
raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) from err

offset = timedelta(hours=hour, minutes=minute, seconds=second)

if negative_offset:
offset *= -1

return offset
offset = dt_util.parse_duration(value)
if offset:
return offset
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
except Exception as err:
raise vol.Invalid(err)


def time_period_seconds(value: float | str) -> timedelta:
Expand Down
36 changes: 36 additions & 0 deletions tests/helpers/test_config_validation.py
Expand Up @@ -1332,3 +1332,39 @@ def test_currency():

for value in ("EUR", "USD"):
assert schema(value)


@pytest.mark.parametrize(
"time_period_str_pass",
(
("23:42:00", timedelta(hours=23, minutes=42)),
("-23:42:25", -timedelta(hours=23, minutes=42, seconds=25)),
("23:42:25.987654", timedelta(hours=23, minutes=42, seconds=25.987654)),
("1 23:42:00", timedelta(days=1, hours=23, minutes=42)),
("-1 -23:42:25", -timedelta(days=1, hours=23, minutes=42, seconds=25)),
(
"1 23:42:25.987654",
timedelta(days=1, hours=23, minutes=42, seconds=25.987654),
),
),
)
def test_time_period_str_pass(time_period_str_pass):
"""Test time_period_str validator and transformer."""
assert cv.time_period_str(time_period_str_pass[0]) == time_period_str_pass[1]


@pytest.mark.parametrize(
"time_period_str_fail",
(
None,
datetime.now().time(),
"2016-11-23",
"2016-11-23T18:59:08",
" 1 23:42:25.987654",
"1 1 23:42:25.987654",
),
)
def test_time_period_str_fail(time_period_str_fail):
"""Test time_period_str validator and transformer."""
with pytest.raises(vol.Invalid):
vol.Schema(cv.time_period_str(time_period_str_fail))