Skip to content

Commit

Permalink
tests(config_flow): add coverage for config exceptions; refactor `Con…
Browse files Browse the repository at this point in the history
…figFlow` tests (#102)
  • Loading branch information
palazzem authored Nov 3, 2023
1 parent 74df799 commit 77beef0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 134 deletions.
11 changes: 7 additions & 4 deletions custom_components/econnect_metronet/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Config flow for E-connect Alarm integration."""
import logging

import voluptuous as vol
from elmo.api.client import ElmoClient
from elmo.api.exceptions import CredentialError
from elmo.systems import ELMO_E_CONNECT as E_CONNECT_DEFAULT
from homeassistant import config_entries
Expand All @@ -21,7 +21,7 @@
SUPPORTED_SYSTEMS,
)
from .exceptions import InvalidAreas
from .helpers import parse_areas_config, validate_credentials
from .helpers import parse_areas_config

_LOGGER = logging.getLogger(__name__)

Expand All @@ -43,8 +43,11 @@ async def async_step_user(self, user_input=None):
errors = {}
if user_input is not None:
try:
# Validate submitted configuration
await validate_credentials(self.hass, user_input)
# Validate credentials
client = ElmoClient(user_input.get(CONF_SYSTEM_URL), domain=user_input.get(CONF_DOMAIN))
await self.hass.async_add_executor_job(
client.auth, user_input.get(CONF_USERNAME), user_input.get(CONF_PASSWORD)
)
except ConnectionError:
errors["base"] = "cannot_connect"
except CredentialError:
Expand Down
29 changes: 2 additions & 27 deletions custom_components/econnect_metronet/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import Union

from elmo.api.client import ElmoClient
from homeassistant import core
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_USERNAME
from homeassistant.util import slugify

from .const import CONF_DOMAIN, CONF_SYSTEM_NAME, CONF_SYSTEM_URL, DOMAIN
from .const import CONF_SYSTEM_NAME, DOMAIN
from .exceptions import InvalidAreas


Expand Down Expand Up @@ -49,29 +47,6 @@ def parse_areas_config(config: str, raises: bool = False):
return []


async def validate_credentials(hass: core.HomeAssistant, config: dict):
"""Validate if user input includes valid credentials to connect.
Initialize the client with an API endpoint and a vendor and authenticate
your connection to retrieve the access token.
Args:
hass: HomeAssistant instance.
data: data that needs validation (configured username/password).
Raises:
ConnectionError: if there is a connection error.
CredentialError: if given credentials are incorrect.
HTTPError: if the API backend answers with errors.
Returns:
`True` if given `data` includes valid credential checked with
e-connect backend.
"""
# Check Credentials
client = ElmoClient(config.get(CONF_SYSTEM_URL), domain=config.get(CONF_DOMAIN))
await hass.async_add_executor_job(client.auth, config.get(CONF_USERNAME), config.get(CONF_PASSWORD))
return True


def generate_entity_id(config: ConfigEntry, name: Union[str, None] = None) -> str:
"""Generate an entity ID based on system configuration or username.
Expand Down
14 changes: 14 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def _(mock_path: str) -> str:
"""Helper to simplify Mock path strings.
Args:
mock_path (str): The partial path to be appended to the standard prefix for mock paths.
Returns:
str: The full mock path combined with the standard prefix.
Example:
>>> _("module.Class.method")
"custom_components.econnect_metronet.module.Class.method"
"""
return f"custom_components.econnect_metronet.{mock_path}"
Loading

0 comments on commit 77beef0

Please sign in to comment.