Skip to content

fix(galevo): cache hybrid menu item IDs and back off on unparsable 429 blocks - #200

Merged
astrinh0 merged 7 commits into
fustom:mainfrom
to3k:fix/galevo-menu-cache-and-rate-limit
Nov 26, 2026
Merged

astrinh0 merged 7 commits into
fustom:mainfrom
to3k:fix/galevo-menu-cache-and-rate-limit

Conversation

@to3k

@to3k to3k commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a regression introduced in 0.19.16 (confirmed live on a GALEVO Nimbus HP_SYS heat pump with Home Assistant 2026.11.2 / integration v0.20.7, causing persistent HTTP 429: b'Requests are blocked' outages that forced a rollback to v0.20.6 / 0.19.15):

  1. Cache discovered GALEVO hybrid menu items per gateway (AristonAPI._hybrid_menu_items)

    • In 0.19.16 (feat(galevo): support heat pump menu items and filter invalid menu values #195), get_menu_items / async_get_menu_items(..., include_hybrid_items=True) sent core_values() plus all 4 hybrid_groups() (and per-item 500 split retries) on every refresh (~5–10 GET requests per poll, doubled when HA triggers async_refresh_after_set() after consecutive writes).
    • Now discovery runs once per gateway (gw_id), recording the core + supported IDs that succeeded into self._hybrid_menu_items[gw_id].
    • Subsequent polls fetch all supported IDs in 1 single GET /menuItems/{gw_id}?menuItems=... request.
    • If that cached combined request ever returns HTTP 500, the gateway's cache entry is cleared and grouped discovery runs again once. Non-500 failures (including 429) never cache partial discovery results.
  2. Exponential cooldown on persistent 429 without a parsable block duration

    • When Ariston's API returns 429 with b'Requests are blocked' (no "blocked for X seconds" message), _rate_limit_wait_seconds fell back to 5s on every single attempt, so retries and HA coordinator polls kept resetting/prolonging the server-side block window (22 consecutive 429 errors observed in 19 minutes).
    • Keep the initial 5s retry on the first unparsable 429 (matching existing contract), also parse an integer Retry-After response header if present, and if 429 persists on retry or recurs (_rate_limit_streak), set _blocked_until using exponential cooldown (60s -> 120s -> 240s -> 480s -> 600s max, reset on 200 OK).
    • Concurrent requests waking up from sleep skip doomed retries if a sibling coroutine already extended _blocked_until / incremented _rate_limit_streak.

Testing

  • Verified with .venv/bin/python -m coverage run --source=ariston -m unittest discover --start-directory tests:
    • 101 / 101 tests passing (including 5 new unit tests in tests/test_ariston_api.py and tests/test_rate_limit.py covering sync/async menu-item caching, HTTP 500 cache invalidation, incomplete 429 discovery non-caching, Retry-After header parsing, and 429 cooldown escalation/reset).
    • 99% total coverage (94% on ariston/ariston_api.py).

cc @astrinh0 (regression affecting 0.19.16 / v0.20.7 on HP_SYS devices)

Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
@to3k

to3k commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @astrinh0 for the sharp review! Addressed all four points in 29b9bbf:

  1. Remember working smaller groups after combined-query fallback (_hybrid_menu_items now caches tuple[str, ...]): when the combined single query fails during cached polling and grouped discovery succeeds, the gateway caches the smaller working group/item queries directly instead of re-caching the failing combined query. Extended test_menu_items_cache_supported_ids_and_recover_on_500_or_550 with a 4th poll verifying the failed combined query (and empty groups) are skipped.
  2. Handle HTTP 550 alongside 500 in cached menu queries (_handle_cached_menu_error): both sync (get_menu_items) and async (async_get_menu_items) now fall back to grouped discovery on 500 and 550.
  3. Single retry ownership on overlapping 429 responses (_claim_rate_limit_retry, _rate_limit_retry_in_flight, _rate_limit_retry_epoch): after sleeping, only one caller can claim the retry; concurrent waiters abort without sending another burst or advancing the shared cooldown. Covered by test_async_overlapping_429_sends_single_retry.
  4. Do not block Retry-After retries on nonzero _rate_limit_streak: once an earlier unparsable-429 cooldown expires, a subsequent 429 with a valid Retry-After / "blocked for X seconds" sleeps and retries normally unless another request claimed ownership or extended _blocked_until during the wait (both sync and async paths). Covered by test_retry_after_still_retries_when_streak_is_nonzero.

All 102 tests passing (99% coverage).

Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
@to3k

to3k commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @astrinh0! Addressed all four follow-up review notes and the SonarCloud checks in 5933b69:

  1. SonarCloud (python:S1192 & duplication): Extracted _RATE_LIMIT_WAIT_LOG and shared menu-item collection helpers (_raise_unless_http_500, _record_menu_item_response, _record_menu_group_result) across sync/async paths.
  2. Sleep from shared deadline & verify deadline elapsed: _prepare_rate_limit_retry now calculates effective_wait from max(self._blocked_until, now + wait_seconds), and _claim_rate_limit_retry rejects any wake-up where the shared deadline has not elapsed yet or was extended during sleep. Added test_async_overlapping_429_respects_longest_shared_deadline testing both (60, 1) and (1, 60) arrival orders.
  3. Expiry on incomplete discoveries: When any individual item raises HTTP 500 during split discovery, _hybrid_menu_items_expires_at[gw_id] sets a 3600s TTL (_INCOMPLETE_MENU_CACHE_TTL_SECONDS) so skipped IDs are periodically re-checked and recovered on both sync and async paths (test_incomplete_menu_discovery_expires_and_recovers_skipped_id).
  4. Persist grouped polling mode per gateway: Added self._hybrid_menu_grouped_mode: set[str] so once a gateway falls back from the combined query to grouped mode, subsequent transient 500/550 rediscoveries stay in grouped mode instead of re-trying the rejected combined query (covered by extending test_menu_items_cache_supported_ids_and_recover_on_500_or_550).

104 / 104 tests passing (99% coverage, 95% on ariston/ariston_api.py).

Comment thread ariston/ariston_api.py Resolved
Comment thread ariston/ariston_api.py Resolved
@Remper1997

Copy link
Copy Markdown

Live test report from a real installation, focused on request volume.

Setup

  • GALEVO, pure heat pump (hpSys: true, hybridSys: false), gateway firmware 104.115.22C
  • Home Assistant 2026.11.0 (container), integration v0.20.9
  • This PR (5933b69) merged onto current main (0.19.18): merges cleanly, 115 tests pass
  • Integration scan interval 600 s (raised from the default 180 s while the account was being blocked)

Requests per state poll (from debug logs)

requests
Before (0.19.17), every poll 6 (1 properties + 5 menu-item queries)
This PR, first poll (discovery) 6 (1 properties + 5 grouped menu queries)
This PR, following polls 2 (1 properties + 1 combined menu query)

Once, the properties POST was sent again ~6 s later after a slow answer. That is the existing transport retry, not this PR.

Before this test the account was stuck in the bare 429 Requests are blocked loop from fustom/ariston-remotethermo-home-assistant-v3#545 (polling every 180 s, 6 requests per poll). With this PR, all polls from 00:58 to 02:20 local time succeeded with 2 requests each and no 429.

Things that may matter for the review

  • From 02:28 to 09:10 all coordinators were unavailable, then recovered on their own. I can't tell the cause: the log was lost to a restart. It may or may not have been a 429.
  • A separate client on the same account that sent ~10 requests within ~20 s got 429 Requests are blocked (no duration). Home Assistant's own polling kept working at the same time, so the block seems to apply per session/token rather than per account. The budget looks very small, which supports reducing requests per poll.
  • A combined menu query can fail as a whole with HTTP 500 when a single id in it can't be read, while the same ids in other combinations succeed. Worth keeping in mind for the combined cached query and its fallback to groups.

I can rerun with debug logging on a newer commit if that helps.

@rudelafuente

Copy link
Copy Markdown

Live data from another GALEVO heat pump (NIMBUS, hpSys: true), in case it helps this PR. I'm the author of #202 and I'd be glad to see this one merged instead.

  • How the block behaved: with the 5 s retry on every bare 429 Requests are blocked, the account (and the Ariston NET app with it) stayed blocked for at least 13 h, at about 45–50 × 429 per hour. Stopping all requests from 11:53 to 15:44 was enough for it to expire. We don't know the real block length.
  • Overnight with a local patch: the same logic as Back off on 429 without block duration; throttle GALEVO hybrid menu items #202, on 0.19.17, at a 300 s scan interval. That's ~38 requests per hour (hybrid groups every 30 min). From 15:44 to 09:45 there were 0 × 429, so its pause never triggered. The only errors were two transient HTTP 500s, handled by keeping the last values.
  • The cooldown cap: our pause starts at 15 min and doubles up to 2 h; this PR caps at 10 min. I have no data showing that a probe every 10 min keeps the block alive, only that a retry every 5 s does. A longer cap might be the safer default, given how small the budget seems (Remper1997's report).
  • In-flight 429s (your latest review note): Back off on 429 without block duration; throttle GALEVO hybrid menu items #202 handles the same case. A parsed delay never shortens an active pause, and a retry is skipped if a longer pause started while it waited. Its concurrency tests (test_async_in_flight_* in tests/test_rate_limit.py) may be reusable here.

@astrinh0

Copy link
Copy Markdown
Collaborator

@to3k when you could fix my comments please, to merge it

@astrinh0
astrinh0 merged commit db70722 into fustom:main Nov 26, 2026
5 checks passed
@astrinh0

Copy link
Copy Markdown
Collaborator

@rudelafuente @Remper1997 and @to3k version v0.20.13 of the integration is ON

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants