Skip to content

Commit a3a02d3

Browse files
fix(octopus): keep Intelligent dispatch markers when export/gas rates are fetched (springfall2008#5286)
fetch_octopus_rates() replaced self.io_adjusted on every call, so the export and gas fetches that follow the import fetch reset it to {} - every BottlecapDave IOG user with metric_octopus_export or metric_octopus_gas set lost all is_intelligent_adjusted markers. Regression from springfall2008#2826, which dropped the old "only when adjust_key is set" guard. Among other things this left dynamic_load_car_strip_feed_rates() with nothing to strip, so a car cancelled by springfall2008#5229 still left its dispatch priced cheap and the house battery was planned into it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e29898e commit a3a02d3

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎apps/predbat/octopus.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,7 +3839,8 @@ def fetch_octopus_rates(self, entity_id, adjust_key=None):
38393839
Fetch the Octopus rates from the sensor
38403840
38413841
:param entity_id: The entity_id of the sensor
3842-
:param adjust_key: The key use to find Octopus Intelligent adjusted rates
3842+
:param adjust_key: The key use to find Octopus Intelligent adjusted rates. self.io_adjusted is only
3843+
replaced when this is given, so a fetch without it (export, gas) keeps the import's markers
38433844
"""
38443845
data_all = []
38453846
rate_data = {}
@@ -3904,7 +3905,11 @@ def fetch_octopus_rates(self, entity_id, adjust_key=None):
39043905
rate_key = "price"
39053906
from_key = "from"
39063907
to_key = "till"
3907-
rate_data, self.io_adjusted = minute_data(data_all, self.forecast_days + 1, self.midnight_utc, rate_key, from_key, backwards=False, to_key=to_key, adjust_key=adjust_key, scale=scale)
3908+
rate_data, io_adjusted = minute_data(data_all, self.forecast_days + 1, self.midnight_utc, rate_key, from_key, backwards=False, to_key=to_key, adjust_key=adjust_key, scale=scale)
3909+
# A fetch without adjust_key has no markers to give, so it must not wipe the ones an earlier
3910+
# import fetch found (#5286)
3911+
if adjust_key:
3912+
self.io_adjusted = io_adjusted
39083913

39093914
return rate_data
39103915

‎apps/predbat/tests/test_fetch_octopus_rates.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_fetch_octopus_rates(my_predbat):
3232
# Setup test environment
3333
old_forecast_days = my_predbat.forecast_days
3434
old_midnight_utc = my_predbat.midnight_utc
35+
old_io_adjusted = my_predbat.io_adjusted
3536

3637
my_predbat.forecast_days = 2
3738
my_predbat.midnight_utc = datetime.strptime("2025-01-01T00:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z")
@@ -204,6 +205,27 @@ def test_fetch_octopus_rates(my_predbat):
204205
print("ERROR: Expected minute 0 to be marked as intelligent adjusted")
205206
failed = True
206207

208+
# Test 5a: A later fetch without adjust_key (export or gas, which fetch_rates() reads after import)
209+
# must keep the import's dispatch markers (#5286)
210+
print("*** Test 5a: Fetch without adjust_key keeps the import's intelligent adjusted markers")
211+
212+
my_predbat.ha_interface.dummy_items["sensor.octopus_export"] = {
213+
"state": "15.0",
214+
"rates": [
215+
{"value_inc_vat": 15.0, "valid_from": "2025-01-01T00:00:00+00:00", "valid_to": "2025-01-01T01:00:00+00:00"},
216+
],
217+
}
218+
rate_data = my_predbat.fetch_octopus_rates("sensor.octopus_export")
219+
220+
if not rate_data:
221+
print("ERROR: Expected export rate data for Test 5a")
222+
failed = True
223+
elif my_predbat.io_adjusted.get(0, False) and not my_predbat.io_adjusted.get(30, False):
224+
print("Test 5a passed - export fetch kept the import's intelligent adjusted markers")
225+
else:
226+
print("ERROR: Expected minute 0 still marked (and minute 30 not) after the export fetch, got {}".format(sorted(my_predbat.io_adjusted)[:5]))
227+
failed = True
228+
207229
# Test 5b: Missing day-rate events - no legacy 'all_rates' sensor fallback (#1699)
208230
print("*** Test 5b: Missing previous/current day-rate events warn about 'rates', not 'all_rates'")
209231

@@ -275,6 +297,7 @@ def test_fetch_octopus_rates(my_predbat):
275297
# Restore original values
276298
my_predbat.forecast_days = old_forecast_days
277299
my_predbat.midnight_utc = old_midnight_utc
300+
my_predbat.io_adjusted = old_io_adjusted
278301

279302
if not failed:
280303
print("**** All fetch_octopus_rates tests PASSED ****")

0 commit comments

Comments
 (0)