Add reconfiguration flow to pyLoad integration (#120485)

This commit is contained in:
Mr. Bubbles 2024-06-26 09:46:50 +02:00 committed by GitHub
parent 4a86693254
commit 82b8b73b8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 154 additions and 2 deletions

View file

@ -199,3 +199,49 @@ class PyLoadConfigFlow(ConfigFlow, domain=DOMAIN):
description_placeholders={CONF_NAME: self.config_entry.data[CONF_USERNAME]},
errors=errors,
)
async def async_step_reconfigure(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform a reconfiguration."""
self.config_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the reconfiguration flow."""
errors = {}
if TYPE_CHECKING:
assert self.config_entry
if user_input is not None:
try:
await validate_input(self.hass, user_input)
except (CannotConnect, ParserError):
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_update_reload_and_abort(
self.config_entry,
data=user_input,
reload_even_if_entry_is_unchanged=False,
reason="reconfigure_successful",
)
return self.async_show_form(
step_id="reconfigure_confirm",
data_schema=self.add_suggested_values_to_schema(
STEP_USER_DATA_SCHEMA,
user_input or self.config_entry.data,
),
description_placeholders={CONF_NAME: self.config_entry.data[CONF_USERNAME]},
errors=errors,
)

View file

@ -15,6 +15,20 @@
"port": "pyLoad uses port 8000 by default."
}
},
"reconfigure_confirm": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"username": "[%key:common::config_flow::data::username%]",
"password": "[%key:common::config_flow::data::password%]",
"ssl": "[%key:common::config_flow::data::ssl%]",
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
"port": "[%key:common::config_flow::data::port%]"
},
"data_description": {
"host": "The hostname or IP address of the device running your pyLoad instance.",
"port": "pyLoad uses port 8000 by default."
}
},
"reauth_confirm": {
"title": "[%key:common::config_flow::title::reauth%]",
"data": {
@ -30,7 +44,8 @@
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
}
},
"entity": {

View file

@ -6,7 +6,12 @@ from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
import pytest
from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER
from homeassistant.config_entries import (
SOURCE_IMPORT,
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
SOURCE_USER,
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -250,3 +255,89 @@ async def test_reauth_errors(
assert result["reason"] == "reauth_successful"
assert config_entry.data == NEW_INPUT
assert len(hass.config_entries.async_entries()) == 1
async def test_reconfiguration(
hass: HomeAssistant,
config_entry: MockConfigEntry,
mock_pyloadapi: AsyncMock,
) -> None:
"""Test reauth flow."""
config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert config_entry.data == USER_INPUT
assert len(hass.config_entries.async_entries()) == 1
@pytest.mark.parametrize(
("side_effect", "error_text"),
[
(InvalidAuth, "invalid_auth"),
(CannotConnect, "cannot_connect"),
(IndexError, "unknown"),
],
)
async def test_reconfigure_errors(
hass: HomeAssistant,
config_entry: MockConfigEntry,
mock_pyloadapi: AsyncMock,
side_effect: Exception,
error_text: str,
) -> None:
"""Test reauth flow."""
config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
mock_pyloadapi.login.side_effect = side_effect
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error_text}
mock_pyloadapi.login.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert config_entry.data == USER_INPUT
assert len(hass.config_entries.async_entries()) == 1