mirror of
https://github.com/jpawlowski/hass.tibber_prices.git
synced 2026-03-30 05:13:40 +00:00
refactoring
This commit is contained in:
parent
73fa6c18a7
commit
5f8abf3a63
5 changed files with 40 additions and 30 deletions
|
|
@ -60,30 +60,35 @@ 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"]
|
||||||
|
if not errors:
|
||||||
|
raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR)
|
||||||
|
|
||||||
errors = response_json["errors"]
|
error = errors[0] # Take first error
|
||||||
if not errors:
|
if not isinstance(error, dict):
|
||||||
raise TibberPricesApiClientError(TibberPricesApiClientError.UNKNOWN_ERROR)
|
raise TibberPricesApiClientError(
|
||||||
|
TibberPricesApiClientError.MALFORMED_ERROR.format(error=error)
|
||||||
|
)
|
||||||
|
|
||||||
error = errors[0] # Take first error
|
message = error.get("message", "Unknown error")
|
||||||
if not isinstance(error, dict):
|
extensions = error.get("extensions", {})
|
||||||
|
|
||||||
|
# Check for authentication errors first
|
||||||
|
if extensions.get("code") == "UNAUTHENTICATED":
|
||||||
|
raise TibberPricesApiClientAuthenticationError(message)
|
||||||
|
|
||||||
|
# Handle all other GraphQL errors
|
||||||
raise TibberPricesApiClientError(
|
raise TibberPricesApiClientError(
|
||||||
TibberPricesApiClientError.MALFORMED_ERROR.format(error=error)
|
TibberPricesApiClientError.GRAPHQL_ERROR.format(message=message)
|
||||||
)
|
)
|
||||||
|
|
||||||
message = error.get("message", "Unknown error")
|
if "data" not in response_json or response_json["data"] is None:
|
||||||
extensions = error.get("extensions", {})
|
raise TibberPricesApiClientError(
|
||||||
|
TibberPricesApiClientError.GRAPHQL_ERROR.format(
|
||||||
# Check for authentication errors first
|
message="Response missing data object"
|
||||||
if extensions.get("code") == "UNAUTHENTICATED":
|
)
|
||||||
raise TibberPricesApiClientAuthenticationError(message)
|
)
|
||||||
|
|
||||||
# Handle all other GraphQL errors
|
|
||||||
raise TibberPricesApiClientError(
|
|
||||||
TibberPricesApiClientError.GRAPHQL_ERROR.format(message=message)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TibberPricesApiClient:
|
class TibberPricesApiClient:
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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."""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue