1+ from datetime import timedelta , datetime
12from http import HTTPStatus
23from typing import List
34
1112from custom_components .optispark .backend .thermostat .model .thermostat_control_response import ThermostatControlResponse
1213from custom_components .optispark .backend .thermostat .model .thermostat_prediction import ThermostatPrediction
1314
14-
15+ CACHE_REFRESH_INTERVAL = timedelta ( seconds = 300 )
1516class 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