Prevent multiple simultaneous WeConnect API calls on start up #245
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thanks for creating and maintaining this integration! 👍
I have observed that it is the last integration to finish loading whenever I (re)start my Home Assistant instance. I have found that, when the integration starts, it makes 6 calls to the WeConnect API
update()
method nearly at once — the sameapi.update()
call that is normally made every 45 seconds. Each such call results in 6 HTTP GET requests to the following VW/Cariad endpoints:https://emea.bff.cariad.digital/vehicle/v1/vehicles
https://emea.bff.cariad.digital/vehicle/v1/vehicles/<VIN>/selectivestatus?jobs=access,activeventilation,automation,auxiliaryheating,userCapabilities,charging,chargingProfiles,batteryChargingCare,climatisation,climatisationTimers,departureTimers,fuelStatus,vehicleLights,lvBattery,readiness,vehicleHealthInspection,vehicleHealthWarnings,oilLevel,measurements,batterySupport,trips
https://emea.bff.cariad.digital/vehicle/v1/vehicles/<VIN>/parkingposition
https://emea.bff.cariad.digital/vehicle/v1/trips/<VIN>/shortterm/last
https://emea.bff.cariad.digital/vehicle/v1/trips/<VIN>/longterm/last
https://emea.bff.cariad.digital/vehicle/v1/trips/<VIN>/cyclic/last
So 6 × 6 = 36 authenticated API endpoint requests within a few seconds from the integration being loaded (in my logs I observed all HTTP requests being started within 3 seconds of the integration loading). In addition, when the config flow executes during installation, an extra
api.update()
call is made for username and password validation, in which case it is 7 × 6 = 42 authenticated API endpoint requests just a few seconds apart.2 of the 6 or 7
update()
calls are explicit, and the others happen as a result of calls tocoordinator.async_config_entry_first_refresh()
:__init__.py#L49
__init__.py#L79
binary_sensor.py#L139
device_tracker.py#L29
number.py#L29
sensor.py#L448
config_flow.py#L41
I have verified this finding by adding debug logging at the relevant WeConnect fetchData
session.get()
call.A single WeConnect API
update()
call would be sufficient at start up. This PR proposes a relatively simple solution to achieve this, even in the config flow scenario when the integration is installed.I tested my solution with HASS version 2024.1.6, including the case of the user entering incorrect username and password in the config flow. I have also exercised the code added to
async_unload_entry()
by deleting and re-adding the integration.