Skip to content

Commit

Permalink
Changing phases' keys
Browse files Browse the repository at this point in the history
  • Loading branch information
valleedelisle committed Dec 7, 2023
1 parent 5871b1e commit 71a8c3a
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions pyhilo/event.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""Event object """
from datetime import datetime, timedelta
import re
from typing import Any, cast

from pyhilo.util import camel_to_snake, from_utc_timestamp
from pyhilo.const import LOG
from pyhilo.util import from_utc_timestamp


class Event:
pre_cold_start: datetime
pre_cold_end: datetime
appreciation_start: datetime
appreciation_end: datetime
preheat_start: datetime
preheat_end: datetime
reduction_start: datetime
Expand Down Expand Up @@ -63,40 +59,25 @@ def _convert_phases(self, phases: dict[str, Any]) -> None:
if not len(phases):
return
self.phases_list = []
for key, value in phases.items():
if not key.endswith("DateUTC"):
continue
phase_match = re.match(r"(.*)DateUTC", key)
if not phase_match:
continue
phase = camel_to_snake(phase_match.group(1))
LOG.debug(f"getting phases for {self.event_id=}: {phases}")
for phase, value in phases.items():
setattr(self, phase, from_utc_timestamp(value))
self.phases_list.append(phase)

def _create_phases(
self, hours: int, phase_name: str, parent_phase: str
) -> datetime:
parent_start = getattr(self, f"{parent_phase}_start")
phase_start = f"{phase_name}_start"
phase_end = f"{phase_name}_end"
setattr(self, phase_start, parent_start - timedelta(hours=hours))
setattr(self, phase_end, parent_start)
if phase_start not in self.phases_list:
self.phases_list[:0] = [phase_start, phase_end]
return getattr(self, phase_start) # type: ignore [no-any-return]

def appreciation(self, hours: int) -> datetime:
return self._create_phases(hours, "appreciation", "preheat")

def pre_cold(self, hours: int) -> datetime:
return self._create_phases(hours, "pre_cold", "appreciation")
"""Wrapper to return X hours before pre_heat.
Will also set appreciation_start and appreciation end phases.
"""
self.appreciation_start = self.preheat_start - timedelta(hours=hours)
self.appreciation_end = self.preheat_start
if "appreciation_start" not in self.phases_list:
self.phases_list[:0] = ["appreciation_start", "appreciation_end"]
return self.appreciation_start

@property
def state(self) -> str:
now = datetime.now(self.preheat_start.tzinfo)
if "pre_cold_start" in self.phases_list and self.pre_cold_start <= now:
return "pre_cold"
elif (
if (
"appreciation_start" in self.phases_list
and self.appreciation_start <= now < self.appreciation_end
):
Expand Down

0 comments on commit 71a8c3a

Please sign in to comment.