hass.tibber_prices/custom_components/tibber_prices/__init__.py
Julian Pawlowski ced6dcf104 fix(coordinator): sync cached_user_data after API call for new integrations
When adding a new integration (no existing cache), metadata sensors
(grid_company, estimated_annual_consumption, etc.) were marked as
unavailable because coordinator._cached_user_data remained None even
after successful API call.

Root cause: update_user_data_if_needed() stored user data in
_data_fetcher.cached_user_data, but the sync back to coordinator
only happened during _load_cache() (before the API call).

Solution: Added explicit sync of cached_user_data after
handle_main_entry_update() completes, ensuring metadata is available
when sensors first access get_user_homes().

Changes:
- coordinator/core.py: Sync _cached_user_data after main entry update
- __init__.py: Kept preload cache call (helps with HA restarts)

Impact: Metadata sensors now show values immediately on fresh integration
setup, without requiring a second update cycle or manual sensor activation.
2025-11-19 20:55:13 +00:00

133 lines
4.5 KiB
Python

"""
Custom integration to integrate tibber_prices with Home Assistant.
For more details about this integration, please refer to
https://github.com/jpawlowski/hass.tibber_prices
"""
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
from homeassistant.loader import async_get_loaded_integration
from .api import TibberPricesApiClient
from .const import (
DOMAIN,
LOGGER,
async_load_standard_translations,
async_load_translations,
)
from .coordinator import STORAGE_VERSION, TibberPricesDataUpdateCoordinator
from .data import TibberPricesData
from .services import async_setup_services
if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
from .data import TibberPricesConfigEntry
PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
]
# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
async def async_setup_entry(
hass: HomeAssistant,
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")
await async_load_standard_translations(hass, "en")
# Try to load translations for the user's configured language if not English
if hass.config.language and hass.config.language != "en":
await async_load_translations(hass, hass.config.language)
await async_load_standard_translations(hass, hass.config.language)
# Register services when a config entry is loaded
async_setup_services(hass)
integration = async_get_loaded_integration(hass, entry.domain)
coordinator = TibberPricesDataUpdateCoordinator(
hass=hass,
config_entry=entry,
version=str(integration.version) if integration.version else "unknown",
)
# CRITICAL: Load cache BEFORE first refresh to ensure user_data is available
# for metadata sensors (grid_company, estimated_annual_consumption, etc.)
# This prevents sensors from being marked as "unavailable" on first setup
await coordinator.load_cache()
entry.runtime_data = TibberPricesData(
client=TibberPricesApiClient(
access_token=entry.data[CONF_ACCESS_TOKEN],
session=async_get_clientsession(hass),
version=str(integration.version) if integration.version else "unknown",
),
integration=integration,
coordinator=coordinator,
)
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities
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)
return True
async def async_unload_entry(
hass: HomeAssistant,
entry: TibberPricesConfigEntry,
) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok and entry.runtime_data is not None:
await entry.runtime_data.coordinator.async_shutdown()
# Unregister services if this was the last config entry
if not hass.config_entries.async_entries(DOMAIN):
for service in [
"get_price",
"get_apexcharts_data",
"get_apexcharts_yaml",
"refresh_user_data",
]:
if hass.services.has_service(DOMAIN, service):
hass.services.async_remove(DOMAIN, service)
return unload_ok
async def async_remove_entry(
hass: HomeAssistant,
entry: TibberPricesConfigEntry,
) -> 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()
async def async_reload_entry(
hass: HomeAssistant,
entry: TibberPricesConfigEntry,
) -> None:
"""Reload config entry."""
await hass.config_entries.async_reload(entry.entry_id)