From df9fb37fe48426b332bf59c4f0eb32f7610432ae Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Fri, 7 Nov 2025 23:43:39 +0000 Subject: [PATCH] refactor: Update integration version handling in TibberPrices components --- custom_components/tibber_prices/__init__.py | 7 ++++++- custom_components/tibber_prices/api.py | 10 +++++----- custom_components/tibber_prices/config_flow.py | 3 +++ custom_components/tibber_prices/const.py | 3 --- custom_components/tibber_prices/coordinator.py | 2 ++ pyproject.toml | 2 +- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/custom_components/tibber_prices/__init__.py b/custom_components/tibber_prices/__init__.py index d55f867..c42c290 100644 --- a/custom_components/tibber_prices/__init__.py +++ b/custom_components/tibber_prices/__init__.py @@ -56,16 +56,21 @@ async def async_setup_entry( # 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=integration.version, ) + entry.runtime_data = TibberPricesData( client=TibberPricesApiClient( access_token=entry.data[CONF_ACCESS_TOKEN], session=async_get_clientsession(hass), + version=integration.version, ), - integration=async_get_loaded_integration(hass, entry.domain), + integration=integration, coordinator=coordinator, ) diff --git a/custom_components/tibber_prices/api.py b/custom_components/tibber_prices/api.py index e4a07e4..3a3c76e 100644 --- a/custom_components/tibber_prices/api.py +++ b/custom_components/tibber_prices/api.py @@ -15,8 +15,6 @@ import aiohttp from homeassistant.const import __version__ as ha_version from homeassistant.util import dt as dt_util -from .const import VERSION - _LOGGER = logging.getLogger(__name__) HTTP_BAD_REQUEST = 400 @@ -273,12 +271,12 @@ def _is_data_empty(data: dict, query_type: str) -> bool: return is_empty -def _prepare_headers(access_token: str) -> dict[str, str]: +def _prepare_headers(access_token: str, version: str) -> dict[str, str]: """Prepare headers for API request.""" return { "Authorization": f"Bearer {access_token}", "Accept": "application/json", - "User-Agent": f"HomeAssistant/{ha_version} tibber_prices/{VERSION}", + "User-Agent": f"HomeAssistant/{ha_version} tibber_prices/{version}", } @@ -361,10 +359,12 @@ class TibberPricesApiClient: self, access_token: str, session: aiohttp.ClientSession, + version: str, ) -> None: """Tibber API Client.""" self._access_token = access_token self._session = session + self._version = version self._request_semaphore = asyncio.Semaphore(2) # Max 2 concurrent requests self._last_request_time = dt_util.now() self._min_request_interval = timedelta(seconds=1) # Min 1 second between requests @@ -798,7 +798,7 @@ class TibberPricesApiClient: query_type: QueryType = QueryType.USER, ) -> Any: """Get information from the API with rate limiting and retry logic.""" - headers = headers or _prepare_headers(self._access_token) + headers = headers or _prepare_headers(self._access_token, self._version) last_error: Exception | None = None for retry in range(self._max_retries + 1): diff --git a/custom_components/tibber_prices/config_flow.py b/custom_components/tibber_prices/config_flow.py index b794fc5..a141bad 100644 --- a/custom_components/tibber_prices/config_flow.py +++ b/custom_components/tibber_prices/config_flow.py @@ -30,6 +30,7 @@ from homeassistant.helpers.selector import ( TextSelectorConfig, TextSelectorType, ) +from homeassistant.loader import async_get_integration from .api import ( TibberPricesApiClient, @@ -311,9 +312,11 @@ class TibberPricesFlowHandler(ConfigFlow, domain=DOMAIN): async def _get_viewer_details(self, access_token: str) -> dict: """Validate credentials and return information about the account (viewer object).""" + integration = await async_get_integration(self.hass, DOMAIN) client = TibberPricesApiClient( access_token=access_token, session=async_create_clientsession(self.hass), + version=integration.version, ) result = await client.async_get_viewer_details() return result["viewer"] diff --git a/custom_components/tibber_prices/const.py b/custom_components/tibber_prices/const.py index 00a3551..3df81c3 100644 --- a/custom_components/tibber_prices/const.py +++ b/custom_components/tibber_prices/const.py @@ -16,9 +16,6 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant -# Version should match manifest.json -VERSION = "0.1.0" - DOMAIN = "tibber_prices" CONF_EXTENDED_DESCRIPTIONS = "extended_descriptions" CONF_BEST_PRICE_FLEX = "best_price_flex" diff --git a/custom_components/tibber_prices/coordinator.py b/custom_components/tibber_prices/coordinator.py index 83b39ec..c795faa 100644 --- a/custom_components/tibber_prices/coordinator.py +++ b/custom_components/tibber_prices/coordinator.py @@ -116,6 +116,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): self, hass: HomeAssistant, config_entry: ConfigEntry, + version: str, ) -> None: """Initialize the coordinator.""" super().__init__( @@ -129,6 +130,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): self.api = TibberPricesApiClient( access_token=config_entry.data[CONF_ACCESS_TOKEN], session=aiohttp_client.async_get_clientsession(hass), + version=version, ) # Storage for persistence diff --git a/pyproject.toml b/pyproject.toml index 9a2dbf8..2c19afa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "tibber_prices" -version = "0.1.0" +version = "0.0.0" # Version is managed in manifest.json only requires-python = ">=3.13" [tool.setuptools]