Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove blocking call in async_update #32

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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