mirror of
https://github.com/esphome/esphome.git
synced 2026-06-01 01:19:45 +08:00
[time] Support */N syntax in cron expressions (#15434)
This commit is contained in:
@@ -123,8 +123,8 @@ def _parse_cron_part(part, min_value, max_value, special_mapping):
|
|||||||
f"Can't have more than two '/' in one time expression, got {part}"
|
f"Can't have more than two '/' in one time expression, got {part}"
|
||||||
)
|
)
|
||||||
offset, repeat = data
|
offset, repeat = data
|
||||||
offset_n = 0
|
offset_n = min_value
|
||||||
if offset:
|
if offset and offset not in ("*", "?"):
|
||||||
offset_n = _parse_cron_int(
|
offset_n = _parse_cron_int(
|
||||||
offset,
|
offset,
|
||||||
special_mapping,
|
special_mapping,
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
"""Tests for time component cron expression parsing."""
|
||||||
|
|
||||||
|
from esphome.components.time import _parse_cron_part
|
||||||
|
|
||||||
|
|
||||||
|
def test_star_slash_seconds() -> None:
|
||||||
|
assert _parse_cron_part("*/10", 0, 60, {}) == {0, 10, 20, 30, 40, 50, 60}
|
||||||
|
|
||||||
|
|
||||||
|
def test_star_slash_minutes() -> None:
|
||||||
|
assert _parse_cron_part("*/5", 0, 59, {}) == {
|
||||||
|
0,
|
||||||
|
5,
|
||||||
|
10,
|
||||||
|
15,
|
||||||
|
20,
|
||||||
|
25,
|
||||||
|
30,
|
||||||
|
35,
|
||||||
|
40,
|
||||||
|
45,
|
||||||
|
50,
|
||||||
|
55,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_star_slash_hours() -> None:
|
||||||
|
assert _parse_cron_part("*/2", 0, 23, {}) == {
|
||||||
|
0,
|
||||||
|
2,
|
||||||
|
4,
|
||||||
|
6,
|
||||||
|
8,
|
||||||
|
10,
|
||||||
|
12,
|
||||||
|
14,
|
||||||
|
16,
|
||||||
|
18,
|
||||||
|
20,
|
||||||
|
22,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_star_slash_days_of_month() -> None:
|
||||||
|
"""days_of_month starts at 1, not 0."""
|
||||||
|
assert _parse_cron_part("*/5", 1, 31, {}) == {1, 6, 11, 16, 21, 26, 31}
|
||||||
|
|
||||||
|
|
||||||
|
def test_question_slash() -> None:
|
||||||
|
assert _parse_cron_part("?/10", 0, 60, {}) == {0, 10, 20, 30, 40, 50, 60}
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_offset_slash() -> None:
|
||||||
|
"""Empty offset defaults to min_value."""
|
||||||
|
assert _parse_cron_part("/10", 0, 60, {}) == {0, 10, 20, 30, 40, 50, 60}
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_offset_slash_nonzero_min() -> None:
|
||||||
|
"""Empty offset defaults to min_value, not 0."""
|
||||||
|
assert _parse_cron_part("/5", 1, 31, {}) == {1, 6, 11, 16, 21, 26, 31}
|
||||||
|
|
||||||
|
|
||||||
|
def test_numeric_offset_slash() -> None:
|
||||||
|
assert _parse_cron_part("5/10", 0, 60, {}) == {5, 15, 25, 35, 45, 55}
|
||||||
|
|
||||||
|
|
||||||
|
def test_star() -> None:
|
||||||
|
assert _parse_cron_part("*", 0, 59, {}) == set(range(0, 60))
|
||||||
|
|
||||||
|
|
||||||
|
def test_question() -> None:
|
||||||
|
assert _parse_cron_part("?", 0, 59, {}) == set(range(0, 60))
|
||||||
|
|
||||||
|
|
||||||
|
def test_range() -> None:
|
||||||
|
assert _parse_cron_part("1-5", 0, 59, {}) == {1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
|
||||||
|
def test_single_value() -> None:
|
||||||
|
assert _parse_cron_part("30", 0, 59, {}) == {30}
|
||||||
Reference in New Issue
Block a user