diff --git a/pyhilo/api.py b/pyhilo/api.py index d02f5d9..07e4b33 100755 --- a/pyhilo/api.py +++ b/pyhilo/api.py @@ -141,7 +141,7 @@ async def async_get_access_token(self) -> str: await self._oauth_session.async_ensure_token_valid() access_token = str(self._oauth_session.token["access_token"]) - LOG.debug("LOCALLY Websocket access token is %s", access_token) + LOG.debug("Websocket access token is %s", access_token) return str(self._oauth_session.token["access_token"]) diff --git a/pyhilo/event.py b/pyhilo/event.py index 0e7b2e9..ba7612c 100755 --- a/pyhilo/event.py +++ b/pyhilo/event.py @@ -27,7 +27,7 @@ class Event: def __init__(self, **event: dict[str, Any]): """Initialize.""" self._convert_phases(cast(dict[str, Any], event.get("phases"))) - params: dict[str, Any] = event.get("parameters", {}) + 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 @@ -71,6 +71,12 @@ def update_wh(self, used_wH: float) -> None: self.used_kWh = round(used_wH / 1000, 2) self.last_update = datetime.now(timezone.utc).astimezone() + 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() + 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 that is made available in the pre_heat phase""" diff --git a/pyhilo/websocket.py b/pyhilo/websocket.py index 7b55cf4..bd23cb7 100755 --- a/pyhilo/websocket.py +++ b/pyhilo/websocket.py @@ -310,6 +310,10 @@ async def async_connect(self) -> None: raise CannotConnectError(err) from err LOG.info(f"Connected to websocket server {self._api.endpoint}") + + # Quick pause to prevent race condition + await asyncio.sleep(0.05) + self._watchdog.trigger() for callback in self._connect_callbacks: schedule_callback(callback)