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:
return
if "errors" in response_json:
errors = response_json["errors"]
if not errors:
raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR)
@ -85,6 +83,13 @@ async def _verify_graphql_response(response_json: dict) -> None:
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:
"""Tibber API Client."""
@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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."""