Skip to content

Commit

Permalink
Merge pull request #274 from jschlyter/check_vins
Browse files Browse the repository at this point in the history
Use explicit list of configured/available VINs
  • Loading branch information
jschlyter authored Dec 17, 2024
2 parents a86f906 + 27a5373 commit 0f9253d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion custom_components/polestar_api/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]
25 changes: 16 additions & 9 deletions custom_components/polestar_api/pypolestar/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.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)
Expand Down Expand Up @@ -81,11 +82,17 @@ async def async_init(self, verbose: bool = False) -> None:
"data": data,
"timestamp": datetime.now(),
}
self.available_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.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.available_vins)

def get_car_information(self, vin: str) -> CarInformationData | None:
"""
Expand All @@ -99,8 +106,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.available_vins:
raise KeyError(vin)
if data := self.data_by_vin[vin].get(CAR_INFO_DATA, {}).get("data"):
try:
return CarInformationData.from_dict(data)
Expand All @@ -119,8 +126,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.available_vins:
raise KeyError(vin)
if data := self.data_by_vin[vin].get(BATTERY_DATA, {}).get("data"):
try:
return CarBatteryData.from_dict(data)
Expand All @@ -139,8 +146,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.available_vins:
raise KeyError(vin)
if data := self.data_by_vin[vin].get(ODO_METER_DATA, {}).get("data"):
try:
return CarOdometerData.from_dict(data)
Expand Down

0 comments on commit 0f9253d

Please sign in to comment.