Skip to content

Commit

Permalink
use homeassistant.helpers.httpx_client.get_async_client()
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter authored and leeyuentuen committed Oct 18, 2024
1 parent f1693af commit 2befab9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
5 changes: 3 additions & 2 deletions custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import logging
from datetime import datetime, timedelta

import httpx
from homeassistant.core import HomeAssistant
from homeassistant.helpers.httpx_client import httpx
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.util.unit_system import METRIC_SYSTEM, UnitSystem
from urllib3 import disable_warnings

Expand All @@ -23,7 +24,7 @@ def __init__(self, hass: HomeAssistant, username: str, password: str) -> None:
"""Initialize the Polestar API."""
self.id = None
self.name = "Polestar "
self.polestarApi = PolestarApi(username, password, httpx.AsyncClient())
self.polestarApi = PolestarApi(username, password, get_async_client(hass))
self.vin = None
self.unit_system = METRIC_SYSTEM
disable_warnings()
Expand Down
17 changes: 7 additions & 10 deletions custom_components/polestar_api/pypolestar/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ class PolestarAuth:
"""base class for Polestar authentication."""

def __init__(
self,
username: str,
password: str,
client_session: httpx.AsyncClient | None = None,
self, username: str, password: str, client_session: httpx.AsyncClient
) -> None:
"""Initialize the Polestar authentication."""
self.username = username
self.password = password
self.client_session = client_session
self.access_token = None
self.refresh_token = None
self.token_expiry = None
self.latest_call_code = None
self._client_session = client_session or httpx.AsyncClient()

async def get_token(self, refresh=False) -> None:
"""Get the token from Polestar."""
Expand Down Expand Up @@ -58,7 +55,7 @@ async def get_token(self, refresh=False) -> None:
"operationName": operationName,
"variables": json.dumps({"token": token}),
}
result = await self._client_session.get(
result = await self.client_session.get(
"https://pc-api.polestar.com/eu-north-1/auth/",
params=params,
headers=headers,
Expand Down Expand Up @@ -99,7 +96,7 @@ async def _get_code(self) -> None:

params = {"client_id": "polmystar"}
data = {"pf.username": self.username, "pf.pass": self.password}
result = await self._client_session.post(
result = await self.client_session.post(
f"https://polestarid.eu.polestar.com/as/{resumePath}/resume/as/authorization.ping",
params=params,
data=data,
Expand All @@ -113,7 +110,7 @@ async def _get_code(self) -> None:
code = result.next_request.url.params.get("code")

# sign-in-callback
result = await self._client_session.get(
result = await self.client_session.get(
result.next_request.url, timeout=HTTPX_TIMEOUT
)
self.latest_call_code = result.status_code
Expand All @@ -125,7 +122,7 @@ async def _get_code(self) -> None:
)

# url encode the code
result = await self._client_session.get(url)
result = await self.client_session.get(url)
self.latest_call_code = result.status_code

return code
Expand All @@ -137,7 +134,7 @@ async def _get_resume_path(self):
"client_id": "polmystar",
"redirect_uri": "https://www.polestar.com/sign-in-callback",
}
result = await self._client_session.get(
result = await self.client_session.get(
"https://polestarid.eu.polestar.com/as/authorization.oauth2",
params=params,
timeout=HTTPX_TIMEOUT,
Expand Down
6 changes: 3 additions & 3 deletions custom_components/polestar_api/pypolestar/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def __init__(
client_session: httpx.AsyncClient | None = None,
) -> None:
"""Initialize the Polestar API."""
self._client_session = client_session or httpx.AsyncClient()
self.auth = PolestarAuth(username, password, client_session)
self.client_session = client_session or httpx.AsyncClient()
self.auth = PolestarAuth(username, password, self.client_session)
self.updating = False
self.cache_data = {}
self.latest_call_code = None
Expand Down Expand Up @@ -225,7 +225,7 @@ async def get_graph_ql(self, params: dict, url: str = BASE_URL):
"authorization": f"Bearer {self.auth.access_token}",
}

result = await self._client_session.get(url, params=params, headers=headers)
result = await self.client_session.get(url, params=params, headers=headers)
self._set_latest_call_code(url, result.status_code)

if result.status_code == 401:
Expand Down

0 comments on commit 2befab9

Please sign in to comment.