From 88ed07c4c0f2592ba34f29ce0e491b8c5ec71fbb Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Sun, 2 Nov 2025 12:13:20 +0000 Subject: [PATCH] fix --- custom_components/tibber_prices/coordinator.py | 13 +++---------- custom_components/tibber_prices/sensor.py | 10 +++++++--- tests/test_coordinator_basic.py | 13 ++++++------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/custom_components/tibber_prices/coordinator.py b/custom_components/tibber_prices/coordinator.py index 057177d..c733d30 100644 --- a/custom_components/tibber_prices/coordinator.py +++ b/custom_components/tibber_prices/coordinator.py @@ -15,8 +15,6 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda from homeassistant.util import dt as dt_util if TYPE_CHECKING: - from collections.abc import Callable - from homeassistant.config_entries import ConfigEntry from .api import ( @@ -338,7 +336,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): homes_data = self.data.get("homes", {}) 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.""" if not self.data: return None @@ -368,9 +366,9 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): if not all_intervals: 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: """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: return [] 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) diff --git a/custom_components/tibber_prices/sensor.py b/custom_components/tibber_prices/sensor.py index 051b2de..12997cc 100644 --- a/custom_components/tibber_prices/sensor.py +++ b/custom_components/tibber_prices/sensor.py @@ -271,7 +271,7 @@ class TibberPricesSensor(TibberPricesEntity, SensorEntity): def _get_current_interval_data(self) -> dict | None: """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: """Get the current price level value as a translated string for the state.""" @@ -488,8 +488,12 @@ class TibberPricesSensor(TibberPricesEntity, SensorEntity): return None price_rating = self.coordinator.data.get("priceRating", {}) now = dt_util.now() - # In the new flat format, price_rating[rating_type] is a list of entries - entries = price_rating.get(rating_type, []) + # price_rating[rating_type] contains a dict with "entries" key, extract it + 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) if entry: difference = entry.get("difference") diff --git a/tests/test_coordinator_basic.py b/tests/test_coordinator_basic.py index 6d2f90e..4278639 100644 --- a/tests/test_coordinator_basic.py +++ b/tests/test_coordinator_basic.py @@ -37,14 +37,13 @@ class TestBasicCoordinator: with patch( "custom_components.tibber_prices.coordinator.aiohttp_client.async_get_clientsession", return_value=mock_session, - ): - with patch("custom_components.tibber_prices.coordinator.Store") as mock_store_class: - mock_store = Mock() - mock_store.async_load = AsyncMock(return_value=None) - mock_store.async_save = AsyncMock() - mock_store_class.return_value = mock_store + ), patch("custom_components.tibber_prices.coordinator.Store") as mock_store_class: + mock_store = Mock() + mock_store.async_load = AsyncMock(return_value=None) + mock_store.async_save = AsyncMock() + 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): """Test that coordinator can be created."""