File size: 2,206 Bytes
17ae545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
from typing import Literal, TypedDict

ApplianceAction = Literal["turn_on", "turn_off", "increase", "decrease", "keep"]

class ApplianceDecision(TypedDict):
    suggested_action: ApplianceAction
    reason: str

def decide_appliance_action(appliance: str, temp_c: float, humidity_pct: float, time_hour: int) -> ApplianceDecision:
    """Very simple rules for basic appliances in a home environment."""
    a = appliance.lower()

    if a == "fan":
        if temp_c >= 29:
            return {"suggested_action": "turn_on", "reason": "Temperature is high; use fan."}
        if temp_c <= 24 and time_hour >= 22:
            return {"suggested_action": "turn_off", "reason": "Cool enough at night; fan can be off."}
        return {"suggested_action": "keep", "reason": "No strong reason to change fan state."}

    if a == "ac":
        if temp_c >= 28 and humidity_pct >= 60:
            return {"suggested_action": "turn_on", "reason": "Hot and humid; AC recommended."}
        if temp_c <= 25:
            return {"suggested_action": "turn_off", "reason": "Temperature is comfortable; AC can be off."}
        return {"suggested_action": "keep", "reason": "Borderline conditions; keep current AC state."}

    if a == "light":
        if time_hour >= 19 or time_hour <= 6:
            return {"suggested_action": "turn_on", "reason": "Likely dark outside; turn on light."}
        return {"suggested_action": "turn_off", "reason": "Daytime; light may not be needed."}

    if a == "dehumidifier":
        if humidity_pct >= 70:
            return {"suggested_action": "turn_on", "reason": "High humidity detected."}
        if humidity_pct <= 50:
            return {"suggested_action": "turn_off", "reason": "Humidity already low enough."}
        return {"suggested_action": "keep", "reason": "Humidity in acceptable range."}

    return {"suggested_action": "keep", "reason": "Unknown appliance; no change suggested."}


if __name__ == "__main__":
    tests = [
        ("fan", 30, 60, 14),
        ("ac", 29, 70, 22),
        ("light", 10, 50, 20),
        ("dehumidifier", 25, 80, 9),
    ]
    for a, t, h, hour in tests:
        print(a, t, h, hour, "->", decide_appliance_action(a, t, h, hour))