main: HA-Integration mit Exception-Handling und Testabdeckung

This commit is contained in:
2026-06-10 22:47:08 +02:00
parent 6b1e2ad0dc
commit d550030a1a
6 changed files with 164 additions and 23 deletions

View File

@@ -5,18 +5,13 @@ from dataclasses import dataclass
import requests
from app.ha.exceptions import HaAuthError, HaHttpError, HaTimeoutError, HaUnexpectedPayloadError
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class HaClientSettings:
url: str
token: str
timeout_seconds: int = 10
class HaClient:
def __init__(self, settings: HaClientSettings) -> None:
def __init__(self, settings) -> None:
self._settings = settings
self._session = requests.Session()
self._session.headers.update({
@@ -24,10 +19,35 @@ class HaClient:
"Content-Type": "application/json",
})
def list_entities(self) -> list[dict[str, object]]:
response = self._session.get(
f"{self._settings.url}/api/states",
timeout=self._settings.timeout_seconds,
)
response.raise_for_status()
return response.json()
def list_entities(self):
url = f"{self._settings.url}/api/states"
try:
response = self._session.get(
url,
timeout=self._settings.timeout_seconds,
)
except requests.Timeout as exc:
raise HaTimeoutError("Zeitüberschreitung beim Zugriff auf Home Assistant.") from exc
except requests.RequestException as exc:
raise HaHttpError(
getattr(exc.response, "status_code", 502),
"Netzwerkfehler beim Zugriff auf Home Assistant.",
) from exc
if response.status_code in (401, 403):
raise HaAuthError(response.status_code, "Authentifizierung bei Home Assistant fehlgeschlagen.")
try:
response.raise_for_status()
except requests.HTTPError as exc:
raise HaHttpError(response.status_code, "Home Assistant meldet einen Fehler.") from exc
try:
payload = response.json()
except ValueError as exc:
raise HaUnexpectedPayloadError("Antwort von Home Assistant ist kein gültiges JSON.") from exc
if not isinstance(payload, list):
raise HaUnexpectedPayloadError("Antwort von Home Assistant hat unerwartetes Format.")
return payload