Skip to content

Commit ac4cf27

Browse files
authored
Merge pull request #362 from fersingb/add_reward_field
Add reward field
2 parents 7304998 + e5d76d2 commit ac4cf27

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

pyhilo/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
LOG: Final = logging.getLogger(__package__)
88
DEFAULT_STATE_FILE: Final = "hilo_state.yaml"
99
REQUEST_RETRY: Final = 9
10-
PYHILO_VERSION: Final = "2025.12.05"
10+
PYHILO_VERSION: Final = "2026.1.01"
1111
# TODO: Find a way to keep previous line in sync with pyproject.toml automatically
1212

1313
CONTENT_TYPE_FORM: Final = "application/x-www-form-urlencoded"

pyhilo/event.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Event object """
1+
"""Event object"""
2+
23
from datetime import datetime, timedelta, timezone
34
import logging
45
import re
@@ -26,15 +27,15 @@ class Event:
2627

2728
def __init__(self, **event: dict[str, Any]):
2829
"""Initialize."""
29-
self._convert_phases(cast(dict[str, Any], event.get("phases")))
30+
self._convert_phases(cast(dict[str, Any], event.get("phases", {})))
3031
params: dict[str, Any] = event.get("parameters") or {}
3132
devices: list[dict[str, Any]] = params.get("devices", [])
3233
consumption: dict[str, Any] = event.get("consumption", {})
3334
allowed_wH: int = consumption.get("baselineWh", 0) or 0
3435
used_wH: int = consumption.get("currentWh", 0) or 0
3536
self.participating: bool = cast(bool, event.get("isParticipating", False))
3637
self.configurable: bool = cast(bool, event.get("isConfigurable", False))
37-
self.period: str = cast(str, event.get("period", ""))
38+
self.period: str = (cast(str, event.get("period", "")) or "").lower()
3839
self.event_id: int = cast(int, event["id"])
3940
self.total_devices: int = len(devices)
4041
self.opt_out_devices: int = len([x for x in devices if x["optOut"]])
@@ -44,6 +45,7 @@ def __init__(self, **event: dict[str, Any]):
4445
self.allowed_kWh: float = round(allowed_wH / 1000, 2)
4546
self.used_kWh: float = round(used_wH / 1000, 2)
4647
self.used_percentage: float = 0
48+
self.reward = cast(float, event.get("reward", 0.0))
4749
self.last_update = datetime.now(timezone.utc).astimezone()
4850
if allowed_wH > 0:
4951
self.used_percentage = round(used_wH / allowed_wH * 100, 2)
@@ -63,19 +65,29 @@ def __init__(self, **event: dict[str, Any]):
6365
"used_kWh",
6466
"used_percentage",
6567
"last_update",
68+
"reward",
6669
]
6770

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

7478
def update_allowed_wh(self, allowed_wH: float) -> None:
7579
"""This function is used to update the allowed_kWh attribute during a Hilo Challenge Event"""
7680
LOG.debug("Updating allowed Wh: %s", allowed_wH)
7781
self.allowed_kWh = round(allowed_wH / 1000, 2)
7882
self.last_update = datetime.now(timezone.utc).astimezone()
83+
self._recalculate_percentage()
84+
85+
def _recalculate_percentage(self) -> None:
86+
"""Recalculate used percentage based on current values"""
87+
if self.allowed_kWh > 0:
88+
self.used_percentage = round(self.used_kWh / self.allowed_kWh * 100, 2)
89+
else:
90+
self.used_percentage = 0
7991

8092
def should_check_for_allowed_wh(self) -> bool:
8193
"""This function is used to authorize subscribing to a specific event in Hilo to receive the allowed_kWh

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ exclude = ".venv/.*"
4040

4141
[tool.poetry]
4242
name = "python-hilo"
43-
version = "2025.12.5"
43+
version = "2026.1.6"
4444
description = "A Python3, async interface to the Hilo API"
4545
readme = "README.md"
4646
authors = ["David Vallee Delisle <me@dvd.dev>"]

0 commit comments

Comments
 (0)