61 lines
2.8 KiB
Python
61 lines
2.8 KiB
Python
from homeassistant import config_entries
|
|
import voluptuous as vol
|
|
from .api import async_login, SunlitResponseCode
|
|
|
|
from .const import DOMAIN
|
|
from .const import KEY_USERNAME, KEY_PASSWORD, KEY_SCAN_INTERVAL
|
|
from homeassistant.helpers.selector import (
|
|
TextSelector,
|
|
TextSelectorConfig,
|
|
TextSelectorType,
|
|
)
|
|
|
|
class MyIntegrationConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
if user_input is not None:
|
|
print("User input received:", user_input)
|
|
err_msg = "api_unreachable"
|
|
# Here you would typically validate the user input, e.g., check credentials
|
|
if user_input.get(KEY_USERNAME) and user_input.get(KEY_PASSWORD) and "@" in user_input[KEY_USERNAME]:
|
|
login_resp = await async_login(user_input[KEY_USERNAME], user_input[KEY_PASSWORD])
|
|
if login_resp is not None and login_resp["code"] == SunlitResponseCode.SUCCESS:
|
|
# If validation is successful, create the entry
|
|
user_input[KEY_SCAN_INTERVAL] = int(user_input.get(KEY_SCAN_INTERVAL, 60))
|
|
return self.async_create_entry(title=user_input[KEY_USERNAME], data={**login_resp["content"], **{KEY_SCAN_INTERVAL: user_input[KEY_SCAN_INTERVAL]}})
|
|
err_msg = "invalid_credentials"
|
|
if login_resp is not None and login_resp["code"] != SunlitResponseCode.SUCCESS:
|
|
for key, value in login_resp["message"].items():
|
|
err_msg = value
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema({
|
|
vol.Required(KEY_USERNAME): TextSelector(
|
|
TextSelectorConfig(type=TextSelectorType.EMAIL, autocomplete="username")
|
|
),
|
|
vol.Required(KEY_PASSWORD): TextSelector(
|
|
TextSelectorConfig(
|
|
type=TextSelectorType.PASSWORD, autocomplete="current-password"
|
|
)
|
|
),
|
|
vol.Optional(KEY_SCAN_INTERVAL, default=60): int,
|
|
}),
|
|
errors={"base": err_msg}
|
|
)
|
|
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema({
|
|
vol.Required(KEY_USERNAME): TextSelector(
|
|
TextSelectorConfig(type=TextSelectorType.EMAIL, autocomplete="username")
|
|
),
|
|
vol.Required(KEY_PASSWORD): TextSelector(
|
|
TextSelectorConfig(
|
|
type=TextSelectorType.PASSWORD, autocomplete="current-password"
|
|
)
|
|
),
|
|
vol.Optional(KEY_SCAN_INTERVAL, default=60): int,
|
|
}),
|
|
) |