Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/embertide/engine/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ async def _tick_person(self, p: Person, now: float, frame, guest_zones: set[str]
self._ctx_home[p.id] = home
e.busy_until = ctx.busy_until
e.follow_alarm(now, ctx.next_alarm, ctx.alarm_reported) # the warm wake lines up with the phone's alarm
e.follow_calendar(now, ctx.early_start) # ... and comes earlier for an early first event
except Exception: # noqa: BLE001
log.exception("context failed for %s", p.id)
# away for a while (engine/away.py): back when they use their side or the end date passes
Expand Down
127 changes: 101 additions & 26 deletions core/embertide/engine/night.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class NightState:
lie_in: bool = False
lie_in_over: bool = False # it ended (got up / cap): getting back in doesn't start another
snoozed_until: float | None = None # a later alarm after the followed one rang (a snooze or an extended alarm)
alarms_seen: list = field(default_factory=list) # later alarms the phone showed after the followed one rang, still in bed
ended: bool = False # "I'm up": the night was ended by hand (the dial, the app, a watch)
# the person's own phone / watch (the "phone:bedside" and "watch:me" votes): ever voted this night, since when
# neither has, and whether the pad was empty for a while after that. Used only to end a lie-in (see _lie_in)
Expand All @@ -93,6 +94,7 @@ def __init__(self, person: Person, tz: ZoneInfo) -> None:
self.s = NightState()
self.night: ResolvedNight | None = None
self.busy_until: float | None = None # set by the controller from calendar context
self.last_present: bool | None = None # presence at the last tick
self.away = False # away for a while (engine/away.py), set by the controller each tick
# settling in (controller._start_gate): False = the bed radar / phone don't back this side's presence yet, so
# lying there doesn't start the night; None = no opinion (no radar), the usual start
Expand Down Expand Up @@ -222,55 +224,127 @@ def _end_flash(self, now: float, source: str = "app") -> None:

# ---------- the phone's alarm ----------
ALARM_EARLIER_S = 4 * 3600 # an alarm up to 4 h before the planned wake moves it earlier
ALARM_LATER_S = 3 * 3600 # and up to 3 h after (a second, later alarm to sleep in) moves it later
ALARM_LATER_S = 3 * 3600 # and up to 3 h after (a later alarm set to sleep in) moves it later, until one rings

SNOOZE_S = 30 * 60 # a later alarm this soon after the first one rang = snooze

def _wake_day(self, now: float):
"""(tonight's date, its planned wake) for following an alarm or the calendar, or None when it can't apply now:
no rhythm, or last night still running past noon (a lie-in) while the date already says tonight."""
from datetime import datetime
d = night_date(datetime.fromtimestamp(now, self.tz))
if self.night is not None and self.night.date != d:
return None
rhythm = pick_rhythm(self.person.rhythms, d)
if rhythm is None:
return None
return d, resolve(rhythm, d, self.tz).wake

def follow_alarm(self, now: float, alarm: float | None, reported: bool = False) -> bool:
"""Line tonight's wake up with the phone's next alarm (Settings: "Wake with my alarm"). Returns True when the
wake moved. A wake set by hand in the app wins; an alarm far from tonight's planned wake is someone else's day.
`reported`: the phone sent its next alarm lately. Then "no alarm" (None: none set, or the next one a day or more
away) means the followed alarm was turned off before it rang, and the planned wake comes back. A phone that has
gone quiet says nothing either way."""
if not getattr(self.person.settings, "wake_with_alarm", True) or self.s.wake_by_hand:
s = self.s
if s.wake_by_hand or s.ended:
return False
if alarm is None:
fa = self.s.wake_from_alarm
if reported and fa and now < fa and self.s.wake_override == fa:
self.s.wake_override, self.s.wake_from_alarm, self.s.wake_source = None, None, None
self._ev(now, "wake_alarm_cleared", was=fa, reason="no alarm")
fa = s.wake_from_alarm
if not getattr(self.person.settings, "wake_with_alarm", True):
if fa and now < fa and s.wake_override == fa: # switched off: an alarm it followed lets go
s.wake_override, s.wake_from_alarm, s.wake_source = None, None, None
self._ev(now, "wake_alarm_cleared", was=fa, reason="setting off")
return True
return False
from datetime import datetime
d = night_date(datetime.fromtimestamp(now, self.tz))
rhythm = pick_rhythm(self.person.rhythms, d)
if rhythm is None or alarm <= now:

def drop(**why) -> bool:
# the followed alarm was turned off before it rang
if s.wake_override == fa:
s.wake_override, s.wake_from_alarm, s.wake_source = None, None, None
self._ev(now, "wake_alarm_cleared", was=fa, **why)
return True
s.wake_from_alarm = None # the wake came from the calendar: keep it, forget the alarm
return False
if alarm is None:
if reported and fa and now < fa:
return drop(reason="no alarm")
return False
planned = resolve(rhythm, d, self.tz).wake
day = self._wake_day(now)
if day is None or alarm <= now:
return False
planned = day[1]
if not (planned - self.ALARM_EARLIER_S <= alarm <= planned + self.ALARM_LATER_S):
# the alarm this wake came from was turned off before it rang (the phone's next alarm is now a later,
# other day's one): back to the planned wake, so sleeping in really is sleeping in. An earlier odd alarm
# (a 3 AM reminder) means nothing about the morning one, which is still set.
fa = self.s.wake_from_alarm
if fa and now < fa < alarm and self.s.wake_override == fa:
self.s.wake_override, self.s.wake_from_alarm, self.s.wake_source = None, None, None
self._ev(now, "wake_alarm_cleared", was=fa, planned=planned)
return True
if fa and now < fa < alarm:
return drop(planned=planned)
return False
if self.s.wake_from_alarm == alarm:
if fa == alarm:
return False
fa = self.s.wake_from_alarm
if fa and fa <= now and fa < alarm <= fa + self.SNOOZE_S:
if self.s.snoozed_until != alarm:
self.s.snoozed_until = alarm
self._ev(now, "alarm_snoozed", until=alarm, alarm=fa)
# the followed alarm already rang and the next one is a few minutes after it: a snooze. The wake holds
# at the time the alarm first rang (a reset alarm well after it is a real change of plan and is followed)
morning = s.wake_override if s.wake_override is not None else planned
if morning <= now and alarm > morning:
# the wake already came (the followed alarm rang, or the planned wake passed): the morning has begun and
# the wake holds. A later alarm the phone shows meanwhile is a snooze or a backup alarm, never a reason to
# push the wake later: that put the side back into its night phases, so getting up was read as a trip to
# the bathroom and the side kept running (and the phone's next alarm flipped between the snooze and the
# backup alarm, 2026-11-27). Staying in bed is the lie-in's job. Only a NEW alarm, set after they got up
# (the night over), followed once they lie back down, means going back to bed.
up = s.state == "done" or s.lie_in_over
if not up or alarm in s.alarms_seen or not self.last_present:
if not up and alarm not in s.alarms_seen:
s.alarms_seen = (s.alarms_seen + [alarm])[-12:]
if fa and fa <= now and alarm <= fa + self.SNOOZE_S and s.snoozed_until != alarm:
s.snoozed_until = alarm
self._ev(now, "alarm_snoozed", until=alarm, alarm=fa)
return False
s.alarms_seen = []
elif s.wake_source == "calendar" and s.wake_override is not None and alarm > s.wake_override:
s.wake_from_alarm = alarm # an early start in the calendar already wakes them earlier
return False
self.s.wake_override, self.s.wake_from_alarm, self.s.wake_source = alarm, alarm, None
s.wake_override, s.wake_from_alarm, s.wake_source = alarm, alarm, None
self._ev(now, "wake_from_alarm", wake=alarm, planned=planned)
return True

# ---------- an early start in the calendar ----------
CALENDAR_LEAD_S = 45 * 60 # warm wake this long before the first event (the app's "Wake up earlier" button)

def follow_calendar(self, now: float, early_start: float | None) -> bool:
"""Move tonight's wake earlier for an early first event tomorrow morning (the phone's or an HA calendar),
Settings: "Wake earlier for early events". Only ever earlier than the wake it would otherwise have (an earlier
alarm wins), at most ALARM_EARLIER_S before the planned wake, and never once the morning has begun. A wake set
by hand wins. When the event goes away (or the setting is switched off) before the wake, the wake goes back to
the alarm's, if one is followed, or the planned one."""
s = self.s
if s.wake_by_hand or s.ended or s.lie_in or s.lie_in_over or s.state == "done":
return False
day = self._wake_day(now)
if day is None:
return False
planned = day[1]
if (s.wake_override if s.wake_override is not None else planned) <= now:
return False # the morning has begun: the wake stays where it came
cur = s.wake_override if s.wake_source == "calendar" else None
# the wake without the calendar: the followed alarm's, or the planned one
fa = s.wake_from_alarm if (s.wake_from_alarm and s.wake_from_alarm > now
and (cur is not None or s.wake_override == s.wake_from_alarm)) else None
base = fa or planned
on = getattr(self.person.settings, "wake_with_calendar", True)
want = early_start - self.CALENDAR_LEAD_S if (early_start and on) else None
if want is not None and now < want and planned - self.ALARM_EARLIER_S <= want < base - 60:
if cur is not None and abs(cur - want) < 60:
return False
s.wake_override, s.wake_source = want, "calendar"
self._ev(now, "wake_from_calendar", wake=want, event=early_start, planned=planned)
return True
if cur is not None: # the event went away or moved later, or the setting was turned off
s.wake_override, s.wake_source = fa, None
if fa is None:
s.wake_from_alarm = None
self._ev(now, "wake_calendar_cleared", was=cur)
return True
return False

# ---------- lying in ----------
# the wake target runs on while they're still in bed (getting up ends it: LIE_IN_UP_S), up to this long past the
# night's end only as a safety net for a sensor stuck on "occupied" (2026-11-15: a 45-minute cap turned the side off
Expand Down Expand Up @@ -347,6 +421,7 @@ def _track_personal(self, now: float, present: bool | None) -> None:

def tick(self, now: float, present: bool | None, home: bool | None = None, active: float | None = None) -> float | None:
"""`active` = since when the people in bed have been awake and moving (engine/active.py), else None."""
self.last_present = present # follow_alarm reads it on the next tick (back in bed after getting up?)
from datetime import date, datetime
dt = datetime.fromtimestamp(now, self.tz)
d = night_date(dt)
Expand Down
1 change: 1 addition & 0 deletions core/embertide/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class PersonSettings(BaseModel):
wearable: str = Field("", max_length=20, pattern=r"^[a-z_]*$") # first-time setup's pick ("oura", "apple", "none"...)
setup_steps: list[str] = Field(default_factory=list, max_length=30) # first-time setup steps already done (skipped next time)
tone: Literal["calm", "friendly", "cheeky"] = "calm" # how notifications and suggestions are worded (voice.py); alerts stay plain
wake_with_calendar: bool = True # an early first event tomorrow morning (phone or HA calendar) moves the warm wake earlier
wake_with_alarm: bool = True # the warm wake lines up with the phone's next alarm (shared from the phone), earlier or a bit later
pet_questions: bool = True # ask "was the pet on the bed?" after something lay on your side that didn't look like you
# in / out of bed tuning (None = recommended): minutes lying down before the night starts, and minutes out of
Expand Down
7 changes: 4 additions & 3 deletions core/tests/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ async def test_the_warm_wake_follows_the_phones_alarm(tmp_path):

async def test_snoozing_the_alarm_holds_the_wake(tmp_path):
"""The followed alarm rings and is snoozed: the phone's next alarm is now 9 min later. The wake stays at the
time the alarm first rang; resetting the alarm an hour later (sleeping in on purpose) is still followed."""
time the alarm first rang. An alarm an hour later doesn't move it either (2026-11-27: a backup alarm moved the wake
back into the night, so getting up was read as a night-time trip); staying in bed is the lie-in's job."""
n = Night(tmp_path, "2026-11-05T22:00")
n.present("left", True)
await n.until("2026-11-06T03:00")
Expand All @@ -527,8 +528,8 @@ async def test_snoozing_the_alarm_holds_the_wake(tmp_path):
assert not e.follow_alarm(now, alarm + 9 * 60) # snooze
assert not e.follow_alarm(now, alarm + 18 * 60) # and again
assert abs(e.s.wake_override - alarm) < 1
assert e.follow_alarm(now, alarm + 3600) # a new alarm an hour later: followed
assert abs(e.s.wake_override - (alarm + 3600)) < 1
assert not e.follow_alarm(now, alarm + 3600) # a backup alarm an hour later: the wake holds
assert abs(e.s.wake_override - alarm) < 1


def test_a_short_sit_down_hours_before_isnt_getting_into_bed():
Expand Down
Loading