|
| 1 | +"""Config flow for the SensorPush Cloud integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from sensorpush_ha import SensorPushCloudApi, SensorPushCloudAuthError |
| 8 | +import voluptuous as vol |
| 9 | + |
| 10 | +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult |
| 11 | +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD |
| 12 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
| 13 | +from homeassistant.helpers.selector import ( |
| 14 | + TextSelector, |
| 15 | + TextSelectorConfig, |
| 16 | + TextSelectorType, |
| 17 | +) |
| 18 | + |
| 19 | +from .const import DOMAIN, LOGGER |
| 20 | + |
| 21 | + |
| 22 | +class SensorPushCloudConfigFlow(ConfigFlow, domain=DOMAIN): |
| 23 | + """Handle a config flow for SensorPush Cloud.""" |
| 24 | + |
| 25 | + async def async_step_user( |
| 26 | + self, user_input: dict[str, Any] | None = None |
| 27 | + ) -> ConfigFlowResult: |
| 28 | + """Handle the initial step.""" |
| 29 | + errors: dict[str, str] = {} |
| 30 | + if user_input is not None: |
| 31 | + email, password = user_input[CONF_EMAIL], user_input[CONF_PASSWORD] |
| 32 | + await self.async_set_unique_id(email) |
| 33 | + self._abort_if_unique_id_configured() |
| 34 | + clientsession = async_get_clientsession(self.hass) |
| 35 | + api = SensorPushCloudApi(email, password, clientsession) |
| 36 | + try: |
| 37 | + await api.async_authorize() |
| 38 | + except SensorPushCloudAuthError: |
| 39 | + errors["base"] = "invalid_auth" |
| 40 | + except Exception: # noqa: BLE001 |
| 41 | + LOGGER.exception("Unexpected error") |
| 42 | + errors["base"] = "unknown" |
| 43 | + else: |
| 44 | + return self.async_create_entry(title=email, data=user_input) |
| 45 | + |
| 46 | + return self.async_show_form( |
| 47 | + step_id="user", |
| 48 | + data_schema=vol.Schema( |
| 49 | + { |
| 50 | + vol.Required(CONF_EMAIL): TextSelector( |
| 51 | + TextSelectorConfig( |
| 52 | + type=TextSelectorType.EMAIL, autocomplete="username" |
| 53 | + ) |
| 54 | + ), |
| 55 | + vol.Required(CONF_PASSWORD): TextSelector( |
| 56 | + TextSelectorConfig( |
| 57 | + type=TextSelectorType.PASSWORD, |
| 58 | + autocomplete="current-password", |
| 59 | + ) |
| 60 | + ), |
| 61 | + } |
| 62 | + ), |
| 63 | + errors=errors, |
| 64 | + ) |
0 commit comments