2022-01-21 05:34:25 +00:00
|
|
|
"""Config flow for Aussie Broadband integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-20 07:08:11 +00:00
|
|
|
from collections.abc import Mapping
|
2022-01-21 05:34:25 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from aiohttp import ClientError
|
|
|
|
from aussiebb.asyncio import AussieBB, AuthenticationException
|
2022-03-20 06:35:34 +00:00
|
|
|
from aussiebb.const import FETCH_TYPES
|
2022-01-21 05:34:25 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2022-02-16 22:38:05 +00:00
|
|
|
from .const import CONF_SERVICES, DOMAIN
|
2022-01-21 05:34:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Aussie Broadband."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the config flow."""
|
|
|
|
self.data: dict = {}
|
|
|
|
self.options: dict = {CONF_SERVICES: []}
|
|
|
|
self.services: list[dict[str]] = []
|
|
|
|
self.client: AussieBB | None = None
|
|
|
|
self._reauth_username: str | None = None
|
|
|
|
|
|
|
|
async def async_auth(self, user_input: dict[str, str]) -> dict[str, str] | None:
|
|
|
|
"""Reusable Auth Helper."""
|
|
|
|
self.client = AussieBB(
|
|
|
|
user_input[CONF_USERNAME],
|
|
|
|
user_input[CONF_PASSWORD],
|
|
|
|
async_get_clientsession(self.hass),
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
await self.client.login()
|
|
|
|
except AuthenticationException:
|
|
|
|
return {"base": "invalid_auth"}
|
|
|
|
except ClientError:
|
|
|
|
return {"base": "cannot_connect"}
|
2022-02-16 22:38:05 +00:00
|
|
|
return None
|
2022-01-21 05:34:25 +00:00
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors: dict[str, str] | None = None
|
|
|
|
if user_input is not None:
|
|
|
|
if not (errors := await self.async_auth(user_input)):
|
|
|
|
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
self.data = user_input
|
2022-03-20 06:35:34 +00:00
|
|
|
self.services = await self.client.get_services(drop_types=FETCH_TYPES) # type: ignore[union-attr]
|
2022-01-21 05:34:25 +00:00
|
|
|
|
|
|
|
if not self.services:
|
|
|
|
return self.async_abort(reason="no_services_found")
|
|
|
|
|
2022-02-16 22:38:05 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=self.data[CONF_USERNAME],
|
|
|
|
data=self.data,
|
|
|
|
)
|
2022-01-21 05:34:25 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
2022-06-28 21:23:32 +00:00
|
|
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
2022-02-16 22:38:05 +00:00
|
|
|
"""Handle reauth on credential failure."""
|
2022-06-28 21:23:32 +00:00
|
|
|
self._reauth_username = entry_data[CONF_USERNAME]
|
2022-01-21 05:34:25 +00:00
|
|
|
|
2022-02-16 22:38:05 +00:00
|
|
|
return await self.async_step_reauth_confirm()
|
2022-01-21 05:34:25 +00:00
|
|
|
|
2022-02-16 22:38:05 +00:00
|
|
|
async def async_step_reauth_confirm(
|
|
|
|
self, user_input: dict[str, str] | None = None
|
2022-01-21 05:34:25 +00:00
|
|
|
) -> FlowResult:
|
2022-02-16 22:38:05 +00:00
|
|
|
"""Handle users reauth credentials."""
|
|
|
|
|
2022-01-21 05:34:25 +00:00
|
|
|
errors: dict[str, str] | None = None
|
|
|
|
|
2022-02-16 22:38:05 +00:00
|
|
|
if user_input and self._reauth_username:
|
2022-01-21 05:34:25 +00:00
|
|
|
data = {
|
|
|
|
CONF_USERNAME: self._reauth_username,
|
|
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
|
|
|
}
|
|
|
|
|
|
|
|
if not (errors := await self.async_auth(data)):
|
|
|
|
entry = await self.async_set_unique_id(self._reauth_username.lower())
|
|
|
|
if entry:
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
entry,
|
|
|
|
data=data,
|
|
|
|
)
|
|
|
|
await self.hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
return self.async_create_entry(title=self._reauth_username, data=data)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2022-02-16 22:38:05 +00:00
|
|
|
step_id="reauth_confirm",
|
2022-01-21 05:34:25 +00:00
|
|
|
description_placeholders={"username": self._reauth_username},
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
|
|
|
)
|