Skip to content

Commit

Permalink
fix(sensor): Updated min consumption data to be in calculation
Browse files Browse the repository at this point in the history
This has been done to reduce unnecessary API calls
  • Loading branch information
BottlecapDave committed Dec 18, 2022
1 parent 820abde commit 4f55e8d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
8 changes: 4 additions & 4 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def state(self):
self._latest_date
)

if (consumption != None and len(consumption["consumptions"]) > 2):
if (consumption != None):
_LOGGER.debug(f"Calculated previous electricity consumption for '{self._mpan}/{self._serial_number}'...")
self._state = consumption["total"]
self._latest_date = consumption["last_calculated_timestamp"]
Expand Down Expand Up @@ -603,7 +603,7 @@ async def async_update(self):
self._is_smart_meter
)

if (consumption_cost != None and len(consumption_cost["charges"]) > 2):
if (consumption_cost != None):
_LOGGER.debug(f"Calculated previous electricity consumption cost for '{self._mpan}/{self._serial_number}'...")
self._latest_date = consumption_cost["last_calculated_timestamp"]
self._state = consumption_cost["total"]
Expand Down Expand Up @@ -890,7 +890,7 @@ def state(self):
self._native_consumption_units
)

if (consumption != None and len(consumption["consumptions"]) > 2):
if (consumption != None):
_LOGGER.debug(f"Calculated previous gas consumption for '{self._mprn}/{self._serial_number}'...")
self._state = consumption["total_m3"]
self._latest_date = consumption["last_calculated_timestamp"]
Expand Down Expand Up @@ -996,7 +996,7 @@ async def async_update(self):
self._native_consumption_units
)

if (consumption_cost != None and len(consumption_cost["charges"]) > 2):
if (consumption_cost != None):
_LOGGER.debug(f"Calculated previous gas consumption cost for '{self._mprn}/{self._serial_number}'...")
self._latest_date = consumption_cost["last_calculated_timestamp"]
self._state = consumption_cost["total"]
Expand Down
10 changes: 6 additions & 4 deletions custom_components/octopus_energy/sensor_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .api_client import OctopusEnergyApiClient

minimum_consumption_records = 2

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

Expand Down Expand Up @@ -37,7 +39,7 @@ async def async_get_consumption_data(
return []

def calculate_electricity_consumption(consumption_data, last_calculated_timestamp):
if (consumption_data != None and len(consumption_data) > 0):
if (consumption_data != None and len(consumption_data) > minimum_consumption_records):

sorted_consumption_data = __sort_consumption(consumption_data)

Expand Down Expand Up @@ -65,7 +67,7 @@ def calculate_electricity_consumption(consumption_data, last_calculated_timestam
}

async def async_calculate_electricity_cost(client: OctopusEnergyApiClient, consumption_data, last_calculated_timestamp, period_from, period_to, tariff_code, is_smart_meter):
if (consumption_data != None and len(consumption_data) > 0):
if (consumption_data != None and len(consumption_data) > minimum_consumption_records):

sorted_consumption_data = __sort_consumption(consumption_data)

Expand Down Expand Up @@ -126,7 +128,7 @@ def convert_kwh_to_m3(value):
return round(m3_value / 1.02264, 3) # Volume correction factor

def calculate_gas_consumption(consumption_data, last_calculated_timestamp, consumption_units):
if (consumption_data != None and len(consumption_data) > 0):
if (consumption_data != None and len(consumption_data) > minimum_consumption_records):

sorted_consumption_data = __sort_consumption(consumption_data)

Expand Down Expand Up @@ -168,7 +170,7 @@ def calculate_gas_consumption(consumption_data, last_calculated_timestamp, consu
}

async def async_calculate_gas_cost(client: OctopusEnergyApiClient, consumption_data, last_calculated_timestamp, period_from, period_to, sensor, consumption_units):
if (consumption_data != None and len(consumption_data) > 0):
if (consumption_data != None and len(consumption_data) > minimum_consumption_records):

sorted_consumption_data = __sort_consumption(consumption_data)

Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_calculate_electricity_consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ async def test_when_electricity_consumption_is_none_then_no_calculation_is_retur
assert consumption_cost == None

@pytest.mark.asyncio
async def test_when_electricity_consumption_is_empty_then_no_calculation_is_returned():
async def test_when_electricity_consumption_is_less_than_three_records_then_no_calculation_is_returned():
# Arrange
latest_date = datetime.strptime("2022-02-09T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
consumption_data = create_consumption_data(
datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
datetime.strptime("2022-02-28T01:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
)

# Act
consumption_cost = calculate_electricity_consumption(
[],
consumption_data,
latest_date,
)

Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_calculate_electricity_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ async def test_when_electricity_consumption_is_none_then_no_calculation_is_retur
assert consumption_cost == None

@pytest.mark.asyncio
async def test_when_electricity_consumption_is_empty_then_no_calculation_is_returned():
async def test_when_electricity_consumption_is_less_than_three_records_then_no_calculation_is_returned():
# Arrange
client = OctopusEnergyApiClient("NOT_REAL")
period_from = datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
period_to = datetime.strptime("2022-03-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
latest_date = datetime.strptime("2022-02-09T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
tariff_code = "E-1R-SUPER-GREEN-24M-21-07-30-A"
consumption_data = create_consumption_data(
datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
datetime.strptime("2022-02-28T01:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
)

# Act
consumption_cost = await async_calculate_electricity_cost(
client,
[],
consumption_data,
latest_date,
period_from,
period_to,
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_calculate_gas_consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ async def test_when_gas_consumption_is_none_then_no_calculation_is_returned():
assert consumption == None

@pytest.mark.asyncio
async def test_when_gas_consumption_is_empty_then_no_calculation_is_returned():
async def test_when_gas_consumption_is_less_than_three_records_then_no_calculation_is_returned():
# Arrange
latest_date = datetime.strptime("2022-02-09T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
consumption_data = create_consumption_data(
datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
datetime.strptime("2022-02-28T01:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
)

# Act
consumption = calculate_gas_consumption(
[],
consumption_data,
latest_date,
"m³"
)
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_calculate_gas_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ async def test_when_gas_consumption_is_none_then_no_calculation_is_returned():
assert consumption_cost == None

@pytest.mark.asyncio
async def test_when_gas_consumption_is_empty_then_no_calculation_is_returned():
async def test_when_gas_consumption_is_less_than_three_records_then_no_calculation_is_returned():
# Arrange
client = OctopusEnergyApiClient("NOT_REAL")
period_from = datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
period_to = datetime.strptime("2022-03-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
latest_date = datetime.strptime("2022-02-09T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
tariff_code = "G-1R-SUPER-GREEN-24M-21-07-30-A"
consumption_data = create_consumption_data(
datetime.strptime("2022-02-28T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
datetime.strptime("2022-02-28T01:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
)

# Act
consumption_cost = await async_calculate_gas_cost(
client,
[],
consumption_data,
latest_date,
period_from,
period_to,
Expand Down

0 comments on commit 4f55e8d

Please sign in to comment.