Skip to content

Commit

Permalink
Merge pull request #58 from BAERnado/master
Browse files Browse the repository at this point in the history
Fix #56 - Add versioning to test_connection and test_auth
  • Loading branch information
peribeir authored Sep 21, 2024
2 parents a5e49a4 + 9471c02 commit dded97a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions homepilot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, host, password, api_version = 1) -> None:
self._host = host
self._password = password
self._api_version = api_version
self._base_path = "/hp" if api_version == 2 else ""
self._base_path = HomePilotApi.get_base_path(api_version)

@staticmethod
async def test_connection(host: str) -> str:
Expand All @@ -43,23 +43,43 @@ async def test_connection(host: str) -> str:
if response.status != 200:
response = await session.get(f"http://{host}/hp/devices/0")
if response.status != 200:
return "error"
return "ok_v2"
if response.status != 401:
return "error"
# Otherwise try for login requirements
else:
return "ok_v2"

response = await session.post(
f"http://{host}/authentication/password_salt"
)
if response.status == 500:
return "ok"
else:
if response.status == 401:
response = await session.post(
f"http://{host}/hp/authentication/password_salt"
)
if response.status == 200:
return "auth_required_v2"
else:
return "error"
return "auth_required"
except ClientConnectorError:
return "error"

@staticmethod
async def test_auth(host: str, password: str) -> AbstractCookieJar:
def get_base_path(api_version) -> str:
if api_version == 2:
return "/hp"
else:
return ""

@staticmethod
async def test_auth(host: str, password: str, api_version: int = 1) -> AbstractCookieJar:
cookie_jar = aiohttp.CookieJar(unsafe=True)
base_path = HomePilotApi.get_base_path(api_version)
async with aiohttp.ClientSession(cookie_jar=cookie_jar) as session:
response = await session.post(f"http://{host}/authentication/password_salt")
response = await session.post(f"http://{host}{base_path}/authentication/password_salt")
response_data = await response.json()
if response.status == 500 and response_data["error_code"] == 5007:
raise AuthError()
Expand All @@ -71,7 +91,7 @@ async def test_auth(host: str, password: str) -> AbstractCookieJar:
f"{salt}{hashed_password}".encode("utf-8")
).hexdigest()
response = await session.post(
f"http://{host}/authentication/login",
f"http://{host}{base_path}/authentication/login",
json={"password": salted_password, "password_salt": salt},
)
if response.status != 200:
Expand All @@ -80,7 +100,7 @@ async def test_auth(host: str, password: str) -> AbstractCookieJar:

async def authenticate(self):
if not self.authenticated and self.password != "":
self.cookie_jar = await HomePilotApi.test_auth(self.host, self.password)
self.cookie_jar = await HomePilotApi.test_auth(self.host, self.password, self.api_version)
self._authenticated = True

async def get_devices(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyrademacher",
version="0.13.2",
version="0.13.5",
author="Pedro Ribeiro",
author_email="[email protected]",
description="Control devices connected to your Rademacher Homepilot "
Expand Down

0 comments on commit dded97a

Please sign in to comment.