|
| 1 | +"""Tests for the long-term-statistics backfill module.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from datetime import UTC, datetime |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from eac_dso_portal import Reading |
| 9 | +from homeassistant.const import UnitOfEnergy |
| 10 | +from homeassistant.core import HomeAssistant |
| 11 | +from homeassistant.helpers import entity_registry as er |
| 12 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 13 | + |
| 14 | +from custom_components.eac_cyprus.const import CONF_EMAIL, CONF_PASSWORD, DOMAIN |
| 15 | +from custom_components.eac_cyprus.statistics import import_cumulative_history |
| 16 | + |
| 17 | + |
| 18 | +def _entry(hass: HomeAssistant) -> MockConfigEntry: |
| 19 | + entry = MockConfigEntry( |
| 20 | + domain=DOMAIN, |
| 21 | + unique_id="test@example.com", |
| 22 | + data={CONF_EMAIL: "test@example.com", CONF_PASSWORD: "x"}, |
| 23 | + title="Test User", |
| 24 | + ) |
| 25 | + entry.add_to_hass(hass) |
| 26 | + return entry |
| 27 | + |
| 28 | + |
| 29 | +def _readings(*pairs: tuple[str, float | None, float]) -> tuple[Reading, ...]: |
| 30 | + """Build Readings from (iso_dt, reading|None, value) triples.""" |
| 31 | + return tuple( |
| 32 | + Reading(dt=datetime.fromisoformat(dt), reading=r, value=v) |
| 33 | + for dt, r, v in pairs |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +async def test_no_op_when_entity_not_registered(hass: HomeAssistant) -> None: |
| 38 | + """First-run safety: skip silently if the sensor is not in the registry yet.""" |
| 39 | + rdgs = _readings(("2026-05-01T00:00:00", 100.0, 5.0)) |
| 40 | + with patch( |
| 41 | + "custom_components.eac_cyprus.statistics.async_import_statistics" |
| 42 | + ) as mock_import: |
| 43 | + import_cumulative_history(hass, "999", "mc-X", "S-KWH-24H", rdgs) |
| 44 | + mock_import.assert_not_called() |
| 45 | + |
| 46 | + |
| 47 | +async def test_no_op_when_no_cumulative_readings(hass: HomeAssistant) -> None: |
| 48 | + """Interval channels (reading=None) produce no statistics.""" |
| 49 | + registry = er.async_get(hass) |
| 50 | + registry.async_get_or_create("sensor", DOMAIN, "111_mc-30min") |
| 51 | + interval_only = _readings( |
| 52 | + ("2026-05-01T01:00:00", None, 0.5), |
| 53 | + ("2026-05-01T01:30:00", None, 0.6), |
| 54 | + ) |
| 55 | + with patch( |
| 56 | + "custom_components.eac_cyprus.statistics.async_import_statistics" |
| 57 | + ) as mock_import: |
| 58 | + import_cumulative_history(hass, "111", "mc-30min", "KWH-30MIN-LP-IMP", interval_only) |
| 59 | + mock_import.assert_not_called() |
| 60 | + |
| 61 | + |
| 62 | +async def test_imports_cumulative_kwh_with_correct_metadata( |
| 63 | + hass: HomeAssistant, |
| 64 | +) -> None: |
| 65 | + registry = er.async_get(hass) |
| 66 | + entry = registry.async_get_or_create("sensor", DOMAIN, "111_mc-total") |
| 67 | + rdgs = _readings( |
| 68 | + ("2026-05-01T00:00:00", 2900.0, 23.0), |
| 69 | + ("2026-05-02T00:00:00", 2924.0, 24.0), |
| 70 | + ("2026-05-03T00:00:00", 2950.0, 26.0), |
| 71 | + ) |
| 72 | + |
| 73 | + with patch( |
| 74 | + "custom_components.eac_cyprus.statistics.async_import_statistics" |
| 75 | + ) as mock_import: |
| 76 | + import_cumulative_history(hass, "111", "mc-total", "S-KWH-24H", rdgs) |
| 77 | + |
| 78 | + mock_import.assert_called_once() |
| 79 | + _, metadata, stats = mock_import.call_args.args |
| 80 | + assert metadata["statistic_id"] == entry.entity_id |
| 81 | + assert metadata["source"] == "recorder" |
| 82 | + assert metadata["has_sum"] is True |
| 83 | + assert metadata["has_mean"] is False |
| 84 | + assert metadata["unit_of_measurement"] == UnitOfEnergy.KILO_WATT_HOUR |
| 85 | + assert len(stats) == 3 |
| 86 | + # Each stat has tz-aware UTC start at the hour boundary, sum and state set. |
| 87 | + for s, expected_reading in zip(stats, [2900.0, 2924.0, 2950.0], strict=True): |
| 88 | + assert s["start"].tzinfo is UTC |
| 89 | + assert s["start"].minute == 0 and s["start"].second == 0 |
| 90 | + assert s["sum"] == expected_reading |
| 91 | + assert s["state"] == expected_reading |
| 92 | + |
| 93 | + |
| 94 | +async def test_coordinator_pushes_stats_for_cumulative_channel_only( |
| 95 | + hass: HomeAssistant, mock_client |
| 96 | +) -> None: |
| 97 | + """Integration check: cumulative channel triggers import, 30-min channel does not.""" |
| 98 | + from custom_components.eac_cyprus.coordinator import EacCoordinator |
| 99 | + |
| 100 | + entry = _entry(hass) |
| 101 | + |
| 102 | + # Pre-register the two sensors so import has somewhere to attach. |
| 103 | + registry = er.async_get(hass) |
| 104 | + registry.async_get_or_create("sensor", DOMAIN, "111111111111_mc-total-24h") |
| 105 | + registry.async_get_or_create("sensor", DOMAIN, "111111111111_mc-30min-imp") |
| 106 | + |
| 107 | + coord = EacCoordinator(hass, mock_client, entry) |
| 108 | + with patch( |
| 109 | + "custom_components.eac_cyprus.coordinator.import_cumulative_history" |
| 110 | + ) as mock_import: |
| 111 | + await coord._async_update_data() |
| 112 | + |
| 113 | + # The shared mock_client returns a 30-min reading (no `reading`) and a |
| 114 | + # daily total (with `reading`); only the latter qualifies. |
| 115 | + called_channels = [c.kwargs.get("channel_id") or c.args[2] for c in mock_import.call_args_list] |
| 116 | + assert "mc-total-24h" in called_channels |
| 117 | + assert "mc-30min-imp" not in called_channels |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.usefixtures("hass") |
| 121 | +def _unused_marker(): |
| 122 | + """Anchor for collecting pytest-homeassistant-custom-component fixtures.""" |
| 123 | + pass |
0 commit comments