Skip to content

Commit

Permalink
fix: Improved Home Pro connection errors (30 minutes dev time)
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Jun 22, 2024
1 parent e53834e commit 40582ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
10 changes: 5 additions & 5 deletions custom_components/octopus_energy/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
CONFIG_MAIN_HOME_PRO_ADDRESS,
CONFIG_MAIN_HOME_PRO_API_KEY
)
from ..api_client import OctopusEnergyApiClient, RequestException, ServerException
from ..api_client import AuthenticationException, OctopusEnergyApiClient, RequestException, ServerException
from ..api_client_home_pro import OctopusEnergyHomeProApiClient

async def async_migrate_main_config(version: int, data: {}):
Expand Down Expand Up @@ -117,13 +117,13 @@ async def async_validate_main_config(data, account_ids = []):
data[CONFIG_MAIN_HOME_PRO_API_KEY] is not None):
home_pro_client = OctopusEnergyHomeProApiClient(data[CONFIG_MAIN_HOME_PRO_ADDRESS], data[CONFIG_MAIN_HOME_PRO_API_KEY])

can_connect = False
try:
can_connect = await home_pro_client.async_ping()
if can_connect == False:
errors[CONFIG_MAIN_HOME_PRO_ADDRESS] = "home_pro_not_responding"
except AuthenticationException:
errors[CONFIG_MAIN_HOME_PRO_ADDRESS] = "home_pro_authentication_failed"
except:
can_connect = False

if can_connect == False:
errors[CONFIG_MAIN_HOME_PRO_ADDRESS] = "home_pro_connection_failed"

return errors
8 changes: 6 additions & 2 deletions custom_components/octopus_energy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
"weighting_not_supported": "Weighting is only supported for continuous target rates",
"invalid_product_or_tariff": "Product or tariff code does not exist",
"all_home_pro_values_not_set": "Either both Home Pro address and API key must be set, or neither must be set",
"home_pro_connection_failed": "Cannot connect to Home Pro device"
"home_pro_connection_failed": "Cannot connect to Home Pro device.",
"home_pro_authentication_failed": "Cannot authenticate with API on Home Pro device. Please check authentication token.",
"home_pro_not_responding": "Connected to Home Pro device, but responding with unsuccessful status"
},
"abort": {
"not_supported": "Configuration for target rates is not supported at the moment.",
Expand Down Expand Up @@ -192,7 +194,9 @@
"weighting_not_supported": "Weighting is only supported for continuous target rates",
"invalid_product_or_tariff": "Product or tariff code does not exist",
"all_home_pro_values_not_set": "Either both Home Pro address and API key must be set, or neither must be set",
"home_pro_connection_failed": "Cannot connect to Home Pro device"
"home_pro_connection_failed": "Cannot connect to Home Pro device.",
"home_pro_authentication_failed": "Cannot authenticate with API on Home Pro device. Please check authentication token.",
"home_pro_not_responding": "Connected to Home Pro device, but responding with unsuccessful status"
},
"abort": {
"not_supported": "Configuration for target rates is not supported at the moment.",
Expand Down
42 changes: 39 additions & 3 deletions 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, RequestException, ServerException
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient, RequestException, ServerException, AuthenticationException
from custom_components.octopus_energy.api_client_home_pro import OctopusEnergyHomeProApiClient
from custom_components.octopus_energy.config.main import async_validate_main_config
from custom_components.octopus_energy.const import (
Expand Down Expand Up @@ -470,12 +470,48 @@ async def async_mocked_ping_home_pro(*args, **kwargs):

# Assert
assert CONFIG_MAIN_HOME_PRO_ADDRESS in errors
assert errors[CONFIG_MAIN_HOME_PRO_ADDRESS] == "home_pro_connection_failed"
assert errors[CONFIG_MAIN_HOME_PRO_ADDRESS] == "home_pro_not_responding"

assert_errors_not_present(errors, config_keys, CONFIG_MAIN_HOME_PRO_ADDRESS)

@pytest.mark.asyncio
async def test_when_connect_to_home_pro_throws_authentication_exception_then_error_returned():
# Arrange
data = {
CONFIG_MAIN_API_KEY: "test-api-key",
CONFIG_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,
CONFIG_MAIN_HOME_PRO_ADDRESS: "http://localhost:8000",
CONFIG_MAIN_HOME_PRO_API_KEY: "supersecret"
}

account_info = get_account_info()
async def async_mocked_get_account(*args, **kwargs):
return account_info

async def async_mocked_ping_home_pro(*args, **kwargs):
raise AuthenticationException("cannot connect", [])

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

# Assert
assert CONFIG_MAIN_HOME_PRO_ADDRESS in errors
assert errors[CONFIG_MAIN_HOME_PRO_ADDRESS] == "home_pro_authentication_failed"

assert_errors_not_present(errors, config_keys, CONFIG_MAIN_HOME_PRO_ADDRESS)

@pytest.mark.asyncio
async def test_when_connect_to_home_pro_throws_exception_then_error_returned():
async def test_when_connect_to_home_pro_throws_general_exception_then_error_returned():
# Arrange
data = {
CONFIG_MAIN_API_KEY: "test-api-key",
Expand Down

0 comments on commit 40582ae

Please sign in to comment.