Skip to content

Commit

Permalink
fix: Fixed issue when failing to retrieve account during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Nov 13, 2023
1 parent b68147f commit c9d04fb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
13 changes: 10 additions & 3 deletions custom_components/octopus_energy/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION
)
from ..api_client import OctopusEnergyApiClient
from ..api_client import OctopusEnergyApiClient, RequestError, ServerError

def merge_main_config(data: dict, options: dict, updated_config: dict = None):
config = dict(data)
Expand All @@ -31,9 +31,16 @@ async def async_validate_main_config(data):
errors = {}

client = OctopusEnergyApiClient(data[CONFIG_MAIN_API_KEY])
account_info = await client.async_get_account(data[CONFIG_MAIN_ACCOUNT_ID])

try:
account_info = await client.async_get_account(data[CONFIG_MAIN_ACCOUNT_ID])
except RequestError:
# Treat errors as not finding the account
account_info = None
except ServerError:
errors[CONFIG_MAIN_API_KEY] = "server_error"

if (account_info is None):
if (CONFIG_MAIN_API_KEY not in errors and account_info is None):
errors[CONFIG_MAIN_API_KEY] = "account_not_found"

if data[CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION] == True:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/octopus_energy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
}
},
"error": {
"server_error": "Failed to connect to OE servers. Please try again later",
"account_not_found": "Invalid API key or account id specified",
"value_greater_than_zero": "Value must be greater or equal to 1",
"invalid_target_hours": "Target hours must be in half hour increments (e.g. 0.5 = 30 minutes; 1 = 60 minutes).",
Expand Down Expand Up @@ -88,6 +89,7 @@
}
},
"error": {
"server_error": "Failed to connect to OE servers. Please try again later",
"account_not_found": "Invalid API key or account id specified",
"value_greater_than_zero": "Value must be greater or equal to 1",
"invalid_target_hours": "Target hours must be in half hour increments (e.g. 0.5 = 30 minutes; 1 = 60 minutes).",
Expand Down
76 changes: 75 additions & 1 deletion tests/unit/config/test_validate_main_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

from custom_components.octopus_energy.api_client import OctopusEnergyApiClient
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient, RequestError, ServerError
from custom_components.octopus_energy.config.main import async_validate_main_config
from custom_components.octopus_energy.const import (
CONFIG_MAIN_ACCOUNT_ID,
Expand Down Expand Up @@ -111,6 +111,80 @@ async def async_mocked_get_account(*args, **kwargs):
assert CONFIG_MAIN_ELECTRICITY_PRICE_CAP not in errors
assert CONFIG_MAIN_GAS_PRICE_CAP not in errors

@pytest.mark.asyncio
async def test_when_account_info_raises_server_error_then_errors_returned():
# Arrange
data = {
CONFIG_MAIN_API_KEY: "test-api-key",
CONFIG_MAIN_ACCOUNT_ID: "A-123",
CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION: True,
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES: 1,
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES: 1,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET: 1,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET: 1,
CONFIG_MAIN_CALORIFIC_VALUE: 40,
CONFIG_MAIN_ELECTRICITY_PRICE_CAP: 38.5,
CONFIG_MAIN_GAS_PRICE_CAP: 10.5,
}

async def async_mocked_get_account(*args, **kwargs):
raise ServerError()

# Act
with mock.patch.multiple(OctopusEnergyApiClient, async_get_account=async_mocked_get_account):
errors = await async_validate_main_config(data)

# Assert
assert CONFIG_MAIN_API_KEY in errors
assert errors[CONFIG_MAIN_API_KEY] == "server_error"

assert CONFIG_MAIN_ACCOUNT_ID not in errors
assert CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION not in errors
assert CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES not in errors
assert CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES not in errors
assert CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET not in errors
assert CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET not in errors
assert CONFIG_MAIN_CALORIFIC_VALUE not in errors
assert CONFIG_MAIN_ELECTRICITY_PRICE_CAP not in errors
assert CONFIG_MAIN_GAS_PRICE_CAP not in errors

@pytest.mark.asyncio
async def test_when_account_info_raises_request_error_then_errors_returned():
# Arrange
data = {
CONFIG_MAIN_API_KEY: "test-api-key",
CONFIG_MAIN_ACCOUNT_ID: "A-123",
CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION: True,
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES: 1,
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES: 1,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET: 1,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET: 1,
CONFIG_MAIN_CALORIFIC_VALUE: 40,
CONFIG_MAIN_ELECTRICITY_PRICE_CAP: 38.5,
CONFIG_MAIN_GAS_PRICE_CAP: 10.5,
}

async def async_mocked_get_account(*args, **kwargs):
raise RequestError("blah", [])

# Act
with mock.patch.multiple(OctopusEnergyApiClient, async_get_account=async_mocked_get_account):
errors = await async_validate_main_config(data)

# Assert
assert CONFIG_MAIN_API_KEY in errors
assert errors[CONFIG_MAIN_API_KEY] == "account_not_found"

assert CONFIG_MAIN_ACCOUNT_ID not in errors
assert CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION not in errors
assert CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES not in errors
assert CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES not in errors
assert CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET not in errors
assert CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET not in errors
assert CONFIG_MAIN_CALORIFIC_VALUE not in errors
assert CONFIG_MAIN_ELECTRICITY_PRICE_CAP not in errors
assert CONFIG_MAIN_GAS_PRICE_CAP not in errors

@pytest.mark.asyncio
async def test_when_live_electricity_less_than_one_and_supports_live_consumption_is_false_then_no_errors_returned():
# Arrange
Expand Down

0 comments on commit c9d04fb

Please sign in to comment.