File size: 1,558 Bytes
0da05f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from typing import Literal, TypedDict

TriageLevel = Literal["ignore", "log_only", "notify", "alarm"]

class SecurityTriage(TypedDict):
    level: TriageLevel
    reason: str


def triage_security_event(event_type: str, time_of_day: str, severity: str, away_mode: bool) -> SecurityTriage:
    """Very small rule-based triage for security events."""
    et = event_type.lower()
    t = time_of_day.lower()
    sv = severity.lower()

    # Critical door/motion at night while away -> alarm
    if away_mode and t == "night" and et in {"door_open", "motion"} and sv in {"warning", "critical"}:
        return {"level": "alarm", "reason": "Unexpected movement or door open at night in away mode."}

    # Critical severity always at least notify
    if sv == "critical":
        return {"level": "notify", "reason": "Critical event; user should be notified."}

    # Warnings in away mode -> notify
    if away_mode and sv == "warning":
        return {"level": "notify", "reason": "Warning event while away; send notification."}

    # Info level -> log only
    if sv == "info":
        return {"level": "log_only", "reason": "Informational event; keep in log only."}

    return {"level": "ignore", "reason": "Low-importance event in home mode."}


if __name__ == "__main__":
    tests = [
        ("door_open", "night", "critical", True),
        ("sound", "night", "warning", True),
        ("motion", "afternoon", "info", False),
        ("unknown", "evening", "warning", False),
    ]
    for e in tests:
        print(e, "->", triage_security_event(*e))