Use reconfigure_confirm in homeworks config flow (#127218)

* Use reconfigure_confirm in homeworks config flow

* Fix tests
pull/127231/head
epenet 2024-10-01 15:17:50 +02:00 committed by GitHub
parent 3fb7547d4d
commit 3b7ae1639c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 18 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Mapping
from functools import partial
import logging
from typing import Any
@ -557,6 +558,8 @@ OPTIONS_FLOW = {
class HomeworksConfigFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for Lutron Homeworks."""
_context_entry: ConfigEntry
async def _validate_edit_controller(
self, user_input: dict[str, Any]
) -> dict[str, Any]:
@ -580,18 +583,24 @@ class HomeworksConfigFlowHandler(ConfigFlow, domain=DOMAIN):
return user_input
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle a reconfigure flow."""
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
assert entry
self._context_entry = entry
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a reconfigure flow."""
errors = {}
suggested_values = {
CONF_HOST: entry.options[CONF_HOST],
CONF_PORT: entry.options[CONF_PORT],
CONF_USERNAME: entry.data.get(CONF_USERNAME),
CONF_PASSWORD: entry.data.get(CONF_PASSWORD),
CONF_HOST: self._context_entry.options[CONF_HOST],
CONF_PORT: self._context_entry.options[CONF_PORT],
CONF_USERNAME: self._context_entry.data.get(CONF_USERNAME),
CONF_PASSWORD: self._context_entry.data.get(CONF_PASSWORD),
}
if user_input:
@ -608,16 +617,16 @@ class HomeworksConfigFlowHandler(ConfigFlow, domain=DOMAIN):
else:
password = user_input.pop(CONF_PASSWORD, None)
username = user_input.pop(CONF_USERNAME, None)
new_data = entry.data | {
new_data = self._context_entry.data | {
CONF_PASSWORD: password,
CONF_USERNAME: username,
}
new_options = entry.options | {
new_options = self._context_entry.options | {
CONF_HOST: user_input[CONF_HOST],
CONF_PORT: user_input[CONF_PORT],
}
return self.async_update_reload_and_abort(
entry,
self._context_entry,
data=new_data,
options=new_options,
reason="reconfigure_successful",
@ -625,7 +634,7 @@ class HomeworksConfigFlowHandler(ConfigFlow, domain=DOMAIN):
)
return self.async_show_form(
step_id="reconfigure",
step_id="reconfigure_confirm",
data_schema=self.add_suggested_values_to_schema(
DATA_SCHEMA_EDIT_CONTROLLER, suggested_values
),

View File

@ -22,7 +22,7 @@
"name": "[%key:component::homeworks::config::step::user::data_description::name%]"
}
},
"reconfigure": {
"reconfigure_confirm": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"port": "[%key:common::config_flow::data::port%]",
@ -45,8 +45,8 @@
},
"data_description": {
"name": "A unique name identifying the Lutron Homeworks controller",
"password": "[%key:component::homeworks::config::step::reconfigure::data_description::password%]",
"username": "[%key:component::homeworks::config::step::reconfigure::data_description::username%]"
"password": "[%key:component::homeworks::config::step::reconfigure_confirm::data_description::password%]",
"username": "[%key:component::homeworks::config::step::reconfigure_confirm::data_description::username%]"
},
"description": "Add a Lutron Homeworks controller"
}

View File

@ -246,7 +246,7 @@ async def test_reconfigure_flow(
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -314,7 +314,7 @@ async def test_reconfigure_flow_flow_duplicate(
context={"source": SOURCE_RECONFIGURE, "entry_id": entry1.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -324,7 +324,7 @@ async def test_reconfigure_flow_flow_duplicate(
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": "duplicated_host_port"}
@ -339,7 +339,7 @@ async def test_reconfigure_flow_flow_no_change(
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -387,7 +387,7 @@ async def test_reconfigure_flow_credentials_password_only(
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -398,7 +398,7 @@ async def test_reconfigure_flow_credentials_password_only(
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": "need_username_with_password"}