mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Use reconfigure helpers in enphase envoy config flow (#127977)
This commit is contained in:
parent
ff1ea46c46
commit
f6188949f3
@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
import logging
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from awesomeversion import AwesomeVersion
|
||||
@ -58,7 +57,6 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
|
||||
_reauth_entry: ConfigEntry
|
||||
_reconnect_entry: ConfigEntry
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize an envoy flow."""
|
||||
@ -238,24 +236,20 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Add reconfigure step to allow to manually reconfigure a config entry."""
|
||||
self._reconnect_entry = self._get_reconfigure_entry()
|
||||
return await self.async_step_reconfigure_confirm()
|
||||
|
||||
async def async_step_reconfigure_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Add reconfigure step to allow to manually reconfigure a config entry."""
|
||||
reconfigure_entry = self._get_reconfigure_entry()
|
||||
errors: dict[str, str] = {}
|
||||
description_placeholders: dict[str, str] = {}
|
||||
suggested_values: dict[str, Any] | MappingProxyType[str, Any] = (
|
||||
user_input or self._reconnect_entry.data
|
||||
)
|
||||
|
||||
host: Any = suggested_values.get(CONF_HOST)
|
||||
username: Any = suggested_values.get(CONF_USERNAME)
|
||||
password: Any = suggested_values.get(CONF_PASSWORD)
|
||||
|
||||
if user_input is not None:
|
||||
host: str = user_input[CONF_HOST]
|
||||
username: str = user_input[CONF_USERNAME]
|
||||
password: str = user_input[CONF_PASSWORD]
|
||||
try:
|
||||
envoy = await validate_input(
|
||||
self.hass,
|
||||
@ -273,29 +267,23 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
if self.unique_id != envoy.serial_number:
|
||||
errors["base"] = "unexpected_envoy"
|
||||
description_placeholders = {
|
||||
"reason": f"target: {self.unique_id}, actual: {envoy.serial_number}"
|
||||
}
|
||||
else:
|
||||
# If envoy exists in configuration update fields and exit
|
||||
self._abort_if_unique_id_configured(
|
||||
{
|
||||
CONF_HOST: host,
|
||||
CONF_USERNAME: username,
|
||||
CONF_PASSWORD: password,
|
||||
},
|
||||
error="reconfigure_successful",
|
||||
)
|
||||
if not self.unique_id:
|
||||
await self.async_set_unique_id(self._reconnect_entry.unique_id)
|
||||
await self.async_set_unique_id(envoy.serial_number)
|
||||
self._abort_if_unique_id_mismatch()
|
||||
return self.async_update_reload_and_abort(
|
||||
reconfigure_entry,
|
||||
data_updates={
|
||||
CONF_HOST: host,
|
||||
CONF_USERNAME: username,
|
||||
CONF_PASSWORD: password,
|
||||
},
|
||||
)
|
||||
|
||||
self.context["title_placeholders"] = {
|
||||
CONF_SERIAL: self.unique_id or "-",
|
||||
CONF_HOST: host,
|
||||
CONF_SERIAL: reconfigure_entry.unique_id or "-",
|
||||
CONF_HOST: reconfigure_entry.data[CONF_HOST],
|
||||
}
|
||||
|
||||
suggested_values: Mapping[str, Any] = user_input or reconfigure_entry.data
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure_confirm",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
|
@ -28,12 +28,12 @@
|
||||
"error": {
|
||||
"cannot_connect": "Cannot connect: {reason}",
|
||||
"invalid_auth": "Invalid authentication: {reason}",
|
||||
"unexpected_envoy": "Unexpected Envoy: {reason}",
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||
},
|
||||
"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%]",
|
||||
"unique_id_mismatch": "The serial number of the device does not match the previous serial number"
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
|
@ -790,34 +790,14 @@ async def test_reconfigure_otherenvoy(
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "unexpected_envoy"}
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
||||
|
||||
# entry should still be original entry
|
||||
assert config_entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert config_entry.data[CONF_USERNAME] == "test-username"
|
||||
assert config_entry.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
# set serial back to original to finsich flow
|
||||
mock_envoy.serial_number = "1234"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
|
||||
# updated original entry
|
||||
assert config_entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert config_entry.data[CONF_USERNAME] == "test-username"
|
||||
assert config_entry.data[CONF_PASSWORD] == "new-password"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user