Skip to content

Commit

Permalink
Merge pull request #165 from dvd-dev/event_update_time
Browse files Browse the repository at this point in the history
Event update time
  • Loading branch information
ic-dev21 authored Jan 20, 2024
2 parents 25141db + 28c2ced commit d74ba7b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
1 change: 0 additions & 1 deletion .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- main
- master

jobs:
labeler:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- main
- master
jobs:
draft_release:
name: Release Drafter
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- main
- master
- dev
pull_request:
schedule:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ repos:
- id: no-commit-to-branch
args:
- --branch=dev
- --branch=master
- --branch=main
# - repo: https://github.com/PyCQA/pydocstyle
# rev: 5.0.2
# hooks:
Expand Down
31 changes: 29 additions & 2 deletions pyhilo/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Event object """
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
import re
from typing import Any, cast

Expand Down Expand Up @@ -38,8 +38,12 @@ def __init__(self, **event: dict[str, Any]):
self.allowed_kWh: float = round(allowed_wH / 1000, 2)
self.used_kWh: float = round(used_wH / 1000, 2)
self.used_percentage: float = 0
self.last_update = datetime.now(timezone.utc).astimezone()
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 All @@ -52,6 +56,7 @@ def __init__(self, **event: dict[str, Any]):
"allowed_kWh",
"used_kWh",
"used_percentage",
"last_update",
]

def as_dict(self) -> dict[str, Any]:
Expand All @@ -75,7 +80,7 @@ def _convert_phases(self, phases: dict[str, Any]) -> None:
self.phases_list.append(phase)
for phase in self.__annotations__:
if phase not in self.phases_list:
# On t'aime Carl
# On t'aime Karl
setattr(self, phase, from_utc_timestamp("2023-11-15T20:00:00+00:00"))

def _create_phases(
Expand All @@ -96,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", "scheduled", "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
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 = "2024.1.1"
version = "2024.1.2"
description = "A Python3, async interface to the Hilo API"
readme = "README.md"
authors = ["David Vallee Delisle <[email protected]>"]
Expand Down

0 comments on commit d74ba7b

Please sign in to comment.