From 2befab9a56624c7d764b238d7c5b25b1a4c0d1d9 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Fri, 18 Oct 2024 07:03:42 +0200 Subject: [PATCH] use homeassistant.helpers.httpx_client.get_async_client() --- custom_components/polestar_api/polestar.py | 5 +++-- .../polestar_api/pypolestar/auth.py | 17 +++++++---------- .../polestar_api/pypolestar/polestar.py | 6 +++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index 708f2c4..923b3a7 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -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 @@ -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() diff --git a/custom_components/polestar_api/pypolestar/auth.py b/custom_components/polestar_api/pypolestar/auth.py index d6e94ba..4db5800 100644 --- a/custom_components/polestar_api/pypolestar/auth.py +++ b/custom_components/polestar_api/pypolestar/auth.py @@ -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.""" @@ -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, @@ -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, @@ -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 @@ -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 @@ -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, diff --git a/custom_components/polestar_api/pypolestar/polestar.py b/custom_components/polestar_api/pypolestar/polestar.py index 06c45ba..950a066 100644 --- a/custom_components/polestar_api/pypolestar/polestar.py +++ b/custom_components/polestar_api/pypolestar/polestar.py @@ -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 @@ -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: