Skip to content

Commit

Permalink
fix(custom): updated value_inc_vat and rates to be in pounds/pence fo…
Browse files Browse the repository at this point in the history
…r consistency

BREAKING CHANGE:
All references to value_inc_vat and rates should be in pounds/pence. If you are relying on this
structure, you may need to update your logic
  • Loading branch information
BottlecapDave committed Oct 30, 2023
1 parent 913336a commit 439c081
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ..api_client import OctopusEnergyApiClient
from ..api_client.intelligent_dispatches import IntelligentDispatches
from ..coordinators.intelligent_dispatches import IntelligentDispatchesCoordinatorResult

from ..utils import private_rates_to_public_rates
from . import get_electricity_meter_tariff_code, raise_rate_events
from ..intelligent import adjust_intelligent_rates

Expand Down Expand Up @@ -79,7 +79,7 @@ async def async_refresh_electricity_rates_data(
_LOGGER.debug(f"Rates adjusted: {new_rates}; dispatches: {dispatches}")

raise_rate_events(current,
new_rates,
private_rates_to_public_rates(new_rates),
{ "mpan": target_mpan, "serial_number": target_serial_number, "tariff_code": tariff_code },
fire_event,
EVENT_ELECTRICITY_PREVIOUS_DAY_RATES,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/octopus_energy/coordinators/gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

from ..api_client import OctopusEnergyApiClient

from ..utils import private_rates_to_public_rates
from . import get_gas_meter_tariff_code, raise_rate_events

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,7 +63,7 @@ async def async_refresh_gas_rates_data(
_LOGGER.debug(f'Gas rates retrieved for {target_mprn}/{target_serial_number} ({tariff_code});')

raise_rate_events(current,
new_rates,
private_rates_to_public_rates(new_rates),
{ "mprn": target_mprn, "serial_number": target_serial_number, "tariff_code": tariff_code },
fire_event,
EVENT_GAS_PREVIOUS_DAY_RATES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from ..api_client import (OctopusEnergyApiClient)
from ..api_client.intelligent_dispatches import IntelligentDispatches
from ..utils import private_rates_to_public_rates

from ..intelligent import adjust_intelligent_rates

Expand Down Expand Up @@ -83,9 +84,9 @@ async def async_fetch_consumption_and_rates(
consumption_data = __sort_consumption(consumption_data)

if (is_electricity == True):
fire_event(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_RATES, { "mpan": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": rate_data })
fire_event(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_RATES, { "mpan": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": private_rates_to_public_rates(rate_data) })
else:
fire_event(EVENT_GAS_PREVIOUS_CONSUMPTION_RATES, { "mprn": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": rate_data })
fire_event(EVENT_GAS_PREVIOUS_CONSUMPTION_RATES, { "mprn": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": private_rates_to_public_rates(rate_data) })

_LOGGER.debug(f"Fired event for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}")

Expand Down
3 changes: 2 additions & 1 deletion custom_components/octopus_energy/electricity/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

from ..utils.conversions import value_inc_vat_to_pounds
from ..utils import get_off_peak_cost

def __get_interval_end(item):
Expand Down Expand Up @@ -61,7 +62,7 @@ def calculate_electricity_consumption_and_cost(
charges.append({
"from": rate["valid_from"],
"to": rate["valid_to"],
"rate": value,
"rate": value_inc_vat_to_pounds(value),
"consumption": consumption_value,
"cost": round(cost / 100, 2)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["current_rate"]["value_inc_vat"] / 100
self._state = rate_information["current_rate"]["value_inc_vat"]
else:
self._attributes = {
"mpan": self._mpan,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/electricity/next_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["next_rate"]["value_inc_vat"] / 100
self._state = rate_information["next_rate"]["value_inc_vat"]
else:
self._attributes = {
"mpan": self._mpan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["previous_rate"]["value_inc_vat"] / 100
self._state = rate_information["previous_rate"]["value_inc_vat"]
else:
self._attributes = {
"mpan": self._mpan,
Expand Down
4 changes: 3 additions & 1 deletion custom_components/octopus_energy/gas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from ..utils.conversions import value_inc_vat_to_pounds

def __get_interval_end(item):
return item["interval_end"]

Expand Down Expand Up @@ -69,7 +71,7 @@ def calculate_gas_consumption_and_cost(
charges.append({
"from": rate["valid_from"],
"to": rate["valid_to"],
"rate": value,
"rate": value_inc_vat_to_pounds(value),
"consumption_m3": current_consumption_m3,
"consumption_kwh": current_consumption_kwh,
"cost": round(cost / 100, 2)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/current_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["current_rate"]["value_inc_vat"] / 100
self._state = rate_information["current_rate"]["value_inc_vat"]
else:
self._attributes = {
"mprn": self._mprn,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/next_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["next_rate"]["value_inc_vat"] / 100
self._state = rate_information["next_rate"]["value_inc_vat"]
else:
self._attributes = {
"mprn": self._mprn,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/previous_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def state(self):
"applicable_rates": rate_information["applicable_rates"],
}

self._state = rate_information["previous_rate"]["value_inc_vat"] / 100
self._state = rate_information["previous_rate"]["value_inc_vat"]
else:
self._attributes = {
"mprn": self._mprn,
Expand Down
6 changes: 5 additions & 1 deletion custom_components/octopus_energy/target_rates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from homeassistant.util.dt import (as_utc, parse_datetime)

from ..utils.conversions import value_inc_vat_to_pounds
from ..const import REGEX_OFFSET_PARTS

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -62,7 +63,10 @@ def __get_applicable_rates(current_date: datetime, target_start_time: str, targe
if rates is not None:
for rate in rates:
if rate["valid_from"] >= target_start and (target_end is None or rate["valid_to"] <= target_end):
applicable_rates.append(rate)
new_rate = dict(rate)
new_rate["value_inc_vat"] = value_inc_vat_to_pounds(rate["value_inc_vat"])

applicable_rates.append(new_rate)

# Make sure that we have enough rates that meet our target period
date_diff = target_end - target_start
Expand Down
25 changes: 23 additions & 2 deletions custom_components/octopus_energy/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import re
from datetime import datetime, timedelta


from homeassistant.util.dt import (as_utc, parse_datetime)

from ..const import (
REGEX_TARIFF_PARTS,
)

from ..utils.conversions import value_inc_vat_to_pounds
from .rate_information import get_current_rate_information

class TariffParts:
Expand Down Expand Up @@ -82,4 +83,24 @@ def is_off_peak(current: datetime, rates):

rate_information = get_current_rate_information(rates, current)

return off_peak_value is not None and rate_information is not None and off_peak_value == rate_information["current_rate"]["value_inc_vat"]
return off_peak_value is not None and rate_information is not None and value_inc_vat_to_pounds(off_peak_value) == rate_information["current_rate"]["value_inc_vat"]

def private_rates_to_public_rates(rates: list):
new_rates = []

for rate in rates:
new_rate = {
"valid_from": rate["valid_from"],
"valid_to": rate["valid_to"],
"value_inc_vat": value_inc_vat_to_pounds(rate["value_inc_vat"])
}

if "is_capped" in rate:
new_rate["is_capped"] = rate["is_capped"]

if "is_intelligent_adjusted" in rate:
new_rate["is_intelligent_adjusted"] = rate["is_intelligent_adjusted"]

new_rates.append(new_rate)

return new_rates
3 changes: 3 additions & 0 deletions custom_components/octopus_energy/utils/conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def value_inc_vat_to_pounds(value: float):
# We want our rates in a consistent pounds/pence but what the accuracy
return round(value / 100, 6)
22 changes: 12 additions & 10 deletions custom_components/octopus_energy/utils/rate_information.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import (datetime, timedelta)

from ..utils.conversions import value_inc_vat_to_pounds

def get_current_rate_information(rates, now: datetime):
min_target = now.replace(hour=0, minute=0, second=0, microsecond=0)
max_target = min_target + timedelta(days=1)
Expand Down Expand Up @@ -41,27 +43,27 @@ def get_current_rate_information(rates, now: datetime):
"all_rates": list(map(lambda x: {
"valid_from": x["valid_from"],
"valid_to": x["valid_to"],
"value_inc_vat": x["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(x["value_inc_vat"]),
"is_capped": x["is_capped"],
"is_intelligent_adjusted": x["is_intelligent_adjusted"] if "is_intelligent_adjusted" in x else False
}, rates)),
"applicable_rates": list(map(lambda x: {
"valid_from": x["valid_from"],
"valid_to": x["valid_to"],
"value_inc_vat": x["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(x["value_inc_vat"]),
"is_capped": x["is_capped"],
"is_intelligent_adjusted": x["is_intelligent_adjusted"] if "is_intelligent_adjusted" in x else False
}, applicable_rates)),
"current_rate": {
"valid_from": applicable_rates[0]["valid_from"],
"valid_to": applicable_rates[-1]["valid_to"],
"value_inc_vat": applicable_rates[0]["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(applicable_rates[0]["value_inc_vat"]),
"is_capped": current_rate["is_capped"],
"is_intelligent_adjusted": current_rate["is_intelligent_adjusted"] if "is_intelligent_adjusted" in current_rate else False
},
"min_rate_today": min_rate_value,
"max_rate_today": max_rate_value,
"average_rate_today": total_rate_value / total_rates
"min_rate_today": value_inc_vat_to_pounds(min_rate_value),
"max_rate_today": value_inc_vat_to_pounds(max_rate_value),
"average_rate_today": value_inc_vat_to_pounds(total_rate_value / total_rates)
}

return None
Expand Down Expand Up @@ -91,14 +93,14 @@ def get_previous_rate_information(rates, now: datetime):
"applicable_rates": list(map(lambda x: {
"valid_from": x["valid_from"],
"valid_to": x["valid_to"],
"value_inc_vat": x["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(x["value_inc_vat"]),
"is_capped": x["is_capped"],
"is_intelligent_adjusted": x["is_intelligent_adjusted"] if "is_intelligent_adjusted" in x else False
}, applicable_rates)),
"previous_rate": {
"valid_from": applicable_rates[0]["valid_from"],
"valid_to": applicable_rates[-1]["valid_to"],
"value_inc_vat": applicable_rates[0]["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(applicable_rates[0]["value_inc_vat"]),
}
}

Expand All @@ -124,14 +126,14 @@ def get_next_rate_information(rates, now: datetime):
"applicable_rates": list(map(lambda x: {
"valid_from": x["valid_from"],
"valid_to": x["valid_to"],
"value_inc_vat": x["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(x["value_inc_vat"]),
"is_capped": x["is_capped"],
"is_intelligent_adjusted": x["is_intelligent_adjusted"] if "is_intelligent_adjusted" in x else False
}, applicable_rates)),
"next_rate": {
"valid_from": applicable_rates[0]["valid_from"],
"valid_to": applicable_rates[-1]["valid_to"],
"value_inc_vat": applicable_rates[0]["value_inc_vat"],
"value_inc_vat": value_inc_vat_to_pounds(applicable_rates[0]["value_inc_vat"]),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def test_when_electricity_consumption_available_then_calculation_returned(
expected_valid_from = expected_valid_to

assert "rate" in item
assert item["rate"] == expected_rate_price
assert item["rate"] == round(expected_rate_price / 100, 6)

assert "cost" in item
assert item["cost"] == round(expected_rate_price / 100, 2)
Expand Down Expand Up @@ -216,7 +216,7 @@ async def test_when_electricity_consumption_starting_at_latest_date_then_calcula
expected_valid_from = expected_valid_to

assert "rate" in item
assert item["rate"] == expected_rate_price
assert item["rate"] == round(expected_rate_price / 100, 6)

assert "cost" in item
assert item["cost"] == round(expected_rate_price / 100, 2)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/gas/test_calculate_gas_consumption_and_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ async def test_when_gas_consumption_available_then_calculation_returned(latest_d
assert "to" in item
assert item["to"] == expected_valid_to

assert "rate" in item
assert item["rate"] == round(expected_rate_price / 100, 6)

assert "consumption_m3" in item
if consumption_units == "m³":
assert item["consumption_m3"] == 1
Expand Down Expand Up @@ -243,6 +246,9 @@ async def test_when_gas_consumption_starting_at_latest_date_then_calculation_ret
assert "to" in item
assert item["to"] == expected_valid_to

assert "rate" in item
assert item["rate"] == round(expected_rate_price / 100, 6)

assert "consumption_m3" in item
if consumption_units == "m³":
assert item["consumption_m3"] == 1
Expand Down
Loading

0 comments on commit 439c081

Please sign in to comment.