Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge sensor test #144

Merged
merged 14 commits into from
Dec 8, 2023
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Python ${{ env.DEFAULT_PYTHON }}
uses: actions/setup-python@v5.0.0
uses: actions/setup-python@v4.8.0
with:
python-version: ${{ env.DEFAULT_PYTHON }}

Expand Down
2 changes: 1 addition & 1 deletion pyhilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
TOKEN_EXPIRATION_PADDING: Final = 300
VERIFY: Final = True
DEVICE_REFRESH_TIME: Final = 1800
PYHILO_VERSION: Final = "2023.11.02"
PYHILO_VERSION: Final = "2023.12.01"
# TODO: Find a way to keep previous line in sync with pyproject.toml automatically

CONTENT_TYPE_FORM: Final = "application/x-www-form-urlencoded"
Expand Down
58 changes: 37 additions & 21 deletions pyhilo/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


class Event:
setting_deadline: datetime
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 @@ -56,36 +61,47 @@ def as_dict(self) -> dict[str, Any]:
return rep

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))
setattr(self, phase, from_utc_timestamp(value))
phase_match = re.match(r"(.*)(DateUTC|Utc)", key)
if phase_match:
phase = camel_to_snake(phase_match.group(1))
else:
phase = key
try:
setattr(self, phase, from_utc_timestamp(value))
except TypeError:
setattr(self, phase, value)
self.phases_list.append(phase)
for phase in self.__annotations__:
if phase not in self.phases_list:
# On t'aime Carl
setattr(self, phase, from_utc_timestamp("2023-11-15T20:00:00+00:00"))

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:
"""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
return self._create_phases(hours, "appreciation", "preheat")

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

@property
def state(self) -> str:
now = datetime.now(self.preheat_start.tzinfo)
if (
"appreciation_start" in self.phases_list
and self.appreciation_start <= now < self.appreciation_end
):
if self.pre_cold_start <= now < self.pre_cold_end:
return "pre_cold"
elif self.appreciation_start <= now < self.appreciation_end:
return "appreciation"
elif self.preheat_start > now:
return "scheduled"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exclude = ".venv/.*"

[tool.poetry]
name = "python-hilo"
version = "2023.11.01"
version = "2023.12.01"
description = "A Python3, async interface to the Hilo API"
readme = "README.md"
authors = ["David Vallee Delisle <[email protected]>"]
Expand Down