mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
tests
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
93b0051ade
commit
04192bbdc9
@ -134,7 +134,7 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
for start, state in hourly_data.items():
|
for start, state in hourly_data.items():
|
||||||
if state is None:
|
if state is None:
|
||||||
continue
|
continue
|
||||||
if last_stats_time and (start < last_stats_time or start > now):
|
if (last_stats_time and start < last_stats_time) or start > now:
|
||||||
continue
|
continue
|
||||||
_sum += state
|
_sum += state
|
||||||
statistics.append(
|
statistics.append(
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from mill import Heater, Mill
|
from mill import Heater, Mill, Sensor
|
||||||
|
|
||||||
from homeassistant.components.mill.const import DOMAIN
|
from homeassistant.components.mill.const import DOMAIN
|
||||||
from homeassistant.components.mill.coordinator import MillHistoricDataUpdateCoordinator
|
from homeassistant.components.mill.coordinator import MillHistoricDataUpdateCoordinator
|
||||||
@ -89,3 +89,137 @@ async def test_mill_historic_data(recorder_mock: Recorder, hass: HomeAssistant)
|
|||||||
|
|
||||||
_sum += val
|
_sum += val
|
||||||
assert stat["sum"] == _sum
|
assert stat["sum"] == _sum
|
||||||
|
|
||||||
|
|
||||||
|
async def test_mill_historic_data_no_heater(
|
||||||
|
recorder_mock: Recorder, hass: HomeAssistant
|
||||||
|
) -> None:
|
||||||
|
"""Test historic data from Mill."""
|
||||||
|
|
||||||
|
data = {
|
||||||
|
dt_util.parse_datetime("2024-12-03T00:00:00+01:00"): 2,
|
||||||
|
dt_util.parse_datetime("2024-12-03T01:00:00+01:00"): 3,
|
||||||
|
dt_util.parse_datetime("2024-12-03T02:00:00+01:00"): 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
mill_data_connection = Mill("", "", websession=AsyncMock())
|
||||||
|
mill_data_connection.fetch_heater_and_sensor_data = AsyncMock(return_value=None)
|
||||||
|
mill_data_connection.devices = {"dev_id": Sensor(name="sensor_name")}
|
||||||
|
mill_data_connection.fetch_historic_energy_usage = AsyncMock(return_value=data)
|
||||||
|
|
||||||
|
statistic_id = f"{DOMAIN}:energy_dev_id"
|
||||||
|
|
||||||
|
coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
|
hass, mill_data_connection=mill_data_connection
|
||||||
|
)
|
||||||
|
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) == 0
|
||||||
|
|
||||||
|
|
||||||
|
async def test_mill_historic_data_no_data(
|
||||||
|
recorder_mock: Recorder, hass: HomeAssistant
|
||||||
|
) -> None:
|
||||||
|
"""Test historic data from Mill."""
|
||||||
|
|
||||||
|
data = {
|
||||||
|
dt_util.parse_datetime("2024-12-03T00:00:00+01:00"): 2,
|
||||||
|
dt_util.parse_datetime("2024-12-03T01:00:00+01:00"): 3,
|
||||||
|
dt_util.parse_datetime("2024-12-03T02:00:00+01:00"): 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
mill_data_connection = Mill("", "", websession=AsyncMock())
|
||||||
|
mill_data_connection.fetch_heater_and_sensor_data = AsyncMock(return_value=None)
|
||||||
|
mill_data_connection.devices = {"dev_id": Heater(name="heater_name")}
|
||||||
|
mill_data_connection.fetch_historic_energy_usage = AsyncMock(return_value=data)
|
||||||
|
|
||||||
|
coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
|
hass, mill_data_connection=mill_data_connection
|
||||||
|
)
|
||||||
|
await coordinator._async_update_data()
|
||||||
|
await async_wait_recording_done(hass)
|
||||||
|
|
||||||
|
statistic_id = f"{DOMAIN}:energy_dev_id"
|
||||||
|
|
||||||
|
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]) == 3
|
||||||
|
|
||||||
|
mill_data_connection.fetch_historic_energy_usage = AsyncMock(return_value=None)
|
||||||
|
|
||||||
|
coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
|
hass, mill_data_connection=mill_data_connection
|
||||||
|
)
|
||||||
|
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]) == 3
|
||||||
|
|
||||||
|
|
||||||
|
async def test_mill_historic_data_invalid_data(
|
||||||
|
recorder_mock: Recorder, hass: HomeAssistant
|
||||||
|
) -> None:
|
||||||
|
"""Test historic data from Mill."""
|
||||||
|
|
||||||
|
data = {
|
||||||
|
dt_util.parse_datetime("2024-12-03T00:00:00+01:00"): None,
|
||||||
|
dt_util.parse_datetime("2024-12-03T01:00:00+01:00"): 3,
|
||||||
|
dt_util.parse_datetime("3024-12-03T02:00:00+01:00"): 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
mill_data_connection = Mill("", "", websession=AsyncMock())
|
||||||
|
mill_data_connection.fetch_heater_and_sensor_data = AsyncMock(return_value=None)
|
||||||
|
mill_data_connection.devices = {"dev_id": Heater(name="heater_name")}
|
||||||
|
mill_data_connection.fetch_historic_energy_usage = AsyncMock(return_value=data)
|
||||||
|
|
||||||
|
statistic_id = f"{DOMAIN}:energy_dev_id"
|
||||||
|
|
||||||
|
coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
|
hass, mill_data_connection=mill_data_connection
|
||||||
|
)
|
||||||
|
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]) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user