Skip to content

Commit 39edbab

Browse files
authored
Merge pull request #350 from dvd-dev/test_kwh
Fix pour updater les valeurs de kwh
2 parents 66099c7 + f7a1545 commit 39edbab

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

pyhilo/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async def async_get_access_token(self) -> str:
141141
await self._oauth_session.async_ensure_token_valid()
142142

143143
access_token = str(self._oauth_session.token["access_token"])
144-
LOG.debug("LOCALLY Websocket access token is %s", access_token)
144+
LOG.debug("Websocket access token is %s", access_token)
145145

146146
return str(self._oauth_session.token["access_token"])
147147

pyhilo/event.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Event:
2727
def __init__(self, **event: dict[str, Any]):
2828
"""Initialize."""
2929
self._convert_phases(cast(dict[str, Any], event.get("phases")))
30-
params: dict[str, Any] = event.get("parameters", {})
30+
params: dict[str, Any] = event.get("parameters") or {}
3131
devices: list[dict[str, Any]] = params.get("devices", [])
3232
consumption: dict[str, Any] = event.get("consumption", {})
3333
allowed_wH: int = consumption.get("baselineWh", 0) or 0
@@ -71,6 +71,12 @@ def update_wh(self, used_wH: float) -> None:
7171
self.used_kWh = round(used_wH / 1000, 2)
7272
self.last_update = datetime.now(timezone.utc).astimezone()
7373

74+
def update_allowed_wh(self, allowed_wH: float) -> None:
75+
"""This function is used to update the allowed_kWh attribute during a Hilo Challenge Event"""
76+
LOG.debug("Updating allowed Wh: %s", allowed_wH)
77+
self.allowed_kWh = round(allowed_wH / 1000, 2)
78+
self.last_update = datetime.now(timezone.utc).astimezone()
79+
7480
def should_check_for_allowed_wh(self) -> bool:
7581
"""This function is used to authorize subscribing to a specific event in Hilo to receive the allowed_kWh
7682
that is made available in the pre_heat phase"""

pyhilo/websocket.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ async def async_connect(self) -> None:
310310
raise CannotConnectError(err) from err
311311

312312
LOG.info(f"Connected to websocket server {self._api.endpoint}")
313+
314+
# Quick pause to prevent race condition
315+
await asyncio.sleep(0.05)
316+
313317
self._watchdog.trigger()
314318
for callback in self._connect_callbacks:
315319
schedule_callback(callback)

0 commit comments

Comments
 (0)