|
| 1 | +"""Tier-2 tests for the grid-cost breakdown attributes on the current-price sensor. |
| 2 | +
|
| 3 | +Live reconciliation (2026-07-12): all_in = (spot + Σ net grid components) × (1 + vat). |
| 4 | +The itemised components are NET (ex-VAT); the SDK scalar grid_costs_total is GROSS |
| 5 | +and is intentionally NOT surfaced. The sensor exposes the net adder so |
| 6 | +`spot_price + grid_costs` reconciles to the pre-VAT price. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import datetime |
| 12 | +from unittest.mock import MagicMock, patch |
| 13 | + |
| 14 | +from homeassistant.core import HomeAssistant |
| 15 | +from homeassistant.helpers import entity_registry as er |
| 16 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 17 | + |
| 18 | +from custom_components.onekommafive.const import ( |
| 19 | + CONF_PASSWORD, |
| 20 | + CONF_SYSTEM_ID, |
| 21 | + CONF_USERNAME, |
| 22 | + DOMAIN, |
| 23 | +) |
| 24 | + |
| 25 | +SPOT = 0.126 |
| 26 | +ENERGY_TAX = 0.13756 |
| 27 | +VAT = 0.19 |
| 28 | +ALL_IN = round((SPOT + ENERGY_TAX) * (1 + VAT), 7) # 0.3136364 |
| 29 | + |
| 30 | + |
| 31 | +def _market_prices(*, uses_fallback: bool = False) -> MagicMock: |
| 32 | + slot = ( |
| 33 | + (datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(hours=1)) |
| 34 | + .isoformat() |
| 35 | + .replace("+00:00", "Z") |
| 36 | + ) |
| 37 | + return MagicMock( |
| 38 | + prices={slot: SPOT}, |
| 39 | + prices_with_grid_costs={slot: round(SPOT + ENERGY_TAX, 7)}, |
| 40 | + prices_with_grid_costs_and_vat={slot: ALL_IN}, |
| 41 | + average_price_all_in=ALL_IN, |
| 42 | + lowest_price_all_in=ALL_IN, |
| 43 | + highest_price_all_in=ALL_IN, |
| 44 | + grid_cost_energy_tax=ENERGY_TAX, |
| 45 | + grid_cost_purchasing=0.0, |
| 46 | + grid_cost_fixed_tariff=0.0, |
| 47 | + grid_cost_dynamic_markup=0.0, |
| 48 | + grid_cost_feed_in_remuneration_adj=0.0, |
| 49 | + vat=VAT, |
| 50 | + uses_fallback_grid_costs=uses_fallback, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +async def _setup(hass: HomeAssistant, prices: MagicMock, mock_system_factory) -> None: |
| 55 | + system = mock_system_factory(system_id="sys-1", prices=prices) |
| 56 | + entry = MockConfigEntry( |
| 57 | + domain=DOMAIN, |
| 58 | + unique_id="sys-1", |
| 59 | + data={CONF_USERNAME: "u@x.de", CONF_PASSWORD: "pw", CONF_SYSTEM_ID: "sys-1"}, |
| 60 | + ) |
| 61 | + entry.add_to_hass(hass) |
| 62 | + with ( |
| 63 | + patch("onekommafive.systems.Systems") as mock_systems_cls, |
| 64 | + patch("onekommafive.client.Client"), |
| 65 | + ): |
| 66 | + mock_systems_cls.return_value.get_system.return_value = system |
| 67 | + mock_systems_cls.return_value.get_systems.return_value = [system] |
| 68 | + await hass.config_entries.async_setup(entry.entry_id) |
| 69 | + await hass.async_block_till_done() |
| 70 | + |
| 71 | + |
| 72 | +def _current_price_state(hass: HomeAssistant): |
| 73 | + entity_id = er.async_get(hass).async_get_entity_id( |
| 74 | + "sensor", "onekommafive", "sys-1_current_electricity_price" |
| 75 | + ) |
| 76 | + assert entity_id is not None |
| 77 | + return hass.states.get(entity_id) |
| 78 | + |
| 79 | + |
| 80 | +async def test_breakdown_attributes_reconcile(hass: HomeAssistant, mock_system_factory) -> None: |
| 81 | + await _setup(hass, _market_prices(), mock_system_factory) |
| 82 | + attrs = _current_price_state(hass).attributes |
| 83 | + |
| 84 | + assert attrs["spot_price"] == SPOT |
| 85 | + assert attrs["grid_costs"] == ENERGY_TAX |
| 86 | + assert attrs["vat_rate"] == VAT |
| 87 | + assert attrs["uses_fallback_grid_costs"] is False |
| 88 | + assert attrs["grid_cost_components"]["energy_tax"] == ENERGY_TAX |
| 89 | + # zero components are surfaced (present, not None), not dropped |
| 90 | + assert attrs["grid_cost_components"]["dynamic_markup"] == 0.0 |
| 91 | + |
| 92 | + # The exposed net figures must reconcile to the all-in state. |
| 93 | + reconstructed = round((attrs["spot_price"] + attrs["grid_costs"]) * (1 + attrs["vat_rate"]), 7) |
| 94 | + assert reconstructed == ALL_IN |
| 95 | + |
| 96 | + |
| 97 | +async def test_fallback_flag_surfaces_true(hass: HomeAssistant, mock_system_factory) -> None: |
| 98 | + await _setup(hass, _market_prices(uses_fallback=True), mock_system_factory) |
| 99 | + attrs = _current_price_state(hass).attributes |
| 100 | + assert attrs["uses_fallback_grid_costs"] is True |
0 commit comments