Skip to content

Commit

Permalink
Fix connection failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Apr 9, 2024
1 parent 6aa7dd8 commit 63752e1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions pyhon/connection/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def create(self) -> Self:
self._password,
session=self._session,
mobile_id=self._mobile_id,
refresh_token=self._refresh_token,
).create()
return self

Expand Down
3 changes: 2 additions & 1 deletion pyhon/connection/handler/hon.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async def _check_headers(self, headers: Dict[str, str]) -> Dict[str, str]:
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
Expand All @@ -77,7 +78,7 @@ async def _intercept(
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:
Expand Down
6 changes: 4 additions & 2 deletions pyhon/connection/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def client(self) -> mqtt5.Client:
async def create(self) -> "MQTTClient":
await self._start()
self._subscribe_appliances()
await self.start_watchdog()
return self

def _on_lifecycle_stopped(
Expand Down Expand Up @@ -63,6 +64,7 @@ def _on_lifecycle_connection_failure(
self,
lifecycle_connection_failure_data: mqtt5.LifecycleConnectFailureData,
) -> None:
self._connection = False
_LOGGER.info(
"Lifecycle Connection Failure - %s", str(lifecycle_connection_failure_data)
)
Expand All @@ -88,7 +90,6 @@ def _on_publish_received(self, data: mqtt5.PublishReceivedData) -> None:
parameter
)
appliance.sync_params_to_command("settings")
self._hon.notify()
elif topic and "disconnected" in topic:
_LOGGER.info(
"Disconnected %s: %s",
Expand All @@ -101,6 +102,7 @@ def _on_publish_received(self, data: mqtt5.PublishReceivedData) -> None:
_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:
Expand Down Expand Up @@ -133,7 +135,7 @@ def _subscribe(self, appliance: HonAppliance) -> None:

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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="pyhOn",
version="0.17.3",
version="0.17.4",
author="Andre Basche",
description="Control hOn devices with python",
long_description=long_description,
Expand Down

0 comments on commit 63752e1

Please sign in to comment.