feat: data persistence and rate limit handling - #82
Merged
Conversation
## Summary Adds Store-backed caching and HTTP 429 handling so the integration survives rapid HA restarts without hitting the Parcel API's 20 req/hr rate limit. - Persists API responses to `.storage/parcelapp_cache_*` via HA's `Store` helper — restarts reuse cached data when fresh enough - `_async_setup()` lifecycle hook loads cache before first poll, skipping the API call entirely if data is younger than update_interval - Explicit 429 detection on both deliveries and carrier codes endpoints — parses `Retry-After` header, stretches polling interval, serves stale data instead of failing - Generic error fallback returns cached data when available instead of raising `UpdateFailed` - `RestoreEntity` mixin on all 4 sensors for immediate state on boot - Sensors now use `entry.runtime_data` instead of `hass.data[DOMAIN]` ## Changed files - `coordinator.py` — Store, `_async_setup()`, 429 handling, cache fallback - `sensor.py` — RestoreEntity + `async_added_to_hass()` on all sensors - `const.py` — `STORAGE_KEY`, `STORAGE_VERSION`, `DEFAULT_RETRY_AFTER_SECONDS` - `__init__.py` — runtime_data assignment moved before platform setup ## New files - `tests/test_persistence.py` — 9 tests covering cache load/save, 429 with/without cache, retry-after parsing, interval reset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds Store-backed caching and HTTP 429 handling so the integration survives rapid HA restarts without hitting the Parcel API's 20 req/hr rate limit.
.storage/parcelapp_cache_*via HA'sStorehelper — restarts reuse cached data when fresh enough_async_setup()lifecycle hook loads cache before first poll, skipping the API call entirely if data is younger than update_intervalRetry-Afterheader, stretches polling interval, serves stale data instead of failingUpdateFailedRestoreEntitymixin on all 4 sensors for immediate state on bootentry.runtime_datainstead ofhass.data[DOMAIN]Changed files
coordinator.py— Store,_async_setup(), 429 handling, cache fallbacksensor.py— RestoreEntity +async_added_to_hass()on all sensorsconst.py—STORAGE_KEY,STORAGE_VERSION,DEFAULT_RETRY_AFTER_SECONDS__init__.py— runtime_data assignment moved before platform setupNew files
tests/test_persistence.py— 9 tests covering cache load/save, 429 with/without cache, retry-after parsing, interval reset