Skip to content

Commit

Permalink
Resolve 'Detected blocking call' warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Sep 10, 2024
1 parent df069e0 commit 84e0b98
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![version](https://img.shields.io/github/v/release/ankohanse/hass-dab-pumps?style=for-the-badge)](https://github.com/ankohanse/hass-dab-pumps)
[![maintained](https://img.shields.io/maintenance/yes/2024?style=for-the-badge)](https://github.com/ankohanse/hass-dab-pumps)
[![license](https://img.shields.io/github/license/toreamun/amshan-homeassistant?style=for-the-badge)](LICENSE)
[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)<br/>
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)<br/>
[![buy_me_a_coffee](https://img.shields.io/badge/If%20you%20like%20it-Buy%20me%20a%20coffee-yellow.svg?style=for-the-badge)](https://www.buymeacoffee.com/ankohanse)


Expand Down
41 changes: 20 additions & 21 deletions custom_components/dabpumps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from homeassistant.components.sensor import SensorStateClass
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import IntegrationError
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.storage import Store

from httpx import RequestError, TimeoutException
Expand Down Expand Up @@ -51,22 +52,15 @@ def create(hass: HomeAssistant, username, password):

key = f"{username.lower()}_{hash(password) % 10**8}"

# if a DabPumpsApi instance for these credentials is already available then e-use it
if hass:
if not API in hass.data[DOMAIN]:
hass.data[DOMAIN][API] = {}

api = hass.data[DOMAIN][API].get(key, None)
else:
api = None
if not API in hass.data[DOMAIN]:
hass.data[DOMAIN][API] = {}

# if a DabPumpsApi instance for these credentials is already available then e-use it
api = hass.data[DOMAIN][API].get(key, None)
if not api:
# Create a new DabPumpsApi instance
api = DabPumpsApi(hass, username, password)

# cache this new DabPumpsApi instance
if hass:
hass.data[DOMAIN][API][key] = api
api = DabPumpsApi(hass, username, password, use_history_store=True)
hass.data[DOMAIN][API][key] = api

return api

Expand All @@ -78,31 +72,30 @@ def create_temp(hass: HomeAssistant, username, password):
"""

# Create a new DabPumpsApi instance
api = DabPumpsApi(hass, username, password)
api = DabPumpsApi(hass, username, password, use_history_store=False)

return api


# DabPumpsAPI to detect device and get device info, fetch the actual data from the Resol device, and parse it
class DabPumpsApi:

def __init__(self, hass, username, password):
def __init__(self, hass, username, password, use_history_store=True):
self._hass = hass
self._username = username
self._password = password
self._client = None
self._login_method = None

if hass:
if use_history_store:
# maintain calls history for diagnostics during normal operations
self._hass = hass
self._history_key = username.lower()
self._history_store = DabPumpsApiHistoryStore(hass, self._history_key)

# Cleanup the history store after each restart.
asyncio.run_coroutine_threadsafe(self._async_cleanup_diagnostics(), hass.loop)
else:
# Use from a temporary coordinator during config-flow first time setup of component
self._hass = None
self._history_key = None
self._history_store = None

Expand Down Expand Up @@ -163,7 +156,9 @@ async def async_login(self):
async def async_login_dablive_app(self, isDabLive=1):
# Step 1: get authorization token
# Use a fresh client to keep track of cookies during login and subsequent calls
client = httpx.AsyncClient(follow_redirects=True, timeout=120.0)
client = get_async_client(self._hass)
client.follow_redirects = True
client.timeout = 120.0

context = f"login DabLive_app (isDabLive={isDabLive})"
verb = "POST"
Expand Down Expand Up @@ -197,7 +192,9 @@ async def async_login_dablive_app(self, isDabLive=1):
async def async_login_dconnect_app(self):
# Step 1: get authorization token
# Use a fresh client to keep track of cookies during login and subsequent calls
client = httpx.AsyncClient(follow_redirects=True, timeout=120.0)
client = get_async_client(self._hass)
client.follow_redirects = True
client.timeout = 120.0

context = f"login DConnect_app"
verb = "POST"
Expand Down Expand Up @@ -244,7 +241,9 @@ async def async_login_dconnect_app(self):
async def async_login_dconnect_web(self):
# Step 1: get login url
# Use a fresh client to keep track of cookies during login and subsequent calls
client = httpx.AsyncClient(follow_redirects=True, timeout=120.0)
client = get_async_client(self._hass)
client.follow_redirects = True
client.timeout = 120.0

_LOGGER.debug(f"DAB Pumps retrieve login page via GET {url}")
context = f"login DConnect_web home"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/dabpumps/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"issue_tracker": "https://github.com/ankohanse/hass-dab-pumps/issues",
"loggers": ["custom_components.dabpumps"],
"requirements": [],
"version": "2024.08.2"
"version": "2024.09.1"
}

0 comments on commit 84e0b98

Please sign in to comment.