feat: extend HA client to fetch entity states (lights/climate/doors/alerts)

This commit is contained in:
2026-04-06 07:40:51 +00:00
parent 0b0bd25cdb
commit b60d52242f
@@ -29,11 +29,7 @@ class HomeAssistantClient(BaseHTTPClient):
started_at = perf_counter()
api_info = await self._request_json("GET", "/api/", headers=headers)
if api_info is None:
logger.warning("home assistant base API check failed")
return snapshot
logger.info("home assistant raw /api response: %s", api_info)
logger.info("home assistant raw /api/ response: %s", api_info)
elapsed_ms = int((perf_counter() - started_at) * 1000)
config = await self._request_json("GET", "/api/config", headers=headers)
@@ -43,11 +39,45 @@ class HomeAssistantClient(BaseHTTPClient):
if isinstance(config, dict):
version = config.get("version")
# Fetch entity states for widget counts
lights_on = 0
lights_total = 0
climate_active = 0
doors_open = 0
alerts = 0
try:
states = await self._request_json("GET", "/api/states", headers=headers)
if isinstance(states, list):
for entity in states:
eid = entity.get("entity_id", "")
state = entity.get("state", "")
if eid.startswith("light."):
lights_total += 1
if state == "on":
lights_on += 1
elif eid.startswith("climate."):
if state not in ("off", "unavailable", "unknown"):
climate_active += 1
elif eid.startswith("binary_sensor.") and "door" in eid:
if state == "on":
doors_open += 1
elif eid.startswith("persistent_notification."):
if state == "notifying":
alerts += 1
except Exception as exc:
logger.warning("home assistant fetch states failed: %s", exc)
normalized = HomeAssistantSnapshot(
status="online",
version=str(version) if version else None,
response_time_ms=elapsed_ms,
last_checked=datetime.now(timezone.utc),
lights_on=lights_on,
lights_total=lights_total,
climate_active=climate_active,
doors_open=doors_open,
alerts=alerts,
)
logger.info("home assistant normalized snapshot: %s", normalized.model_dump())
return normalized