This commit is contained in:
Julian Pawlowski 2025-11-02 12:13:20 +00:00
parent fc5e4ca6f4
commit 88ed07c4c0
3 changed files with 16 additions and 20 deletions

View file

@ -15,8 +15,6 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Callable
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from .api import ( from .api import (
@ -338,7 +336,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
homes_data = self.data.get("homes", {}) homes_data = self.data.get("homes", {})
return homes_data.get(home_id) return homes_data.get(home_id)
def get_current_interval_data(self) -> dict[str, Any] | None: def get_current_interval(self) -> dict[str, Any] | None:
"""Get the price data for the current interval.""" """Get the price data for the current interval."""
if not self.data: if not self.data:
return None return None
@ -368,9 +366,9 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
if not all_intervals: if not all_intervals:
return None return None
from .sensor import detect_interval_granularity from .sensor import detect_interval_granularity as detect_granularity
return detect_interval_granularity(all_intervals) return detect_granularity(all_intervals)
async def refresh_user_data(self) -> bool: async def refresh_user_data(self) -> bool:
"""Force refresh of user data and return True if data was updated.""" """Force refresh of user data and return True if data was updated."""
@ -399,8 +397,3 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
if not self._cached_user_data: if not self._cached_user_data:
return [] return []
return self._cached_user_data.get("homes", []) return self._cached_user_data.get("homes", [])
@callback
def async_add_listener(self, update_callback: Callable[[], None]) -> Callable[[], None]:
"""Add a listener for updates."""
return super().async_add_listener(update_callback)

View file

@ -271,7 +271,7 @@ class TibberPricesSensor(TibberPricesEntity, SensorEntity):
def _get_current_interval_data(self) -> dict | None: def _get_current_interval_data(self) -> dict | None:
"""Get the price data for the current interval using coordinator utility.""" """Get the price data for the current interval using coordinator utility."""
return self.coordinator.get_current_interval_data() return self.coordinator.get_current_interval()
def _get_price_level_value(self) -> str | None: def _get_price_level_value(self) -> str | None:
"""Get the current price level value as a translated string for the state.""" """Get the current price level value as a translated string for the state."""
@ -488,8 +488,12 @@ class TibberPricesSensor(TibberPricesEntity, SensorEntity):
return None return None
price_rating = self.coordinator.data.get("priceRating", {}) price_rating = self.coordinator.data.get("priceRating", {})
now = dt_util.now() now = dt_util.now()
# In the new flat format, price_rating[rating_type] is a list of entries # price_rating[rating_type] contains a dict with "entries" key, extract it
entries = price_rating.get(rating_type, []) rating_data = price_rating.get(rating_type, {})
if isinstance(rating_data, dict):
entries = rating_data.get("entries", [])
else:
entries = rating_data if isinstance(rating_data, list) else []
entry = self._find_rating_entry(entries, now, rating_type) entry = self._find_rating_entry(entries, now, rating_type)
if entry: if entry:
difference = entry.get("difference") difference = entry.get("difference")

View file

@ -37,14 +37,13 @@ class TestBasicCoordinator:
with patch( with patch(
"custom_components.tibber_prices.coordinator.aiohttp_client.async_get_clientsession", "custom_components.tibber_prices.coordinator.aiohttp_client.async_get_clientsession",
return_value=mock_session, return_value=mock_session,
): ), patch("custom_components.tibber_prices.coordinator.Store") as mock_store_class:
with patch("custom_components.tibber_prices.coordinator.Store") as mock_store_class: mock_store = Mock()
mock_store = Mock() mock_store.async_load = AsyncMock(return_value=None)
mock_store.async_load = AsyncMock(return_value=None) mock_store.async_save = AsyncMock()
mock_store.async_save = AsyncMock() mock_store_class.return_value = mock_store
mock_store_class.return_value = mock_store
return TibberPricesDataUpdateCoordinator(mock_hass, mock_config_entry) return TibberPricesDataUpdateCoordinator(mock_hass, mock_config_entry)
def test_coordinator_creation(self, coordinator): def test_coordinator_creation(self, coordinator):
"""Test that coordinator can be created.""" """Test that coordinator can be created."""