mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Fix mqtt reconfigure flow (#133315)
* FIx mqtt reconfigure flow * Follow up on code review
This commit is contained in:
parent
9ca9e787b2
commit
ac6d718094
@ -470,7 +470,6 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
fields: OrderedDict[Any, Any] = OrderedDict()
|
fields: OrderedDict[Any, Any] = OrderedDict()
|
||||||
validated_user_input: dict[str, Any] = {}
|
validated_user_input: dict[str, Any] = {}
|
||||||
broker_config: dict[str, Any] = {}
|
|
||||||
if is_reconfigure := (self.source == SOURCE_RECONFIGURE):
|
if is_reconfigure := (self.source == SOURCE_RECONFIGURE):
|
||||||
reconfigure_entry = self._get_reconfigure_entry()
|
reconfigure_entry = self._get_reconfigure_entry()
|
||||||
if await async_get_broker_settings(
|
if await async_get_broker_settings(
|
||||||
@ -482,29 +481,25 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
errors,
|
errors,
|
||||||
):
|
):
|
||||||
if is_reconfigure:
|
if is_reconfigure:
|
||||||
broker_config.update(
|
|
||||||
update_password_from_user_input(
|
update_password_from_user_input(
|
||||||
reconfigure_entry.data.get(CONF_PASSWORD), validated_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(
|
can_connect = await self.hass.async_add_executor_job(
|
||||||
try_connection,
|
try_connection,
|
||||||
broker_config,
|
validated_user_input,
|
||||||
)
|
)
|
||||||
|
|
||||||
if can_connect:
|
if can_connect:
|
||||||
if is_reconfigure:
|
if is_reconfigure:
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
reconfigure_entry,
|
reconfigure_entry,
|
||||||
data_updates=broker_config,
|
data=validated_user_input,
|
||||||
)
|
)
|
||||||
validated_user_input[CONF_DISCOVERY] = DEFAULT_DISCOVERY
|
validated_user_input[CONF_DISCOVERY] = DEFAULT_DISCOVERY
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=broker_config[CONF_BROKER],
|
title=validated_user_input[CONF_BROKER],
|
||||||
data=broker_config,
|
data=validated_user_input,
|
||||||
)
|
)
|
||||||
|
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
@ -2162,7 +2162,7 @@ async def test_setup_with_advanced_settings(
|
|||||||
async def test_change_websockets_transport_to_tcp(
|
async def test_change_websockets_transport_to_tcp(
|
||||||
hass: HomeAssistant, mock_try_connection: MagicMock
|
hass: HomeAssistant, mock_try_connection: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test option flow setup with websockets transport settings."""
|
"""Test reconfiguration flow changing websockets transport settings."""
|
||||||
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
@ -2178,7 +2178,7 @@ async def test_change_websockets_transport_to_tcp(
|
|||||||
|
|
||||||
mock_try_connection.return_value = True
|
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["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "broker"
|
assert result["step_id"] == "broker"
|
||||||
assert result["data_schema"].schema["transport"]
|
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"]
|
assert result["data_schema"].schema["ws_headers"]
|
||||||
|
|
||||||
# Change transport to tcp
|
# Change transport to tcp
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
mqtt.CONF_BROKER: "test-broker",
|
mqtt.CONF_BROKER: "test-broker",
|
||||||
@ -2196,25 +2196,14 @@ async def test_change_websockets_transport_to_tcp(
|
|||||||
mqtt.CONF_WS_PATH: "/some_path",
|
mqtt.CONF_WS_PATH: "/some_path",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["step_id"] == "options"
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# Check config entry result
|
# Check config entry result
|
||||||
assert config_entry.data == {
|
assert config_entry.data == {
|
||||||
mqtt.CONF_BROKER: "test-broker",
|
mqtt.CONF_BROKER: "test-broker",
|
||||||
CONF_PORT: 1234,
|
CONF_PORT: 1234,
|
||||||
mqtt.CONF_TRANSPORT: "tcp",
|
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:
|
) -> None:
|
||||||
"""Test reconfigure flow."""
|
"""Test reconfigure flow."""
|
||||||
await mqtt_mock_entry()
|
await mqtt_mock_entry()
|
||||||
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
|
entry: MockConfigEntry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await entry.start_reconfigure_flow(hass, show_advanced_options=True)
|
||||||
mqtt.DOMAIN,
|
|
||||||
context={
|
|
||||||
"source": config_entries.SOURCE_RECONFIGURE,
|
|
||||||
"entry_id": entry.entry_id,
|
|
||||||
"show_advanced_options": True,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "broker"
|
assert result["step_id"] == "broker"
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user