| from typing import Literal, TypedDict | |
| EscalationLevel = Literal["ignore", "log_only", "notify_user", "alarm"] | |
| class EscalationDecision(TypedDict): | |
| level: EscalationLevel | |
| reason: str | |
| def decide_escalation(event_type: str, is_night: bool, human_confirmed: bool) -> EscalationDecision: | |
| """Decide how to escalate a simple security event.""" | |
| et = event_type.lower() | |
| if et == "door_open": | |
| if is_night and not human_confirmed: | |
| return {"level": "alarm", "reason": "Door opened at night with no human confirmed."} | |
| return {"level": "notify_user", "reason": "Door opened, but appears normal."} | |
| if et in {"window_open", "device_disconnected"}: | |
| if is_night: | |
| return {"level": "notify_user", "reason": "Event at night; user should review."} | |
| return {"level": "log_only", "reason": "Event during daytime; log for later review."} | |
| if et in {"motion_detected", "noise_high"}: | |
| if is_night and not human_confirmed: | |
| return {"level": "notify_user", "reason": "Unexpected motion/noise at night."} | |
| if not human_confirmed: | |
| return {"level": "log_only", "reason": "No human detected; log event."} | |
| return {"level": "ignore", "reason": "Human presence detected; likely normal."} | |
| return {"level": "log_only", "reason": "Unknown event type; logging by default."} | |
| if __name__ == "__main__": | |
| tests = [ | |
| ("door_open", True, False), | |
| ("door_open", False, True), | |
| ("motion_detected", True, False), | |
| ("noise_high", False, True), | |
| ("device_disconnected", True, False), | |
| ] | |
| for et, night, human in tests: | |
| print(et, night, human, "->", decide_escalation(et, night, human)) | |