Improve sleep state detection

This commit is contained in:
Oliver Großkloß 2025-12-15 15:27:16 +01:00
parent 05fddea91d
commit 13c551aaf4

25
main.py
View File

@ -111,7 +111,22 @@ class GadgetbridgeMQTT:
@staticmethod
def _compute_awake(is_awake_flag, wakeup_raw, stage_code, stage_timestamp_ms, now_ms):
"""Decide awake state using most reliable signals first"""
"""Decide awake state using most reliable signals first.
Priority:
1. If past wakeup time -> awake (session ended)
2. If is_awake_flag set -> awake
3. If recent sleep stage shows awake -> awake
4. If recent sleep stage shows sleep (deep/light/REM) -> sleeping
5. Default: awake
"""
# If we're past the wakeup time, the session is over -> awake
if wakeup_raw is not None and wakeup_raw <= now_ms:
return True
if is_awake_flag == 1:
return True
recent_stage = stage_timestamp_ms is not None and now_ms - stage_timestamp_ms <= 30 * 60 * 1000
if recent_stage and stage_code is not None:
@ -119,13 +134,7 @@ class GadgetbridgeMQTT:
return True
if stage_code in (2, 3, 4): # Deep, Light, REM
return False
# stage_code 0/1 fall through to other signals
if is_awake_flag == 1:
return True
if wakeup_raw is not None and wakeup_raw <= now_ms:
return True
# stage_code 0/1 fall through
return False