Skip to content

Commit

Permalink
refactor: remove validate_credentials; ConfigFlow uses directly the…
Browse files Browse the repository at this point in the history
… client
  • Loading branch information
palazzem committed Nov 3, 2023
1 parent 1ef5a8b commit cb78288
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 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

0 comments on commit cb78288

Please sign in to comment.