mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add test
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
a6f508c624
commit
eaf5350aa1
@ -88,7 +88,6 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
last_stats = await recoder_instance.async_add_executor_job(
|
last_stats = await recoder_instance.async_add_executor_job(
|
||||||
get_last_statistics, self.hass, 1, statistic_id, True, set()
|
get_last_statistics, self.hass, 1, statistic_id, True, set()
|
||||||
)
|
)
|
||||||
|
|
||||||
if not last_stats or not last_stats.get(statistic_id):
|
if not last_stats or not last_stats.get(statistic_id):
|
||||||
hourly_data = (
|
hourly_data = (
|
||||||
await self.mill_data_connection.fetch_historic_energy_usage(
|
await self.mill_data_connection.fetch_historic_energy_usage(
|
||||||
@ -115,7 +114,6 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
continue
|
continue
|
||||||
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
||||||
start_time = next(iter(hourly_data))
|
start_time = next(iter(hourly_data))
|
||||||
|
|
||||||
stats = await recoder_instance.async_add_executor_job(
|
stats = await recoder_instance.async_add_executor_job(
|
||||||
statistics_during_period,
|
statistics_during_period,
|
||||||
self.hass,
|
self.hass,
|
||||||
@ -128,7 +126,7 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
)
|
)
|
||||||
stat = stats[statistic_id][0]
|
stat = stats[statistic_id][0]
|
||||||
|
|
||||||
_sum = cast(float, stat["sum"])
|
_sum = cast(float, stat["sum"]) - cast(float, stat["state"])
|
||||||
last_stats_time = dt_util.utc_from_timestamp(stat["start"])
|
last_stats_time = dt_util.utc_from_timestamp(stat["start"])
|
||||||
|
|
||||||
statistics = []
|
statistics = []
|
||||||
|
@ -14,8 +14,8 @@ from homeassistant.util import dt as dt_util
|
|||||||
from tests.components.recorder.common import async_wait_recording_done
|
from tests.components.recorder.common import async_wait_recording_done
|
||||||
|
|
||||||
|
|
||||||
async def test_async_setup_entry(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
async def test_mill_historic_data(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
||||||
"""Test setup Mill."""
|
"""Test historic data from Mill."""
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
dt_util.parse_datetime("2024-12-03T00:00:00+01:00"): 2,
|
dt_util.parse_datetime("2024-12-03T00:00:00+01:00"): 2,
|
||||||
@ -57,3 +57,35 @@ async def test_async_setup_entry(recorder_mock: Recorder, hass: HomeAssistant) -
|
|||||||
|
|
||||||
_sum += data[start]
|
_sum += data[start]
|
||||||
assert stat["sum"] == _sum
|
assert stat["sum"] == _sum
|
||||||
|
|
||||||
|
data2 = {
|
||||||
|
dt_util.parse_datetime("2024-12-03T02:00:00+01:00"): 4.5,
|
||||||
|
dt_util.parse_datetime("2024-12-03T03:00:00+01:00"): 5,
|
||||||
|
dt_util.parse_datetime("2024-12-03T04:00:00+01:00"): 6,
|
||||||
|
dt_util.parse_datetime("2024-12-03T05:00:00+01:00"): 7,
|
||||||
|
}
|
||||||
|
mill_data_connection.fetch_historic_energy_usage = AsyncMock(return_value=data2)
|
||||||
|
await coordinator._async_update_data()
|
||||||
|
await async_wait_recording_done(hass)
|
||||||
|
stats = await hass.async_add_executor_job(
|
||||||
|
statistics_during_period,
|
||||||
|
hass,
|
||||||
|
next(iter(data)),
|
||||||
|
None,
|
||||||
|
{statistic_id},
|
||||||
|
"hour",
|
||||||
|
None,
|
||||||
|
{"start", "state", "mean", "min", "max", "last_reset", "sum"},
|
||||||
|
)
|
||||||
|
assert len(stats) == 1
|
||||||
|
assert len(stats[statistic_id]) == 6
|
||||||
|
_sum = 0
|
||||||
|
for stat in stats[statistic_id]:
|
||||||
|
start = dt_util.utc_from_timestamp(stat["start"])
|
||||||
|
val = data2.get(start) if start in data2 else data.get(start)
|
||||||
|
assert val is not None
|
||||||
|
assert stat["state"] == val
|
||||||
|
assert stat["last_reset"] is None
|
||||||
|
|
||||||
|
_sum += val
|
||||||
|
assert stat["sum"] == _sum
|
||||||
|
Loading…
x
Reference in New Issue
Block a user