Skip to content

Fix HomeAssistant forecast cache validation to check specific future hours#232

Merged
MaStr merged 3 commits intomainfrom
copilot/fix-forecast-caching-issue
Nov 27, 2025
Merged

Fix HomeAssistant forecast cache validation to check specific future hours#232
MaStr merged 3 commits intomainfrom
copilot/fix-forecast-caching-issue

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

Cache validation incorrectly used len(cache) < hours to trigger refresh. Since cache keys are weekday_hour (e.g., "0_14" for Monday 14:00), old entries for different time slots don't satisfy forecast requirements.

Changes

  • Cache validation: Check if specific required cache keys exist for forecast hours, not total cache size
  • Race condition fix: Calculate now inside cache lock to avoid edge cases around hour boundaries
  • Test coverage: Added test verifying stale cache entries trigger refresh when required future hours are missing

Before/After

# Before: total cache size check (incorrect)
if cache_size == 0 or cache_size < hours:
    self.refresh_data_with_limit(hours)

# After: check specific required keys
with self._cache_lock:
    now = datetime.datetime.now(tz=self.timezone)
    for h in range(hours):
        future_time = now + datetime.timedelta(hours=h)
        cache_key = self._get_cache_key(future_time.weekday(), future_time.hour)
        if cache_key not in self.consumption_cache:
            missing_keys = True
            break
Original prompt

This section details on the original issue you should resolve

<issue_title>Homassistant forecast caching issues</issue_title>
<issue_description>https://github.com/muexxl/batcontrol/blob/main/src/batcontrol/forecastconsumption/forecast_homeassistant.py#L730

This line is not correct.
The length of the cache does not reflect the forecast amount in the future as older entries can be existing.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 27, 2025 06:39
Co-authored-by: MaStr <1036501+MaStr@users.noreply.github.com>
Co-authored-by: MaStr <1036501+MaStr@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix forecast caching issues in Home Assistant Fix HomeAssistant forecast cache validation to check specific future hours Nov 27, 2025
Copilot AI requested a review from MaStr November 27, 2025 06:43
@MaStr MaStr marked this pull request as ready for review November 27, 2025 07:34
@MaStr MaStr merged commit 0980435 into main Nov 27, 2025
20 checks passed
@MaStr MaStr deleted the copilot/fix-forecast-caching-issue branch November 27, 2025 07:48
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.

Homassistant forecast caching issues

2 participants