mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Correct end time for monthly statistics summary (#59551)
* Correct end time for monthly statistics summary * Add tests
This commit is contained in:
parent
0ae5b9e880
commit
bcd9f3c05f
@ -751,12 +751,12 @@ def _reduce_statistics_per_month(
|
|||||||
|
|
||||||
def period_start_end(time: datetime) -> tuple[datetime, datetime]:
|
def period_start_end(time: datetime) -> tuple[datetime, datetime]:
|
||||||
"""Return the start and end of the period (month) time is within."""
|
"""Return the start and end of the period (month) time is within."""
|
||||||
start = dt_util.as_utc(
|
start_local = dt_util.as_local(time).replace(
|
||||||
dt_util.as_local(time).replace(
|
|
||||||
day=1, hour=0, minute=0, second=0, microsecond=0
|
day=1, hour=0, minute=0, second=0, microsecond=0
|
||||||
)
|
)
|
||||||
)
|
start = dt_util.as_utc(start_local)
|
||||||
end = (start + timedelta(days=31)).replace(day=1)
|
end_local = (start_local + timedelta(days=31)).replace(day=1)
|
||||||
|
end = dt_util.as_utc(end_local)
|
||||||
return (start, end)
|
return (start, end)
|
||||||
|
|
||||||
return _reduce_statistics(stats, same_period, period_start_end, timedelta(days=31))
|
return _reduce_statistics(stats, same_period, period_start_end, timedelta(days=31))
|
||||||
|
@ -544,6 +544,95 @@ def test_external_statistics_errors(hass_recorder, caplog):
|
|||||||
assert get_metadata(hass, statistic_ids=("test:total_energy_import",)) == {}
|
assert get_metadata(hass, statistic_ids=("test:total_energy_import",)) == {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("timezone", ["America/Regina", "Europe/Vienna", "UTC"])
|
||||||
|
@pytest.mark.freeze_time("2021-08-01 00:00:00+00:00")
|
||||||
|
def test_monthly_statistics(hass_recorder, caplog, timezone):
|
||||||
|
"""Test inserting external statistics."""
|
||||||
|
dt_util.set_default_time_zone(dt_util.get_time_zone(timezone))
|
||||||
|
|
||||||
|
hass = hass_recorder()
|
||||||
|
wait_recording_done(hass)
|
||||||
|
assert "Compiling statistics for" not in caplog.text
|
||||||
|
assert "Statistics already compiled" not in caplog.text
|
||||||
|
|
||||||
|
zero = dt_util.utcnow()
|
||||||
|
period1 = dt_util.as_utc(dt_util.parse_datetime("2021-09-01 00:00:00"))
|
||||||
|
period2 = dt_util.as_utc(dt_util.parse_datetime("2021-09-30 23:00:00"))
|
||||||
|
period3 = dt_util.as_utc(dt_util.parse_datetime("2021-10-01 00:00:00"))
|
||||||
|
period4 = dt_util.as_utc(dt_util.parse_datetime("2021-10-31 23:00:00"))
|
||||||
|
|
||||||
|
external_statistics = (
|
||||||
|
{
|
||||||
|
"start": period1,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": 0,
|
||||||
|
"sum": 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"start": period2,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": 1,
|
||||||
|
"sum": 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"start": period3,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": 2,
|
||||||
|
"sum": 4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"start": period4,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": 3,
|
||||||
|
"sum": 5,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
external_metadata = {
|
||||||
|
"has_mean": False,
|
||||||
|
"has_sum": True,
|
||||||
|
"name": "Total imported energy",
|
||||||
|
"source": "test",
|
||||||
|
"statistic_id": "test:total_energy_import",
|
||||||
|
"unit_of_measurement": "kWh",
|
||||||
|
}
|
||||||
|
|
||||||
|
async_add_external_statistics(hass, external_metadata, external_statistics)
|
||||||
|
wait_recording_done(hass)
|
||||||
|
stats = statistics_during_period(hass, zero, period="month")
|
||||||
|
sep_start = dt_util.as_utc(dt_util.parse_datetime("2021-09-01 00:00:00"))
|
||||||
|
sep_end = dt_util.as_utc(dt_util.parse_datetime("2021-10-01 00:00:00"))
|
||||||
|
oct_start = dt_util.as_utc(dt_util.parse_datetime("2021-10-01 00:00:00"))
|
||||||
|
oct_end = dt_util.as_utc(dt_util.parse_datetime("2021-11-01 00:00:00"))
|
||||||
|
assert stats == {
|
||||||
|
"test:total_energy_import": [
|
||||||
|
{
|
||||||
|
"statistic_id": "test:total_energy_import",
|
||||||
|
"start": sep_start.isoformat(),
|
||||||
|
"end": sep_end.isoformat(),
|
||||||
|
"max": None,
|
||||||
|
"mean": None,
|
||||||
|
"min": None,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": approx(1.0),
|
||||||
|
"sum": approx(3.0),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"statistic_id": "test:total_energy_import",
|
||||||
|
"start": oct_start.isoformat(),
|
||||||
|
"end": oct_end.isoformat(),
|
||||||
|
"max": None,
|
||||||
|
"mean": None,
|
||||||
|
"min": None,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": approx(3.0),
|
||||||
|
"sum": approx(5.0),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
dt_util.set_default_time_zone(dt_util.get_time_zone("UTC"))
|
||||||
|
|
||||||
|
|
||||||
def record_states(hass):
|
def record_states(hass):
|
||||||
"""Record some test states.
|
"""Record some test states.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user