From 5f8abf3a633fd13d4ed813fbfbab705e6de6c5a4 Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Fri, 18 Apr 2025 17:08:08 +0000 Subject: [PATCH] refactoring --- custom_components/tibber_prices/api.py | 58 +++++++++++-------- .../tibber_prices/config_flow.py | 2 +- custom_components/tibber_prices/data.py | 2 +- custom_components/tibber_prices/entity.py | 4 +- custom_components/tibber_prices/switch.py | 4 +- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/custom_components/tibber_prices/api.py b/custom_components/tibber_prices/api.py index f724d4b..a6568bc 100644 --- a/custom_components/tibber_prices/api.py +++ b/custom_components/tibber_prices/api.py @@ -60,30 +60,35 @@ async def _verify_graphql_response(response_json: dict) -> None: }] } """ - if "errors" not in response_json: - return + if "errors" in response_json: + errors = response_json["errors"] + if not errors: + raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR) - errors = response_json["errors"] - if not errors: - raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR) + error = errors[0] # Take first error + if not isinstance(error, dict): + raise TibberPricesApiClientError( + TibberPricesApiClientError.MALFORMED_ERROR.format(error=error) + ) - error = errors[0] # Take first error - if not isinstance(error, dict): + message = error.get("message", "Unknown error") + extensions = error.get("extensions", {}) + + # Check for authentication errors first + if extensions.get("code") == "UNAUTHENTICATED": + raise TibberPricesApiClientAuthenticationError(message) + + # Handle all other GraphQL errors raise TibberPricesApiClientError( - TibberPricesApiClientError.MALFORMED_ERROR.format(error=error) + TibberPricesApiClientError.GRAPHQL_ERROR.format(message=message) ) - message = error.get("message", "Unknown error") - extensions = error.get("extensions", {}) - - # Check for authentication errors first - if extensions.get("code") == "UNAUTHENTICATED": - raise TibberPricesApiClientAuthenticationError(message) - - # Handle all other GraphQL errors - raise TibberPricesApiClientError( - TibberPricesApiClientError.GRAPHQL_ERROR.format(message=message) - ) + if "data" not in response_json or response_json["data"] is None: + raise TibberPricesApiClientError( + TibberPricesApiClientError.GRAPHQL_ERROR.format( + message="Response missing data object" + ) + ) class TibberPricesApiClient: @@ -108,8 +113,8 @@ class TibberPricesApiClient: name } } - """, - }, + """ + } ) async def async_get_data(self) -> Any: @@ -142,7 +147,12 @@ class TibberPricesApiClient: data: dict | None = None, headers: dict | None = None, ) -> Any: - """Get information from the API.""" + """ + Get information from the API. + + Returns the contents of the 'data' object from the GraphQL response. + Raises an error if the response doesn't contain a 'data' object. + """ try: async with async_timeout.timeout(10): headers = headers or {} @@ -163,11 +173,11 @@ class TibberPricesApiClient: _verify_response_or_raise(response) response_json = await response.json() await _verify_graphql_response(response_json) - return response_json + return response_json["data"] except TimeoutError as exception: raise TibberPricesApiClientCommunicationError( - TibberPricesApiClientCommunicationError.TIMEOUT_ERROR.format( + TibberPricesApiClientCommunicationError.CONNECTION_ERROR.format( exception=exception ) ) from exception diff --git a/custom_components/tibber_prices/config_flow.py b/custom_components/tibber_prices/config_flow.py index 28bd8dc..77c4504 100644 --- a/custom_components/tibber_prices/config_flow.py +++ b/custom_components/tibber_prices/config_flow.py @@ -18,7 +18,7 @@ from .api import ( from .const import DOMAIN, LOGGER -class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): +class TibberPricesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for tibber_prices.""" VERSION = 1 diff --git a/custom_components/tibber_prices/data.py b/custom_components/tibber_prices/data.py index 2faf813..ab249dd 100644 --- a/custom_components/tibber_prices/data.py +++ b/custom_components/tibber_prices/data.py @@ -18,7 +18,7 @@ type TibberPricesConfigEntry = ConfigEntry[TibberPricesData] @dataclass class TibberPricesData: - """Data for the Blueprint integration.""" + """Data for the tibber_prices integration.""" client: TibberPricesApiClient coordinator: TibberPricesDataUpdateCoordinator diff --git a/custom_components/tibber_prices/entity.py b/custom_components/tibber_prices/entity.py index ab2de5c..dbabb48 100644 --- a/custom_components/tibber_prices/entity.py +++ b/custom_components/tibber_prices/entity.py @@ -1,4 +1,4 @@ -"""BlueprintEntity class.""" +"""TibberPricesEntity class.""" from __future__ import annotations @@ -10,7 +10,7 @@ from .coordinator import TibberPricesDataUpdateCoordinator class TibberPricesEntity(CoordinatorEntity[TibberPricesDataUpdateCoordinator]): - """BlueprintEntity class.""" + """TibberPricesEntity class.""" _attr_attribution = ATTRIBUTION diff --git a/custom_components/tibber_prices/switch.py b/custom_components/tibber_prices/switch.py index ece6125..09300fa 100644 --- a/custom_components/tibber_prices/switch.py +++ b/custom_components/tibber_prices/switch.py @@ -12,7 +12,7 @@ if TYPE_CHECKING: from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback - from .coordinator import BlueprintDataUpdateCoordinator + from .coordinator import TibberPricesDataUpdateCoordinator from .data import TibberPricesConfigEntry ENTITY_DESCRIPTIONS = ( @@ -44,7 +44,7 @@ class TibberPricesSwitch(TibberPricesEntity, SwitchEntity): def __init__( self, - coordinator: BlueprintDataUpdateCoordinator, + coordinator: TibberPricesDataUpdateCoordinator, entity_description: SwitchEntityDescription, ) -> None: """Initialize the switch class."""