Skip to content

Commit

Permalink
Don't raise an error on setup if double authentication is required
Browse files Browse the repository at this point in the history
  • Loading branch information
Giga77 committed Apr 26, 2024
1 parent 95b54d6 commit e1f3b96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
25 changes: 16 additions & 9 deletions custom_components/ecole_directe/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from __future__ import annotations

import json
import os.path
import logging
from typing import Any

import voluptuous as vol

from homeassistant import config_entries
Expand All @@ -13,14 +14,12 @@
from homeassistant.core import callback

from .ecole_directe_helper import (
QCMError,
get_ecoledirecte_session,
check_ecoledirecte_session,
)

from .const import (
DOMAIN,
DEFAULT_REFRESH_INTERVAL,
EVENT_TYPE,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -47,24 +46,32 @@ async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
"""Handle a flow initialized by the user."""
_LOGGER.debug("ED - Setup process initiated by user.")
errors: dict[str, str] = {}

path = self.hass.config.config_dir + "/custom_components/ecole_directe/qcm.json"
if not os.path.isfile(path):
with open(
path,
"w",
encoding="utf-8",
) as f:
json.dump({}, f, ensure_ascii=False, indent=4)

if user_input is not None:
try:
self._user_inputs.update(user_input)
session = await self.hass.async_add_executor_job(
get_ecoledirecte_session,
check_ecoledirecte_session,
self._user_inputs,
self.hass.config.config_dir,
self.hass,
)

if session is None:
if not session:
raise InvalidAuth

return self.async_create_entry(
title=session.identifiant, data=self._user_inputs
title=self._user_inputs["username"], data=self._user_inputs
)
except QCMError:
errors["base"] = "double_auth"
except InvalidAuth:
errors["base"] = "invalid_auth"

Expand Down
15 changes: 13 additions & 2 deletions custom_components/ecole_directe/ecole_directe_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ def __init__(self, data):
self.elements_programme = ""


def check_ecoledirecte_session(data, config_path, hass) -> bool:
"""check if credentials to Ecole Directe are ok"""
try:
session = get_ecoledirecte_session(data, config_path, hass)
except QCMError:
return True

return session is not None


def get_ecoledirecte_session(data, config_path, hass) -> EDSession | None:
"""Function connecting to Ecole Directe"""
try:
Expand Down Expand Up @@ -353,7 +363,7 @@ def get_ecoledirecte_session(data, config_path, hass) -> EDSession | None:

if try_login == 0:
raise QCMError(
"Vérifiez le fichier qcm.json, et recharchez l'intégration Ecole Directe."
"Vérifiez le fichier qcm.json, et rechargez l'intégration Ecole Directe."
)

_LOGGER.debug("cn: [%s] - cv: [%s]", cn, cv)
Expand All @@ -378,7 +388,8 @@ def get_ecoledirecte_session(data, config_path, hass) -> EDSession | None:
login["data"]["accounts"][0]["identifiant"],
)
return EDSession(login)
except QCMError:
except QCMError as err:
_LOGGER.warning(err)
raise
except Exception as err:
_LOGGER.critical(err)
Expand Down
2 changes: 0 additions & 2 deletions custom_components/ecole_directe/qcm.json

This file was deleted.

0 comments on commit e1f3b96

Please sign in to comment.