65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
|
"""Config flow for ohme integration."""
|
||
|
|
||
|
from typing import Any
|
||
|
|
||
|
from ohme import ApiException, AuthException, OhmeApiClient
|
||
|
import voluptuous as vol
|
||
|
|
||
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||
|
from homeassistant.helpers.selector import (
|
||
|
TextSelector,
|
||
|
TextSelectorConfig,
|
||
|
TextSelectorType,
|
||
|
)
|
||
|
|
||
|
from .const import DOMAIN
|
||
|
|
||
|
USER_SCHEMA = vol.Schema(
|
||
|
{
|
||
|
vol.Required(CONF_EMAIL): TextSelector(
|
||
|
TextSelectorConfig(
|
||
|
type=TextSelectorType.EMAIL,
|
||
|
autocomplete="email",
|
||
|
),
|
||
|
),
|
||
|
vol.Required(CONF_PASSWORD): TextSelector(
|
||
|
TextSelectorConfig(
|
||
|
type=TextSelectorType.PASSWORD,
|
||
|
autocomplete="current-password",
|
||
|
),
|
||
|
),
|
||
|
}
|
||
|
)
|
||
|
|
||
|
|
||
|
class OhmeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||
|
"""Config flow."""
|
||
|
|
||
|
async def async_step_user(
|
||
|
self, user_input: dict[str, Any] | None = None
|
||
|
) -> ConfigFlowResult:
|
||
|
"""First config step."""
|
||
|
|
||
|
errors: dict[str, str] = {}
|
||
|
|
||
|
if user_input is not None:
|
||
|
self._async_abort_entries_match({CONF_EMAIL: user_input[CONF_EMAIL]})
|
||
|
|
||
|
instance = OhmeApiClient(user_input[CONF_EMAIL], user_input[CONF_PASSWORD])
|
||
|
try:
|
||
|
await instance.async_login()
|
||
|
except AuthException:
|
||
|
errors["base"] = "invalid_auth"
|
||
|
except ApiException:
|
||
|
errors["base"] = "unknown"
|
||
|
|
||
|
if not errors:
|
||
|
return self.async_create_entry(
|
||
|
title=user_input[CONF_EMAIL], data=user_input
|
||
|
)
|
||
|
|
||
|
return self.async_show_form(
|
||
|
step_id="user", data_schema=USER_SCHEMA, errors=errors
|
||
|
)
|