Skip to content

Commit

Permalink
Adding an invalid attribute to event
Browse files Browse the repository at this point in the history
We determine if an event status is valid if the last_update time is
older than the current phase's start time.
  • Loading branch information
valleedelisle committed Jan 14, 2024
1 parent 10af3d1 commit b85f440
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pyhilo/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def __init__(self, **event: dict[str, Any]):
self.last_update = datetime.now()
if allowed_wH > 0:
self.used_percentage = round(used_wH / allowed_wH * 100, 2)
self._phase_time_mapping = {
"pre_heat": "preheat",
}
self.dict_items = [
"event_id",
"participating",
Expand Down Expand Up @@ -98,6 +101,28 @@ def appreciation(self, hours: int) -> datetime:
def pre_cold(self, hours: int) -> datetime:
return self._create_phases(hours, "pre_cold", "appreciation")

@property
def invalid(self) -> bool:
return cast(
bool,
(
self.current_phase_times
and self.last_update < self.current_phase_times["start"]
),
)

@property
def current_phase_times(self) -> dict[str, datetime]:
if self.state in ["completed", "off", "unknown"]:
return {}
phase_timestamp = self._phase_time_mapping.get(self.state, self.state)
phase_start = f"{phase_timestamp}_start"
phase_end = f"{phase_timestamp}_end"
return {
"start": getattr(self, phase_start),
"end": getattr(self, phase_end),
}

@property
def state(self) -> str:
now = datetime.now(self.preheat_start.tzinfo)
Expand Down

0 comments on commit b85f440

Please sign in to comment.