Skip to content

Commit

Permalink
Merge pull request #20 from wefabricate/async_support
Browse files Browse the repository at this point in the history
Async support
  • Loading branch information
jesperraemaekers authored Jan 14, 2025
2 parents db34606 + e5f86a2 commit cf4018e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
8 changes: 4 additions & 4 deletions tests/test_ha_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down
8 changes: 6 additions & 2 deletions weheat/abstractions/discovery.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from dataclasses import dataclass

from weheat import DeviceState
Expand All @@ -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
Expand Down
20 changes: 14 additions & 6 deletions weheat/abstractions/heat_pump.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Weheat heat pump abstraction from the API."""
import asyncio
from enum import Enum, auto

from weheat import HeatPumpApi
Expand Down Expand Up @@ -33,25 +34,30 @@ 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)

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

Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions weheat/abstractions/user.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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)

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:
Expand Down

0 comments on commit cf4018e

Please sign in to comment.