refactoring

This commit is contained in:
Julian Pawlowski 2025-04-18 17:08:08 +00:00
parent 73fa6c18a7
commit 5f8abf3a63
5 changed files with 40 additions and 30 deletions

View file

@ -60,9 +60,7 @@ async def _verify_graphql_response(response_json: dict) -> None:
}] }]
} }
""" """
if "errors" not in response_json: if "errors" in response_json:
return
errors = response_json["errors"] errors = response_json["errors"]
if not errors: if not errors:
raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR) raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR)
@ -85,6 +83,13 @@ async def _verify_graphql_response(response_json: dict) -> None:
TibberPricesApiClientError.GRAPHQL_ERROR.format(message=message) 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: class TibberPricesApiClient:
"""Tibber API Client.""" """Tibber API Client."""
@ -108,8 +113,8 @@ class TibberPricesApiClient:
name name
} }
} }
""", """
}, }
) )
async def async_get_data(self) -> Any: async def async_get_data(self) -> Any:
@ -142,7 +147,12 @@ class TibberPricesApiClient:
data: dict | None = None, data: dict | None = None,
headers: dict | None = None, headers: dict | None = None,
) -> Any: ) -> 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: try:
async with async_timeout.timeout(10): async with async_timeout.timeout(10):
headers = headers or {} headers = headers or {}
@ -163,11 +173,11 @@ class TibberPricesApiClient:
_verify_response_or_raise(response) _verify_response_or_raise(response)
response_json = await response.json() response_json = await response.json()
await _verify_graphql_response(response_json) await _verify_graphql_response(response_json)
return response_json return response_json["data"]
except TimeoutError as exception: except TimeoutError as exception:
raise TibberPricesApiClientCommunicationError( raise TibberPricesApiClientCommunicationError(
TibberPricesApiClientCommunicationError.TIMEOUT_ERROR.format( TibberPricesApiClientCommunicationError.CONNECTION_ERROR.format(
exception=exception exception=exception
) )
) from exception ) from exception

View file

@ -18,7 +18,7 @@ from .api import (
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): class TibberPricesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for tibber_prices.""" """Config flow for tibber_prices."""
VERSION = 1 VERSION = 1

View file

@ -18,7 +18,7 @@ type TibberPricesConfigEntry = ConfigEntry[TibberPricesData]
@dataclass @dataclass
class TibberPricesData: class TibberPricesData:
"""Data for the Blueprint integration.""" """Data for the tibber_prices integration."""
client: TibberPricesApiClient client: TibberPricesApiClient
coordinator: TibberPricesDataUpdateCoordinator coordinator: TibberPricesDataUpdateCoordinator

View file

@ -1,4 +1,4 @@
"""BlueprintEntity class.""" """TibberPricesEntity class."""
from __future__ import annotations from __future__ import annotations
@ -10,7 +10,7 @@ from .coordinator import TibberPricesDataUpdateCoordinator
class TibberPricesEntity(CoordinatorEntity[TibberPricesDataUpdateCoordinator]): class TibberPricesEntity(CoordinatorEntity[TibberPricesDataUpdateCoordinator]):
"""BlueprintEntity class.""" """TibberPricesEntity class."""
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION

View file

@ -12,7 +12,7 @@ if TYPE_CHECKING:
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import TibberPricesDataUpdateCoordinator
from .data import TibberPricesConfigEntry from .data import TibberPricesConfigEntry
ENTITY_DESCRIPTIONS = ( ENTITY_DESCRIPTIONS = (
@ -44,7 +44,7 @@ class TibberPricesSwitch(TibberPricesEntity, SwitchEntity):
def __init__( def __init__(
self, self,
coordinator: BlueprintDataUpdateCoordinator, coordinator: TibberPricesDataUpdateCoordinator,
entity_description: SwitchEntityDescription, entity_description: SwitchEntityDescription,
) -> None: ) -> None:
"""Initialize the switch class.""" """Initialize the switch class."""