Skip to content

Commit

Permalink
Merge pull request #32 from aallen90/master
Browse files Browse the repository at this point in the history
Remove blocking call in async_update
  • Loading branch information
rsnodgrass authored Oct 14, 2024
2 parents 640eaf5 + 9735cb2 commit 5295f8f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions custom_components/poolmath/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import aiohttp
import json
import logging
import re

import httpx

from .const import (
ATTR_TARGET_MAX,
Expand Down Expand Up @@ -50,19 +50,21 @@ def __init__(self, share_id: str, name=DEFAULT_NAME, timeout=DEFAULT_TIMEOUT):
async def async_update(self):
"""Fetch latest json formatted data from the Pool Math API"""

async with httpx.AsyncClient() as client:
LOG.info(
f"GET {self._json_url} (timeout={self._timeout}; name={self._name}; id={self._share_id})"
)
response = await client.request(
"GET", self._json_url, timeout=self._timeout, follow_redirects=True
)
LOG.debug(f"GET {self._json_url} response: {response.status_code}")

if response.status_code == httpx.codes.OK:
return json.loads(response.text)

return None
async with aiohttp.ClientSession() as session:
try:
LOG.info(
f"GET {self._json_url} (timeout={self._timeout}; name={self._name}; id={self._share_id})"
)
async with session.get(self._json_url, timeout=self._timeout) as response:
LOG.debug(f"GET {self._json_url} response: {response.status}")
if response.status == 200:
return await response.json()
else:
LOG.error(f"Error: Received status code {response.status} from API")
return None
except aiohttp.ClientError as e:
LOG.error(f"Error fetching data from PoolMath API: {e}")
return None

async def process_log_entry_callbacks(self, poolmath_json, async_callback):
"""Call provided async callback once for each type of log entry"""
Expand Down

0 comments on commit 5295f8f

Please sign in to comment.