Add native HA integration and dashboard
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from collections.abc import Callable
|
||||
|
||||
from app.core.decision import DecisionEngineV2
|
||||
from app.core.handoff import HandoffMatrix
|
||||
from app.core.models import (
|
||||
AuditEvent,
|
||||
ControlProfile,
|
||||
Decision,
|
||||
EntityState,
|
||||
LearningProfile,
|
||||
StateEvent,
|
||||
@@ -31,7 +33,12 @@ class EventCore:
|
||||
self._decision = DecisionEngineV2()
|
||||
self._handoff = HandoffMatrix()
|
||||
|
||||
def process_state_event(self, event: StateEvent) -> list[AuditEvent]:
|
||||
def process_state_event(
|
||||
self,
|
||||
event: StateEvent,
|
||||
*,
|
||||
execute: Callable[[Decision], bool] | None = None,
|
||||
) -> list[AuditEvent]:
|
||||
runtime = self._stores.runtime()
|
||||
learning = self._stores.learning()
|
||||
control = self._stores.control()
|
||||
@@ -69,6 +76,8 @@ class EventCore:
|
||||
learning=learning_profile,
|
||||
control=control_profile,
|
||||
)
|
||||
if callable(execute) and decision.allowed and not decision.dry_run:
|
||||
decision = _execute_decision(decision, execute)
|
||||
audit.append(
|
||||
AuditEvent(
|
||||
event_id=_event_id("decision"),
|
||||
@@ -87,3 +96,17 @@ class EventCore:
|
||||
def _event_id(prefix: str) -> str:
|
||||
return f"{prefix}-{datetime.now(timezone.utc).strftime('%Y%m%d%H%M%S%f')}"
|
||||
|
||||
|
||||
def _execute_decision(decision: Decision, execute: Callable[[Decision], bool]) -> Decision:
|
||||
try:
|
||||
result = execute(decision)
|
||||
except Exception as exc:
|
||||
return decision.model_copy(
|
||||
update={
|
||||
"executed": False,
|
||||
"allowed": False,
|
||||
"reason": f"Ausfuehrung fehlgeschlagen: {exc}",
|
||||
"blockers": [*decision.blockers, str(exc)],
|
||||
}
|
||||
)
|
||||
return decision.model_copy(update={"executed": bool(result)})
|
||||
|
||||
Reference in New Issue
Block a user