Skip to content

Commit

Permalink
Fix rate limit on health endpoint
Browse files Browse the repository at this point in the history
Only fetch the health endpoint (warning-lights) every 24h.

Fixes #468

Depends on updated myskoda package.
  • Loading branch information
dvx76 committed Feb 24, 2025
1 parent cdd226a commit 7a04fec
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions custom_components/myskoda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .const import (
API_COOLDOWN_IN_SECONDS,
CACHE_USER_ENDPOINT_IN_HOURS,
CACHE_VEHICLE_HEALTH_IN_HOURS,
CONF_POLL_INTERVAL,
COORDINATORS,
DEFAULT_FETCH_INTERVAL_IN_MINUTES,
Expand Down Expand Up @@ -133,30 +134,33 @@ async def _async_get_minimal_data(self) -> Vehicle:
return await self.myskoda.get_partial_vehicle(self.vin, [])

async def _async_get_vehicle_data(self) -> Vehicle:
"""Internal method that fetches vehicle data."""
if self.data:
if (
self.data.vehicle.info.device_platform == "MBB"
and self.data.vehicle.info.specification.model == "CitigoE iV"
):
"""Internal method that fetches vehicle data.
Get health only when missing and every 24h.
This avoids triggering battery protection, such as in Citigoe and Karoq.
https://github.com/skodaconnect/homeassistant-myskoda/issues/468
"""
excluded_capabilities = []
if (
self.data.vehicle
and self.data.vehicle.health
and self.data.vehicle.health.timestamp
):
cache_expiry_time = self.data.vehicle.health.timestamp + timedelta(
hours=CACHE_VEHICLE_HEALTH_IN_HOURS
)

if datetime.now(UTC) > cache_expiry_time:
_LOGGER.debug(
"Detected Citigo iV, requesting only partial update without health"
)
vehicle = await self.myskoda.get_partial_vehicle(
self.vin,
[
CapabilityId.AIR_CONDITIONING,
CapabilityId.CHARGING,
CapabilityId.PARKING_POSITION,
CapabilityId.STATE,
CapabilityId.TRIP_STATISTICS,
],
"Updating health - cache expired at %s",
self.data.vehicle.health.timestamp,
)
else:
vehicle = await self.myskoda.get_vehicle(self.vin)
else:
vehicle = await self.myskoda.get_vehicle(self.vin)
return vehicle
excluded_capabilities.append(CapabilityId.VEHICLE_HEALTH_INSPECTION)

return await self.myskoda.get_vehicle(
self.vin, excluded_capabilities=excluded_capabilities
)

async def _async_update_data(self) -> State:
vehicle = None
Expand Down Expand Up @@ -527,7 +531,7 @@ async def _update_vehicle(self) -> None:

_LOGGER.debug("Updating full vehicle for %s", self.vin)
try:
vehicle = await self.myskoda.get_vehicle(self.vin)
vehicle = await self._async_get_vehicle_data()
except ClientResponseError as err:
handle_aiohttp_error("vehicle update", err, self.hass, self.entry)
except ClientError as err:
Expand Down

0 comments on commit 7a04fec

Please sign in to comment.