From ac6d7180949358d8f8708ae4a903312ca0bb739d Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 17 Dec 2024 09:37:46 +0100 Subject: [PATCH] Fix mqtt reconfigure flow (#133315) * FIx mqtt reconfigure flow * Follow up on code review --- homeassistant/components/mqtt/config_flow.py | 17 ++++------- tests/components/mqtt/test_config_flow.py | 32 +++++--------------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index ad3f3d35457..0081246c705 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -470,7 +470,6 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} fields: OrderedDict[Any, Any] = OrderedDict() validated_user_input: dict[str, Any] = {} - broker_config: dict[str, Any] = {} if is_reconfigure := (self.source == SOURCE_RECONFIGURE): reconfigure_entry = self._get_reconfigure_entry() if await async_get_broker_settings( @@ -482,29 +481,25 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): errors, ): if is_reconfigure: - broker_config.update( - update_password_from_user_input( - reconfigure_entry.data.get(CONF_PASSWORD), validated_user_input - ), + update_password_from_user_input( + reconfigure_entry.data.get(CONF_PASSWORD), validated_user_input ) - else: - broker_config = validated_user_input can_connect = await self.hass.async_add_executor_job( try_connection, - broker_config, + validated_user_input, ) if can_connect: if is_reconfigure: return self.async_update_reload_and_abort( reconfigure_entry, - data_updates=broker_config, + data=validated_user_input, ) validated_user_input[CONF_DISCOVERY] = DEFAULT_DISCOVERY return self.async_create_entry( - title=broker_config[CONF_BROKER], - data=broker_config, + title=validated_user_input[CONF_BROKER], + data=validated_user_input, ) errors["base"] = "cannot_connect" diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index fc1221956de..38dbda50cdd 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -2162,7 +2162,7 @@ async def test_setup_with_advanced_settings( async def test_change_websockets_transport_to_tcp( hass: HomeAssistant, mock_try_connection: MagicMock ) -> None: - """Test option flow setup with websockets transport settings.""" + """Test reconfiguration flow changing websockets transport settings.""" config_entry = MockConfigEntry(domain=mqtt.DOMAIN) config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( @@ -2178,7 +2178,7 @@ async def test_change_websockets_transport_to_tcp( mock_try_connection.return_value = True - result = await hass.config_entries.options.async_init(config_entry.entry_id) + result = await config_entry.start_reconfigure_flow(hass, show_advanced_options=True) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "broker" assert result["data_schema"].schema["transport"] @@ -2186,7 +2186,7 @@ async def test_change_websockets_transport_to_tcp( assert result["data_schema"].schema["ws_headers"] # Change transport to tcp - result = await hass.config_entries.options.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ mqtt.CONF_BROKER: "test-broker", @@ -2196,25 +2196,14 @@ async def test_change_websockets_transport_to_tcp( mqtt.CONF_WS_PATH: "/some_path", }, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "options" - - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={ - mqtt.CONF_DISCOVERY: True, - mqtt.CONF_DISCOVERY_PREFIX: "homeassistant_test", - }, - ) - assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" # Check config entry result assert config_entry.data == { mqtt.CONF_BROKER: "test-broker", CONF_PORT: 1234, mqtt.CONF_TRANSPORT: "tcp", - mqtt.CONF_DISCOVERY: True, - mqtt.CONF_DISCOVERY_PREFIX: "homeassistant_test", } @@ -2238,15 +2227,8 @@ async def test_reconfigure_flow_form( ) -> None: """Test reconfigure flow.""" await mqtt_mock_entry() - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - result = await hass.config_entries.flow.async_init( - mqtt.DOMAIN, - context={ - "source": config_entries.SOURCE_RECONFIGURE, - "entry_id": entry.entry_id, - "show_advanced_options": True, - }, - ) + entry: MockConfigEntry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] + result = await entry.start_reconfigure_flow(hass, show_advanced_options=True) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "broker" assert result["errors"] == {}