diff --git a/homeassistant/util/dt.py b/homeassistant/util/dt.py index 6babfafb7fcd..de7f98466ddf 100644 --- a/homeassistant/util/dt.py +++ b/homeassistant/util/dt.py @@ -306,8 +306,9 @@ def parse_time(time_str: str) -> dt.time | None: minute = int(parts[1]) second = int(parts[2]) if len(parts) > 2 else 0 return dt.time(hour, minute, second) - except ValueError: - # ValueError if value cannot be converted to an int or not in range + except ValueError, OverflowError: + # ValueError if value cannot be converted to an int or not in range, + # OverflowError if it does not even fit in a C int return None diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index a3df521a5611..3512c62fa32c 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -848,7 +848,12 @@ def test_time() -> None: """Test date validation.""" schema = probatio.Schema(cv.time) - for value in ("Not a time", "2016-11-23", "2016-11-23T18:59:08"): + for value in ( + "Not a time", + "2016-11-23", + "2016-11-23T18:59:08", + "0:2147483648", + ): with pytest.raises(probatio.Invalid): schema(value) diff --git a/tests/util/test_dt.py b/tests/util/test_dt.py index f0dcc77ab121..5c69bcbc7881 100644 --- a/tests/util/test_dt.py +++ b/tests/util/test_dt.py @@ -1,7 +1,7 @@ """Test Home Assistant date util methods.""" # pylint: disable=home-assistant-enforce-utcnow -from datetime import UTC, datetime, timedelta +from datetime import UTC, datetime, time, timedelta import pytest @@ -185,6 +185,22 @@ def test_parse_duration( assert dt_util.parse_duration(duration_string) == expected_result +@pytest.mark.parametrize( + ("time_string", "expected_result"), + [ + pytest.param("12:34", time(12, 34), id="hours_minutes"), + pytest.param("12:34:56", time(12, 34, 56), id="hours_minutes_seconds"), + pytest.param("24:00", None, id="hour_out_of_range"), + pytest.param("0:2147483648", None, id="minute_overflows_c_int"), + pytest.param("2147483648:00", None, id="hour_overflows_c_int"), + pytest.param("0:0:99999999999999999999", None, id="second_overflows_c_long"), + ], +) +def test_parse_time(time_string: str, expected_result: time | None) -> None: + """Test that parse_time returns the expected result.""" + assert dt_util.parse_time(time_string) == expected_result + + def test_get_age() -> None: """Test get_age.""" diff = dt_util.now() - timedelta(seconds=0)