lapctl 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lapctl/core/battery.py ADDED
@@ -0,0 +1,69 @@
1
+ """Battery monitoring loop emitting low/full events."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from collections.abc import Callable
7
+ from dataclasses import dataclass
8
+ from typing import Literal
9
+
10
+ from lapctl.drivers.base import BaseDriver
11
+
12
+
13
+ @dataclass(frozen=True)
14
+ class BatteryEvent:
15
+ """Event emitted when battery crosses low or full threshold."""
16
+
17
+ kind: Literal["low", "full"]
18
+ capacity: int
19
+
20
+
21
+ class BatteryMonitor:
22
+ """Polls driver battery metrics and emits threshold transition events."""
23
+
24
+ def __init__(self, driver: BaseDriver, low_threshold: int = 79) -> None:
25
+ self.driver = driver
26
+ self.low_threshold = low_threshold
27
+ self.low_battery_notified = False
28
+ self.full_battery_notified = False
29
+
30
+ def check_once(self) -> BatteryEvent | None:
31
+ """Read current battery state via driver and return an event if crossed."""
32
+ state = self.driver.get_battery_state()
33
+ if state is None:
34
+ return None
35
+
36
+ cap, is_discharging, is_charging_or_full = state
37
+
38
+ if cap <= self.low_threshold and is_discharging:
39
+ if not self.low_battery_notified:
40
+ self.low_battery_notified = True
41
+ return BatteryEvent(kind="low", capacity=cap)
42
+ else:
43
+ if cap > self.low_threshold or not is_discharging:
44
+ self.low_battery_notified = False
45
+
46
+ if cap == 100 and is_charging_or_full:
47
+ if not self.full_battery_notified:
48
+ self.full_battery_notified = True
49
+ return BatteryEvent(kind="full", capacity=cap)
50
+ else:
51
+ if cap < 100 or not is_charging_or_full:
52
+ self.full_battery_notified = False
53
+
54
+ return None
55
+
56
+ def run(
57
+ self,
58
+ on_event: Callable[[BatteryEvent], None],
59
+ interval_seconds: int = 60,
60
+ ) -> None:
61
+ """Continuously monitor battery and invoke on_event when triggered."""
62
+ while True:
63
+ time.sleep(interval_seconds)
64
+ try:
65
+ event = self.check_once()
66
+ if event is not None:
67
+ on_event(event)
68
+ except Exception as e:
69
+ print(f"Error in battery monitor loop: {e}")