Add HA history and model evaluation
This commit is contained in:
@@ -72,6 +72,46 @@ class FutureHaClient:
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
def read_history(
|
||||
self,
|
||||
entity_ids: list[str],
|
||||
start_time: datetime,
|
||||
end_time: datetime,
|
||||
) -> dict[str, list[StateEvent]]:
|
||||
params = {
|
||||
"filter_entity_id": ",".join(entity_ids),
|
||||
"end_time": end_time.isoformat(),
|
||||
"minimal_response": "1",
|
||||
}
|
||||
with httpx.Client(timeout=self._config.timeout_seconds) as client:
|
||||
response = client.get(
|
||||
f"{self._core_url}/api/history/period/{start_time.isoformat()}",
|
||||
headers=self._headers,
|
||||
params=params,
|
||||
)
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
result: dict[str, list[StateEvent]] = {entity_id: [] for entity_id in entity_ids}
|
||||
for series in payload if isinstance(payload, list) else []:
|
||||
if not isinstance(series, list):
|
||||
continue
|
||||
for item in series:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
entity_id = item.get("entity_id")
|
||||
if not isinstance(entity_id, str):
|
||||
continue
|
||||
result.setdefault(entity_id, []).append(
|
||||
StateEvent(
|
||||
entity_id=entity_id,
|
||||
new_state=item.get("state") if isinstance(item.get("state"), str) else None,
|
||||
changed_at=_parse_datetime(
|
||||
item.get("last_changed") or item.get("last_updated")
|
||||
),
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
async def listen_state_events(self) -> AsyncIterator[StateEvent]:
|
||||
websocket_url = self._config.websocket_url or _default_websocket_url(self._core_url)
|
||||
async with websockets.connect(websocket_url, ping_interval=None) as websocket:
|
||||
@@ -160,4 +200,3 @@ def _parse_datetime(value: object) -> datetime:
|
||||
if parsed.tzinfo is None:
|
||||
return parsed.replace(tzinfo=timezone.utc)
|
||||
return parsed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user