From 63752e13d6c8e74284ad72e04306e9fedde58b40 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Tue, 9 Apr 2024 22:46:47 +0200 Subject: [PATCH] Fix connection failures --- pyhon/connection/api.py | 1 + pyhon/connection/handler/hon.py | 3 ++- pyhon/connection/mqtt.py | 6 ++++-- setup.py | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pyhon/connection/api.py b/pyhon/connection/api.py index 788a067..2140fa4 100644 --- a/pyhon/connection/api.py +++ b/pyhon/connection/api.py @@ -78,6 +78,7 @@ class HonAPI: self._password, session=self._session, mobile_id=self._mobile_id, + refresh_token=self._refresh_token, ).create() return self diff --git a/pyhon/connection/handler/hon.py b/pyhon/connection/handler/hon.py index 988c8f4..9cce8ee 100644 --- a/pyhon/connection/handler/hon.py +++ b/pyhon/connection/handler/hon.py @@ -62,6 +62,7 @@ class HonConnectionHandler(ConnectionHandler): await self.auth.refresh(self._refresh_token) if not (self.auth.cognito_token and self.auth.id_token): await self.auth.authenticate() + self._refresh_token = self.auth.refresh_token headers["cognito-token"] = self.auth.cognito_token headers["id-token"] = self.auth.id_token return self._HEADERS | headers @@ -77,7 +78,7 @@ class HonConnectionHandler(ConnectionHandler): self.auth.token_expires_soon or response.status in [401, 403] ) and loop == 0: _LOGGER.info("Try refreshing token...") - await self.auth.refresh() + await self.auth.refresh(self._refresh_token) async with self._intercept( method, url, *args, loop=loop + 1, **kwargs ) as result: diff --git a/pyhon/connection/mqtt.py b/pyhon/connection/mqtt.py index 3210c6e..7c60a53 100644 --- a/pyhon/connection/mqtt.py +++ b/pyhon/connection/mqtt.py @@ -35,6 +35,7 @@ class MQTTClient: async def create(self) -> "MQTTClient": await self._start() self._subscribe_appliances() + await self.start_watchdog() return self def _on_lifecycle_stopped( @@ -63,6 +64,7 @@ class MQTTClient: self, lifecycle_connection_failure_data: mqtt5.LifecycleConnectFailureData, ) -> None: + self._connection = False _LOGGER.info( "Lifecycle Connection Failure - %s", str(lifecycle_connection_failure_data) ) @@ -88,7 +90,6 @@ class MQTTClient: parameter ) appliance.sync_params_to_command("settings") - self._hon.notify() elif topic and "disconnected" in topic: _LOGGER.info( "Disconnected %s: %s", @@ -101,6 +102,7 @@ class MQTTClient: _LOGGER.info("Connected %s", appliance.nick_name) elif topic and "discovery" in topic: _LOGGER.info("Discovered %s", appliance.nick_name) + self._hon.notify() _LOGGER.info("%s - %s", topic, payload) async def _start(self) -> None: @@ -133,7 +135,7 @@ class MQTTClient: async def start_watchdog(self) -> None: if not self._watchdog_task or self._watchdog_task.done(): - await asyncio.create_task(self._watchdog()) + self._watchdog_task = asyncio.create_task(self._watchdog()) async def _watchdog(self) -> None: while True: diff --git a/setup.py b/setup.py index 58a9454..115205d 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r", encoding="utf-8") as f: setup( name="pyhOn", - version="0.17.3", + version="0.17.4", author="Andre Basche", description="Control hOn devices with python", long_description=long_description,