This commit is contained in:
Julian Pawlowski 2025-05-21 11:29:50 +00:00
parent b23697036a
commit 8c43f2750f
3 changed files with 17 additions and 10 deletions

View file

@ -9,6 +9,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.const import CONF_ACCESS_TOKEN, Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
@ -37,6 +38,7 @@ async def async_setup_entry(
entry: TibberPricesConfigEntry, entry: TibberPricesConfigEntry,
) -> bool: ) -> bool:
"""Set up this integration using UI.""" """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 # Preload translations to populate the cache
await async_load_translations(hass, "en") 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 # 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) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
return True return True
@ -83,7 +88,7 @@ async def async_unload_entry(
# Unregister services if this was the last config entry # Unregister services if this was the last config entry
if not hass.config_entries.async_entries(DOMAIN): 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): if hass.services.has_service(DOMAIN, service):
hass.services.async_remove(DOMAIN, service) hass.services.async_remove(DOMAIN, service)
@ -96,6 +101,7 @@ async def async_remove_entry(
) -> None: ) -> None:
"""Handle removal of an entry.""" """Handle removal of an entry."""
if storage := Store(hass, STORAGE_VERSION, f"{DOMAIN}.{entry.entry_id}"): 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() await storage.async_remove()
@ -104,5 +110,6 @@ async def async_reload_entry(
entry: TibberPricesConfigEntry, entry: TibberPricesConfigEntry,
) -> None: ) -> None:
"""Reload config entry.""" """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_unload_entry(hass, entry)
await async_setup_entry(hass, entry) await async_setup_entry(hass, entry)

View file

@ -150,7 +150,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity):
"""Return True if tomorrow's data is fully available, False if not, None if unknown.""" """Return True if tomorrow's data is fully available, False if not, None if unknown."""
if not self.coordinator.data: if not self.coordinator.data:
return None return None
price_info = self.coordinator.data["priceInfo"] price_info = self.coordinator.data.get("priceInfo", {})
tomorrow_prices = price_info.get("tomorrow", []) tomorrow_prices = price_info.get("tomorrow", [])
interval_count = len(tomorrow_prices) interval_count = len(tomorrow_prices)
if interval_count in TOMORROW_INTERVAL_COUNTS: if interval_count in TOMORROW_INTERVAL_COUNTS:
@ -163,7 +163,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity):
"""Return attributes for tomorrow_data_available binary sensor.""" """Return attributes for tomorrow_data_available binary sensor."""
if not self.coordinator.data: if not self.coordinator.data:
return None return None
price_info = self.coordinator.data["priceInfo"] price_info = self.coordinator.data.get("priceInfo", {})
tomorrow_prices = price_info.get("tomorrow", []) tomorrow_prices = price_info.get("tomorrow", [])
interval_count = len(tomorrow_prices) interval_count = len(tomorrow_prices)
if interval_count == 0: if interval_count == 0:
@ -195,7 +195,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity):
if not self.coordinator.data: if not self.coordinator.data:
return None return None
price_info = self.coordinator.data["priceInfo"] price_info = self.coordinator.data.get("priceInfo", {})
today_prices = price_info.get("today", []) today_prices = price_info.get("today", [])
if not today_prices: 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.""" """Get price interval attributes with support for 15-minute intervals and period grouping."""
if not self.coordinator.data: if not self.coordinator.data:
return None return None
price_info = self.coordinator.data["priceInfo"] price_info = self.coordinator.data.get("priceInfo", {})
yesterday_prices = price_info.get("yesterday", []) yesterday_prices = price_info.get("yesterday", [])
today_prices = price_info.get("today", []) today_prices = price_info.get("today", [])
tomorrow_prices = price_info.get("tomorrow", []) tomorrow_prices = price_info.get("tomorrow", [])
@ -530,7 +530,7 @@ class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity):
if not self.coordinator.data: if not self.coordinator.data:
return None return None
price_info = self.coordinator.data["priceInfo"] price_info = self.coordinator.data.get("priceInfo", {})
today_prices = price_info.get("today", []) today_prices = price_info.get("today", [])
if not today_prices: if not today_prices:

View file

@ -407,7 +407,7 @@ def _find_previous_interval(
) -> Any: ) -> Any:
"""Find previous interval from previous day if needed.""" """Find previous interval from previous day if needed."""
if merged and day == "today": if merged and day == "today":
yday_info = coordinator.data["priceInfo"].get("yesterday") or [] yday_info = coordinator.data.get("priceInfo", {}).get("yesterday", [])
if yday_info: if yday_info:
yday_ratings = [ yday_ratings = [
r r
@ -428,7 +428,7 @@ def _find_next_interval(
) -> Any: ) -> Any:
"""Find next interval from next day if needed.""" """Find next interval from next day if needed."""
if merged and day == "today": if merged and day == "today":
tmrw_info = coordinator.data["priceInfo"].get("tomorrow") or [] tmrw_info = coordinator.data.get("priceInfo", {}).get("tomorrow", [])
if tmrw_info: if tmrw_info:
tmrw_ratings = [ tmrw_ratings = [
r r