|
| 1 | +"""Config flow for Ituran integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Mapping |
| 6 | +import logging |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from pyituran import Ituran |
| 10 | +from pyituran.exceptions import IturanApiError, IturanAuthError |
| 11 | +import voluptuous as vol |
| 12 | + |
| 13 | +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult |
| 14 | + |
| 15 | +from .const import ( |
| 16 | + CONF_ID_OR_PASSPORT, |
| 17 | + CONF_MOBILE_ID, |
| 18 | + CONF_OTP, |
| 19 | + CONF_PHONE_NUMBER, |
| 20 | + DOMAIN, |
| 21 | +) |
| 22 | + |
| 23 | +_LOGGER = logging.getLogger(__name__) |
| 24 | + |
| 25 | +STEP_USER_DATA_SCHEMA = vol.Schema( |
| 26 | + { |
| 27 | + vol.Required(CONF_ID_OR_PASSPORT): str, |
| 28 | + vol.Required(CONF_PHONE_NUMBER): str, |
| 29 | + vol.Optional(CONF_MOBILE_ID): str, |
| 30 | + } |
| 31 | +) |
| 32 | + |
| 33 | +STEP_OTP_DATA_SCHEMA = vol.Schema( |
| 34 | + { |
| 35 | + vol.Required(CONF_OTP, description="OTP"): str, |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +class IturanConfigFlow(ConfigFlow, domain=DOMAIN): |
| 41 | + """Handle a config flow for Ituran.""" |
| 42 | + |
| 43 | + _user_info: dict[str, Any] |
| 44 | + |
| 45 | + async def async_step_user( |
| 46 | + self, user_input: dict[str, Any] | None = None |
| 47 | + ) -> ConfigFlowResult: |
| 48 | + """Handle the inial step.""" |
| 49 | + errors: dict[str, str] = {} |
| 50 | + if user_input is not None: |
| 51 | + if self.unique_id is None: |
| 52 | + await self.async_set_unique_id(user_input[CONF_ID_OR_PASSPORT]) |
| 53 | + self._abort_if_unique_id_configured() |
| 54 | + |
| 55 | + try: |
| 56 | + ituran = Ituran( |
| 57 | + user_input[CONF_ID_OR_PASSPORT], |
| 58 | + user_input[CONF_PHONE_NUMBER], |
| 59 | + user_input.get(CONF_MOBILE_ID), |
| 60 | + ) |
| 61 | + user_input[CONF_MOBILE_ID] = ituran.mobile_id |
| 62 | + authenticated = await ituran.is_authenticated() |
| 63 | + if not authenticated: |
| 64 | + await ituran.request_otp() |
| 65 | + except IturanApiError: |
| 66 | + errors["base"] = "cannot_connect" |
| 67 | + except IturanAuthError: |
| 68 | + errors["base"] = "invalid_auth" |
| 69 | + except Exception: |
| 70 | + _LOGGER.exception("Unexpected exception") |
| 71 | + errors["base"] = "unknown" |
| 72 | + else: |
| 73 | + if authenticated: |
| 74 | + return self.async_create_entry( |
| 75 | + title=f"Ituran {user_input[CONF_ID_OR_PASSPORT]}", |
| 76 | + data=user_input, |
| 77 | + ) |
| 78 | + self._user_info = user_input |
| 79 | + return await self.async_step_otp() |
| 80 | + |
| 81 | + return self.async_show_form( |
| 82 | + step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors |
| 83 | + ) |
| 84 | + |
| 85 | + async def async_step_reauth( |
| 86 | + self, entry_data: Mapping[str, Any] |
| 87 | + ) -> ConfigFlowResult: |
| 88 | + """Handle configuration by re-auth.""" |
| 89 | + return await self.async_step_user() |
| 90 | + |
| 91 | + async def async_step_otp( |
| 92 | + self, user_input: dict[str, Any] | None = None |
| 93 | + ) -> ConfigFlowResult: |
| 94 | + """Handle the inial step.""" |
| 95 | + errors: dict[str, str] = {} |
| 96 | + if user_input is not None: |
| 97 | + try: |
| 98 | + ituran = Ituran( |
| 99 | + self._user_info[CONF_ID_OR_PASSPORT], |
| 100 | + self._user_info[CONF_PHONE_NUMBER], |
| 101 | + self._user_info[CONF_MOBILE_ID], |
| 102 | + ) |
| 103 | + await ituran.authenticate(user_input[CONF_OTP]) |
| 104 | + except IturanApiError: |
| 105 | + errors["base"] = "cannot_connect" |
| 106 | + except IturanAuthError: |
| 107 | + errors["base"] = "invalid_otp" |
| 108 | + except Exception: |
| 109 | + _LOGGER.exception("Unexpected exception") |
| 110 | + errors["base"] = "unknown" |
| 111 | + else: |
| 112 | + return self.async_create_entry( |
| 113 | + title=f"Ituran {self._user_info[CONF_ID_OR_PASSPORT]}", |
| 114 | + data=self._user_info, |
| 115 | + ) |
| 116 | + |
| 117 | + return self.async_show_form( |
| 118 | + step_id="otp", data_schema=STEP_OTP_DATA_SCHEMA, errors=errors |
| 119 | + ) |
0 commit comments