Compare commits

..

4 Commits

Author SHA1 Message Date
Andre Basche 59e3d9949f Merge pull request #29 from addshore/updatedLogin
Update for new hOn login
2024-08-14 22:42:39 +02:00
addshore 3f39be53f3 Bump 0.17.5 2024-07-10 12:47:30 +01:00
addshore afb5b8c3ea Update for new hOn login
https://github.com/Andre0512/hon/issues/230
2024-07-10 12:39:18 +01:00
Andre Basche 63752e13d6 Fix connection failures 2024-04-09 22:47:28 +02:00
5 changed files with 16 additions and 7 deletions
+1
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
+8 -3
View File
@@ -120,18 +120,23 @@ class HonAuth:
async with self._request.get(url) as response: async with self._request.get(url) as response:
text = await response.text() text = await response.text()
self._expires = datetime.utcnow() self._expires = datetime.utcnow()
login_url: List[str] = re.findall("url = '(.+?)'", text) login_url: List[str] = re.findall("(?:url|href) ?= ?'(.+?)'", text)
if not login_url: if not login_url:
if "oauth/done#access_token=" in text: if "oauth/done#access_token=" in text:
self._parse_token_data(text) self._parse_token_data(text)
raise exceptions.HonNoAuthenticationNeeded() raise exceptions.HonNoAuthenticationNeeded()
await self._error_logger(response) await self._error_logger(response)
# As of July 2024 the login page has changed, and we started getting a /NewhOnLogin based relative URL in JS to parse
if login_url[0].startswith("/NewhOnLogin"):
# Force use of the old login page to avoid having to make the new one work..
login_url[0] = f"{const.AUTH_API}/s/login{login_url[0]}"
return login_url[0] return login_url[0]
async def _manual_redirect(self, url: str) -> str: async def _manual_redirect(self, url: str) -> str:
async with self._request.get(url, allow_redirects=False) as response: async with self._request.get(url, allow_redirects=False) as response:
if not (new_location := response.headers.get("Location", "")): new_location = response.headers.get("Location", "")
await self._error_logger(response) if not new_location:
return url
return new_location return new_location
async def _handle_redirects(self, login_url: str) -> str: async def _handle_redirects(self, login_url: str) -> str:
+2 -1
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:
+4 -2
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:
+1 -1
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.5",
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,