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