From 78e8296b06267ba3e1b2fa8bf367d16740057c65 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Mon, 16 Dec 2024 07:16:03 +0100 Subject: [PATCH 1/2] Use explicit list of configured/found VINs --- custom_components/polestar_api/config_flow.py | 2 +- custom_components/polestar_api/polestar.py | 2 +- .../polestar_api/pypolestar/polestar.py | 23 +++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/custom_components/polestar_api/config_flow.py b/custom_components/polestar_api/config_flow.py index 298ce4e..895e7bd 100644 --- a/custom_components/polestar_api/config_flow.py +++ b/custom_components/polestar_api/config_flow.py @@ -87,7 +87,7 @@ async def _test_credentials( ) await api_client.async_init() - if found_vins := api_client.vins: + if found_vins := api_client.get_available_vins(): _LOGGER.debug("Found %d VINs for %s", len(found_vins), username) else: _LOGGER.warning("No VINs found for %s", username) diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index 1e0bf19..bf6cb86 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -240,5 +240,5 @@ async def async_init(self): def get_cars(self) -> list[PolestarCar]: return [ PolestarCar(api=self.polestar_api, vin=vin, unique_id=self.unique_id) - for vin in self.polestar_api.vins + for vin in self.polestar_api.get_available_vins() ] diff --git a/custom_components/polestar_api/pypolestar/polestar.py b/custom_components/polestar_api/pypolestar/polestar.py index 17cd375..8861b95 100644 --- a/custom_components/polestar_api/pypolestar/polestar.py +++ b/custom_components/polestar_api/pypolestar/polestar.py @@ -53,6 +53,7 @@ def __init__( 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.vins: set[str] = set() self.logger = _LOGGER.getChild(unique_id) if unique_id else _LOGGER self.api_url = API_MYSTAR_V2_URL self.gql_client = get_gql_client(url=self.api_url, client=self.client_session) @@ -81,11 +82,15 @@ async def async_init(self, verbose: bool = False) -> None: "data": data, "timestamp": datetime.now(), } + self.vins.add(vin) self.logger.debug("API setup for VIN %s", vin) - @property - def vins(self) -> list[str]: - return list(self.data_by_vin.keys()) + if self.configured_vins and (missing_vins := self.configured_vins - self.vins): + self.logger.warning("Could not found configured VINs %s", missing_vins) + + def get_available_vins(self) -> list[str]: + """Get list of all available VINs""" + return list(self.vins) def get_car_information(self, vin: str) -> CarInformationData | None: """ @@ -99,8 +104,8 @@ def get_car_information(self, vin: str) -> CarInformationData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.data_by_vin: - raise KeyError(f"No data found for VIN: {vin}") + if vin not in self.vins: + raise KeyError(vin) if data := self.data_by_vin[vin].get(CAR_INFO_DATA, {}).get("data"): try: return CarInformationData.from_dict(data) @@ -119,8 +124,8 @@ def get_car_battery(self, vin: str) -> CarBatteryData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.data_by_vin: - raise KeyError(f"No data found for VIN: {vin}") + if vin not in self.vins: + raise KeyError(vin) if data := self.data_by_vin[vin].get(BATTERY_DATA, {}).get("data"): try: return CarBatteryData.from_dict(data) @@ -139,8 +144,8 @@ def get_car_odometer(self, vin: str) -> CarOdometerData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.data_by_vin: - raise KeyError(f"No data found for VIN: {vin}") + if vin not in self.vins: + raise KeyError(vin) if data := self.data_by_vin[vin].get(ODO_METER_DATA, {}).get("data"): try: return CarOdometerData.from_dict(data) From 27a53730944e02d22e86f48eb715fe2c68b03e2f Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Mon, 16 Dec 2024 07:19:39 +0100 Subject: [PATCH 2/2] rename --- .../polestar_api/pypolestar/polestar.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/custom_components/polestar_api/pypolestar/polestar.py b/custom_components/polestar_api/pypolestar/polestar.py index 8861b95..c13b2c0 100644 --- a/custom_components/polestar_api/pypolestar/polestar.py +++ b/custom_components/polestar_api/pypolestar/polestar.py @@ -53,7 +53,7 @@ def __init__( 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.vins: set[str] = set() + self.available_vins: set[str] = set() self.logger = _LOGGER.getChild(unique_id) if unique_id else _LOGGER self.api_url = API_MYSTAR_V2_URL self.gql_client = get_gql_client(url=self.api_url, client=self.client_session) @@ -82,15 +82,17 @@ async def async_init(self, verbose: bool = False) -> None: "data": data, "timestamp": datetime.now(), } - self.vins.add(vin) + self.available_vins.add(vin) self.logger.debug("API setup for VIN %s", vin) - if self.configured_vins and (missing_vins := self.configured_vins - self.vins): + if self.configured_vins and ( + missing_vins := self.configured_vins - self.available_vins + ): self.logger.warning("Could not found configured VINs %s", missing_vins) def get_available_vins(self) -> list[str]: """Get list of all available VINs""" - return list(self.vins) + return list(self.available_vins) def get_car_information(self, vin: str) -> CarInformationData | None: """ @@ -104,7 +106,7 @@ def get_car_information(self, vin: str) -> CarInformationData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.vins: + if vin not in self.available_vins: raise KeyError(vin) if data := self.data_by_vin[vin].get(CAR_INFO_DATA, {}).get("data"): try: @@ -124,7 +126,7 @@ def get_car_battery(self, vin: str) -> CarBatteryData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.vins: + if vin not in self.available_vins: raise KeyError(vin) if data := self.data_by_vin[vin].get(BATTERY_DATA, {}).get("data"): try: @@ -144,7 +146,7 @@ def get_car_odometer(self, vin: str) -> CarOdometerData | None: KeyError: If the VIN doesn't exist ValueError: If data conversion fails """ - if vin not in self.vins: + if vin not in self.available_vins: raise KeyError(vin) if data := self.data_by_vin[vin].get(ODO_METER_DATA, {}).get("data"): try: