diff --git a/tests/test_ha_api.py b/tests/test_ha_api.py index 7a88c77..395830b 100644 --- a/tests/test_ha_api.py +++ b/tests/test_ha_api.py @@ -4,7 +4,7 @@ from weheat.abstractions.heat_pump import HeatPump from weheat.abstractions.discovery import HeatPumpDiscovery -from weheat.abstractions.user import get_user_id_from_token +from weheat.abstractions.user import async_get_user_id_from_token from weheat.exceptions import UnauthorizedException, ForbiddenException @@ -16,7 +16,7 @@ @pytest.mark.asyncio async def test_discovery(api_fixture): - discovery = await HeatPumpDiscovery.discover_active(api_url=api_fixture.api_url, access_token=api_fixture.access_token) + discovery = await HeatPumpDiscovery.async_discover_active(api_url=api_fixture.api_url, access_token=api_fixture.access_token) #The account is configured to find at least one Blackbird, one sparrow and one flint assert len(discovery) >= 3 @@ -47,7 +47,7 @@ async def test_discovery(api_fixture): @pytest.mark.asyncio async def test_user(api_fixture): - user = await get_user_id_from_token(api_fixture.api_url, api_fixture.access_token) + user = await async_get_user_id_from_token(api_fixture.api_url, api_fixture.access_token) #check we got a UUID assert len(user) == 36 @@ -59,7 +59,7 @@ async def test_hp_log(api_fixture, uuid): # if the API is changed, this call will fail on a python validation error try: - heatpump.get_status(api_fixture.access_token) + await heatpump.async_get_status(api_fixture.access_token) except ValidationError as e: print(f'Validation unsuccessful: {e}') pytest.fail('Validation failed') diff --git a/weheat/abstractions/discovery.py b/weheat/abstractions/discovery.py index 1e7010f..4e6e858 100644 --- a/weheat/abstractions/discovery.py +++ b/weheat/abstractions/discovery.py @@ -1,3 +1,4 @@ +import asyncio from dataclasses import dataclass from weheat import DeviceState @@ -16,14 +17,17 @@ class HeatPumpInfo: has_dhw: bool = False @staticmethod - async def discover_active(api_url: str, access_token: str) -> list[HeatPumpInfo]: + async def async_discover_active(api_url: str, access_token: str) -> list[HeatPumpInfo]: discovered_pumps = [] config = Configuration(host=api_url, access_token=access_token) with ApiClient(configuration=config) as client: - response = HeatPumpApi(client).api_v1_heat_pumps_get_with_http_info('', 1, 1000, DeviceState.NUMBER_3 ,async_req=True).get() + response = HeatPumpApi(client).api_v1_heat_pumps_get_with_http_info('', 1, 1000, DeviceState.NUMBER_3 ,async_req=True) + + response = await asyncio.to_thread(response.get) + if response.status_code == 200: for pump in response.data: # Model of the heat pump diff --git a/weheat/abstractions/heat_pump.py b/weheat/abstractions/heat_pump.py index 4ae2a31..76bcea4 100644 --- a/weheat/abstractions/heat_pump.py +++ b/weheat/abstractions/heat_pump.py @@ -1,4 +1,5 @@ """Weheat heat pump abstraction from the API.""" +import asyncio from enum import Enum, auto from weheat import HeatPumpApi @@ -33,7 +34,7 @@ def __init__(self, api_url: str, uuid: str) -> None: self._energy_output = None self._nominal_max_power = None - def get_status(self, access_token: str): + async def async_get_status(self, access_token: str): """Updates the heat pump instance with data from the API.""" try: config = Configuration(host=self._api_url, access_token=access_token) @@ -41,17 +42,22 @@ def get_status(self, access_token: str): with ApiClient(configuration=config) as client: # Set the max power once if self._nominal_max_power is None: - repsonse = HeatPumpApi(client).api_v1_heat_pumps_heat_pump_id_get_with_http_info(heat_pump_id=self._uuid) + response = HeatPumpApi(client).api_v1_heat_pumps_heat_pump_id_get_with_http_info(heat_pump_id=self._uuid, async_req=True) - if repsonse.status_code == 200: - self._set_nominal_max_power_for_model(repsonse.data.model) + response = await asyncio.to_thread(response.get) + + if response.status_code == 200: + self._set_nominal_max_power_for_model(response.data.model) response = HeatPumpLogApi( client ).api_v1_heat_pumps_heat_pump_id_logs_latest_get_with_http_info( - heat_pump_id=self._uuid + heat_pump_id=self._uuid, async_req=True ) + + response = await asyncio.to_thread(response.get) + if response.status_code == 200: self._last_log = response.data @@ -60,7 +66,9 @@ def get_status(self, access_token: str): response = EnergyLogApi(client).api_v1_energy_logs_heat_pump_id_get_with_http_info(heat_pump_id=self._uuid, start_time=START_DATE, end_time=datetime.now() + timedelta(days=1), - interval='Month') + interval='Month', async_req=True) + + response = await asyncio.to_thread(response.get) if response.status_code == 200: # aggregate the energy consumption diff --git a/weheat/abstractions/user.py b/weheat/abstractions/user.py index 2b7753d..6336686 100644 --- a/weheat/abstractions/user.py +++ b/weheat/abstractions/user.py @@ -1,9 +1,11 @@ +import asyncio + from weheat.configuration import Configuration from weheat.api_client import ApiClient from weheat.api.user_api import UserApi -async def get_user_id_from_token(api_url: str, access_token: str): +async def async_get_user_id_from_token(api_url: str, access_token: str): """ Get the user id from the current logged-in user. """ try: config = Configuration(host=api_url, access_token=access_token) @@ -11,7 +13,10 @@ async def get_user_id_from_token(api_url: str, access_token: str): with ApiClient(configuration=config) as client: response = UserApi( client - ).api_v1_users_me_get_with_http_info(async_req=True).get() + ).api_v1_users_me_get_with_http_info(async_req=True) + + response = await asyncio.to_thread(response.get) + if response.status_code == 200: return response.data.id except Exception as e: