Use reconfigure_confirm in homeworks config flow (#127218)

* Use reconfigure_confirm in homeworks config flow

* Fix tests
pull/126782/head
epenet 2024-10-01 15:17:50 +02:00 committed by Franck Nijhof
parent bce7552d4d
commit 03553b8bb9
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 27 additions and 18 deletions

View File

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

View File

@ -22,7 +22,7 @@
"name": "[%key:component::homeworks::config::step::user::data_description::name%]" "name": "[%key:component::homeworks::config::step::user::data_description::name%]"
} }
}, },
"reconfigure": { "reconfigure_confirm": {
"data": { "data": {
"host": "[%key:common::config_flow::data::host%]", "host": "[%key:common::config_flow::data::host%]",
"port": "[%key:common::config_flow::data::port%]", "port": "[%key:common::config_flow::data::port%]",
@ -45,8 +45,8 @@
}, },
"data_description": { "data_description": {
"name": "A unique name identifying the Lutron Homeworks controller", "name": "A unique name identifying the Lutron Homeworks controller",
"password": "[%key:component::homeworks::config::step::reconfigure::data_description::password%]", "password": "[%key:component::homeworks::config::step::reconfigure_confirm::data_description::password%]",
"username": "[%key:component::homeworks::config::step::reconfigure::data_description::username%]" "username": "[%key:component::homeworks::config::step::reconfigure_confirm::data_description::username%]"
}, },
"description": "Add a Lutron Homeworks controller" "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}, context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
) )
assert result["type"] is FlowResultType.FORM 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 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -314,7 +314,7 @@ async def test_reconfigure_flow_flow_duplicate(
context={"source": SOURCE_RECONFIGURE, "entry_id": entry1.entry_id}, context={"source": SOURCE_RECONFIGURE, "entry_id": entry1.entry_id},
) )
assert result["type"] is FlowResultType.FORM 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 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -324,7 +324,7 @@ async def test_reconfigure_flow_flow_duplicate(
}, },
) )
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure" assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": "duplicated_host_port"} 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}, context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
) )
assert result["type"] is FlowResultType.FORM 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 = await hass.config_entries.flow.async_configure(
result["flow_id"], 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}, context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
) )
assert result["type"] is FlowResultType.FORM 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 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -398,7 +398,7 @@ async def test_reconfigure_flow_credentials_password_only(
}, },
) )
assert result["type"] is FlowResultType.FORM 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"} assert result["errors"] == {"base": "need_username_with_password"}