Add re-auth flow to glances integration (#100929)
* Add reauth flow to glances integration. * add reauth string * add reauth strings
This commit is contained in:
parent
c823e407fd
commit
785b46af22
5 changed files with 155 additions and 7 deletions
|
@ -1,6 +1,7 @@
|
||||||
"""Config flow for Glances."""
|
"""Config flow for Glances."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from glances_api.exceptions import (
|
from glances_api.exceptions import (
|
||||||
|
@ -47,6 +48,49 @@ class GlancesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a Glances config flow."""
|
"""Handle a Glances config flow."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
_reauth_entry: config_entries.ConfigEntry | None
|
||||||
|
|
||||||
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
||||||
|
"""Perform reauth upon an API authentication error."""
|
||||||
|
self._reauth_entry = self.hass.config_entries.async_get_entry(
|
||||||
|
self.context["entry_id"]
|
||||||
|
)
|
||||||
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
|
async def async_step_reauth_confirm(
|
||||||
|
self, user_input: dict[str, str] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
|
"""Confirm reauth dialog."""
|
||||||
|
errors = {}
|
||||||
|
assert self._reauth_entry
|
||||||
|
if user_input is not None:
|
||||||
|
user_input = {**self._reauth_entry.data, **user_input}
|
||||||
|
api = get_api(self.hass, user_input)
|
||||||
|
try:
|
||||||
|
await api.get_ha_sensor_data()
|
||||||
|
except GlancesApiAuthorizationError:
|
||||||
|
errors["base"] = "invalid_auth"
|
||||||
|
except GlancesApiConnectionError:
|
||||||
|
errors["base"] = "cannot_connect"
|
||||||
|
else:
|
||||||
|
self.hass.config_entries.async_update_entry(
|
||||||
|
self._reauth_entry, data=user_input
|
||||||
|
)
|
||||||
|
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
|
||||||
|
return self.async_abort(reason="reauth_successful")
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
description_placeholders={
|
||||||
|
CONF_USERNAME: self._reauth_entry.data[CONF_USERNAME]
|
||||||
|
},
|
||||||
|
step_id="reauth_confirm",
|
||||||
|
data_schema=vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_PASSWORD): str,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
errors=errors,
|
||||||
|
)
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
@ -64,7 +108,7 @@ class GlancesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
except GlancesApiConnectionError:
|
except GlancesApiConnectionError:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
if not errors:
|
else:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}",
|
title=f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}",
|
||||||
data=user_input,
|
data=user_input,
|
||||||
|
|
|
@ -7,6 +7,7 @@ from glances_api import Glances, exceptions
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||||
|
@ -36,6 +37,8 @@ class GlancesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""Get the latest data from the Glances REST API."""
|
"""Get the latest data from the Glances REST API."""
|
||||||
try:
|
try:
|
||||||
data = await self.api.get_ha_sensor_data()
|
data = await self.api.get_ha_sensor_data()
|
||||||
|
except exceptions.GlancesApiAuthorizationError as err:
|
||||||
|
raise ConfigEntryAuthFailed from err
|
||||||
except exceptions.GlancesApiError as err:
|
except exceptions.GlancesApiError as err:
|
||||||
raise UpdateFailed from err
|
raise UpdateFailed from err
|
||||||
return data or {}
|
return data or {}
|
||||||
|
|
|
@ -11,6 +11,12 @@
|
||||||
"ssl": "[%key:common::config_flow::data::ssl%]",
|
"ssl": "[%key:common::config_flow::data::ssl%]",
|
||||||
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"reauth_confirm": {
|
||||||
|
"description": "The password for {username} is invalid.",
|
||||||
|
"data": {
|
||||||
|
"password": "[%key:common::config_flow::data::password%]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
|
@ -18,7 +24,8 @@
|
||||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
|
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,3 +85,81 @@ async def test_form_already_configured(hass: HomeAssistant) -> None:
|
||||||
)
|
)
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reauth_success(hass: HomeAssistant) -> None:
|
||||||
|
"""Test we can reauth."""
|
||||||
|
entry = MockConfigEntry(domain=glances.DOMAIN, data=MOCK_USER_INPUT)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
glances.DOMAIN,
|
||||||
|
context={
|
||||||
|
"source": config_entries.SOURCE_REAUTH,
|
||||||
|
"entry_id": entry.entry_id,
|
||||||
|
},
|
||||||
|
data=MOCK_USER_INPUT,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
assert result["description_placeholders"] == {"username": "username"}
|
||||||
|
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
"password": "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result2["type"] == FlowResultType.ABORT
|
||||||
|
assert result2["reason"] == "reauth_successful"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("error", "message"),
|
||||||
|
[
|
||||||
|
(GlancesApiAuthorizationError, "invalid_auth"),
|
||||||
|
(GlancesApiConnectionError, "cannot_connect"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_reauth_fails(
|
||||||
|
hass: HomeAssistant, error: Exception, message: str, mock_api: MagicMock
|
||||||
|
) -> None:
|
||||||
|
"""Test we can reauth."""
|
||||||
|
entry = MockConfigEntry(domain=glances.DOMAIN, data=MOCK_USER_INPUT)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
mock_api.return_value.get_ha_sensor_data.side_effect = [error, HA_SENSOR_DATA]
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
glances.DOMAIN,
|
||||||
|
context={
|
||||||
|
"source": config_entries.SOURCE_REAUTH,
|
||||||
|
"entry_id": entry.entry_id,
|
||||||
|
},
|
||||||
|
data=MOCK_USER_INPUT,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
assert result["description_placeholders"] == {"username": "username"}
|
||||||
|
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
"password": "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result2["type"] == FlowResultType.FORM
|
||||||
|
assert result2["errors"] == {"base": message}
|
||||||
|
|
||||||
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
"password": "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result3["type"] == FlowResultType.ABORT
|
||||||
|
assert result3["reason"] == "reauth_successful"
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
"""Tests for Glances integration."""
|
"""Tests for Glances integration."""
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
from glances_api.exceptions import GlancesApiConnectionError
|
from glances_api.exceptions import (
|
||||||
|
GlancesApiAuthorizationError,
|
||||||
|
GlancesApiConnectionError,
|
||||||
|
)
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.glances.const import DOMAIN
|
from homeassistant.components.glances.const import DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
|
@ -23,15 +27,27 @@ async def test_successful_config_entry(hass: HomeAssistant) -> None:
|
||||||
assert entry.state == ConfigEntryState.LOADED
|
assert entry.state == ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
|
||||||
async def test_conn_error(hass: HomeAssistant, mock_api: MagicMock) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test Glances failed due to connection error."""
|
("error", "entry_state"),
|
||||||
|
[
|
||||||
|
(GlancesApiAuthorizationError, ConfigEntryState.SETUP_ERROR),
|
||||||
|
(GlancesApiConnectionError, ConfigEntryState.SETUP_RETRY),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_setup_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
error: Exception,
|
||||||
|
entry_state: ConfigEntryState,
|
||||||
|
mock_api: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test Glances failed due to api error."""
|
||||||
|
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
|
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
mock_api.return_value.get_ha_sensor_data.side_effect = GlancesApiConnectionError
|
mock_api.return_value.get_ha_sensor_data.side_effect = error
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
assert entry.state is entry_state
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry(hass: HomeAssistant) -> None:
|
async def test_unload_entry(hass: HomeAssistant) -> None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue