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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recorder "year" period in leap year #132167

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/recorder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def resolve_period(
start_time = (start_time + timedelta(days=cal_offset * 366)).replace(
month=1, day=1
)
end_time = (start_time + timedelta(days=365)).replace(day=1)
end_time = (start_time + timedelta(days=366)).replace(day=1)

start_time = dt_util.as_utc(start_time)
end_time = dt_util.as_utc(end_time)
Expand Down
92 changes: 66 additions & 26 deletions tests/components/recorder/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any
from unittest.mock import MagicMock, Mock, patch

from freezegun.api import FrozenDateTimeFactory
import pytest
from sqlalchemy import lambda_stmt, text
from sqlalchemy.engine.result import ChunkedIteratorResult
Expand Down Expand Up @@ -1052,55 +1053,94 @@ def all(self):
assert rows == ["mock_row"]


@pytest.mark.freeze_time(datetime(2022, 10, 21, 7, 25, tzinfo=UTC))
async def test_resolve_period(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("start_time", "periods"),
[
(
# Test 00:25 local time, during DST
datetime(2022, 10, 21, 7, 25, 50, 123, tzinfo=UTC),
{
"hour": ["2022-10-21T07:00:00+00:00", "2022-10-21T08:00:00+00:00"],
"hour-1": ["2022-10-21T06:00:00+00:00", "2022-10-21T07:00:00+00:00"],
"day": ["2022-10-21T07:00:00+00:00", "2022-10-22T07:00:00+00:00"],
"day-1": ["2022-10-20T07:00:00+00:00", "2022-10-21T07:00:00+00:00"],
"week": ["2022-10-17T07:00:00+00:00", "2022-10-24T07:00:00+00:00"],
"week-1": ["2022-10-10T07:00:00+00:00", "2022-10-17T07:00:00+00:00"],
"month": ["2022-10-01T07:00:00+00:00", "2022-11-01T07:00:00+00:00"],
"month-1": ["2022-09-01T07:00:00+00:00", "2022-10-01T07:00:00+00:00"],
"year": ["2022-01-01T08:00:00+00:00", "2023-01-01T08:00:00+00:00"],
"year-1": ["2021-01-01T08:00:00+00:00", "2022-01-01T08:00:00+00:00"],
},
),
(
# Test 00:25 local time, standard time, February 28th a leap year
datetime(2024, 2, 28, 8, 25, 50, 123, tzinfo=UTC),
{
"hour": ["2024-02-28T08:00:00+00:00", "2024-02-28T09:00:00+00:00"],
"hour-1": ["2024-02-28T07:00:00+00:00", "2024-02-28T08:00:00+00:00"],
"day": ["2024-02-28T08:00:00+00:00", "2024-02-29T08:00:00+00:00"],
"day-1": ["2024-02-27T08:00:00+00:00", "2024-02-28T08:00:00+00:00"],
"week": ["2024-02-26T08:00:00+00:00", "2024-03-04T08:00:00+00:00"],
"week-1": ["2024-02-19T08:00:00+00:00", "2024-02-26T08:00:00+00:00"],
"month": ["2024-02-01T08:00:00+00:00", "2024-03-01T08:00:00+00:00"],
"month-1": ["2024-01-01T08:00:00+00:00", "2024-02-01T08:00:00+00:00"],
"year": ["2024-01-01T08:00:00+00:00", "2025-01-01T08:00:00+00:00"],
"year-1": ["2023-01-01T08:00:00+00:00", "2024-01-01T08:00:00+00:00"],
},
),
],
)
async def test_resolve_period(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
start_time: datetime,
periods: dict[str, tuple[str, str]],
) -> None:
"""Test statistic_during_period."""
assert hass.config.time_zone == "US/Pacific"
freezer.move_to(start_time)

now = dt_util.utcnow()

start_t, end_t = resolve_period({"calendar": {"period": "hour"}})
assert start_t.isoformat() == "2022-10-21T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-21T08:00:00+00:00"

start_t, end_t = resolve_period({"calendar": {"period": "hour"}})
assert start_t.isoformat() == "2022-10-21T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-21T08:00:00+00:00"
assert start_t.isoformat() == periods["hour"][0]
assert end_t.isoformat() == periods["hour"][1]

start_t, end_t = resolve_period({"calendar": {"period": "hour", "offset": -1}})
assert start_t.isoformat() == "2022-10-21T06:00:00+00:00"
assert end_t.isoformat() == "2022-10-21T07:00:00+00:00"
assert start_t.isoformat() == periods["hour-1"][0]
assert end_t.isoformat() == periods["hour-1"][1]

start_t, end_t = resolve_period({"calendar": {"period": "day"}})
assert start_t.isoformat() == "2022-10-21T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-22T07:00:00+00:00"
assert start_t.isoformat() == periods["day"][0]
assert end_t.isoformat() == periods["day"][1]

start_t, end_t = resolve_period({"calendar": {"period": "day", "offset": -1}})
assert start_t.isoformat() == "2022-10-20T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-21T07:00:00+00:00"
assert start_t.isoformat() == periods["day-1"][0]
assert end_t.isoformat() == periods["day-1"][1]

start_t, end_t = resolve_period({"calendar": {"period": "week"}})
assert start_t.isoformat() == "2022-10-17T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-24T07:00:00+00:00"
assert start_t.isoformat() == periods["week"][0]
assert end_t.isoformat() == periods["week"][1]

start_t, end_t = resolve_period({"calendar": {"period": "week", "offset": -1}})
assert start_t.isoformat() == "2022-10-10T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-17T07:00:00+00:00"
assert start_t.isoformat() == periods["week-1"][0]
assert end_t.isoformat() == periods["week-1"][1]

start_t, end_t = resolve_period({"calendar": {"period": "month"}})
assert start_t.isoformat() == "2022-10-01T07:00:00+00:00"
assert end_t.isoformat() == "2022-11-01T07:00:00+00:00"
assert start_t.isoformat() == periods["month"][0]
assert end_t.isoformat() == periods["month"][1]

start_t, end_t = resolve_period({"calendar": {"period": "month", "offset": -1}})
assert start_t.isoformat() == "2022-09-01T07:00:00+00:00"
assert end_t.isoformat() == "2022-10-01T07:00:00+00:00"
assert start_t.isoformat() == periods["month-1"][0]
assert end_t.isoformat() == periods["month-1"][1]

start_t, end_t = resolve_period({"calendar": {"period": "year"}})
assert start_t.isoformat() == "2022-01-01T08:00:00+00:00"
assert end_t.isoformat() == "2023-01-01T08:00:00+00:00"
assert start_t.isoformat() == periods["year"][0]
assert end_t.isoformat() == periods["year"][1]

start_t, end_t = resolve_period({"calendar": {"period": "year", "offset": -1}})
assert start_t.isoformat() == "2021-01-01T08:00:00+00:00"
assert end_t.isoformat() == "2022-01-01T08:00:00+00:00"
assert start_t.isoformat() == periods["year-1"][0]
assert end_t.isoformat() == periods["year-1"][1]

# Fixed period
assert resolve_period({}) == (None, None)
Expand Down
Loading