Improve logging

This commit is contained in:
Andre Basche 2023-04-10 06:42:40 +02:00
parent 3e3fc7ad66
commit ba208ffd98
2 changed files with 15 additions and 7 deletions

View File

@ -3,6 +3,7 @@ import logging
import re import re
import secrets import secrets
import urllib import urllib
from pprint import pformat
from urllib import parse from urllib import parse
from yarl import URL from yarl import URL
@ -118,11 +119,14 @@ class HonAuth:
) as response: ) as response:
if response.status == 200: if response.status == 200:
try: try:
return (await response.json())["events"][0]["attributes"]["values"][ data = await response.json()
"url" return data["events"][0]["attributes"]["values"]["url"]
]
except json.JSONDecodeError: except json.JSONDecodeError:
pass pass
except KeyError:
_LOGGER.error(
"Can't get login url - %s", pformat(await response.json())
)
_LOGGER.error( _LOGGER.error(
"Unable to login: %s\n%s", response.status, await response.text() "Unable to login: %s\n%s", response.status, await response.text()
) )
@ -133,7 +137,10 @@ class HonAuth:
if resp.status != 200: if resp.status != 200:
_LOGGER.error("Unable to get token: %s", resp.status) _LOGGER.error("Unable to get token: %s", resp.status)
return False return False
url = re.findall("href\\s*=\\s*[\"'](.*?)[\"']", await resp.text()) url = re.findall("href\\s*=\\s*[\"'](http.+?)[\"']", await resp.text())
if not url:
_LOGGER.error("Can't get login url - \n%s", await resp.text())
raise PermissionError
async with self._session.get(url[0]) as resp: async with self._session.get(url[0]) as resp:
if resp.status != 200: if resp.status != 200:
_LOGGER.error("Unable to get token: %s", resp.status) _LOGGER.error("Unable to get token: %s", resp.status)
@ -190,3 +197,4 @@ class HonAuth:
data = await resp.json() data = await resp.json()
self._id_token = data["id_token"] self._id_token = data["id_token"]
self._access_token = data["access_token"] self._access_token = data["access_token"]
return True

View File

@ -7,7 +7,7 @@ with open("README.md", "r") as f:
setup( setup(
name="pyhOn", name="pyhOn",
version="0.5.0", version="0.6.0",
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,