Skip to content

Commit

Permalink
fix: Added checks to ensure current gas consumptions were not negative
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Nov 27, 2023
1 parent 388e64d commit b8418e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions custom_components/octopus_energy/coordinators/gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ async def async_refresh_gas_rates_data(
new_rates = await client.async_get_gas_rates(tariff_code, period_from, period_to)
except:
_LOGGER.debug(f'Failed to retrieve gas rates for {target_mprn}/{target_serial_number} ({tariff_code})')

# Make sure our rate information doesn't contain any negative values https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/issues/506
if new_rates is not None:
for rate in new_rates:
if rate["value_inc_vat"] < 0:
new_rates = None
break

if new_rates is not None:
_LOGGER.debug(f'Gas rates retrieved for {target_mprn}/{target_serial_number} ({tariff_code});')
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/coordinators/test_async_refresh_gas_rates_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,46 @@ def fire_event(name, metadata):
assert retrieved_rates.last_retrieved == existing_rates.last_retrieved
assert retrieved_rates.rates == existing_rates.rates
assert rates_returned == True
assert len(actual_fired_events.keys()) == 0

@pytest.mark.asyncio
async def test_when_negative_rates_present_then_rates_retrieved():
expected_period_from = (current - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
expected_period_to = (current + timedelta(days=2)).replace(hour=0, minute=0, second=0, microsecond=0)
expected_rates = create_rate_data(expected_period_from, expected_period_to, [1, 2])
expected_rates[0]["value_inc_vat"] = -0.01

rates_returned = False
async def async_mocked_get_gas_rates(*args, **kwargs):
nonlocal rates_returned
rates_returned = True
return expected_rates

actual_fired_events = {}
def fire_event(name, metadata):
nonlocal actual_fired_events
actual_fired_events[name] = metadata
return None

account_info = get_account_info()
existing_rates = GasRatesCoordinatorResult(period_from, create_rate_data(period_from - timedelta(days=60), period_to - timedelta(days=60), [2, 4]))

with mock.patch.multiple(OctopusEnergyApiClient, async_get_gas_rates=async_mocked_get_gas_rates):
client = OctopusEnergyApiClient("NOT_REAL")
retrieved_rates = await async_refresh_gas_rates_data(
current,
client,
account_info,
mprn,
serial_number,
existing_rates,
fire_event
)

assert retrieved_rates is not None
assert retrieved_rates.last_retrieved == existing_rates.last_retrieved
assert retrieved_rates.rates == existing_rates.rates
assert rates_returned == True
assert expected_rates[0]["value_inc_vat"] < 0

assert len(actual_fired_events.keys()) == 0

0 comments on commit b8418e1

Please sign in to comment.