Address ruckus_unleashed late review (#99411)

This commit is contained in:
Tony 2023-09-10 17:49:17 +01:00 committed by GitHub
parent 7acc606dd8
commit 3b25262d6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 290 additions and 98 deletions

View file

@ -1,9 +1,10 @@
"""Config flow for Ruckus Unleashed integration."""
from collections.abc import Mapping
import logging
from typing import Any
from aioruckus import AjaxSession, SystemStat
from aioruckus.exceptions import AuthenticationError
from aioruckus.exceptions import AuthenticationError, SchemaError
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
@ -19,6 +20,8 @@ from .const import (
KEY_SYS_TITLE,
)
_LOGGER = logging.getLogger(__package__)
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
@ -38,26 +41,29 @@ async def validate_input(hass: core.HomeAssistant, data):
async with AjaxSession.async_create(
data[CONF_HOST], data[CONF_USERNAME], data[CONF_PASSWORD]
) as ruckus:
system_info = await ruckus.api.get_system_info(
SystemStat.SYSINFO,
)
mesh_name = (await ruckus.api.get_mesh_info())[API_MESH_NAME]
zd_serial = system_info[API_SYS_SYSINFO][API_SYS_SYSINFO_SERIAL]
return {
KEY_SYS_TITLE: mesh_name,
KEY_SYS_SERIAL: zd_serial,
}
mesh_info = await ruckus.api.get_mesh_info()
system_info = await ruckus.api.get_system_info(SystemStat.SYSINFO)
except AuthenticationError as autherr:
raise InvalidAuth from autherr
except (ConnectionRefusedError, ConnectionError, KeyError) as connerr:
except (ConnectionError, SchemaError) as connerr:
raise CannotConnect from connerr
mesh_name = mesh_info[API_MESH_NAME]
zd_serial = system_info[API_SYS_SYSINFO][API_SYS_SYSINFO_SERIAL]
return {
KEY_SYS_TITLE: mesh_name,
KEY_SYS_SERIAL: zd_serial,
}
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Ruckus Unleashed."""
VERSION = 1
_reauth_entry: config_entries.ConfigEntry | None = None
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
@ -70,30 +76,40 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(info[KEY_SYS_SERIAL])
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=info[KEY_SYS_TITLE], data=user_input
)
if self._reauth_entry is None:
await self.async_set_unique_id(info[KEY_SYS_SERIAL])
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=info[KEY_SYS_TITLE], data=user_input
)
if info[KEY_SYS_SERIAL] == self._reauth_entry.unique_id:
self.hass.config_entries.async_update_entry(
self._reauth_entry, data=user_input
)
self.hass.async_create_task(
self.hass.config_entries.async_reload(
self._reauth_entry.entry_id
)
)
return self.async_abort(reason="reauth_successful")
errors["base"] = "invalid_host"
data_schema = self.add_suggested_values_to_schema(
DATA_SCHEMA, self._reauth_entry.data if self._reauth_entry else {}
)
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
step_id="user", data_schema=data_schema, errors=errors
)
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Perform reauth upon an API authentication error."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Dialog that informs the user that reauth is required."""
if user_input is None:
return self.async_show_form(
step_id="reauth_confirm",
data_schema=DATA_SCHEMA,
)
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_user()