* Modernization rework - config entry support, with override support from huawei_lte platform in YAML - device tracker entity registry support - refactor for easier addition of more features - internal code cleanups * Remove log level dependent subscription/data debug hack No longer needed, because pretty much all keys from supported categories are exposed as sensors. Closes https://github.com/home-assistant/home-assistant/issues/23819 * Upgrade huawei-lte-api to 1.4.1 https://github.com/Salamek/huawei-lte-api/releases * Add support for access without username and password * Use subclass init instead of config_entries.HANDLERS * Update huawei-lte-api to 1.4.3 (#27269) * Convert device state attributes to snake_case * Simplify scanner entity initialization * Remove not needed hass reference from Router * Return explicit None from unsupported old device tracker setup * Mark unknown connection errors during config as such * Drop some dead config flow code * Run config flow sync I/O in executor * Parametrize config flow login error tests * Forward entry unload to platforms * Async/sync fixups * Improve data subscription debug logging * Implement on the fly add of new and tracking of seen device tracker entities * Handle device tracker entry unload cleanup in component * Remove unnecessary _async_setup_lte, just have code in async_setup_entry * Remove time tracker on unload * Fix to not use same mutable default subscription set for all routers * Pylint fixes * Remove some redundant defensive device tracker code * Add back explicit get_scanner None return, hush pylint * Adjust approach to set system_options on entry create * Enable some sensors on first add instead of disabling everything * Fix SMS notification recipients default value * Add option to skip new device tracker entities * Fix SMS notification recipient option default * Work around https://github.com/PyCQA/pylint/issues/3202 * Remove unrelated type hint additions * Change async_add_new_entities to a regular function * Remove option to disable polling for new device tracker entries
208 lines
7.6 KiB
Python
208 lines
7.6 KiB
Python
"""Config flow for the Huawei LTE platform."""
|
|
|
|
from collections import OrderedDict
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from huawei_lte_api.AuthorizedConnection import AuthorizedConnection
|
|
from huawei_lte_api.Client import Client
|
|
from huawei_lte_api.Connection import Connection
|
|
from huawei_lte_api.exceptions import (
|
|
LoginErrorUsernameWrongException,
|
|
LoginErrorPasswordWrongException,
|
|
LoginErrorUsernamePasswordWrongException,
|
|
LoginErrorUsernamePasswordOverrunException,
|
|
ResponseErrorException,
|
|
)
|
|
from url_normalize import url_normalize
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_PASSWORD, CONF_RECIPIENT, CONF_URL, CONF_USERNAME
|
|
from homeassistant.core import callback
|
|
from .const import DEFAULT_DEVICE_NAME
|
|
|
|
# https://github.com/PyCQA/pylint/issues/3202
|
|
from .const import DOMAIN # pylint: disable=unused-import
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle Huawei LTE config flow."""
|
|
|
|
VERSION = 1
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
|
|
|
@staticmethod
|
|
@callback
|
|
def async_get_options_flow(config_entry):
|
|
"""Get options flow."""
|
|
return OptionsFlowHandler(config_entry)
|
|
|
|
async def _async_show_user_form(self, user_input=None, errors=None):
|
|
if user_input is None:
|
|
user_input = {}
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema(
|
|
OrderedDict(
|
|
(
|
|
(
|
|
vol.Required(
|
|
CONF_URL, default=user_input.get(CONF_URL, "")
|
|
),
|
|
str,
|
|
),
|
|
(
|
|
vol.Optional(
|
|
CONF_USERNAME, default=user_input.get(CONF_USERNAME, "")
|
|
),
|
|
str,
|
|
),
|
|
(
|
|
vol.Optional(
|
|
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "")
|
|
),
|
|
str,
|
|
),
|
|
)
|
|
)
|
|
),
|
|
errors=errors or {},
|
|
)
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
"""Handle import initiated config flow."""
|
|
return await self.async_step_user(user_input)
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
"""Handle user initiated config flow."""
|
|
if user_input is None:
|
|
return await self._async_show_user_form()
|
|
|
|
errors = {}
|
|
|
|
# Normalize URL
|
|
user_input[CONF_URL] = url_normalize(
|
|
user_input[CONF_URL], default_scheme="http"
|
|
)
|
|
if "://" not in user_input[CONF_URL]:
|
|
errors[CONF_URL] = "invalid_url"
|
|
return await self._async_show_user_form(
|
|
user_input=user_input, errors=errors
|
|
)
|
|
|
|
# See if we already have a router configured with this URL
|
|
existing_urls = { # existing entries
|
|
url_normalize(entry.data[CONF_URL], default_scheme="http")
|
|
for entry in self._async_current_entries()
|
|
}
|
|
if user_input[CONF_URL] in existing_urls:
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
conn = None
|
|
|
|
def logout():
|
|
if hasattr(conn, "user"):
|
|
try:
|
|
conn.user.logout()
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.debug("Could not logout", exc_info=True)
|
|
|
|
def try_connect(username: Optional[str], password: Optional[str]) -> Connection:
|
|
"""Try connecting with given credentials."""
|
|
if username or password:
|
|
conn = AuthorizedConnection(
|
|
user_input[CONF_URL], username=username, password=password
|
|
)
|
|
else:
|
|
try:
|
|
conn = AuthorizedConnection(
|
|
user_input[CONF_URL], username="", password=""
|
|
)
|
|
user_input[CONF_USERNAME] = ""
|
|
user_input[CONF_PASSWORD] = ""
|
|
except ResponseErrorException:
|
|
_LOGGER.debug(
|
|
"Could not login with empty credentials, proceeding unauthenticated",
|
|
exc_info=True,
|
|
)
|
|
conn = Connection(user_input[CONF_URL])
|
|
del user_input[CONF_USERNAME]
|
|
del user_input[CONF_PASSWORD]
|
|
return conn
|
|
|
|
def get_router_title(conn: Connection) -> str:
|
|
"""Get title for router."""
|
|
title = None
|
|
client = Client(conn)
|
|
try:
|
|
info = client.device.basic_information()
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.debug("Could not get device.basic_information", exc_info=True)
|
|
else:
|
|
title = info.get("devicename")
|
|
if not title:
|
|
try:
|
|
info = client.device.information()
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.debug("Could not get device.information", exc_info=True)
|
|
else:
|
|
title = info.get("DeviceName")
|
|
return title or DEFAULT_DEVICE_NAME
|
|
|
|
username = user_input.get(CONF_USERNAME)
|
|
password = user_input.get(CONF_PASSWORD)
|
|
try:
|
|
conn = await self.hass.async_add_executor_job(
|
|
try_connect, username, password
|
|
)
|
|
except LoginErrorUsernameWrongException:
|
|
errors[CONF_USERNAME] = "incorrect_username"
|
|
except LoginErrorPasswordWrongException:
|
|
errors[CONF_PASSWORD] = "incorrect_password"
|
|
except LoginErrorUsernamePasswordWrongException:
|
|
errors[CONF_USERNAME] = "incorrect_username_or_password"
|
|
except LoginErrorUsernamePasswordOverrunException:
|
|
errors["base"] = "login_attempts_exceeded"
|
|
except ResponseErrorException:
|
|
_LOGGER.warning("Response error", exc_info=True)
|
|
errors["base"] = "response_error"
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.warning("Unknown error connecting to device", exc_info=True)
|
|
errors[CONF_URL] = "unknown_connection_error"
|
|
if errors:
|
|
await self.hass.async_add_executor_job(logout)
|
|
return await self._async_show_user_form(
|
|
user_input=user_input, errors=errors
|
|
)
|
|
|
|
title = await self.hass.async_add_executor_job(get_router_title, conn)
|
|
await self.hass.async_add_executor_job(logout)
|
|
|
|
return self.async_create_entry(title=title, data=user_input)
|
|
|
|
|
|
class OptionsFlowHandler(config_entries.OptionsFlow):
|
|
"""Huawei LTE options flow."""
|
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry):
|
|
"""Initialize options flow."""
|
|
self.config_entry = config_entry
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
"""Handle options flow."""
|
|
if user_input is not None:
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
data_schema = vol.Schema(
|
|
{
|
|
vol.Optional(
|
|
CONF_RECIPIENT,
|
|
default=self.config_entry.options.get(CONF_RECIPIENT, ""),
|
|
): str
|
|
}
|
|
)
|
|
return self.async_show_form(step_id="init", data_schema=data_schema)
|