diff --git a/custom_components/econnect_metronet/config_flow.py b/custom_components/econnect_metronet/config_flow.py index f90afe7..63685e1 100644 --- a/custom_components/econnect_metronet/config_flow.py +++ b/custom_components/econnect_metronet/config_flow.py @@ -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 @@ -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__) @@ -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: diff --git a/custom_components/econnect_metronet/helpers.py b/custom_components/econnect_metronet/helpers.py index 92862bd..68f273d 100644 --- a/custom_components/econnect_metronet/helpers.py +++ b/custom_components/econnect_metronet/helpers.py @@ -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 @@ -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.