Skip to content

Read the resource tree only once and cache it - #22

Merged
StephanU merged 2 commits into
StephanU:mainfrom
Xitee1:read-resources-only-once
Nov 1, 2026
Merged

StephanU merged 2 commits into
StephanU:mainfrom
Xitee1:read-resources-only-once

Conversation

@Xitee1

@Xitee1 Xitee1 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

What

DataProvider.read_data() fetched both /api/v1/var (the full resource tree) and /api/v1/var/status on every call. The resource tree is the largest response the Query API produces and only changes when the myGEKKO configuration itself is changed, so this PR fetches it only on the first read_data() call and caches it afterwards. Every subsequent cycle only requests /api/v1/var/status.

Why

  • Halves the number of requests per polling cycle — relevant for the Plus Query API, which enforces rate/burst limits (HTTP 429).
  • Avoids repeatedly transferring the biggest payload, which is especially noticeable with the 30s polling interval of the Home Assistant integration.

Notes

  • If the initial resources fetch fails (or its JSON is invalid), resources stays None and the next read_data() call retries it, matching the previous behavior.
  • A config change on the controller (e.g. renaming/adding items) is picked up again after re-creating the client — same as the Home Assistant integration behaves today, where entities are only created at setup anyway.
  • Added a test asserting /api/v1/var is requested once across two read_data() calls while /api/v1/var/status is requested each time.

🤖 Generated with Claude Code

The /api/v1/var resource tree only changes when the myGEKKO
configuration is changed, but it was fetched on every read_data()
call together with /api/v1/var/status. Fetching it once and caching
it halves the number of requests per polling cycle and avoids
transferring the largest API response over and over.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Xitee1
Xitee1 marked this pull request as draft July 15, 2026 07:15
@Xitee1
Xitee1 marked this pull request as ready for review August 17, 2026 12:14
@StephanU
StephanU requested a balanced review from Copilot November 1, 2026 11:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the resource-tree caching change.

No open issues 🎉

What changed in this PR

Caches the myGEKKO resource tree in DataProvider to reduce polling requests and data transfer while continuing to refresh status on every cycle.

Changes:

  • Fetches /api/v1/var only while resources is None, preserving retries after an initial fetch or JSON parsing failure.
  • Adds a regression test verifying one resource-tree request and two status requests across two polling calls.
File Description
tests/​test_pymygekko.py Tests resource caching and continued status polling.
PyMyGekko/​data_provider.py Reuses the cached resource tree on subsequent reads.

Review effort: Balanced

@StephanU
StephanU merged commit 0446456 into StephanU:main Nov 1, 2026
3 checks passed
@Xitee1
Xitee1 deleted the read-resources-only-once branch November 2, 2026 11:12
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.

3 participants