From bf4b4e60653dfdb60e050e26178dd049507234e3 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Mon, 11 Nov 2024 11:00:44 +0100 Subject: [PATCH] removing data cache --- custom_components/polestar_api/const.py | 1 - custom_components/polestar_api/image.py | 1 - custom_components/polestar_api/polestar.py | 14 ++-- .../polestar_api/pypolestar/const.py | 2 - .../polestar_api/pypolestar/polestar.py | 71 ++++++------------- custom_components/polestar_api/sensor.py | 15 +--- 6 files changed, 30 insertions(+), 74 deletions(-) diff --git a/custom_components/polestar_api/const.py b/custom_components/polestar_api/const.py index 7665edb..449aadb 100644 --- a/custom_components/polestar_api/const.py +++ b/custom_components/polestar_api/const.py @@ -6,7 +6,6 @@ DOMAIN = "polestar_api" TIMEOUT = 90 -CACHE_TIME = 600 DEFAULT_SCAN_INTERVAL = timedelta(seconds=60) CONF_VIN: Final[str] = "vin" diff --git a/custom_components/polestar_api/image.py b/custom_components/polestar_api/image.py index 9d626aa..a3fe531 100644 --- a/custom_components/polestar_api/image.py +++ b/custom_components/polestar_api/image.py @@ -86,7 +86,6 @@ async def async_update_image_url(self) -> None: value = self.car.get_value( query=self.entity_description.query, field_name=self.entity_description.field_name, - skip_cache=True, ) if value is None: _LOGGER.debug("No image URL found") diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index 111796c..c93a3f5 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -53,12 +53,6 @@ def get_device_info(self) -> DeviceInfo: serial_number=self.vin, ) - def get_latest_data(self, query: str, field_name: str): - """Get the latest data from the Polestar API.""" - return self.polestar_api.get_latest_data( - vin=self.vin, query=query, field_name=field_name - ) - async def async_update(self) -> None: """Update data from Polestar.""" try: @@ -85,10 +79,12 @@ async def async_update(self) -> None: self.polestar_api.next_update = datetime.now() + timedelta(seconds=60) self.polestar_api.latest_call_code_v2 = 500 - def get_value(self, query: str, field_name: str, skip_cache: bool = False): + def get_value(self, query: str, field_name: str): """Get the latest value from the Polestar API.""" - data = self.polestar_api.get_cache_data( - vin=self.vin, query=query, field_name=field_name, skip_cache=skip_cache + if query is None or field_name is None: + return None + data = self.polestar_api.get_latest_data( + vin=self.vin, query=query, field_name=field_name ) if data is None: # if amp and voltage can be null, so we will return 0 diff --git a/custom_components/polestar_api/pypolestar/const.py b/custom_components/polestar_api/pypolestar/const.py index c94ce41..5d38b6f 100644 --- a/custom_components/polestar_api/pypolestar/const.py +++ b/custom_components/polestar_api/pypolestar/const.py @@ -1,7 +1,5 @@ """Constants for Polestar.""" -CACHE_TIME = 30 - CAR_INFO_DATA = "getConsumerCarsV2" ODO_METER_DATA = "getOdometerData" BATTERY_DATA = "getBatteryData" diff --git a/custom_components/polestar_api/pypolestar/polestar.py b/custom_components/polestar_api/pypolestar/polestar.py index e6c3d3a..e1df949 100644 --- a/custom_components/polestar_api/pypolestar/polestar.py +++ b/custom_components/polestar_api/pypolestar/polestar.py @@ -11,14 +11,7 @@ from graphql import DocumentNode from .auth import PolestarAuth -from .const import ( - BASE_URL, - BASE_URL_V2, - BATTERY_DATA, - CACHE_TIME, - CAR_INFO_DATA, - ODO_METER_DATA, -) +from .const import BASE_URL, BASE_URL_V2, BATTERY_DATA, CAR_INFO_DATA, ODO_METER_DATA from .exception import ( PolestarApiException, PolestarAuthException, @@ -55,9 +48,7 @@ def __init__( self.latest_call_code = None self.latest_call_code_2 = None self.next_update = None - self.car_data_by_vin: dict[str, dict] = {} - self.cache_data_by_vin: dict[str, dict] = defaultdict(dict) - self.cache_ttl = timedelta(seconds=CACHE_TIME) + self.data_by_vin: dict[str, dict] = defaultdict(dict) self.next_update_delay = timedelta(seconds=5) self.configured_vins = set(vins) if vins else None self.logger = _LOGGER.getChild(unique_id) if unique_id else _LOGGER @@ -83,26 +74,33 @@ async def async_init(self, verbose: bool = False) -> None: vin = data["vin"] if self.configured_vins and vin not in self.configured_vins: continue - self.car_data_by_vin[vin] = data - self.cache_data_by_vin[vin][CAR_INFO_DATA] = { - "data": self.car_data_by_vin[vin], + self.data_by_vin[vin][CAR_INFO_DATA] = { + "data": data, "timestamp": datetime.now(), } self.logger.debug("API setup for VIN %s", vin) @property def vins(self) -> list[str]: - return list(self.car_data_by_vin.keys()) + return list(self.data_by_vin.keys()) - def get_latest_data( - self, vin: str, query: str, field_name: str - ) -> dict or bool or None: + def get_latest_data(self, vin: str, query: str, field_name: str) -> dict | None: """Get the latest data from the Polestar API.""" - if self.cache_data_by_vin and self.cache_data_by_vin[vin][query]: - data = self.cache_data_by_vin[vin][query]["data"] - if data is None: - return False + self.logger.debug( + "get_latest_data %s %s %s", + vin, + query, + field_name, + ) + query_result = self.data_by_vin[vin].get(query) + if query_result and (data := query_result.get("data")) is not None: return self._get_field_name_value(field_name, data) + self.logger.debug( + "get_latest_data returning None for %s %s %s", + vin, + query, + field_name, + ) return None async def get_ev_data(self, vin: str) -> None: @@ -152,31 +150,8 @@ async def call_api(func): t2 = time.perf_counter() self.logger.debug("Update took %.2f seconds", t2 - t1) - def get_cache_data( - self, vin: str, query: str, field_name: str, skip_cache: bool = False - ) -> dict | None: - """Get the latest data from the cache.""" - if query is None: - return None - self.logger.debug( - "get_cache_data %s %s %s%s", - vin, - query, - field_name, - " (skip_cache)" if skip_cache else "", - ) - if self.cache_data_by_vin and self.cache_data_by_vin[vin].get(query): - cache_entry = self.cache_data_by_vin[vin][query] - data = cache_entry["data"] - if data is not None and ( - skip_cache is True - or cache_entry["timestamp"] + self.cache_ttl > datetime.now() - ): - return self._get_field_name_value(field_name, data) - return None - @staticmethod - def _get_field_name_value(field_name: str, data: dict) -> str or bool or None: + def _get_field_name_value(field_name: str, data: dict) -> str | bool | None: if field_name is None or data is None: return None @@ -203,7 +178,7 @@ async def _get_odometer_data(self, vin: str) -> None: variable_values={"vin": vin}, ) - res = self.cache_data_by_vin[vin][ODO_METER_DATA] = { + res = self.data_by_vin[vin][ODO_METER_DATA] = { "data": result[ODO_METER_DATA], "timestamp": datetime.now(), } @@ -217,7 +192,7 @@ async def _get_battery_data(self, vin: str) -> None: variable_values={"vin": vin}, ) - res = self.cache_data_by_vin[vin][BATTERY_DATA] = { + res = self.data_by_vin[vin][BATTERY_DATA] = { "data": result[BATTERY_DATA], "timestamp": datetime.now(), } diff --git a/custom_components/polestar_api/sensor.py b/custom_components/polestar_api/sensor.py index f1c8a39..50c27d5 100644 --- a/custom_components/polestar_api/sensor.py +++ b/custom_components/polestar_api/sensor.py @@ -519,7 +519,6 @@ def __init__( self._attr_native_value = self.car.get_value( self.entity_description.query, self.entity_description.field_name, - self.get_skip_cache(), ) if entity_description.round_digits is not None: @@ -532,21 +531,12 @@ def __init__( if self.car is not None and self.car.get_latest_call_code() == 200: self._async_update_attrs() - def get_skip_cache(self) -> bool: - """Get the skip cache.""" - return self.entity_description.key in ( - "vin", - "registration_number", - "model_name", - ) - @callback def _async_update_attrs(self) -> None: """Update the state and attributes.""" self._sensor_data = self.car.get_value( self.entity_description.query, self.entity_description.field_name, - self.get_skip_cache(), ) @property @@ -592,10 +582,10 @@ def state(self) -> StateType: return None if self.entity_description.key in ("estimate_full_charge_range"): - battery_level = self.car.get_latest_data( + battery_level = self.car.get_value( self.entity_description.query, "batteryChargeLevelPercentage" ) - estimate_range = self.car.get_latest_data( + estimate_range = self.car.get_value( self.entity_description.query, self.entity_description.field_name ) @@ -730,7 +720,6 @@ async def async_update(self) -> None: value = self.car.get_value( self.entity_description.query, self.entity_description.field_name, - self.get_skip_cache(), ) if value is not None: