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
2 changes: 1 addition & 1 deletion pyhilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
LOG: Final = logging.getLogger(__package__)
DEFAULT_STATE_FILE: Final = "hilo_state.yaml"
REQUEST_RETRY: Final = 9
PYHILO_VERSION: Final = "2025.12.05"
PYHILO_VERSION: Final = "2026.1.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
18 changes: 15 additions & 3 deletions pyhilo/event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Event object """
"""Event object"""

from datetime import datetime, timedelta, timezone
import logging
import re
Expand Down Expand Up @@ -26,15 +27,15 @@ class Event:

def __init__(self, **event: dict[str, Any]):
"""Initialize."""
self._convert_phases(cast(dict[str, Any], event.get("phases")))
self._convert_phases(cast(dict[str, Any], event.get("phases", {})))
params: dict[str, Any] = event.get("parameters") or {}
devices: list[dict[str, Any]] = params.get("devices", [])
consumption: dict[str, Any] = event.get("consumption", {})
allowed_wH: int = consumption.get("baselineWh", 0) or 0
used_wH: int = consumption.get("currentWh", 0) or 0
self.participating: bool = cast(bool, event.get("isParticipating", False))
self.configurable: bool = cast(bool, event.get("isConfigurable", False))
self.period: str = cast(str, event.get("period", ""))
self.period: str = (cast(str, event.get("period", "")) or "").lower()
self.event_id: int = cast(int, event["id"])
self.total_devices: int = len(devices)
self.opt_out_devices: int = len([x for x in devices if x["optOut"]])
Expand All @@ -44,6 +45,7 @@ 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.reward = cast(float, event.get("reward", 0.0))
self.last_update = datetime.now(timezone.utc).astimezone()
if allowed_wH > 0:
self.used_percentage = round(used_wH / allowed_wH * 100, 2)
Expand All @@ -63,19 +65,29 @@ def __init__(self, **event: dict[str, Any]):
"used_kWh",
"used_percentage",
"last_update",
"reward",
]

def update_wh(self, used_wH: float) -> None:
"""This function is used to update the used_kWh attribute during a Hilo Challenge Event"""
LOG.debug("Updating Wh: %s", used_wH)
self.used_kWh = round(used_wH / 1000, 2)
self.last_update = datetime.now(timezone.utc).astimezone()
self._recalculate_percentage()

def update_allowed_wh(self, allowed_wH: float) -> None:
"""This function is used to update the allowed_kWh attribute during a Hilo Challenge Event"""
LOG.debug("Updating allowed Wh: %s", allowed_wH)
self.allowed_kWh = round(allowed_wH / 1000, 2)
self.last_update = datetime.now(timezone.utc).astimezone()
self._recalculate_percentage()

def _recalculate_percentage(self) -> None:
"""Recalculate used percentage based on current values"""
if self.allowed_kWh > 0:
self.used_percentage = round(self.used_kWh / self.allowed_kWh * 100, 2)
else:
self.used_percentage = 0

def should_check_for_allowed_wh(self) -> bool:
"""This function is used to authorize subscribing to a specific event in Hilo to receive the allowed_kWh
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 = "2025.12.5"
version = "2026.1.6"
description = "A Python3, async interface to the Hilo API"
readme = "README.md"
authors = ["David Vallee Delisle <[email protected]>"]
Expand Down
Loading