1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 14:08:21 +00:00

Add helper to calculate statistic period start and end (#82493)

* Add helper to calculate statistic period start and end

* Don't parse values in resolve_period

* Add specific test for resolve_period

* Improve typing

* Move to recorder/util.py

* Extract period schema
This commit is contained in:
Erik Montnemery
2022-11-26 19:00:40 +01:00
committed by GitHub
parent 405c2ca82d
commit 2fe8e95309
4 changed files with 206 additions and 86 deletions

View File

@@ -1,9 +1,10 @@
"""Test util methods."""
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
import os
import sqlite3
from unittest.mock import MagicMock, Mock, patch
from freezegun import freeze_time
import pytest
from sqlalchemy import text
from sqlalchemy.engine.result import ChunkedIteratorResult
@@ -19,6 +20,7 @@ from homeassistant.components.recorder.models import UnsupportedDialect
from homeassistant.components.recorder.util import (
end_incomplete_runs,
is_second_sunday,
resolve_period,
session_scope,
)
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
@@ -776,3 +778,80 @@ def test_execute_stmt_lambda_element(hass_recorder):
with patch.object(session, "execute", MockExecutor):
rows = util.execute_stmt_lambda_element(session, stmt, now, tomorrow)
assert rows == ["mock_row"]
@freeze_time(datetime(2022, 10, 21, 7, 25, tzinfo=timezone.utc))
async def test_resolve_period(hass):
"""Test statistic_during_period."""
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
# Fixed period
assert resolve_period({}) == (None, None)
assert resolve_period({"fixed_period": {"end_time": now}}) == (None, now)
assert resolve_period({"fixed_period": {"start_time": now}}) == (now, None)
assert resolve_period({"fixed_period": {"end_time": now, "start_time": now}}) == (
now,
now,
)
# Rolling window
assert resolve_period(
{"rolling_window": {"duration": timedelta(hours=1, minutes=25)}}
) == (now - timedelta(hours=1, minutes=25), now)
assert resolve_period(
{
"rolling_window": {
"duration": timedelta(hours=1),
"offset": timedelta(minutes=-25),
}
}
) == (now - timedelta(hours=1, minutes=25), now - timedelta(minutes=25))