From 9b4a5787db295807b689bd55d1fd34307186d790 Mon Sep 17 00:00:00 2001 From: Austin Allen Date: Fri, 11 Oct 2024 14:48:41 -0500 Subject: [PATCH 1/2] client uses aiohttp to fetch data --- custom_components/poolmath/client.py | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/custom_components/poolmath/client.py b/custom_components/poolmath/client.py index 99343f1..7c7ee8c 100644 --- a/custom_components/poolmath/client.py +++ b/custom_components/poolmath/client.py @@ -1,8 +1,8 @@ +import aiohttp import json import logging import re -import httpx from .const import ( ATTR_TARGET_MAX, @@ -66,19 +66,21 @@ def __init__(self, url, 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.pool_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.pool_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""" From 9735cb2f444b9402d8c458e2ffe8368fdec2835f Mon Sep 17 00:00:00 2001 From: Austin Allen Date: Mon, 14 Oct 2024 08:57:52 -0500 Subject: [PATCH 2/2] fix whitespace --- custom_components/poolmath/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/poolmath/client.py b/custom_components/poolmath/client.py index 2e1901b..8bfba09 100644 --- a/custom_components/poolmath/client.py +++ b/custom_components/poolmath/client.py @@ -65,7 +65,7 @@ async def async_update(self): 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""" """ async_callback(measurement_type, timestamp, state, attributes)"""