Skip to content

Commit edcb555

Browse files
committed
chore: thermostat_service cache
1 parent 0af7f8c commit edcb555

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

custom_components/optispark/backend/thermostat/thermostat_service.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import timedelta, datetime
12
from http import HTTPStatus
23
from typing import List
34

@@ -11,7 +12,7 @@
1112
from custom_components.optispark.backend.thermostat.model.thermostat_control_response import ThermostatControlResponse
1213
from custom_components.optispark.backend.thermostat.model.thermostat_prediction import ThermostatPrediction
1314

14-
15+
CACHE_REFRESH_INTERVAL = timedelta(seconds=300)
1516
class ThermostatService:
1617

1718
def __init__(
@@ -23,8 +24,19 @@ def __init__(
2324
self._config_service: ConfigurationService = config_service
2425
self._base_url = config_service.get("backend.baseUrl")
2526
self._ssl = config_service.get("backend.verifySSL")
27+
self._cache = {}
2628

2729
async def get_control(self, thermostat_id: int, access_token: str) -> ThermostatControlResponse:
30+
cache_key = f"{thermostat_id}"
31+
current_time = datetime.now()
32+
33+
# Check if the response is in cache and still valid
34+
if cache_key in self._cache:
35+
cached_response, timestamp = self._cache[cache_key]
36+
if current_time - timestamp < CACHE_REFRESH_INTERVAL:
37+
LOGGER.debug("Returning control from cache")
38+
return cached_response
39+
2840
endpoint = config_service.get("backend.thermostat.control")
2941
thermostat_url = f'{self._base_url}/{endpoint}'.replace("{thermostat_id}", str(thermostat_id))
3042
headers = {
@@ -48,7 +60,12 @@ async def get_control(self, thermostat_id: int, access_token: str) -> Thermostat
4860
) from Exception
4961

5062
json_response = await response.json()
51-
return ThermostatControlResponse.from_json(json_response)
63+
thermostat_control = ThermostatControlResponse.from_json(json_response)
64+
65+
if thermostat_control is not None:
66+
self._cache[cache_key] = (thermostat_control, current_time)
67+
68+
return thermostat_control
5269

5370
except aiohttp.ClientError as e:
5471
LOGGER.error(f"HTTP error occurred: {e}")
@@ -63,7 +80,9 @@ async def create_manual(
6380
request: ThermostatControlRequest,
6481
access_token: str
6582
) -> ThermostatControlResponse:
66-
ssl = config_service.get('backend.verifySSL', default=True)
83+
cache_key = f"{thermostat_id}"
84+
current_time = datetime.now()
85+
# ssl = config_service.get('backend.verifySSL', default=True)
6786
endpoint = config_service.get("backend.thermostat.manual")
6887
thermostat_url = f'{self._base_url}/{endpoint}'.replace("{thermostat_id}", str(thermostat_id))
6988
headers = {
@@ -89,7 +108,10 @@ async def create_manual(
89108
) from Exception
90109

91110
json_response = await response.json()
92-
return ThermostatControlResponse.from_json(json_response)
111+
thermostat_control = ThermostatControlResponse.from_json(json_response)
112+
if thermostat_control is not None:
113+
self._cache[cache_key] = (thermostat_control, current_time)
114+
return thermostat_control
93115

94116
except aiohttp.ClientError as e:
95117
LOGGER.error(f"HTTP error occurred: {e}")

0 commit comments

Comments
 (0)