Skip to content

Commit

Permalink
Use HA helper to prevent AsyncClient blocking calls (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwolsink authored Jan 6, 2025
1 parent 8872bc0 commit 9eddd39
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions custom_components/aguaiot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.httpx_client import get_async_client

from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP

Expand Down Expand Up @@ -67,6 +68,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
login_api_url=login_api_url,
brand_id=brand_id,
brand=brand,
async_client=get_async_client(hass),
)

try:
Expand Down
15 changes: 10 additions & 5 deletions custom_components/aguaiot/aguaiot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
brand_id=None,
brand=None,
application_version="1.9.7",
async_client=None,
):
self.api_url = api_url.rstrip("/")
self.customer_code = customer_code
Expand All @@ -54,6 +55,10 @@ def __init__(
self.token_expires = None
self.refresh_token = None
self.devices = list()
self.async_client = async_client

if not self.async_client:
self.async_client = httpx.AsyncClient()

async def connect(self):
await self.register_app_id()
Expand Down Expand Up @@ -95,7 +100,7 @@ async def register_app_id(self):
_LOGGER.debug(
"POST Register app - HEADERS: %s DATA: %s", self._headers(), payload
)
async with httpx.AsyncClient() as client:
async with self.async_client as client:
response = await client.post(
url,
json=payload,
Expand Down Expand Up @@ -144,7 +149,7 @@ async def login(self):

try:
_LOGGER.debug("POST Login - HEADERS: %s DATA: ***", headers)
async with httpx.AsyncClient() as client:
async with self.async_client as client:
response = await client.post(
url,
json=payload,
Expand Down Expand Up @@ -190,7 +195,7 @@ async def do_refresh_token(self):
_LOGGER.debug(
"POST Refresh token - HEADERS: %s DATA: %s", self._headers(), payload
)
async with httpx.AsyncClient() as client:
async with self.async_client as client:
response = await client.post(
url,
json=payload,
Expand Down Expand Up @@ -275,7 +280,7 @@ async def handle_webcall(self, method, url, payload):
try:
_LOGGER.debug("%s %s - HEADERS: %s DATA: %s", method, url, headers, payload)
if method == "POST":
async with httpx.AsyncClient() as client:
async with self.async_client as client:
response = await client.post(
url,
json=payload,
Expand All @@ -284,7 +289,7 @@ async def handle_webcall(self, method, url, payload):
timeout=DEFAULT_TIMEOUT_VALUE,
)
else:
async with httpx.AsyncClient() as client:
async with self.async_client as client:
response = await client.get(
url,
params=payload,
Expand Down
2 changes: 2 additions & 0 deletions custom_components/aguaiot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers.httpx_client import get_async_client

from .const import (
CONF_API_URL,
Expand Down Expand Up @@ -81,6 +82,7 @@ async def async_step_user(self, user_input=None):
login_api_url=login_api_url,
brand_id=brand_id,
brand=brand,
async_client=get_async_client(hass),
)
await agua.connect()
except UnauthorizedError as e:
Expand Down

0 comments on commit 9eddd39

Please sign in to comment.