Fix connection failures

This commit is contained in:
Andre Basche 2024-04-09 22:46:47 +02:00
parent 6aa7dd8112
commit 63752e13d6
4 changed files with 8 additions and 4 deletions

View File

@ -78,6 +78,7 @@ class HonAPI:
self._password, self._password,
session=self._session, session=self._session,
mobile_id=self._mobile_id, mobile_id=self._mobile_id,
refresh_token=self._refresh_token,
).create() ).create()
return self return self

View File

@ -62,6 +62,7 @@ class HonConnectionHandler(ConnectionHandler):
await self.auth.refresh(self._refresh_token) await self.auth.refresh(self._refresh_token)
if not (self.auth.cognito_token and self.auth.id_token): if not (self.auth.cognito_token and self.auth.id_token):
await self.auth.authenticate() await self.auth.authenticate()
self._refresh_token = self.auth.refresh_token
headers["cognito-token"] = self.auth.cognito_token headers["cognito-token"] = self.auth.cognito_token
headers["id-token"] = self.auth.id_token headers["id-token"] = self.auth.id_token
return self._HEADERS | headers return self._HEADERS | headers
@ -77,7 +78,7 @@ class HonConnectionHandler(ConnectionHandler):
self.auth.token_expires_soon or response.status in [401, 403] self.auth.token_expires_soon or response.status in [401, 403]
) and loop == 0: ) and loop == 0:
_LOGGER.info("Try refreshing token...") _LOGGER.info("Try refreshing token...")
await self.auth.refresh() await self.auth.refresh(self._refresh_token)
async with self._intercept( async with self._intercept(
method, url, *args, loop=loop + 1, **kwargs method, url, *args, loop=loop + 1, **kwargs
) as result: ) as result:

View File

@ -35,6 +35,7 @@ class MQTTClient:
async def create(self) -> "MQTTClient": async def create(self) -> "MQTTClient":
await self._start() await self._start()
self._subscribe_appliances() self._subscribe_appliances()
await self.start_watchdog()
return self return self
def _on_lifecycle_stopped( def _on_lifecycle_stopped(
@ -63,6 +64,7 @@ class MQTTClient:
self, self,
lifecycle_connection_failure_data: mqtt5.LifecycleConnectFailureData, lifecycle_connection_failure_data: mqtt5.LifecycleConnectFailureData,
) -> None: ) -> None:
self._connection = False
_LOGGER.info( _LOGGER.info(
"Lifecycle Connection Failure - %s", str(lifecycle_connection_failure_data) "Lifecycle Connection Failure - %s", str(lifecycle_connection_failure_data)
) )
@ -88,7 +90,6 @@ class MQTTClient:
parameter parameter
) )
appliance.sync_params_to_command("settings") appliance.sync_params_to_command("settings")
self._hon.notify()
elif topic and "disconnected" in topic: elif topic and "disconnected" in topic:
_LOGGER.info( _LOGGER.info(
"Disconnected %s: %s", "Disconnected %s: %s",
@ -101,6 +102,7 @@ class MQTTClient:
_LOGGER.info("Connected %s", appliance.nick_name) _LOGGER.info("Connected %s", appliance.nick_name)
elif topic and "discovery" in topic: elif topic and "discovery" in topic:
_LOGGER.info("Discovered %s", appliance.nick_name) _LOGGER.info("Discovered %s", appliance.nick_name)
self._hon.notify()
_LOGGER.info("%s - %s", topic, payload) _LOGGER.info("%s - %s", topic, payload)
async def _start(self) -> None: async def _start(self) -> None:
@ -133,7 +135,7 @@ class MQTTClient:
async def start_watchdog(self) -> None: async def start_watchdog(self) -> None:
if not self._watchdog_task or self._watchdog_task.done(): 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: async def _watchdog(self) -> None:
while True: while True:

View File

@ -7,7 +7,7 @@ with open("README.md", "r", encoding="utf-8") as f:
setup( setup(
name="pyhOn", name="pyhOn",
version="0.17.3", version="0.17.4",
author="Andre Basche", author="Andre Basche",
description="Control hOn devices with python", description="Control hOn devices with python",
long_description=long_description, long_description=long_description,