From 8c43f2750fc2faaa1b710b098a5e86d5e5151eaa Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Wed, 21 May 2025 11:29:50 +0000 Subject: [PATCH] fix --- custom_components/tibber_prices/__init__.py | 13 ++++++++++--- custom_components/tibber_prices/binary_sensor.py | 10 +++++----- custom_components/tibber_prices/services.py | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/custom_components/tibber_prices/__init__.py b/custom_components/tibber_prices/__init__.py index 92661b5..c4a7ba6 100644 --- a/custom_components/tibber_prices/__init__.py +++ b/custom_components/tibber_prices/__init__.py @@ -9,6 +9,7 @@ from __future__ import annotations from typing import TYPE_CHECKING +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.storage import Store @@ -37,6 +38,7 @@ async def async_setup_entry( entry: TibberPricesConfigEntry, ) -> bool: """Set up this integration using UI.""" + LOGGER.debug(f"[tibber_prices] async_setup_entry called for entry_id={entry.entry_id}") # Preload translations to populate the cache await async_load_translations(hass, "en") @@ -63,10 +65,13 @@ async def async_setup_entry( ) # https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities - await coordinator.async_config_entry_first_refresh() + if entry.state == ConfigEntryState.SETUP_IN_PROGRESS: + await coordinator.async_config_entry_first_refresh() + entry.async_on_unload(entry.add_update_listener(async_reload_entry)) + else: + await coordinator.async_refresh() await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - entry.async_on_unload(entry.add_update_listener(async_reload_entry)) return True @@ -83,7 +88,7 @@ async def async_unload_entry( # Unregister services if this was the last config entry if not hass.config_entries.async_entries(DOMAIN): - for service in "get_price": + for service in ["get_price", "get_apexcharts_data", "get_apexcharts_yaml"]: if hass.services.has_service(DOMAIN, service): hass.services.async_remove(DOMAIN, service) @@ -96,6 +101,7 @@ async def async_remove_entry( ) -> None: """Handle removal of an entry.""" if storage := Store(hass, STORAGE_VERSION, f"{DOMAIN}.{entry.entry_id}"): + LOGGER.debug(f"[tibber_prices] async_remove_entry removing cache store for entry_id={entry.entry_id}") await storage.async_remove() @@ -104,5 +110,6 @@ async def async_reload_entry( entry: TibberPricesConfigEntry, ) -> None: """Reload config entry.""" + LOGGER.debug(f"[tibber_prices] async_reload_entry called for entry_id={entry.entry_id}") await async_unload_entry(hass, entry) await async_setup_entry(hass, entry) diff --git a/custom_components/tibber_prices/binary_sensor.py b/custom_components/tibber_prices/binary_sensor.py index 7df88e5..c9319d8 100644 --- a/custom_components/tibber_prices/binary_sensor.py +++ b/custom_components/tibber_prices/binary_sensor.py @@ -150,7 +150,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity): """Return True if tomorrow's data is fully available, False if not, None if unknown.""" if not self.coordinator.data: return None - price_info = self.coordinator.data["priceInfo"] + price_info = self.coordinator.data.get("priceInfo", {}) tomorrow_prices = price_info.get("tomorrow", []) interval_count = len(tomorrow_prices) if interval_count in TOMORROW_INTERVAL_COUNTS: @@ -163,7 +163,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity): """Return attributes for tomorrow_data_available binary sensor.""" if not self.coordinator.data: return None - price_info = self.coordinator.data["priceInfo"] + price_info = self.coordinator.data.get("priceInfo", {}) tomorrow_prices = price_info.get("tomorrow", []) interval_count = len(tomorrow_prices) if interval_count == 0: @@ -195,7 +195,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity): if not self.coordinator.data: return None - price_info = self.coordinator.data["priceInfo"] + price_info = self.coordinator.data.get("priceInfo", {}) today_prices = price_info.get("today", []) if not today_prices: @@ -488,7 +488,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity): """Get price interval attributes with support for 15-minute intervals and period grouping.""" if not self.coordinator.data: return None - price_info = self.coordinator.data["priceInfo"] + price_info = self.coordinator.data.get("priceInfo", {}) yesterday_prices = price_info.get("yesterday", []) today_prices = price_info.get("today", []) tomorrow_prices = price_info.get("tomorrow", []) @@ -530,7 +530,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity): if not self.coordinator.data: return None - price_info = self.coordinator.data["priceInfo"] + price_info = self.coordinator.data.get("priceInfo", {}) today_prices = price_info.get("today", []) if not today_prices: diff --git a/custom_components/tibber_prices/services.py b/custom_components/tibber_prices/services.py index 1f2a966..e1c6a66 100644 --- a/custom_components/tibber_prices/services.py +++ b/custom_components/tibber_prices/services.py @@ -407,7 +407,7 @@ def _find_previous_interval( ) -> Any: """Find previous interval from previous day if needed.""" if merged and day == "today": - yday_info = coordinator.data["priceInfo"].get("yesterday") or [] + yday_info = coordinator.data.get("priceInfo", {}).get("yesterday", []) if yday_info: yday_ratings = [ r @@ -428,7 +428,7 @@ def _find_next_interval( ) -> Any: """Find next interval from next day if needed.""" if merged and day == "today": - tmrw_info = coordinator.data["priceInfo"].get("tomorrow") or [] + tmrw_info = coordinator.data.get("priceInfo", {}).get("tomorrow", []) if tmrw_info: tmrw_ratings = [ r