Skip to content

fix: re-run automatic_config when tariff structure changes#3487

Merged
springfall2008 merged 1 commit intomainfrom
fix/tariff-change-reconfig
Mar 4, 2026
Merged

fix: re-run automatic_config when tariff structure changes#3487
springfall2008 merged 1 commit intomainfrom
fix/tariff-change-reconfig

Conversation

@mgazza
Copy link
Collaborator

@mgazza mgazza commented Mar 4, 2026

Summary

  • Fixes export rates stuck at 0.0 when a PredBat pod starts before an Octopus export tariff agreement becomes active
  • automatic_config() only runs on first=True and never re-runs when async_find_tariffs() discovers the export tariff later
  • Captures tariff keys before/after discovery and re-runs automatic_config() if the key set changed (e.g. "export" appeared)

Details

When async_find_tariffs() runs every 30 minutes and discovers a newly-active export agreement, the tariff keys change (e.g. {"import"}{"import", "export"}). This triggers automatic_config() to set the metric_octopus_export entity name arg, which was previously never set because the export tariff didn't exist at startup.

The fix is safe because:

  • set_arg() is idempotent (simple dict assignment)
  • automatic_config() only sets sensor entity names — no side effects
  • First-run path unchanged (first=True at line 429-430 still works)
  • Only triggers when tariff keys actually change, not every 30-min cycle
  • Skips the very first run (when self.tariffs is empty {}) since first=True already handles that

Fixes #3486

Test plan

  • Verify existing tariff discovery works unchanged (import-only account)
  • Verify export tariff appearing after startup triggers reconfiguration log message
  • Verify metric_octopus_export gets set correctly after reconfiguration
  • Verify no reconfiguration on subsequent cycles when tariffs haven't changed

🤖 Generated with Claude Code

When a PredBat pod starts before an Octopus export tariff agreement
becomes active, automatic_config() only runs on first=True and never
sets metric_octopus_export. Even though async_find_tariffs() discovers
the tariff later, the config arg is never updated, causing export rates
to be 0.0 indefinitely until pod restart.

Capture tariff keys before the update and compare after. If keys changed
(e.g. "export" appeared) and automatic mode is enabled, re-run
automatic_config() to update the entity name args.

Fixes #3486

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the Octopus integration to re-run auto-configuration when the discovered tariff key set changes at runtime (e.g., when an export agreement becomes active after PredBat has already started), so PredBat can start using newly-available Octopus export rate entities without requiring a restart.

Changes:

  • Capture the previously-known tariff keys before discovery and compare them to the newly-discovered keys.
  • When the tariff key set changes and automatic is enabled, log the change and re-run automatic_config().


# Re-run automatic config if tariff structure changed (e.g. export agreement became active)
new_tariff_keys = set(self.tariffs.keys())
if old_tariff_keys and new_tariff_keys != old_tariff_keys and self.automatic:
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The old_tariff_keys guard means automatic_config() will not run when tariffs transition from empty {} to non-empty on a later cycle (e.g., no active agreements/API data on first run or a temporary gap, then tariffs appear). In that case, metrics like metric_octopus_import/export can remain unset until restart. Consider tracking whether automatic_config() has already been executed (or passing first into async_find_tariffs) and triggering reconfiguration on any keyset change, including empty → non-empty, while still avoiding duplicate runs on the initial startup path.

Suggested change
if old_tariff_keys and new_tariff_keys != old_tariff_keys and self.automatic:
if new_tariff_keys != old_tariff_keys and self.automatic:

Copilot uses AI. Check for mistakes.

# Re-run automatic config if tariff structure changed (e.g. export agreement became active)
new_tariff_keys = set(self.tariffs.keys())
if old_tariff_keys and new_tariff_keys != old_tariff_keys and self.automatic:
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

This change introduces new runtime behavior (re-running automatic_config() when tariff keys change) but there doesn’t appear to be test coverage for the automatic=True path in async_find_tariffs(). Adding a unit test that simulates tariffs changing (e.g. {'import'}{'import','export'}) and asserts automatic_config() is invoked would help prevent regressions.

Suggested change
if old_tariff_keys and new_tariff_keys != old_tariff_keys and self.automatic:
if old_tariff_keys and new_tariff_keys != old_tariff_keys and getattr(self, "automatic", False) and hasattr(self, "automatic_config"):

Copilot uses AI. Check for mistakes.
@springfall2008 springfall2008 merged commit b07f50a into main Mar 4, 2026
5 checks passed
@springfall2008 springfall2008 deleted the fix/tariff-change-reconfig branch March 4, 2026 20:31
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.

automatic_config only runs once - misses tariff changes after startup

3 participants