Skip to content

Commit

Permalink
fix: Removed tracker tariff potentially looking at wrong API for stan…
Browse files Browse the repository at this point in the history
…ding charges
  • Loading branch information
BottlecapDave committed Oct 3, 2023
1 parent a764036 commit 82fecf1
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 107 deletions.
18 changes: 0 additions & 18 deletions custom_components/octopus_energy/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,6 @@ async def async_get_electricity_standing_charge(self, tariff_code, period_from,
return None

product_code = tariff_parts.product_code

if self.__is_tracker_tariff__(tariff_code):
return await self.__async_get_tracker_standing_charge__(tariff_code, period_from, period_to)

result = None
async with aiohttp.ClientSession() as client:
Expand All @@ -659,9 +656,6 @@ async def async_get_gas_standing_charge(self, tariff_code, period_from, period_t

product_code = tariff_parts.product_code

if self.__is_tracker_tariff__(tariff_code):
return await self.__async_get_tracker_standing_charge__(tariff_code, period_from, period_to)

result = None
async with aiohttp.ClientSession() as client:
auth = aiohttp.BasicAuth(self._api_key, '')
Expand Down Expand Up @@ -888,18 +882,6 @@ async def async_get_intelligent_device(self, account_id: str):

return None

def __is_tracker_tariff__(self, tariff_code):
tariff_parts = get_tariff_parts(tariff_code)
if tariff_parts is None:
return None

product_code = tariff_parts.product_code

if product_code in self._product_tracker_cache:
return self._product_tracker_cache[product_code]

return False

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

Expand Down
39 changes: 1 addition & 38 deletions tests/integration/api_client/test_get_electricity_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.util.dt import (now)

from integration import (get_test_context, async_get_tracker_tariff)
from integration import (get_test_context)
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient

default_period_from = datetime.strptime("2022-12-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
Expand Down Expand Up @@ -160,43 +160,6 @@ async def test_when_get_electricity_rates_is_called_with_duel_rate_tariff_smart_
else:
assert item["value_inc_vat"] != cheapest_rate

# @pytest.mark.asyncio
# @pytest.mark.parametrize("tariff,price_cap",[("E-1R-SILVER-FLEX-22-11-25-C", None), ("E-1R-SILVER-FLEX-22-11-25-C", 10)])
# async def test_when_get_electricity_rates_is_called_with_tracker_tariff_then_rates_are_returned(tariff, price_cap):
# # Arrange
# context = get_test_context()
# period_from = now().replace(hour=0, minute=0, second=0, microsecond=0)
# period_to = (now() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
# client = OctopusEnergyApiClient(context["api_key"], price_cap)

# expected_tracker = await async_get_tracker_tariff(context["api_key"], tariff, now())
# assert expected_tracker is not None

# # Act
# data = await client.async_get_electricity_rates(tariff, False, period_from, period_to)

# # Assert
# assert data is not None
# assert len(data) == 48

# # Make sure our data is returned in 30 minute increments
# expected_valid_from = period_from
# for item in data:
# expected_valid_to = expected_valid_from + timedelta(minutes=30)

# assert "valid_from" in item
# assert item["valid_from"] == expected_valid_from
# assert "valid_to" in item
# assert item["valid_to"] == expected_valid_to

# assert "value_inc_vat" in item
# if (price_cap is not None and expected_tracker["unit_rate"] > price_cap):
# assert item["value_inc_vat"] == price_cap
# else:
# assert item["value_inc_vat"] == expected_tracker["unit_rate"]

# expected_valid_from = expected_valid_to

@pytest.mark.asyncio
@pytest.mark.parametrize("tariff",[("E-2R-NOT-A-TARIFF-A"), ("E-1R-NOT-A-TARIFF-A"), ("NOT-A-TARIFF")])
async def test_when_get_electricity_rates_is_called_for_non_existent_tariff_then_none_is_returned(tariff):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.util.dt import (now)

from integration import (get_test_context, async_get_tracker_tariff)
from integration import (get_test_context)
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient

period_from = datetime.strptime("2022-12-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
Expand Down Expand Up @@ -40,16 +40,13 @@ async def test_when_get_electricity_standing_charge_is_called_with_tracker_tarif
period_to = (now() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
client = OctopusEnergyApiClient(context["api_key"])

expected_tracker = await async_get_tracker_tariff(context["api_key"], tariff, now())
assert expected_tracker is not None

# Act
result = await client.async_get_electricity_standing_charge(tariff, period_from, period_to)

# Assert
assert result is not None
assert "value_inc_vat" in result
assert result["value_inc_vat"] == expected_tracker["standing_charge"]
assert result["value_inc_vat"] is not None

@pytest.mark.asyncio
@pytest.mark.parametrize("tariff",[("E-1R-NOT-A-TARIFF-A"), ("NOT-A-TARIFF")])
Expand Down
42 changes: 1 addition & 41 deletions tests/integration/api_client/test_get_gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.util.dt import (now)

from integration import (get_test_context, async_get_tracker_tariff)
from integration import (get_test_context)
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient

@pytest.mark.asyncio
Expand Down Expand Up @@ -42,46 +42,6 @@ async def test_when_get_gas_rates_is_called_for_existent_tariff_then_rates_are_r

expected_valid_from = expected_valid_to

# @pytest.mark.asyncio
# @pytest.mark.parametrize("tariff,price_cap",[
# ("G-1R-SILVER-FLEX-22-11-25-C", None),
# ("G-1R-SILVER-FLEX-22-11-25-C", 2),
# ])
# async def test_when_get_gas_rates_is_called_with_tracker_tariff_then_rates_are_returned(tariff, price_cap):
# # Arrange
# context = get_test_context()
# period_from = now().replace(hour=0, minute=0, second=0, microsecond=0)
# period_to = (now() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
# client = OctopusEnergyApiClient(context["api_key"], None, price_cap)

# expected_tracker = await async_get_tracker_tariff(context["api_key"], tariff, now())
# assert expected_tracker is not None

# # Act
# data = await client.async_get_gas_rates(tariff, period_from, period_to)

# # Assert
# assert data is not None
# assert len(data) == 48

# # Make sure our data is returned in 30 minute increments
# expected_valid_from = period_from
# for item in data:
# expected_valid_to = expected_valid_from + timedelta(minutes=30)

# assert "valid_from" in item
# assert item["valid_from"] == expected_valid_from
# assert "valid_to" in item
# assert item["valid_to"] == expected_valid_to

# assert "value_inc_vat" in item
# if (price_cap is not None and expected_tracker["unit_rate"] > price_cap):
# assert item["value_inc_vat"] == price_cap
# else:
# assert item["value_inc_vat"] == expected_tracker["unit_rate"]

# expected_valid_from = expected_valid_to

@pytest.mark.asyncio
@pytest.mark.parametrize("tariff",[("G-1R-NOT-A-TARIFF-A"), ("NOT-A-TARIFF")])
async def test_when_get_gas_rates_is_called_for_non_existent_tariff_then_none_is_returned(tariff):
Expand Down
7 changes: 2 additions & 5 deletions tests/integration/api_client/test_get_gas_standing_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.util.dt import (now)

from integration import (get_test_context, async_get_tracker_tariff)
from integration import (get_test_context)
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient

period_from = datetime.strptime("2021-12-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
Expand Down Expand Up @@ -34,16 +34,13 @@ async def test_when_get_gas_standing_charge_is_called_with_tracker_tariff_then_r
period_to = (now() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
client = OctopusEnergyApiClient(context["api_key"])

expected_tracker = await async_get_tracker_tariff(context["api_key"], tariff, now())
assert expected_tracker is not None

# Act
result = await client.async_get_gas_standing_charge(tariff, period_from, period_to)

# Assert
assert result is not None
assert "value_inc_vat" in result
assert result["value_inc_vat"] == expected_tracker["standing_charge"]
assert result["value_inc_vat"] is not None

@pytest.mark.asyncio
@pytest.mark.parametrize("tariff",[("G-1R-NOT-A-TARIFF-A"), ("NOT-A-TARIFF")])
Expand Down

0 comments on commit 82fecf1

Please sign in to comment.