add safe home assistant error handling

This commit is contained in:
2026-06-10 21:24:34 +02:00
parent 8841a68c8d
commit 29ec53cc5e
7 changed files with 186 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ from collections.abc import Sequence
from fastapi.testclient import TestClient
from app.ha.exceptions import HaTimeoutError
from app.ha.models import HaEntitySummary
from app.ha.reader import HaReader
from app.main import app
@@ -15,6 +16,14 @@ class FakeHaReader(HaReader):
return [HaEntitySummary(entity_id="sensor.temperature", domain="sensor")]
class TimeoutHaReader(HaReader):
def __init__(self) -> None:
pass
def read_entities(self) -> Sequence[HaEntitySummary]:
raise HaTimeoutError("contains internal details that must not leak")
def test_openapi_docs_are_available() -> None:
with TestClient(app) as client:
response = client.get("/docs")
@@ -44,3 +53,11 @@ def test_entities_returns_503_without_home_assistant_config() -> None:
delattr(app.state, "ha_reader")
response = client.get("/v1/entities")
assert response.status_code == 503
def test_entities_maps_ha_errors_without_leaking_details() -> None:
with TestClient(app) as client:
app.state.ha_reader = TimeoutHaReader()
response = client.get("/v1/entities")
assert response.status_code == 504
assert response.json() == {"detail": "Home Assistant request timed out."}