Skip to content
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
7 changes: 4 additions & 3 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from pytest import approx

from homeassistant.components import history, recorder
from homeassistant.components import history
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.components.recorder.models import process_timestamp
import homeassistant.core as ha
Expand All @@ -20,6 +20,7 @@
from tests.components.recorder.common import (
async_recorder_block_till_done,
async_wait_recording_done,
do_adhoc_statistics,
wait_recording_done,
)

Expand Down Expand Up @@ -879,7 +880,7 @@ async def test_statistics_during_period(
hass.states.async_set("sensor.test", state, attributes=attributes)
await async_wait_recording_done(hass)

hass.data[recorder.DATA_INSTANCE].do_adhoc_statistics(start=now)
do_adhoc_statistics(hass, start=now)
await async_wait_recording_done(hass)

client = await hass_ws_client()
Expand Down Expand Up @@ -1021,7 +1022,7 @@ async def test_list_statistic_ids(
}
]

hass.data[recorder.DATA_INSTANCE].do_adhoc_statistics(start=now)
do_adhoc_statistics(hass, start=now)
await async_recorder_block_till_done(hass)
# Remove the state, statistics will now be fetched from the database
hass.states.async_remove("sensor.test")
Expand Down
11 changes: 10 additions & 1 deletion tests/components/recorder/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
from __future__ import annotations

from datetime import datetime, timedelta
from typing import cast
from typing import Any, cast

from sqlalchemy import create_engine
from sqlalchemy.orm.session import Session

from homeassistant import core as ha
from homeassistant.components import recorder
from homeassistant.components.recorder import get_instance, statistics
from homeassistant.components.recorder.models import RecorderRuns
from homeassistant.components.recorder.tasks import StatisticsTask
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util

Expand All @@ -19,6 +21,13 @@
DEFAULT_PURGE_TASKS = 3


def do_adhoc_statistics(hass: HomeAssistant, **kwargs: Any) -> None:
"""Trigger an adhoc statistics run."""
if not (start := kwargs.get("start")):
start = statistics.get_start_time()
get_instance(hass).queue_task(StatisticsTask(start))


def wait_recording_done(hass: HomeAssistant) -> None:
"""Block till recording is done."""
hass.block_till_done()
Expand Down
19 changes: 8 additions & 11 deletions tests/components/recorder/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from homeassistant.setup import setup_component
import homeassistant.util.dt as dt_util

from .common import async_wait_recording_done
from .common import async_wait_recording_done, do_adhoc_statistics

from tests.common import mock_registry
from tests.components.recorder.common import wait_recording_done
Expand All @@ -57,8 +57,8 @@ def test_compile_hourly_statistics(hass_recorder):
stats = get_last_short_term_statistics(hass, 0, "sensor.test1", True)
assert stats == {}

recorder.do_adhoc_statistics(start=zero)
recorder.do_adhoc_statistics(start=four)
do_adhoc_statistics(hass, start=zero)
do_adhoc_statistics(hass, start=four)
wait_recording_done(hass)
expected_1 = {
"statistic_id": "sensor.test1",
Expand Down Expand Up @@ -197,12 +197,11 @@ def test_compile_periodic_statistics_exception(
"""Test exception handling when compiling periodic statistics."""

hass = hass_recorder()
recorder = hass.data[DATA_INSTANCE]
setup_component(hass, "sensor", {})

now = dt_util.utcnow()
recorder.do_adhoc_statistics(start=now)
recorder.do_adhoc_statistics(start=now + timedelta(minutes=5))
do_adhoc_statistics(hass, start=now)
do_adhoc_statistics(hass, start=now + timedelta(minutes=5))
wait_recording_done(hass)
expected_1 = {
"statistic_id": "sensor.test1",
Expand Down Expand Up @@ -249,7 +248,6 @@ def test_compile_periodic_statistics_exception(
def test_rename_entity(hass_recorder):
"""Test statistics is migrated when entity_id is changed."""
hass = hass_recorder()
recorder = hass.data[DATA_INSTANCE]
setup_component(hass, "sensor", {})

entity_reg = mock_registry(hass)
Expand Down Expand Up @@ -277,7 +275,7 @@ def add_entry():
stats = get_last_short_term_statistics(hass, 0, "sensor.test1", True)
assert stats == {}

recorder.do_adhoc_statistics(start=zero)
do_adhoc_statistics(hass, start=zero)
wait_recording_done(hass)
expected_1 = {
"statistic_id": "sensor.test1",
Expand Down Expand Up @@ -317,7 +315,6 @@ def rename_entry():
def test_statistics_duplicated(hass_recorder, caplog):
"""Test statistics with same start time is not compiled."""
hass = hass_recorder()
recorder = hass.data[DATA_INSTANCE]
setup_component(hass, "sensor", {})
zero, four, states = record_states(hass)
hist = history.get_significant_states(hass, zero, four)
Expand All @@ -331,15 +328,15 @@ def test_statistics_duplicated(hass_recorder, caplog):
"homeassistant.components.sensor.recorder.compile_statistics",
return_value=statistics.PlatformCompiledStatistics([], {}),
) as compile_statistics:
recorder.do_adhoc_statistics(start=zero)
do_adhoc_statistics(hass, start=zero)
wait_recording_done(hass)
assert compile_statistics.called
compile_statistics.reset_mock()
assert "Compiling statistics for" in caplog.text
assert "Statistics already compiled" not in caplog.text
caplog.clear()

recorder.do_adhoc_statistics(start=zero)
do_adhoc_statistics(hass, start=zero)
wait_recording_done(hass)
assert not compile_statistics.called
compile_statistics.reset_mock()
Expand Down
7 changes: 4 additions & 3 deletions tests/components/recorder/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
async_recorder_block_till_done,
async_wait_recording_done,
create_engine_test,
do_adhoc_statistics,
)

from tests.common import async_fire_time_changed
Expand Down Expand Up @@ -84,7 +85,7 @@ async def test_clear_statistics(hass, hass_ws_client, recorder_mock):
hass.states.async_set("sensor.test3", state * 3, attributes=attributes)
await async_wait_recording_done(hass)

hass.data[DATA_INSTANCE].do_adhoc_statistics(start=now)
do_adhoc_statistics(hass, start=now)
await async_recorder_block_till_done(hass)

client = await hass_ws_client()
Expand Down Expand Up @@ -208,7 +209,7 @@ async def test_update_statistics_metadata(
hass.states.async_set("sensor.test", state, attributes=attributes)
await async_wait_recording_done(hass)

hass.data[DATA_INSTANCE].do_adhoc_statistics(period="hourly", start=now)
do_adhoc_statistics(hass, period="hourly", start=now)
await async_recorder_block_till_done(hass)

client = await hass_ws_client()
Expand Down Expand Up @@ -521,7 +522,7 @@ async def test_get_statistics_metadata(
}
]

hass.data[recorder.DATA_INSTANCE].do_adhoc_statistics(start=now)
do_adhoc_statistics(hass, start=now)
await async_recorder_block_till_done(hass)
# Remove the state, statistics will now be fetched from the database
hass.states.async_remove("sensor.test")
Expand Down
Loading