fix: re-run automatic_config when tariff structure changes#3487
fix: re-run automatic_config when tariff structure changes#3487springfall2008 merged 1 commit intomainfrom
Conversation
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>
There was a problem hiding this comment.
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
automaticis enabled, log the change and re-runautomatic_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: |
There was a problem hiding this comment.
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.
| if old_tariff_keys and new_tariff_keys != old_tariff_keys and self.automatic: | |
| if new_tariff_keys != old_tariff_keys and self.automatic: |
|
|
||
| # 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: |
There was a problem hiding this comment.
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.
| 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"): |
Summary
automatic_config()only runs onfirst=Trueand never re-runs whenasync_find_tariffs()discovers the export tariff laterautomatic_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 triggersautomatic_config()to set themetric_octopus_exportentity 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 effectsfirst=Trueat line 429-430 still works)self.tariffsis empty{}) sincefirst=Truealready handles thatFixes #3486
Test plan
metric_octopus_exportgets set correctly after reconfiguration🤖 Generated with Claude Code